利用python3 的pygame模块实现塔防游戏


Posted in Python onDecember 30, 2019

利用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

3.敌军类

import pygame
class Enemy(pygame.sprite.Sprite):
  """小兵类"""
  def __init__(self, position):
    pygame.sprite.Sprite.__init__(self)
    self.img = pygame.image.load(r'image/enemy.png').convert_alpha()
    self.rect = self.img.get_rect()
    self.init_pos = position
    self.rect.left, self.rect.top = self.init_pos
    self.accelerate = 1
    self.speed = [0, -self.accelerate]
    self.active = True
    self.health = 500
    # 竖直
    self.status = 'UP'
  def move(self):
    """移动"""
    self.rect = self.rect.move(self.speed)
    if self.rect.top <= 448 and self.rect.left == 258:
      self.rect.top = 448
      self.status = 'R'
      self.img = pygame.transform.rotate(self.img, 270)
      self.speed = [self.accelerate, 0]
    if self.rect.top == 448 and self.rect.left >= 597:
      self.rect.left = 597
      self.status = 'UP'
      self.img = pygame.transform.rotate(self.img, 90)
      self.speed = [0, -self.accelerate]
    if 320 < self.rect.top <= 335 and self.rect.left == 597:
      self.rect.top = 335
      self.status = 'L'
      self.img = pygame.transform.rotate(self.img, 90)
      self.speed = [-self.accelerate, 0]
    if self.rect.top == 335 and self.rect.left <= 370:
      self.rect.left = 370
      self.status = 'UP'
      self.img = pygame.transform.rotate(self.img, 270)
      self.speed = [0, -self.accelerate]
    if self.rect.top <= 216 and self.rect.left == 370:
      self.rect.top = 216
      self.status = 'R'
      self.img = pygame.transform.rotate(self.img, 270)
      self.speed = [self.accelerate, 0]
    if self.rect.top == 216 and 800 > self.rect.left >= 746:
      self.rect.left = 746
      self.status = 'DW'
      self.img = pygame.transform.rotate(self.img, 270)
      self.speed = [0, self.accelerate]
    if self.rect.top >= 330 and self.rect.left == 746:
      self.rect.top = 330
      self.status = 'R'
      self.img = pygame.transform.rotate(self.img, 90)
      self.speed = [self.accelerate, 0]
    if self.rect.top == 330 and self.rect.left >= 930:
      self.rect.left = 930
      self.status = 'UP'
      self.img = pygame.transform.rotate(self.img, 90)
      self.speed = [0, -self.accelerate]
    if self.rect.top < 180:
      self.active = False
  def drawhealth(self, screen):
    """绘制血量"""
    BLACK = (0, 0, 0)
    RED = (255, 0, 0)
    GREEN = (0, 255, 0)
    health_percentage = float(self.health) / 500
    if self.status == 'UP':
      start = self.rect.left + 8
      pygame.draw.line(screen, BLACK, (start, self.rect.top - 3),
               (start + 32, self.rect.top - 3), 4)
      if health_percentage > 0.5:
        pygame.draw.line(screen, GREEN, (start, self.rect.top - 3),
                 (start + 32 * health_percentage, self.rect.top - 3), 4)
      else:
        pygame.draw.line(screen, RED, (start, self.rect.top - 3),
                 (start + 32 * health_percentage, self.rect.top - 3), 4)
    elif self.status == 'DW':
      start = self.rect.left + 8
      pygame.draw.line(screen, BLACK, (start, self.rect.bottom + 3),
               (start + 32, self.rect.bottom + 3), 4)
      if health_percentage > 0.5:
        pygame.draw.line(screen, GREEN, (start, self.rect.bottom + 3),
                 (start + 32 * health_percentage, self.rect.bottom + 3), 4)
      else:
        pygame.draw.line(screen, RED, (start, self.rect.bottom + 3),
                 (start + 32 * health_percentage, self.rect.bottom + 3), 4)
    elif self.status == 'R':
      start = self.rect.bottom - 8
      pygame.draw.line(screen, BLACK, (self.rect.right + 3, start),
               (self.rect.right + 3, start - 32), 4)
      if health_percentage > 0.5:
        pygame.draw.line(screen, GREEN, (self.rect.right + 3, start),
                 (self.rect.right + 3, start - 32 * health_percentage), 4)
      else:
        pygame.draw.line(screen, RED, (self.rect.right + 3, start),
                 (self.rect.right + 3, start - 32 * health_percentage), 4)
    else:
      start = self.rect.bottom - 8
      pygame.draw.line(screen, BLACK, (self.rect.left - 3, start),
               (self.rect.left - 3, start - 30), 4)
      if health_percentage > 0.5:
        pygame.draw.line(screen, GREEN, (self.rect.left - 3, start),
                 (self.rect.left - 3, start - 30 * health_percentage), 4)
      else:
        pygame.draw.line(screen, RED, (self.rect.left - 3, start),
                 (self.rect.left - 3, start - 30 * health_percentage), 4)
  def reset(self):
    """reset the position"""
    self.rect.left, self.rect.top = self.init_pos

