利用pygame实现了简易版飞机大战。源代码如下:

# -*- coding:utf-8 -*-
import pygame
import sys
from pygame.locals import *
from pygame.font import *
import time
import randomclass Hero(object):#玩家 英雄类def __init__(self, screen_temp):self.x = 210self.y = 700self.life = 21# self.life = 100self.image = pygame.image.load("./feiji/hero1.png")self.screen = screen_tempself.bullet_list = []#用来存储子弹对象的引用#爆炸效果用的如下属性self.hit = False #表示是否要爆炸self.bomb_list = [] #用来存储爆炸时需要的图片self.__create_images() #调用这个方法向bomb_list中添加图片self.image_num = 0 #用来记录while True的次数,当次数达到一定值时才显示一张爆炸的图,然后清空,,当这个次数再次达到时,再显示下一个爆炸效果的图片self.image_index = 0#用来记录当前要显示的爆炸效果的图片的序号def __create_images(self):#添加爆炸图片self.bomb_list.append(pygame.image.load("./feiji/hero_blowup_n1.png"))self.bomb_list.append(pygame.image.load("./feiji/hero_blowup_n2.png"))self.bomb_list.append(pygame.image.load("./feiji/hero_blowup_n3.png"))self.bomb_list.append(pygame.image.load("./feiji/hero_blowup_n4.png"))def display(self):#显示玩家的飞机#如果被击中,就显示爆炸效果,否则显示普通的飞机效果if self.hit == True:self.screen.blit(self.bomb_list[self.image_index], (self.x, self.y))#(self.x, self.y)是指当前英雄的位置#blit方法 (一个对象,左上角位置)self.image_num += 1print(self.image_num)if self.image_num == 7:self.image_num = 0self.image_index += 1print(self.image_index) #这里子弹打住英雄时没有被清除掉,所以打一下,就死了if self.image_index > 3:time.sleep(1)exit()#调用exit让游戏退出#self.image_index = 0else:if self.x< 0:   #控制英雄,不让它跑出界面self.x = 0elif self.x > 382:self.x = 382if self.y < 0:self.y = 0elif self.y > 750:self.y = 750self.screen.blit(self.image,(self.x, self.y))#z这里是只要没有被打中,就一直是刚开始的样子#不管玩家飞机是否被击中,都要显示发射出去的子弹for bullet in self.bullet_list:bullet.display()bullet.move()def move(self, move_x,move_y):self.x += move_xself.y += move_ydef fire(self):#通过创建一个子弹对象,完成发射子弹bullet = Bullet(self.screen, self.x, self.y)#创建一个子弹对象self.bullet_list.append(bullet)def bomb(self):self.hit = Truedef judge(self):global lifeif life <= 0:self.bomb()class Bullet(object):#玩家子弹类def __init__(self, screen_temp, x_temp, y_temp):self.x = x_temp + 40self.y = y_temp - 20self.image = pygame.image.load("./feiji/bullet.png")self.screen = screen_tempdef display(self):self.screen.blit(self.image, (self.x, self.y))def move(self):self.y -= 10class Bullet_Enemy(object):#敌机子弹类def __init__(self, screen_temp, x_temp, y_temp):self.x = x_temp + 25self.y = y_temp + 30self.image = pygame.image.load("./feiji/bullet1.png")self.screen = screen_tempdef display(self):self.screen.blit(self.image,(self.x,self.y))def move(self, hero):self.y += 10global lifeif (hero.y <= self.y and self.y <= hero.y + 40) and (hero.x <= self.x and self.x <= hero.x + 100):#if self.y in range(hero.y, hero.y + 40) and self.x in range(hero.x, hero.x + 40):life -= 10#self.bullet_list.remove()print("---judge_enemy---")return Trueif life<=0:hero.bomb()return Falseclass Bullet_Boss(object):#boss子弹类1def __init__(self, screen_temp, x_temp, y_temp):self.x = x_temp + 80self.y = y_temp + 230self.image = pygame.image.load("./feiji/bullet2.png")self.screen = screen_tempdef display(self):self.screen.blit(self.image, (self.x, self.y))def move(self, hero):self.y += 6self.x += 2global lifeif (hero.y <= self.y and self.y <= hero.y + 40) and (hero.x <= self.x and self.x <= hero.x + 100):#if self.y in range(hero.y, hero.y + 40) and self.x in range(hero.x, hero.x + 40):life -= 20#self.bullet_list.remove()print("---judge_boss---")return Trueif life<=0:hero.bomb()return Falseclass Bullet_Boss1(object):#boss子弹类2def __init__(self, screen_temp, x_temp, y_temp):self.x = x_temp + 80self.y = y_temp + 230self.image = pygame.image.load("./feiji/bullet2.png")self.screen = screen_tempdef display(self):self.screen.blit(self.image, (self.x, self.y))def move(self, hero):self.y += 6self.x -= 2global lifeif (hero.y <= self.y and self.y <= hero.y + 40) and (hero.x <= self.x and self.x <= hero.x + 100):#if self.y in range(hero.y, hero.y + 40) and self.x in range(hero.x, hero.x + 40):life -= 20#self.bullet_list.remove()print("---judge_boss---")return Trueif life<=0:hero.bomb()return Falseclass Bullet_Boss2(object):#boss子弹类3def __init__(self, screen_temp, x_temp, y_temp):self.x = x_temp + 80self.y = y_temp + 230self.image = pygame.image.load("./feiji/bullet2.png")self.screen = screen_tempdef display(self):self.screen.blit(self.image, (self.x, self.y))def move(self, hero):self.y += 6global lifeif (hero.y <= self.y and self.y <= hero.y + 40) and (hero.x <= self.x and self.x <= hero.x + 100):#if self.y in range(hero.y, hero.y + 40) and self.x in range(hero.x, hero.x + 40):life -= 20 #self.bullet_list.remove()print("---judge_boss---")return Trueif life<=0:hero.bomb()return Falseclass Base(object):#基类  类似于抽象类def __init__(self, screen_temp, x, y, image_name):self.x = xself.y = y self.screen = screen_tempself.image = pygame.image.load(image_name)self.alive = Truedef display(self):if self.alive == True:self.screen.blit(self.image, (self.x, self.y))def move(self):self.y += 5class bomb_bullet(Base):#炸弹类def __init__(self, screen_temp):Base.__init__(self, screen_temp, random.randint(45, 400), 0, "./feiji/bomb.png")def judge(self, hero):if (hero.y <= self.y and self.y <= hero.y + 40) and (hero.x <= self.x and self.x <= hero.x + 100):self.alive = Falsehero.bomb()if self.y >= 850:#self.alive = Falseself.y = 0self.x = random.randint(45, 400)#print("bomb.y = %d"%self.y)class supply(Base):#补给类def __init__(self, screen_temp):Base.__init__(self, screen_temp, random.randint(45, 400), -300, "./feiji/bomb-1.gif")def judge(self, hero):global lifeif (hero.y <= self.y and self.y <= hero.y + 40) and (hero.x <= self.x and self.x <= hero.x + 100):self.alive = Falselife += 10if self.y >= 1500:self.y = 0self.x = random.randint(45, 400)self.alive = Trueclass clear_bullet(Base):def __init__(self, screen_temp):Base.__init__(self, screen_temp, random.randint(45, 400), 0, "./feiji/bomb-2.gif")self.alive = Falsedef judge(self, hero, enemies):global qq += 1#self.move()if q == 20:#self.move()self.alive = Trueq = 0if (hero.y <= self.y and self.y <= hero.y + 40) and (hero.x <= self.x and self.x <= hero.x + 100):self.alive = Falsefor enemy in enemies:enemy.hit == Trueclass EnemyPlane(object):#敌机类def __init__(self, screen_temp):self.x = random.randint(15, 480)self.y = 0self.image = pygame.image.load("./feiji/enemy0.png")self.screen = screen_tempself.bullet_list = []#用来存储子弹对象的引用#self.direction = "right"#用来设置这个飞机默认的移动方向self.hit = Falseself.bomb_list = []self.__create_images()self.image_num = 0self.image_index = 0#利用产生的随机数,随机确定飞机初始移动方向self.k = random.randint(1, 20)if self.k <= 10:self.direction = "right"elif self.k > 10:self.direction = "left"def display(self, hero):#显示敌人的飞机if not self.hit:self.screen.blit(self.image, (self.x,self.y))else:self.screen.blit(self.bomb_list[self.image_index], (self.x,self.y))self.image_num += 1if self.image_num == 3 and self.image_index < 3:self.image_num = 0self.image_index += 1#print(self.image_index)# if self.image_index > 2:#     time.sleep(0.1)for bullet in self.bullet_list:bullet.display()if(bullet.move(hero)):self.bullet_list.remove(bullet)def move(self):#利用随机数来控制飞机移动距离,以及移动范围d1 = random.uniform(1,3)d2 = random.uniform(0.2,3)p1 = random.uniform(50,100)p2 = random.uniform(-200,0)if self.direction == "right":self.x += d1elif self.direction == "left":self.x -= d1if self.x > 480 - p1:#480 - 50self.direction="left"elif self.x < p2:self.direction = "right"self.y += d2def bomb(self):self.hit = Truedef __create_images(self):self.bomb_list.append(pygame.image.load("./feiji/enemy0_down1.png"))self.bomb_list.append(pygame.image.load("./feiji/enemy0_down2.png"))self.bomb_list.append(pygame.image.load("./feiji/enemy0_down3.png"))self.bomb_list.append(pygame.image.load("./feiji/enemy0_down4.png"))def fire(self):#利用随机数来控制敌机的开火,1/80的概率s = random.randint(0,800)bullet1 = Bullet_Enemy(self.screen, self.x, self.y)if s < 10:self.bullet_list.append(bullet1)class EnemyPlanes(EnemyPlane):#敌机群类  继承自EnemyPlane类  def __init__(self, screen_temp):EnemyPlane.__init__(self, screen_temp)self.num = 0self.enemy_list = []  #用列表存储产生的多架敌机self.screen = screen_tempdef add_enemy(self, num):  #产生多架敌机的函数self.num = numfor i in range(num):enemy = EnemyPlane(self.screen)self.enemy_list.append(enemy)def display(self, hero):for i in range(self.num):self.enemy_list[i].display(hero)def move(self):for i in range(self.num): self.enemy_list[i].move()def fire(self):#s = random.randint(0,1000)for i in range(self.num):self.enemy_list[i].fire()class Boss(EnemyPlane):#boss敌机类 继承自EnemyPlane类def __init__(self,screen_temp):EnemyPlane.__init__(self,screen_temp)self.x = 150self.y = 0self.bomb_list = []self.__create_images()self.image = pygame.image.load("./feiji/enemy2.png")self.screen = screen_tempself.bullet_list = []def __create_images(self):#self.bomb_list.append(pygame.image.load("./feiji/enemy2.png"))self.bomb_list.append(pygame.image.load("./feiji/enemy2.png"))self.bomb_list.append(pygame.image.load("./feiji/enemy2_down1.png"))self.bomb_list.append(pygame.image.load("./feiji/enemy2_down2.png"))self.bomb_list.append(pygame.image.load("./feiji/enemy2_down3.png"))self.bomb_list.append(pygame.image.load("./feiji/enemy2_down4.png"))self.bomb_list.append(pygame.image.load("./feiji/enemy2_down5.png"))self.bomb_list.append(pygame.image.load("./feiji/enemy2_down6.png"))def display(self, hero):#显示敌人的飞机global g#print(g)self.screen.blit(self.bomb_list[g], (self.x,self.y))for bullet in self.bullet_list:bullet.display()if(bullet.move(hero)):self.bullet_list.remove(bullet)def move(self):d1 = 0self.y += 0def fire(self):global ss += 1bullet1 = Bullet_Boss(self.screen, self.x, self.y)bullet2 = Bullet_Boss1(self.screen, self.x, self.y)bullet3 = Bullet_Boss2(self.screen, self.x, self.y)if s == 20:s = 0 self.bullet_list.append(bullet1)self.bullet_list.append(bullet2)self.bullet_list.append(bullet3)def judge1(hero,enemy):#判断敌机的炸毁for bullet1 in hero.bullet_list:if bullet1.y in range(int(enemy.y),int(enemy.y + 30)) and bullet1.x in range(int(enemy.x-10),int(enemy.x + 50)):hero.bullet_list.remove(bullet1)enemy.bomb()if bullet1.y < 0 or bullet1.x < 0 or bullet1.x > 480: #删除越界的玩家子弹hero.bullet_list.remove(bullet1) def judge3(hero,boss):#判断boss的炸毁global goal, g, goal0for bullet3 in hero.bullet_list:if bullet3.y in range(int(boss.y), int(boss.y + 60)) and bullet3.x in range(int(boss.x), int(boss.x + 100)):hero.bullet_list.remove(bullet3)g += 1boss.image = boss.bomb_list[g]print("g = %d"%g)if g >= 6:boss.y, g, goal = 0, 0, 0boss.bomb()goal0 += 10 def clear_enemy(enemies):#清除敌机群类中被炸毁的敌机global goal, goal0for enemy in enemies.enemy_list:if enemy.hit == True and enemy.image_index == 3:enemies.enemy_list.remove(enemy)enemies.num -= 1goal += 1goal0 += 5print("goal = %d"%goal)if enemy.y >= 850:enemies.enemy_list.remove(enemy)enemies.num -= 1def judge_num(enemies):#判断频幕上敌人的数量,如果为零,继续添加敌人n = random.randint(1,5)if len(enemies.enemy_list) == 0:enemies.add_enemy(n)def show_text(screen_temp):#在屏幕上显示文字text = "GOAL:" + str(goal0) + "Life:" + str(life) font_size = 50pos = (0,0)color = (0,255,0)cur_font = pygame.font.SysFont("宋体",font_size)text_fmt = cur_font.render(text, 1, color)screen_temp.blit(text_fmt, pos)def creat_bomb(screen_temp):bomb = bomb_bullet(screen_temp)bomb_list = []bomb_list.apend(bomb)#定义的全局变量
goal = 0  #玩家得分
goal0 = 0
g = 0 #击中boss的次数
life = 100#生命值
s = 0 #判断大boss是否发射子弹
q = 0def main():#主函数执行#获取事件,比如按键等bb = Falsemove_x = 0move_y = 0pygame.init()screen = pygame.display.set_mode((480,852),0,32)#                                 210,400background = pygame.image.load("./feiji/background.png")pygame.display.set_caption("飞机大战")atlas = pygame.image.load("./feiji/New Atlas.png")#创建玩家飞机hero = Hero(screen)#创建敌机群enemis = EnemyPlanes(screen)enemis.add_enemy(5)#创建boss对象boss = Boss(screen)#创建炸弹对象bomb = bomb_bullet(screen)#创建补给对象supply0 = supply(screen)clear = clear_bullet(screen)left_key, right_key, up_key, down_key, done = 0, 0, 0, 0, 0# mark = 0#用来判断boss发射子弹while True:if done:if done % 8 == 0:done = 1hero.fire()else:done += 1for event in pygame.event.get():#判断是否是点击了退出按钮if event.type == QUIT:print("exit")exit()#判断是否是按下了键if event.type == KEYDOWN :#down#检测按键是否是a或者leftif event.key == K_a or event.key == K_LEFT:#print('left')move_x = -5left_key += 1 #检测按键是否是d或者rightelif event.key == K_d or event.key == K_RIGHT:#print('right')move_x = 5right_key += 1elif event.key == K_w or event.key == K_UP:move_y = -5up_key += 1elif event.key == K_s or event.key == K_DOWN:move_y = 5down_key += 1#检测按键是否是空格键elif event.key == K_SPACE:#print('space')hero.fire()done = 1#enemis.fire()elif event.key == K_b:print('b')hero.bomb()if event.type == KEYUP:if event.key == K_a or event.key == K_LEFT:left_key -= 1if right_key == 0:move_x = 0else:move_x = 5if event.key == K_d or event.key == K_RIGHT:right_key -= 1if left_key == 0:move_x = 0else:move_x = -5if event.key == K_w or event.key == K_UP:up_key -= 1if down_key == 0:move_y = 0else:move_y = 5  if event.key == K_s or event.key == K_DOWN:down_key -= 1if up_key == 0:move_y = 0else:move_y = -5if event.key == K_SPACE:done = 0 screen.blit(background, (0, 0))hero.move(move_x, move_y)hero.display()hero.judge()enemis.display(hero)enemis.move()enemis.fire()bomb.display()bomb.judge(hero)bomb.move()supply0.display()supply0.judge(hero)supply0.move()#clear.display()#clear.judge(hero, enemis)#clear.move()for i in range(enemis.num):judge1(hero, enemis.enemy_list[i])#enemis.enemy_list[i].judge(hero)clear_enemy(enemis)judge_num(enemis)show_text(screen)if goal >= 15:boss.display(hero)boss.move()# mark+=1# if mark==8:boss.fire()# mark = 0#boss.judgejudge3(hero, boss)pygame.display.update()if __name__ == "__main__":main()

