概述

游戏元素:
1.背景
2.我方飞机
3.敌方飞机
4.子弹
5.游戏结束界面
6.游戏主窗口
7.飞机爆炸
8.音效

流程图


音效准备:

import pygame
import syspygame.init()
pygame.mixer.init()#游戏背景音乐
#back_sound = pygame.mixer.Sound('D:\\飞机大战素材\\sound\\game_music.wav')
#back_sound.set_volume(0.05)
pygame.mixer.music.load('D:\\飞机大战素材\\sound\\game_music.wav')  #不知道什么原因当背景音乐和飞机爆炸音效都用sound,则放在前面的播放后面的不会播放
pygame.mixer.music..set_volume(0.05)
#子弹发射音乐
bullet_sound = pygame.mixer.Sound('D:\\飞机大战素材\\sound\\bullet.wav')
bullet_sound.set_volume(0.05)
#敌机死亡音乐
enemy_downsound = pygame.mixer.Sound('D:\\飞机大战素材\\sound\\enemy1_down.wav')
enemy_downsound.set_volume(0.05)
#游戏结束音乐
gameover_sound = pygame.mixer.Sound('D:\\飞机大战素材\\sound\\game_over.wav')
gameover_sound.set_volume(0.05)
#飞机死亡音乐
plane_sound = pygame.mixer.Sound('D:\\飞机大战素材\\sound\\use_bomb.wav')
plane_sound.set_volume(0.05)

窗口准备:

from 音效 import *pygame.init()
size = width , height = 480 , 800       #设置游戏屏幕大小
screen = pygame.display.set_mode(size)
pygame.display.set_caption('飞机大战')
back = pygame.image.load('D:\\飞机大战素材\\background.png')def main():while 1:back_sound.play(-1,)for event in pygame.event.get():if event.type == 12:sys.exit()screen.blit(back,(0,0))pygame.display.flip()
if __name__ == '__main__':main()

注意:若不写if __name__ == '__main__': main()语句,那么程序将会闪退,去掉def main()才会正常,注意是双下划线
子弹准备:

import pygame
class Bullet(pygame.sprite.Sprite):def __init__(self,position):pygame.sprite.Sprite.__init__(self)#子弹图片self.bullet_image = pygame.image.load('D:\\飞机大战素材\\bullet1.png')self.bullet_rect = self.bullet_image.get_rect()self.bullet_rect.top,self.bullet_rect.left = positionself.speed = 20self.life = True# 子弹碰撞检测self.mask = pygame.mask.from_surface(self.bullet_image)#子弹删除def del_bullet(self):self.kill()#子弹移动def bullet_move(self):if self.bullet_rect.left > 0:                   #搞不懂这为啥是left,用top反而会往左发射self.bullet_rect.left -= self.speedelse:self.kill()

角色准备:

import pygame
clock = pygame.time.Clock()
class Ourplane:def __init__(self,width,height):#飞机的图片,带动态效果self.planeimage1 = pygame.image.load('D:\\飞机大战素材\\hero1.png')self.planeimage2 = pygame.image.load('D:\\飞机大战素材\\hero2.png')#飞机位置self.planerect = self.planeimage1.get_rect()self.width, self.height = width, height# 获取飞机图像的掩膜用以更加精确的碰撞检测self.mask = pygame.mask.from_surface(self.planeimage1)# 定义飞机初始化位置,底部预留60像素self.planerect.left, self.planerect.top = (self.width - self.planerect.width) // 2, (self.height - self.planerect.height - 60)# 设置飞机移动速度self.speed = 10# 设置飞机存活状态(True为存活, False为死亡)self.active = True# 加载飞机损毁图片self.destroy_images = []self.destroy_images.extend([pygame.image.load("D:\\飞机大战素材\\hero_blowup_n1.png"),pygame.image.load("D:\\飞机大战素材\\hero_blowup_n2.png"),pygame.image.load("D:\\飞机大战素材\\hero_blowup_n3.png"),pygame.image.load("D:\\飞机大战素材\\hero_blowup_n4.png")])def move_up(self):"""飞机向上移动的操作函数,其余移动函数方法类似"""if self.planerect.top > 0:  # 如果飞机尚未移动出背景区域self.planerect.top -= self.speedelse:  # 若即将移动出背景区域,则及时纠正为背景边缘位置self.planerect.top = 0def move_down(self):"""飞机向下移动"""if self.planerect.bottom < self.height - 60:self.planerect.top += self.speedelse:self.planerect.bottom = self.heightdef move_left(self):"""飞机向左移动"""if self.planerect.left > 0:self.planerect.left -= self.speedelse:self.planerect.left = 0def move_right(self):"""飞机向右移动"""if self.planerect.right < self.width:self.planerect.right += self.speedelse:self.planerect.right = self.widthdef reset(self):# 初始化飞机(飞机挂了, 初始化到初始位置)self.planerect.left, self.planerect.top = (self.width - self.planerect.width) // 2, (self.height - self.planerect.height - 60)# 重置飞机的存活状态self.active = True

敌机准备:

