python实现飞机大战(面向过程)
作者:QLUGCL 发布时间:2022-04-18 10:44:17
标签:python,飞机大战
本文实例为大家分享了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


猜你喜欢
- 概述Go 语言中的 new 和 make 一直是新手比较容易混淆的东西,咋一看很相似。不过解释两者之间的不同也非常容易。new 的主要特性首
- 本文实例为大家分享了vue实现拖拽交换位置的具体代码,供大家参考,具体内容如下<template> <div
- 每点击一次按钮,弹出一个对话框(子窗口),同时开启一个子线程来执行任务并更新对话框内容,关闭对话框则关闭对应子线程1. 建立一个简单的主界面
- mktime()方法是localtime()反函数。它的参数是struct_time或全9元组,它返回一个浮点数,为了兼容时ti
- 本文实例讲述了python写入中英文字符串到文件的方法。分享给大家供大家参考。具体分析如下:python中如果使用系统默认的open方法打开
- f-string 字符串格式化语法f-string 是 Python 3.6 版本引入的一种新的字符串格式化语法。与其他字符串格式
- 废话不多说,直接上官网demopackage mainimport ("log""github.com/fsn
- 好久都没有写博客了,主要是太懒了,尤其是在阳春三月,风和日丽的日子,太阳暖暖的照在身上,真想美美的睡上一觉。就导致了这篇博客拖到现在才开始动
- 导语电脑桌面文件太多查找起来比较花费时间,并且凌乱的电脑桌面也会影响工作心情,于是利用python根据时间自动建立当日文件夹,这样就可以把桌
- python是很容易上手的编程语言,但是有些时候使用python编写的程序并不能保证其运行速度(例如:while 和 for),这个时候我们
- 实现代码# batch_handle_image.pyimport argparseimport globimport osfrom PIL
- 所谓“分块”,顾名思义,就是将数据集分成几块进行读取,比如有105条数据,一次读取10条,读取11次
- 先举个例子,以前负责教育培训类网站的时候,曾经接到过这样一个项目,需求方希望做一个充满趣味性的新手入门频道,页面要炫,最好是flash,用户
- 每次在"万达影城"网上购票总会用到左上角选择城市的功能。如下:今天就在ASP.NET MVC中实现一下。我想最好的方式应
- windows server 2019安装了SQL2016,启动sql agent代理时候,提示“尚未定义空闲cpu条件 onidle作业计
- 问题描述:在pyhton脚本中logging.info("hello world")希望输出'hello wor
- 1、800*600下,网页宽度保持在778以内,就不会出现水平滚动条,高度则视版面和内容决定。2、1024*768下,网页宽度保持在1002
- 目录一、常见orm数据库框架1、peewee 简单demo二、Model 和 Field 关系三、Model 模型四、Filed 字段1、字
- 当然,第一反应是用存储过程。判断原来这个字段值,然后UPDATE。 网上粗粗找了一下没找到方案。自己一动手,居然有个很有趣的结果,连WHER
- 数据迁移需要从mysql导入clickhouse, 总结方案如下,包括clickhouse自身支持的三种方式,第三方工具两种。create