方法是利用面向对象的思想,写了基本的敌机类、英雄类、武器类等,利用继承关系产生多架敌机。
相关文件、图片、素材、源代码下载
提取码:p453

上面链接已失效,新链接地址:

链接: https://pan.baidu.com/s/1guKzPpaPkbsz3dJZJlZl8g 提取码: 2uyb 复制这段内容后打开百度网盘手机App,操作更方便哦

pygame的应用——python版飞机大战相关推荐

  1. python版飞机大战

    python版飞机大战 用python几百行代码搞定飞机大站游戏. 我们利用pygame包进行飞机大战的游戏开发,所以大家首先得安装好pygame包,本游戏一共封装了8个类,大家可以在GitHub上下 ...

  2. python飞机大战源代码-python版飞机大战代码分享

    利用pygame实现了简易版飞机大战.源代码如下: # -*- coding:utf-8 -*- import pygame import sys from pygame.locals import ...

  3. 重温经典:Python版飞机大战源码 神器 玩游戏就玩自己开发的

    目录 导语 正文 一.环境安装 二.我方飞机 三.敌方飞机 四.控制键盘移动 五.检测子弹碰撞 六.效果图 总结 免费源码领取处: 往期文章推荐-- Python从入门到实战-- 绘图Turtle系列 ...

  4. python版飞机大战及码源

    飞机大战是之前学习python时根据网上的代码改编写成的,有完整的代码和素材. 文件结构: images文件:放置程序所需要的图片素材 sound文件:放置程序所需要的声音素材 bullet.py:有 ...

  5. python版飞机大战源码和素材免费

    个人写的python飞机大战 初学python 里面有两个版本 一个是多敌机版 一个是单敌机版 点击打开链接

  6. Python版飞机大战游戏的设计(一)-----敌机出场

    pygame 快速入门 目标 项目准备 使用 pygame 创建图形窗口 理解 图像 并实现图像绘制 理解 游戏循环 和 游戏时钟 理解 精灵 和 精灵组 项目准备 新建 飞机大战 项目 新建一个 h ...

  7. pygame为游戏添加背景_用 Python 制作飞机大战小游戏

    这这次用Python中的pygame模块来完成一个飞机大战的小游戏:基本思路是通过方向键来控制飞机的左右移动射击飞船.先来看下最后的效果为了新手也能完成,本文记录了编写的全部流程,也就是每次修改的代码 ...

  8. python实现飞机大战游戏

    python实现飞机大战小游戏(含源码+视频资源) 导语: 正文: 1.开发工具 2.环境搭建 3.效果如下 Step1:定义精灵类 Step2:实现游戏主循环 Step3:制作简易的游戏开始和结束界 ...

  9. python写飞机大战游戏_python实现飞机大战游戏

    飞机大战(Python)代码分为两个python文件,工具类和主类,需要安装pygame模块,能完美运行(网上好多不完整的,调试得心累.实现出来,成就感还是满满的),如图所示: 完整代码如下: 1.工 ...

