python飞机大战游戏实例讲解
作者:小妮浅浅 时间:2021-12-07 14:43:26
记得刚学python那会,作过一个飞机大战小项目,这个项目非常经典,可以帮助初学者提高动手能力,今天把它分享出来。
一、项目介绍
先放几张图片
二、项目实现
1.首先安装库
pip install pygame
2.主要python代码
import pygame
from pygame.locals import *
import random
#https://blog.csdn.net/qq_36079986/article/details/110395731
class HeroPlane(object):# 创建一个飞机的英雄类
def __init__(self,screen_temp): # 始化方法
self.x = 200
self.y = 500
self.image = pygame.image.load('./img/hero1.png')
self.screen = screen_temp# 因为在display方法中使用到了screen方法# 但是在类中没有 也不能调用 # 所以说 以传参的方法传递到类中
self.bullet_list = []
def display(self):# 创建一个展现飞机的方法
herp_x1, herp_y1, herp_x2, herp_y2, herp_x3, herp_y3 = -100, -100, -100, -100, -100, -100
self.screen.blit(self.image,(self.x, self.y))
for bullet in self.bullet_list:###遍历 * 类对象,赋值给bullet
herp_x1, herp_y1, herp_x2, herp_y2, herp_x3, herp_y3 = bullet.display()###### bullet对象调用display方法, 显示 *
bullet.move()
if self.bullet_list[0].y < 0:
del self.bullet_list[0]
return herp_x1, herp_y1, herp_x2, herp_y2, herp_x3, herp_y3
def move_left(self):
self.x -= 50
def move_right(self):
self.x += 50
def move_up(self):
self.y -= 50
def move_down(self):
self.y += 50
def fire(self):
print(len(self.bullet_list),'个 * ##########################################')
self.bullet_list.append(HeroBullet(self.screen,self.x, self.y))
class HeroBullet(object):
def __init__(self,screen_temp,x_temp,y_temp):
self.x = x_temp
self.y = y_temp
self.image = pygame.image.load('./img/bullet.png')
self.screen = screen_temp
def display(self):
self.screen.blit(self.image,(self.x - 22, self.y-22))
self.screen.blit(self.image, (self.x + 39, self.y-22))
self.screen.blit(self.image, (self.x + 100, self.y-22))
return self.x - 22, self.y - 22, self.x + 39, self.y - 22, self.x + 100, self.y - 22
def move(self):
self.y -= 1
class EnemyPlane(object):
def __init__(self,screen_temp):
self.ra = random.randint(0,2)
self.x = random.randint(0,400)
self.y = 0
self.image = pygame.image.load('./img/enemy1.png')
if 0<=self.x<=540:
if self.ra == 0:
self.flag = "left"
elif self.ra == 1:
self.flag = "right"
else:
self.flag = 'middle'
self.screen.blit(self.image, (self.x, self.y))
if self.x < 0:
self.flag = "left"
elif self.x > 450:
self.flag = 'right'
if self.flag == "left":
self.x += 2
elif self.flag == 'right':
self.x -= 2
self.y += 0.1
if (random.randint(0,50) == 1):
self.bullet_list.append(EnemyBullet(self.screen, self.x, self.y))
# print(len(self.bullet_list),'颗敌方 * ')
for bullet in self.bullet_list:
bullet.display()
if bullet.y > 700:
del bullet
class EnemyBullet(object):
def __init__(self,screen_temp,x,y):
self.x = x + 30
self.y = y + 90
self.image = pygame.image.load('./img/bullet-1.gif')
self.y+= 10
def key_control(hero_temp):
for event in pygame.event.get(): # 判断输入类型
if event.type == QUIT:#判断是否是点击了退出按钮
print("exit")
exit()
elif event.type == KEYDOWN:#判断是否是按下了键# 如果是键盘事件(键盘按下事件)
if event.key == K_a or event.key == K_LEFT:
hero_temp.move_left()
elif event.key == K_d or event.key == K_RIGHT:
hero_temp.move_right()
elif event.key == K_w or event.key == K_UP:
hero_temp.move_up()
elif event.key == K_s or event.key == K_DOWN:
hero_temp.move_down()
elif event.key == K_SPACE:#检测按键是否是空格键
hero_temp.fire()
def main():
screen = pygame.display.set_mode((480, 650), 0, 32) # 使用screen资源来创建背景# 返回值是一个资源
pygame.display.set_caption("aircraft game") # 设置窗口标题
background = pygame.image.load('./img/background.png')## ./ 当前路径 ../ 上一级路径 / 根目录
hero = HeroPlane(screen)
enemy_list = []
j = 0
while True:
screen.blit(background, (0, 0)) # 资源中的blit方法能够加载游戏的画面
herp_x1, herp_y1, herp_x2, herp_y2, herp_x3, herp_y3 = hero.display() #####刷新
if len(enemy_list) <= 10:
enemy_list.append(EnemyPlane(screen))
if enemy_list[0].y >= 700:
del enemy_list[0]
for enemy_each in enemy_list :
# enemy_each.fire()
enemy_each.move()
enemy_each.display()
# print(len(enemy_list), '个敌机')
if (enemy_each.x <= herp_x1 -10 <= enemy_each.x + 80 and enemy_each.y -10<= herp_y1<= enemy_each.y + 90) or (enemy_each.x -10 <= herp_x2<= enemy_each.x + 80 and enemy_each.y -10<= herp_y2<= enemy_each.y + 90) or (enemy_each.x-10 <= herp_x3<= enemy_each.x + 80 and enemy_each.y -10<= herp_y3<= enemy_each.y + 90):
enemy_list.remove(enemy_each)
# print(len(enemy_list), '个敌机')
print('jizhon')
# print(hero.x,type(hero.x))
if j % 100 == 5:
print(len(enemy_list),'个敌机')
pygame.display.update() ### 加载游戏
key_control(hero)# 调用键盘事件函数
j += 1
# print(herp_x1, herp_y1, herp_x2, herp_y2, herp_x3, herp_y3)
if __name__ == '__main__':
main()
三、说明:代码功能都在注释里面,我就不做过多解释了;与本项目有关的图片数据我传到资源里面了,大家可以免费下载,我把代码和数据都打包到一起了,大家下载下来导入库就可以直接一些了。
实例补充
import pygame
class MyPlane(pygame.sprite.Sprite):
def __init__(self,bg_size):
pygame.sprite.Sprite.__init__(self)
self.image1 = pygame.image.load(r'E:\learn pygame\image\me1.png').convert_alpha()
self.image2 = pygame.image.load(r'E:\learn pygame\image\me2.png').convert_alpha()
self.image3 = pygame.image.load(r'E:\learn pygame\image\me_protect.png').convert_alpha()
self.destroy_images = []
self.destroy_images.extend([\
pygame.image.load(r'E:\learn pygame\image\me_destroy_1.png').convert_alpha(), \
pygame.image.load(r'E:\learn pygame\image\me_destroy_2.png').convert_alpha(), \
pygame.image.load(r'E:\learn pygame\image\me_destroy_3.png').convert_alpha(), \
pygame.image.load(r'E:\learn pygame\image\me_destroy_4.png').convert_alpha() \
])
self.rect = self.image1.get_rect()
self.width,self.height = bg_size[0],bg_size[1]
self.rect.left,self.rect.top = (self.width - self.rect.width) // 2,self.height - self.rect.height - 60
self.speed = 10
self.active = True
self.invincible = False
self.mask = pygame.mask.from_surface(self.image1)
def moveUp(self):
if self.rect.top > 0:
self.rect.top -= self.speed
else:
self.rect.top = 0
def moveDown(self):
if self.rect.bottom < self.height - 30:
self.rect.top += self.speed
else:
self.rect.bottom = self.height - 30
def moveLeft(self):
if self.rect.left > 0:
self.rect.left -= self.speed
else:
self.rect.left = 0
def moveRight(self):
if self.rect.right < self.width:
self.rect.left += self.speed
else:
self.rect.right = self.width
def reset(self):
self.rect.left,self.rect.top = (self.width - self.rect.width) // 2,self.height - self.rect.height - 30
self.active = True
self.invincible = True
来源:https://www.py.cn/jishu/jichu/21578.html
标签:python,飞机大战
0
投稿
猜你喜欢
Vue下滚动到页面底部无限加载数据的示例代码
2024-06-05 15:31:44
利用Python pandas对Excel进行合并的方法示例
2022-07-31 13:39:41
php下使用iconv需要注意的问题
2023-11-23 03:05:09
MySQL数据库的多种连接方式及工具
2024-01-13 10:37:14
javascript 用函数语句和表达式定义函数的区别详解
2024-04-16 09:06:26
mysql 查询表中平均分最低的班级
2024-01-22 05:23:45
Python函数必须先定义,后调用说明(函数调用函数例外)
2022-03-06 18:03:53
开心网上input输入框研究
2009-03-06 12:52:00
Bootstrap轮播插件简单使用方法介绍
2024-04-27 15:19:06
关于python常见异常以及处理方法
2021-03-17 06:13:34
使用vuex缓存数据并优化自己的vuex-cache
2024-04-30 10:46:33
python网络爬虫实战
2021-04-16 13:31:07
matplotlib.pyplot画图并导出保存的实例
2022-10-02 13:32:26
python global和nonlocal用法解析
2022-05-17 02:56:17
mysql并发控制原理知识点
2024-01-24 15:21:46
用python统计代码行的示例(包括空行和注释)
2022-06-28 02:15:30
Git基础学习之tag标签操作详解
2023-01-01 08:27:20
Python异常处理操作实例详解
2023-05-06 11:31:05
通过python+selenium3实现浏览器刷简书文章阅读量
2022-11-09 09:04:37
InnoDB引擎数据库主从复制同步新的分享
2024-01-15 10:33:32