python实现简单飞机大战小游戏
作者:HuCheng1997 时间:2023-12-19 06:55:37
为了熟悉Python基础语法,学习了一个经典的案例:飞机大战,最后实现效果如下:
实现步骤:
①下载64位对应python
版本的pygame
:pygame-1.9.6-cp38-cp38-win_amd64.whl
② 更新pip
:python -m pip install --upgrade pip
③ 安装pygame
:pip install pygame-1.9.6-cp38-cp38-win_amd64.whl
④ 实现代码如下:
import sys # 导入系统模块
import random # 随机数模块
import pygame # 导入pygame模块
import pygame.locals # 导入pygame本地策略
ICO_PATH = "images/app.ico" # APP_ICO
APP_NAME = "飞机大战V1.0" # APP_NAME
Enemy_IMGS = ("images/enemy1.png", "images/enemy2.png") # ENEMY_IMGS
# TODO 定义一个公共类
class Model:
window = None # 主窗体window对象
# 初始化函数
def __init__(self, img_path, x, y):
self.img = pygame.image.load(img_path)
self.x = x
self.y = y
# 将图片填充到背景中
def display(self):
Model.window.blit(self.img, (self.x, self.y))
# TODO 背景
class Background(Model):
# 背景移动
def move(self):
if self.y <= Game.WINDOW_HEIGHT:
self.y += 1
else:
self.y = 0
# 背景图片展示
def display(self):
Model.window.blit(self.img, (self.x, self.y)) # 填充背景
Model.window.blit(self.img, (self.x, self.y - Game.WINDOW_HEIGHT)) # 填充辅助背景
# TODO 玩家类
class PlayerPlane(Model):
def __init__(self, img_path, x, y):
super().__init__(img_path, x, y)
# *
self.bullets = []
def display(self, enemys):
super().display()
remove_bullets = []
for bullet in self.bullets:
bullet.move()
bullet.display()
# 如果 * 小于消失于屏幕,删除 *
if bullet.y < -11:
remove_bullets.append(bullet)
# * 的矩阵
bullet_rect = pygame.locals.Rect(bullet.x, bullet.y, 5, 11)
# TODO 进行碰撞检测
for enemy in enemys:
# 敌机的矩阵
enemy_rect = pygame.locals.Rect(enemy.x, enemy.y, 57, 43)
# 如果碰撞
if pygame.Rect.colliderect(bullet_rect, enemy_rect):
# 随机修改敌机的位置模拟敌机销毁并生成新敌机并删除 *
enemy.img = pygame.image.load(Enemy_IMGS[random.randint(0, 1)])
enemy.x = random.randint(0, Game.WINDOW_WIDTH - 57)
enemy.y = random.randint(-Game.WINDOW_WIDTH, -43)
remove_bullets.append(bullet)
break
for bullet in remove_bullets:
self.bullets.remove(bullet)
# TODO 敌机类
class EnemyPlane(Model):
def __init__(self):
self.img = pygame.image.load(Enemy_IMGS[random.randint(0, 1)])
self.x = random.randint(0, Game.WINDOW_WIDTH - 57)
self.y = random.randint(-Game.WINDOW_WIDTH, -43)
def move(self):
if self.y > Game.WINDOW_HEIGHT:
self.y = -43 # 返回初始位置
else:
self.y += 1
# TODO * 类
class Bullet(Model):
def move(self):
self.y -= 1
# TODO 游戏类
class Game:
WINDOW_WIDTH = 410
WINDOW_HEIGHT = 614
# 初始化操作
def __init__(self):
self.window = pygame.display.set_mode([Game.WINDOW_WIDTH, Game.WINDOW_HEIGHT]) # 窗口的设置
self.background = Background("images/background.png", 0, 0) # 窗体模型的设置
self.player = PlayerPlane("images/me1.png", 180, 400)
self.enemyPlanes = []
for _ in range(5):
self.enemyPlanes.append(EnemyPlane())
# 游戏的入口函数
def run(self):
self.frame_init()
while True:
self.background.move() # 背景向上移动,模拟环境移动
self.background.display() # 将图片塞入到窗体中
self.player.display(self.enemyPlanes)
for enemy in self.enemyPlanes:
enemy.move()
enemy.display()
pygame.display.update() # 刷新窗体
self.event_init() # 监听事件
# 初始化窗口
def frame_init(self):
Model.window = self.window # 将window对象赋值给Model公有类
ico = pygame.image.load(ICO_PATH) # 加载图片
pygame.display.set_icon(ico) # 设置icon
pygame.display.set_caption(APP_NAME) # 设置app_name
# 事件初始化方法
def event_init(self):
for event in pygame.event.get():
# 关闭事件
if event.type == pygame.locals.QUIT:
sys.exit()
# 监听鼠标事件
if event.type == pygame.locals.MOUSEMOTION:
pos = pygame.mouse.get_pos()
self.player.x = pos[0] - 51
self.player.y = pos[1] - 63
focus_state = pygame.mouse.get_pressed()
if focus_state[0] == 1:
pos = pygame.mouse.get_pos()
self.player.bullets.append(Bullet("images/bullet1.png", pos[0], pos[1] - 75))
if __name__ == "__main__":
Game().run()
来源:https://blog.csdn.net/qq_38697437/article/details/109246590
标签:python,飞机大战
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MySQL8.0.21.0社区版安装教程(图文详解)
2024-01-20 06:33:58
![](https://img.aspxhome.com/file/2023/3/69243_0s.jpg)
keras.utils.to_categorical和one hot格式解析
2023-10-03 18:27:12
![](https://img.aspxhome.com/file/2023/8/133348_0s.jpg)
详解python持久化文件读写
2023-09-01 15:16:44
CI框架整合smarty步骤详解
2023-11-14 11:18:11
MySQL查询不含周末的五天前的日期
2008-11-11 12:28:00
Qt数据库应用之实现通用数据生成器
2024-01-16 00:37:49
![](https://img.aspxhome.com/file/2023/1/114931_0s.jpg)
python字符串常规操作大全
2021-04-11 17:20:36
![](https://img.aspxhome.com/file/2023/7/80387_0s.png)
最简便的备份MySQL数据库的方法
2008-12-25 13:16:00
怎样使你的 JavaScript 代码简单易读(推荐)
2024-04-16 09:53:22
MySQL实时监控工具orztop的使用介绍
2024-01-13 18:15:17
![](https://img.aspxhome.com/file/2023/9/66619_0s.png)
python操作xlsx格式文件并读取
2021-07-01 20:03:26
![](https://img.aspxhome.com/file/2023/7/110257_0s.png)
php官方微信接口大全(微信支付、微信红包、微信摇一摇、微信小店)
2023-10-17 14:16:56
python实现修改固定模式的字符串内容操作示例
2023-05-13 21:44:04
设置SQLServer数据库中某些表为只读的多种方法分享
2012-07-11 15:41:05
asp如何用WSH获取机器的IP配置信息?
2010-06-13 14:39:00
Python文件操作的方法
2022-10-27 19:54:29
![](https://img.aspxhome.com/file/2023/8/107938_0s.jpg)
页面重构中的模块化思维
2009-06-28 15:36:00
mysql中json_extract的使用方法实例详解
2024-01-19 04:28:04
详解Python异常处理中的Finally else的功能
2023-08-19 01:01:20
python获取网页中所有图片并筛选指定分辨率的方法
2021-07-28 04:57:23
![](https://img.aspxhome.com/file/2023/6/71446_0s.jpg)