最新文章

  1. 共抗疫情,飞书助力学校、企业等组织机构高效远程协作
  2. 深度报告:中国将主宰5G时代?华为中兴的最大机会来了
  3. 1.8 ionic3入门——测滑菜单(side menu)中的界面跳转
  4. python读取txt文件并批量写入不同的excel
  5. 【WPF学习笔记】[转]周银辉之WPF中的动画 晓风影天之wpf动画——new PropertyPath属性链...
  6. 无奈!《花木兰》《速度与激情9》等多部影片宣布撤档
  7. css样式的百分比都相对于谁?
  8. 错排问题(以航电OJ 2048 为例)
  9. JAVA----数组(一)
  10. 存储器火热 大陆厂商3倍薪水赴台挖人
  11. python是免费的、开源的、跨平台的_推荐:3款开源的Python IDE
  12. 手游内存辅助开发教程
  13. 图片格式转换jpg,图片批量转成jpg
  14. 第7章第36节:六图排版:三张图片一组并列排版 [PowerPoint精美幻灯片实战教程]
  15. cfdpost导出图片_科学网—去除 cfd post 输出eps文件中的莫名其妙的点 - 姚程的博文...
  16. 1.27 Daisy Chains(花瓣)
  17. Luogu 3371【模板】单源最短路径
  18. 频繁撤单有风险?--这些委托类型让您享受“撤单豁免权”
  19. 利用触发器设计计数器
  20. C#开发BIMFACE系列19 服务端API之获取模型数据4:获取多个构件的共同属性

热门文章

  1. 如何去掉 Word 文档的标记
  2. Java—代码块详解
  3. CVPR 2022 | 基于GAN生成 艺术文字logo及布局
  4. 【自动驾驶轨迹规划之安全行驶走廊】
  5. html登陆没反应,点击登录没反应怎么办 点击登录没反应解决方法【步骤】
  6. WKWebView 点击无反应
  7. 基于开源CNN的图像压缩算法
  8. db2 linux 导入数据_linux导入db2数据库
  9. ChatGPT提示词指令大全调教指南,6个网站让你轻松玩转AI聊天机器人!
  10. 抖音怎么赚钱?好消息KTV模式来啦