import  random  as r
import pygame
pygame.init()
class Emeryplane(pygame.sprite.Sprite):def __init__(self,width,height):pygame.sprite.Sprite.__init__(self)#敌机图片self.emeryplane_image = pygame.image.load('D:\\飞机大战素材\\enemy1.png')self.emeryplanedie_image = [pygame.image.load('D:\\飞机大战素材\\enemy1_down1.png'),pygame.image.load('D:\\飞机大战素材\\enemy1_down2.png'),pygame.image.load('D:\\飞机大战素材\\enemy1_down3.png')]# 随机出现self.emeryplane_rect = self.emeryplane_image.get_rect()self.width, self.height = width, heightself.emeryplane_rect.left,self.emeryplane_rect.top = (r.randint(0,self.width-self.emeryplane_rect.left),r.randint(-100,-30))#敌机状态self.emeryplane_life =  True#敌机碰撞检测self.mask = pygame.mask.from_surface(self.emeryplane_image)#敌机移动def emery_move(self):if self.emeryplane_rect.top < self.height:self.emeryplane_rect.top += 3else:self.kill()#敌机重置def emery_reset(self):self.emeryplane_rect.left, self.emeryplane_rect.top = (r.randint(0, self.width - self.emeryplane_rect.left), r.randint(-100, -30))self.emeryplane_life = True

Python项目:飞机大战相关推荐

  1. Python项目——飞机大战!

    文章目录 一.项目介绍--飞机大战 实战步骤 确认模块 -- pygame 安装 pygame 验证安装 二.pygame 快速入门 项目准备 1. 使用 `pygame` 创建图形窗口 小节目标 1 ...

  2. python项目:飞机大战(爆炸效果,血条,音效,buff加成,boss,菜单,完整详细注释的源码)

    文章目录 一. 总体概览 基本功能 细节部分(全部可以自定义) 可增添需求 二,技术框架 核心技术概述 1.游戏的初始化和退出 2 理解游戏中的坐标系 3 创建游戏主窗口 4. 理解 **图像** 并 ...

  3. python项目飞机大战

    实现步骤 1.创建窗口 2.创建一个玩家飞机,按方向键可以左右移动 3.给玩家飞机添加按空格键发射子弹功能 4.创建一个敌机 5.敌机自动左右移动 6.敌机自动发射子弹 1.创建窗口 import p ...

  4. python项目-飞机大战

    目录 安装Pygame 创建Pygame窗口以及响应用户输入 设置背景色 创建设置类 添加飞船图像 创建ship类 重构:模块game_functions 函数check_events() 函数upd ...

  5. python实现飞机大战游戏

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

  6. 用python做飞机大战打到不同部位扣分不同_python制作飞机大战需要哪些python术语...

    怎么样用Python写飞机大战游戏 为什么写出来一直都是未响应,哪里写错了吗?就算小编们没能走到最后,小编也不会心存遗憾,你有你的苦辣酸甜,小编有小编的喜怒哀乐,如果小编们不曾相遇,就没有那些美好记忆 ...

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

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

  8. Python项目-----外星人大战

    Python项目-----外星人大战 alien_invasion.py import sys from time import sleep import pygame from settings i ...

  9. 小甲鱼python游戏代码_【小甲鱼】零基础学习python pygame 飞机大战可执行源代码...

    [实例简介] [小甲鱼]零基础学习python pygame 飞机大战可执行源代码,觉得挺有意思,故此分享. [实例截图] [核心代码] 飞机大战 └── 飞机大战 ├── bullet.py ├── ...

  10. python的飞机大战

    python的飞机大战的完整代码 alien.py import pygame from pygame.sprite import Sprite class Alien(Sprite):"& ...

最新文章

  1. setuid和setgid
  2. 根据时间点截取wav文件
  3. golang string int int32 int64 float32 float64 time 互相转换
  4. Spring框架设计
  5. 新手学习Linux——rsync+shell脚本完成自动化备份
  6. Varchar的最大长度
  7. C#代码总结02---使用泛型来获取Asp前台页面全部控件,并进行属性修改
  8. “约见”面试官系列之常见面试题之第九十四篇之MVVM框架(建议收藏)
  9. “函数调用的左操作数”的理解
  10. 微软发布全新3D Emoji表情系统:与Win11的UI风格一致
  11. 用对 gitignore
  12. Mysql基础知识--视图
  13. java中的“+”运算符,产生新对象问题。(非常好的面试题!)
  14. 16qam星座图 matlab,16qam星形和矩形星座图调制解调matlab代码.doc
  15. java邮件发送不成功,javamail发送邮件成功 但是却接收不到邮件?解决方法
  16. Android开发之手机震动器
  17. python图书管理实训报告总结_图书管理系统心得-总结报告模板
  18. SpriteKit从零开始~Physics and Collisions
  19. Derby ij工具
  20. MySQL常用操作总结

热门文章

  1. 用Python绘制MACD、KDJ、布林线技术指标图
  2. python安装后找不到目录_将python setup.py安装到其他路径中找不到已安装的packag
  3. 计算机教师线下研修方式与内容,网络研修线下心得体会
  4. 最小二乘估计(Least squares estimation)
  5. 【愚公系列】2023年03月 MES生产制造执行系统-002.Dapper和EFCode的使用
  6. MAC电脑zsh终端git使用
  7. 前端炫酷3D银河登录页面
  8. # Java1.8 安装出现1603错误代码解决方法
  9. Bootstrap 学习之(十)------ 导航与导航条
  10. 月入一万,活在北京 (zz)