4.炮塔建造位置类

"""初始化炮塔可占据的位置"""
import pygame
class Position(pygame.sprite.Sprite):
  """pos"""
  def __init__(self, pos):
    pygame.sprite.Sprite.__init__(self)
    self.image = pygame.image.load('image/pt.png')
    self.rect = self.image.get_rect()
    self.rect.left, self.rect.top = pos

5.说明

本游戏通过鼠标左键点击炮塔可以建造的位置俩建造炮塔。

6.效果图

利用python3 的pygame模块实现塔防游戏

可以看到图确实不好看,但美术功底不够,所以不怎么好看。当然需要素材的也可以给你。

Python 相关文章推荐
python模拟登陆Tom邮箱示例分享
Jan 13 Python
使用Python中的线程进行网络编程的入门教程
Apr 15 Python
Python 专题二 条件语句和循环语句的基础知识
Mar 19 Python
django 解决manage.py migrate无效的问题
May 27 Python
Python实现的多进程和多线程功能示例
May 29 Python
python 实现分页显示从es中获取的数据方法
Dec 26 Python
Python数据类型之Tuple元组实例详解
May 08 Python
OpenCV3.0+Python3.6实现特定颜色的物体追踪
Jul 23 Python
Python递归调用实现数字累加的代码
Feb 25 Python
零基础学python应该从哪里入手
Aug 11 Python
Python根据URL地址下载文件并保存至对应目录的实现
Nov 15 Python
python中time.ctime()实例用法
Feb 03 Python
pytorch 批次遍历数据集打印数据的例子
Dec 30 #Python
python多线程使用方法实例详解
Dec 30 #Python
Python动态声明变量赋值代码实例
Dec 30 #Python
使用pytorch实现可视化中间层的结果
Dec 30 #Python
在Pytorch中计算自己模型的FLOPs方式
Dec 30 #Python
Pytorch之保存读取模型实例
Dec 30 #Python
Python爬虫解析网页的4种方式实例及原理解析
Dec 30 #Python
You might like
PHP - Html Transfer Code
2006/10/09 PHP
php 将bmp图片转为jpg等其他任意格式的图片
2009/06/21 PHP
[原创]php求圆周率的简单实现方法
2016/05/30 PHP
微信公众平台开发教程②微信端分享功能图文详解
2019/04/10 PHP
javascript显示隐藏层比较不错的方法分析
2008/09/30 Javascript
Mootools 1.2教程 Fx.Morph、Fx选项和Fx事件
2009/09/15 Javascript
JQuery防止退格键网页后退的实现代码
2012/03/23 Javascript
js中call与apply的用法小结
2013/12/28 Javascript
JavaScript利用append添加元素报错的解决方法
2014/07/01 Javascript
ExtJs动态生成treepanel的Json格式
2015/07/19 Javascript
jQuery实现只允许输入数字和小数点的方法
2016/03/02 Javascript
jQuery中的Deferred和promise 的区别
2016/04/03 Javascript
Javascript简写条件语句(推荐)
2016/06/12 Javascript
js实现3d悬浮效果
2017/02/16 Javascript
Nodejs进阶:express+session实现简易登录身份认证
2017/04/24 NodeJs
详解Node.js 命令行程序开发教程
2017/06/07 Javascript
详解如何用babel转换es6的class语法
2018/04/03 Javascript
原生JS实现动态加载js文件并在加载成功后执行回调函数的方法
2020/12/30 Javascript
手把手教你 CKEDITOR 4 实现Dialog 内嵌 IFrame操作详解
2019/06/18 Javascript
vue响应式更新机制及不使用框架实现简单的数据双向绑定问题
2019/06/27 Javascript
vue如何实现自定义底部菜单栏
2019/07/01 Javascript
vue项目中实现缓存的最佳方案详解
2019/07/11 Javascript
利用Python中的pandas库对cdn日志进行分析详解
2017/03/07 Python
python利用标准库如何获取本地IP示例详解
2017/11/01 Python
python实现Zabbix-API监控
2018/09/17 Python
python3字符串操作总结
2019/07/24 Python
Python对接 xray 和微信实现自动告警
2019/09/17 Python
如何在windows下安装配置python工具Ulipad
2020/10/27 Python
全球性的在线鞋类品牌:Public Desire
2019/04/03 全球购物
西铁城美国官方网站:Citizen Watch美国
2019/11/08 全球购物
英国豪华家具和家居用品购物网站:Teddy Beau
2020/10/12 全球购物
汽车促销活动方案
2014/03/31 职场文书
购房意向书范本
2014/04/01 职场文书
2014年创先争优活动总结
2014/05/04 职场文书
2014年第四季度入党积极分子思想汇报(十八届四中全会)
2014/11/03 职场文书
餐饮店长岗位职责
2015/04/14 职场文书