python实现飞机大战(面向过程)
作者:QLUGCL 时间:2022-04-18 10:44:17
本文实例为大家分享了python实现飞机大战的具体代码,供大家参考,具体内容如下
游戏的实现本质是多个图片的快速切换,类似动画一样,达到动态的效果。
比如 * 的发射,实际上是一个 * 的照片根据列表中存储的位置多次粘贴到界面上。
还有飞机的移动是首先收集到移动信息将坐标变化,然后下一次覆盖页面的时候进行粘贴。
import pygame
import time
from pygame.locals import *
hero_x = 150
hero_y = 600
# * 夹
mybullet = []
# 导弹夹
bomb_list = []
enemy_x = 0
enemy_y = 0
flag = 0
enemy_life = "live"
hero_life = "live"
# 飞机 *
a = pygame.image.load("./feiji/enemy1_down1.png")
b = pygame.image.load("./feiji/enemy1_down2.png")
c = pygame.image.load("./feiji/enemy1_down3.png")
d = pygame.image.load("./feiji/enemy1_down4.png")
e = pygame.image.load("./feiji/enemy1_hit.png")
a1 = pygame.image.load("./feiji/hero_blowup_n1.png")
b1 = pygame.image.load("./feiji/hero_blowup_n2.png")
c1 = pygame.image.load("./feiji/hero_blowup_n3.png")
d1 = pygame.image.load("./feiji/hero_blowup_n4.png")
hero_explode = [a1, b1, c1, d1]
explode = [a, b, c, d, e]
num = 0
hnum = 0
count = 0
def enemy_plant(screen, enemy, bomb):
global enemy_x
global enemy_y
global flag
global num
global count
global bomb_list
global enemy_life
if enemy_life == "live":
if flag == 1 and enemy_x >= 0:
enemy_x -= 20
else:
flag = 0
if flag == 0 and enemy_x <= 320:
enemy_x += 20
else:
flag = 1
screen.blit(enemy, (enemy_x, enemy_y))
elif enemy_life == "death":
screen.blit(explode[num], (enemy_x, enemy_y))
bomb_list.clear()
num += 1
if num == 4:
enemy_life = "live"
num = 0
if count % 10 == 0:
bomb_list.append({"x": enemy_x + 30, "y": enemy_y + 20})
count += 1
for b in bomb_list:
screen.blit(bomb, (b["x"], b["y"]))
b["y"] += 10
def hero_plant(screen, hero, bullet):
global hero_x
global hero_y
global enemy_life
global hero_explode
global enemy_x
global enemy_y
global hnum
global bomb_list
global hero_life
for b in bomb_list:
# 注意区间的取值
if b["x"] <= hero_x + 30 and b["x"] >= hero_x and b["y"] >= hero_y and b["y"] <= hero_y + 30:
hero_life = "death"
break
if hero_life == "death":
mybullet.clear()
screen.blit(hero_explode[hnum], (hero_x, hero_y))
hnum += 1
if hnum == 4:
hnum = 0
hero_life = "live"
if hero_life == "live":
screen.blit(hero, (hero_x, hero_y))
# 事件捕获,将捕获的事件放在列表中
# 快速运行然后接受命令造成连续性的画面,有的时候可能为空。
for event in pygame.event.get():
# 关闭游戏
if event.type == pygame.QUIT:
# 退出游戏
exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
hero_x += 10
elif event.key == pygame.K_LEFT:
hero_x -= 10
elif event.key == pygame.K_DOWN:
hero_y += 10
elif event.key == pygame.K_UP:
hero_y -= 10
elif event.key == pygame.K_SPACE:
print("发射 * ")
mybullet.append({"x": hero_x + 30, "y": hero_y - 20})
for i in mybullet:
# 注意出界的 * 所以要大于0
if i["x"] <= enemy_x + 20 and i["x"] >= enemy_x and i["y"] <= 20 and i["y"] >= 0:
enemy_life = "death"
screen.blit(bullet, (i["x"], i["y"]))
# * 模式
# screen.blit(bullet, (i["x"]-20, i["y"]))
# screen.blit(bullet, (i["x"]+20, i["y"]))
# 这样就可以自动控制上升和时间间隔了
i["y"] -= 10
def main():
'''流程控制'''
# 1 创建一个游戏窗口
# display方法:展示相关的都会用到这个方法
# 参数1:元组(长,高)像素
# 参数2:有无特殊功能
# 参数3:像素深度
screen = pygame.display.set_mode((400, 800), 0, 32)
# 加载背景图片
background = pygame.image.load("./feiji/background.png")
# 加载飞机图片
hero = pygame.image.load("./feiji/hero1.png")
# 加载 * 照片
bullet = pygame.image.load("./feiji/plane.png")
# 加载导弹
bomb = pygame.image.load("./feiji/bomb-1.gif")
# 加载敌人飞机照片
enemy = pygame.image.load("./feiji/enemy1.png")
# 图片添加到屏幕
# blit剪切,粘贴
# screen 类似指针的使用带动目的地址的数据改动
while True:
screen.blit(background, (0, 0))
# 显示英雄飞机
hero_plant(screen, hero, bullet)
# 显示敌人飞机
enemy_plant(screen, enemy, bomb)
# 数据更新加载出来
pygame.display.update()
# 图片多显示一会
time.sleep(0.1)
main()
来源:https://blog.csdn.net/qq_43813373/article/details/107104241
标签:python,飞机大战
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python将三维数组展开成二维数组的实现
2023-01-07 06:34:30
简单介绍Python中的struct模块
2023-09-22 16:27:01
Oracle7.X 回滚表空间数据文件误删除处理方法
2010-07-28 12:54:00
老生常谈Python startswith()函数与endswith函数
2023-06-03 02:02:42
比较详细PHP生成静态页面教程
2023-10-14 18:54:31
Python异常处理知识点总结
2023-01-04 16:03:36
可以用ASP生成由客户浏览器处理的客户端脚本吗?
2009-11-01 18:06:00
php实现的一段简单概率相关代码
2023-11-23 02:20:28
网页设计经验谈
2007-10-30 13:11:00
python opencv将图片转为灰度图的方法示例
2021-03-24 22:22:54
![](https://img.aspxhome.com/file/2023/2/95372_0s.jpg)
用css制作星级投票评分功能
2008-01-08 21:12:00
![](https://img.aspxhome.com/file/UploadPic/20071/2007131101422899s.gif)
Python集中化管理平台Ansible介绍与YAML简介
2023-09-23 12:52:34
![](https://img.aspxhome.com/file/2023/6/88026_0s.png)
基于python if 判断选择结构的实例详解
2023-01-15 22:45:03
机器学习10大经典算法详解
2021-02-21 01:39:57
JavaScript创建可维护幻灯片效果
2008-06-21 16:42:00
![](https://img.aspxhome.com/file/UploadPic/20086/21/2008621165112211s.gif)
Mac下python包管理工具pip的安装
2023-11-19 11:09:46
![](https://img.aspxhome.com/file/2023/1/87701_0s.png)
一文详解Go语言fmt标准库的常用占位符使用
2023-08-07 01:57:56
MySQL五个查询优化方法
2009-08-29 15:05:00
纯CSS实现导航下拉菜单
2007-11-25 15:11:00
![](https://img.aspxhome.com/file/UploadPic/200711/25/2007112515291686s.gif)
如何爬取通过ajax加载数据的网站
2022-05-03 15:06:01
![](https://img.aspxhome.com/file/2023/7/94077_0s.png)