利用python3的pygame模块实现塔防游戏-创新互联

利用python3的pygame模块基本实现塔防游戏的基本功能,包括血量和分数显示,bgm,防御塔建造,防御塔攻击范围内的敌军,暂停和加速功能。由于实在没有素材,用的都是自己截图P的,所以美化不好。但基本保证功能,其中有一个BUG,但不影响游戏效果。

网站建设哪家好,找创新互联!专注于网页设计、网站建设、微信开发、重庆小程序开发公司、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了汉川免费建站欢迎大家使用!

1.运行主类

"""主程序"""
from pygame.locals import *
from TowerDefend.enemy import *
from TowerDefend.towerposSet import *
from TowerDefend.tower import *
import pygame
def run():
  """运行函数"""
  pygame.init()
  size = width, height = 1200, 600
  screen = pygame.display.set_mode(size)
  background_img = pygame.image.load(r'image/background.png').convert_alpha()
  background_img = pygame.transform.scale(background_img, (width, height))
  # 创建分数和血量
  health_count = 5
  score_count = 0
  score = pygame.font.Font('font/score_health.ttf', 30)
  health = pygame.font.Font('font/score_health.ttf', 30)
  # 创建背景音乐
  bg_music = pygame.mixer.music
  bg_music.load('media/bg.mp3')
  bg_music.set_volume(2)
  # 创建敌军类
  enemies = pygame.sprite.Group()
  ENEMY_NUM = 5
  position = [[258, 600], [258, 670], [258, 740], [258, 810], [258, 880]]
  for i in range(ENEMY_NUM):
    enemies.add(Enemy(position[i]))
  # 创建炮塔
  towers = pygame.sprite.Group()
  # 加载暂停键
  pause_img = pygame.image.load('image/pause.png').convert_alpha()
  pause_rect = pause_img.get_rect()
  pause_rect.left, pause_rect.top = 1145, 0
  # 加载血量和金币显示
  health_money_img = pygame.image.load('image/health_money.png').convert_alpha()
  health_money_rect = health_money_img.get_rect()
  health_money_rect.left, health_money_rect.top = 0, 0
  # 加载加速键
  speed_img = pygame.image.load('image/speed.png').convert_alpha()
  speed_rect = speed_img.get_rect()
  speed_rect.left, speed_rect.top = 1090, 0
  # 设置炮塔位置
  towers_pos = pygame.sprite.Group()
  position_list = [[225, 495], [264, 428], [312, 428], [362, 428], [410, 428], [460, 428], [508, 428], [561, 428],
           [561, 373], [377, 373]]
  for i in range(len(position_list)):
    towers_pos.add(Position(position_list[i]))
  # 设置循环条件
  running = True
  clock = pygame.time.Clock()
  paused = False
  # 播放音乐
  if not bg_music.get_busy():
    bg_music.play(-1)
  while running:
    clock.tick(100)
    for event in pygame.event.get():
      if event.type == QUIT:
        running = False
      if event.type == MOUSEBUTTONDOWN:
        if event.button == 1:
          if speed_rect.collidepoint(event.pos):
            for each in enemies:
              each.accelerate *= 2
          if pause_rect.collidepoint(event.pos):
            paused = not paused
          for each in towers_pos:
            if each.rect.collidepoint(event.pos):
              tower = Tower([each.rect.left, each.rect.top])
              towers.add(tower)
              towers_pos.remove(each)
    if not paused:
      for enemy in enemies:
        if enemy.active:
          enemy.move()
        else:
          if enemy.rect.top <= 180:
            health_count -= 1
          if enemy.rect.top > 180:
            score_count += 20
          enemies.remove(enemy)
    # 绘制界面设置
    screen.blit(background_img, (0, 0))
    screen.blit(health.render(str(health_count), True, (255, 255, 255)), (60, 0.3))
    screen.blit(score.render(str(score_count), True, (255, 255, 255)), (130, 0.5))
    screen.blit(health_money_img, health_money_rect)
    screen.blit(pause_img, pause_rect)
    screen.blit(speed_img, speed_rect)
    # 绘制炮塔
    for each in towers:
      each.draw(screen, enemies)
      each.hit(enemies)
    # 绘制炮塔位置
    towers_pos.draw(screen)
    # 敌军若存活则绘制其和血量
    for enemy in enemies:
      if enemy.active:
        screen.blit(enemy.img, enemy.rect)
        enemy.drawhealth(screen)
    pygame.display.flip()
  pygame.quit()
if __name__ == "__main__":
  run()

2.炮塔类

"""TOWER"""
import pygame
import math
class Tower(pygame.sprite.Sprite):
  """tower"""
  def __init__(self, pos):
    pygame.sprite.Sprite.__init__(self)
    self.img0 = pygame.image.load('image/tower0.png')
    self.img1 = pygame.image.load('image/tower1.png')
    self.img2 = pygame.image.load('image/tower2.png')
    self.rect = self.img0.get_rect()
    self.rect.left, self.rect.top = pos
    self.count = 1
  def draw(self, screen, enemies):
    """绘制"""
    if self.count > 90:
      self.count = 1
    if 1 <= self.count < 30:
      screen.blit(self.img0, self.rect)
    elif 30 <= self.count < 60:
      screen.blit(self.img1, self.rect)
    else:
      screen.blit(self.img2, self.rect)
    for enemy in enemies:
      distance = math.sqrt(
        math.pow((self.rect.left - enemy.rect.left), 2) + math.pow((self.rect.top - enemy.rect.top), 2))
      if distance < 50 and enemy.active is True:
        self.count += 1
  def hit(self, enemies):
    """攻击"""
    for enemy in enemies:
      distance = math.sqrt(
        math.pow((self.rect.left - enemy.rect.left), 2) + math.pow((self.rect.top - enemy.rect.top), 2))
      if distance < 50:
        enemy.health -= 1
      if enemy.health == 0:
        enemy.active = False

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


新闻名称:利用python3的pygame模块实现塔防游戏-创新互联
文章路径:http://azwzsj.com/article/dehcgg.html