1.用python做一个飞机大战(一)
2.添加自己飞机的生命,击落敌机的分数,背景音乐(二)
用到的一个python插件,pygame,想要深入学习python游戏开发可访问官网:https://www.pygame.org/news
实现后的飞机大战:

1.创建一个飞机的精灵

import pygame
import randomSCREEN_SIZE = None
CLOCK_TICK = 60
EMENY_FLY_MAX_SPEED = 12#背景类
class BackGround(pygame.sprite.Sprite):def __init__(self,bg_pathname,speed = 1,is_first = True):global SCREEN_SIZE# print(SCREEN_SIZE)super().__init__()self.image = pygame.image.load(bg_pathname)self.rect = self.image.get_rect()  # 0 0 width heightself.speed = speedif not is_first:self.rect.y = -SCREEN_SIZE.heightdef update(self, *args):self.rect.y += self.speedif self.rect.y >SCREEN_SIZE.height:self.rect.y = -SCREEN_SIZE.height#敌机类
class Enemy(pygame.sprite.Sprite):def __init__(self,image_path,plane_size = 1):super().__init__()self.image = pygame.image.load(image_path)self.rect = self.image.get_rect()self.rect.bottom = 0# self.hp = 1self.rect.x = random.randint(0,SCREEN_SIZE.width-self.rect.width)if plane_size == 1:self.speed = random.randint(1,EMENY_FLY_MAX_SPEED)# self.hp = 1elif plane_size == 2:self.speed = random.randint(1,EMENY_FLY_MAX_SPEED/2)# self.hp = EMENY_FLY_MAX_SPEED/2else:self.speed = random.randint(1, EMENY_FLY_MAX_SPEED / 3)# self.hp = EMENY_FLY_MAX_SPEED * 2def update(self, *args):self.rect.y += self.speedif self.rect.y >= SCREEN_SIZE.height:self.kill()def __del__(self):#print('over...')pass#英雄类
class Hero(pygame.sprite.Sprite):def __init__(self,pathname1,pathname2):super().__init__()self.image = Noneself.image_flag = Falseself.speed = {'x':0,'y':0}self.image_all = [pygame.image.load(pathname1),pygame.image.load(pathname2)]self.rect = self.image_all[0].get_rect()self.rect.centerx  = SCREEN_SIZE.centerxself.rect.bottom = SCREEN_SIZE.height - 100self.bullets = pygame.sprite.Group()def update(self):self.image = self.image_all[int(self.image_flag)]self.image_flag = not self.image_flagself.rect.x += self.speed['x']self.rect.y += self.speed['y']if self.rect.centerx < 0:self.rect.centerx = 0if self.rect.centerx >SCREEN_SIZE.width:self.rect.centerx = SCREEN_SIZE.widthif self.rect.top > SCREEN_SIZE.height-self.rect.height:self.rect.bottom = SCREEN_SIZE.heightif self.rect.bottom < 0:self.rect.bottom = 0class Bullet(pygame.sprite.Sprite):def __init__(self,pathname,centerx,top,speed = -3):super().__init__()self.speed = speedself.image = pygame.image.load(pathname)self.rect = self.image.get_rect()self.rect.centerx = centerxself.rect.bottom = topdef update(self, *args):self.rect.y += self.speedif self.rect.bottom < 0:self.kill()def __del__(self):print("子弹被销毁...")2.创建主类
强调:
![在这里插入图片描述](https://img-blog.csdnimg.cn/2021061416545376.png#pic_center)```python
import pygame
import plane_spritesENEMY_SMALL_ENVENT1 = pygame.USEREVENT + 1
ENEMY_SMALL_ENVENT2 = pygame.USEREVENT + 2class PlaneGame(object):def __init__(self):# 模块初始化,backgroud.png是背景图pygame.init()plane_sprites.SCREEN_SIZE = pygame.image.load('./images/background.png').get_rect()self.screen = pygame.display.set_mode(plane_sprites.SCREEN_SIZE.size)# 游戏时钟self.clock = pygame.time.Clock()# 各种各样精灵和精灵组self.__create_sprites()def start_game(self):while True:# 设置游戏的刷新频率self.clock.tick(plane_sprites.CLOCK_TICK)# 事件检测self.__event_handler()# 碰撞的检测self.__check_collide()# 重新绘制元素self.__sprites_update()# 游戏屏幕更新pygame.display.update()def __game_exit(self):pygame.quit()exit(0)def __event_handler(self):for event in pygame.event.get():if event.type == pygame.QUIT:self.__game_exit()elif event.type == ENEMY_SMALL_ENVENT1:e1 = plane_sprites.Enemy('./images/enemy1.png')self.enemy1_group.add(e1)elif event.type == ENEMY_SMALL_ENVENT2:e1 = plane_sprites.Bullet('./images/bullet2.png',self.hero.rect.centerx,self.hero.rect.top,-8)self.bullet_group.add(e1)keys_pressed = pygame.key.get_pressed()# print(len(keys_pressed))if keys_pressed[pygame.K_RIGHT] or keys_pressed[pygame.K_d]:self.hero.speed['x'] = 4elif keys_pressed[pygame.K_LEFT] or keys_pressed[pygame.K_a]:self.hero.speed['x'] = -4elif keys_pressed[pygame.K_UP] or keys_pressed[pygame.K_w]:self.hero.speed['y'] = -4elif keys_pressed[pygame.K_DOWN] or keys_pressed[pygame.K_s]:self.hero.speed['y'] = 4else:self.hero.speed['x'] = 0self.hero.speed['y'] = 0def __check_collide(self):# 1.子弹摧毁飞机pygame.sprite.groupcollide(self.bullet_group, self.enemy1_group, True, True)# 2.敌机撞毁英雄enemies = pygame.sprite.spritecollide(self.hero, self.enemy1_group, True)if len(enemies) > 0:self.hero.kill()PlaneGame.__game_over()# 让英雄牺牲def __sprites_update(self):for grp in [self.bg_group,self.enemy1_group,self.hero_group,self.bullet_group]:grp.update()grp.draw(self.screen)def __create_sprites(self):# 构建背景图的精灵和精灵组bg1 = plane_sprites.BackGround('./images/background.png',2)bg2 = plane_sprites.BackGround('./images/background.png',2,False)self.bg_group = pygame.sprite.Group()self.bg_group.add(bg1,bg2)#敌方飞机pygame.time.set_timer(ENEMY_SMALL_ENVENT1,800)self.enemy1_group = pygame.sprite.Group()# 我方飞机self.hero = plane_sprites.Hero('./images/me1.png','./images/me2.png')self.hero_group = pygame.sprite.Group()self.hero_group.add(self.hero)# 子弹pygame.time.set_timer(ENEMY_SMALL_ENVENT2, 100)self.bullet_group = pygame.sprite.Group()if __name__ == '__main__':p1 = PlaneGame()p1.start_game()

图片:
enemy1.png

background.png

me1.png

me2.png


用python做一个飞机大战(一)相关推荐

  1. 用python做一个坦克大战

    这是一个非常有意思的项目!如果你想用 Python 来做一个坦克大战游戏,可以考虑使用 Pygame 这个库.它是一个专门用于制作游戏的 Python 库,能够帮助你快速开发出一个坦克大战游戏. 首先 ...

  2. 经典回顾:用pygame模块做一个飞机大战

    目录 前言 一.安装pygame: 1.用pip包管理器安装: 2.二进制安装包安装: 二.学习pygame的内置模块: 1.初始化: 2.精灵组: 3.页面的渲染和刷新 三.使用步骤 1.准备游戏素 ...

  3. 怎么用java做全民飞机大战_Java飞机大战游戏设计与实现

    1 概述 本次Java课程设计是做一个飞机大战的游戏,应用Swing编程,完成一个界面简洁流畅.游戏方式简单,玩起来易于上手的桌面游戏.该飞机大战项目运用的主要技术即是Swing编程中的一些窗口类库. ...

  4. 教你用JAVA设计一个飞机大战的游戏

    本次Java课程设计是做一个飞机大战的游戏,应用Swing编程,完成一个界面简洁流畅.游戏方式简单,玩起来易于上手的桌面游戏.该飞机大战项目运用的主要技术即是Swing编程中的一些窗口类库.事件监听以 ...

  5. python语言实现飞机大战

    第一步:pygame的安装 同时按下win+R打开运行 Enter打开命令提示符,先输入pip -V查看pip版本号,主要是为了查看pip是否正常. 然后先尝试安装pygame测试下能否正常安装,输入 ...

  6. python入门笔记——飞机大战(极简版、未进行继承优化)

    python入门笔记--飞机大战(极简版.未进行继承优化) import random import pygame# 引用pygame里的模块 from pygame.locals import *# ...

  7. 雷霆战机9.5全新上线,Python+Pygame开发飞机大战完整游戏项目(附源码)

    项目名称:太空大战 开发环境:Python3.6.4 第三方库:Pygame1.9.6 代码编辑器:Sublime Text 先来看一下游戏画面吧!  游戏画面动态且丰富哦!   需求分析 利用Pyt ...

  8. 来得瑟一下!用Python做一个缩放自如的圣诞老人

    圣诞节又要到了,虽说我们中国人不提倡过西方的节日,但是商家们还是很喜欢的,估计有对象的男孩纸女孩纸们也很喜欢吧. 今天的主题是为大家展示如何用python做一个不断变大的圣诞老人,就像西游记中能够随意 ...

  9. 用python写搜索引擎_用python做一个搜索引擎(Pylucene)的实例代码

    1.什么是搜索引擎? 搜索引擎是"对网络信息资源进行搜集整理并提供信息查询服务的系统,包括信息搜集.信息整理和用户查询三部分".如图1是搜索引擎的一般结构,信息搜集模块从网络采集信 ...

  10. 在哪里能收到python实例代码-用python做一个搜索引擎(Pylucene)的实例代码

    1.什么是搜索引擎? 搜索引擎是"对网络信息资源进行搜集整理并提供信息查询服务的系统,包括信息搜集.信息整理和用户查询三部分".如图1是搜索引擎的一般结构,信息搜集模块从网络采集信 ...

最新文章

  1. CSAPP第五章就在“扯淡”!
  2. WIN2000 Apache php mysql 安装及安全手册
  3. webflux 对url参数的接收处理
  4. 微信喊你来找工作:上千家企业将提供超10万个就业岗位
  5. python encode函数_python_base64和encode函数
  6. K8S - 为 Docker 而生
  7. docker 容器的常用命令及配置
  8. H3C查看系统启动配置文件
  9. 西门子PLCSIM仿真与第三方组态软件(包括HMI)的通信
  10. MAC之U盘(制作U盘启动必须是在mac系统中)
  11. 下载喜马拉雅FM的音频
  12. web前端需要学习什么?
  13. Web前端全栈开发_node源码笔记【爱创课堂】
  14. Python创建自己的聊天机器人
  15. golang从channel读数据的各种情况
  16. 1012: 8除不尽的数
  17. python——删除文件夹下的所有文件和子文件夹(含代码)
  18. android吸附菜单,Android RecycleView实现滑动停止后自动吸附效果
  19. python 资源文件_如何用 Python 正确读取资源文件
  20. 远程预付费系统在建设公寓民生保障项目的研究与应用

热门文章

  1. 如何解决——打印出的纸张黑底白字?
  2. Python 离散小波变换(DWT) pywt库
  3. 【线性代数笔记】幂等矩阵的性质
  4. 最好的开源网络入侵检测工具(网址及版本已验证并更新)
  5. 安科瑞无线测温产品的实际应用
  6. Windows10快捷键合集
  7. Mac 本地起一个html 服务
  8. 50组顶级4K彩色水墨溶解飞溅动画视频素材合集 mbackground ink
  9. 11210怎么等于24_算24点
  10. C语言随机获取小写字母