Python飞机大战游戏 附素材及源码

news/2024/12/15 23:45:33/

 效果图:

 

完整素材源码打包:https://pan.baidu.com/s/1YLn1FZ2IXBxIs53x8pqlrw 提取码: m28g

运行方式:将完整文件夹导入Pycharm后运行 雷霆战机3.0.py 即可

主要代码:

# coding:UTF-8
import pygame
from pygame import locals
from sys import exit
from pygame.sprite import Sprite,Group
import random , time , mathclass BGSprite(Sprite):                             #背景精灵def __init__(self,imagepath):super().__init__()self.image = pygame.image.load(imagepath)self.rect = self.image.get_rect()class EnemySprite(Sprite):                          #敌方战机def __init__(self,imagepath,pos,speed,screen,movetype,bullettype,hp): #self,图片路径,创建位置,速度,背景,移动方式,子弹发射方式,血量super().__init__()self.image = pygame.image.load(imagepath)# self.image = self.image.subsurface(rect)self.rect = self.image.get_rect()self.rect.x = pos[0]self.rect.y = pos[1]self.speed = speedself.xspeed = random.randint(0,speed+1) * movetypeself.timer = 0self.timer2 = 3self.bulletgroup = Group()self.screen = screenself.bullettype = bullettypeself.hp = hpdef update(self):self.bulletgroup.update()self.bulletgroup.draw(self.screen)if self.bullettype == 0:self.timer += 0.01if self.timer > 1:# print("敌机发射子弹")bullet = EnemyBulletSprite("images/bullet/enemy_bullet1.png", [self.rect.x + 40, self.rect.y + 39.5],-2, 0)bullet2 = EnemyBulletSprite("images/bullet/enemy_bullet1.png", [self.rect.x + 40, self.rect.y + 39.5],-2, -1)bullet3 = EnemyBulletSprite("images/bullet/enemy_bullet1.png", [self.rect.x + 40, self.rect.y + 39.5],-2, 1)self.bulletgroup.add(bullet)self.bulletgroup.add(bullet2)self.bulletgroup.add(bullet3)self.timer = 0else:self.timer += 0.01self.timer2 += 0.01if self.timer > 1:bullet = EnemyBulletSprite("images/bullet/enemy_bullet2.png", [self.rect.x + 40, self.rect.y + 39.5],-2, -1)bullet2 = EnemyBulletSprite("images/bullet/enemy_bullet2.png", [self.rect.x + 120, self.rect.y + 39.5],-2, 1)self.bulletgroup.add(bullet)self.bulletgroup.add(bullet2)self.timer = 0if self.timer2 > 4:     #BOSS发射导弹bullet = EnemyBulletSprite("images/bullet/boss_bullet.png", [self.rect.x + 80, self.rect.y + 39.5],-2, 0)self.bulletgroup.add(bullet)self.timer2 = 0self.rect.y += self.speedself.rect.x += self.xspeedif self.rect.x > 400:self.xspeed *= -1self.rect.x += self.xspeedif self.rect.x < 0:self.xspeed *= -1self.rect.x += self.xspeedif self.rect.y > 800:self.kill()def bomb(self):            #爆炸效果bombimages = ["images/boom/boom01.png","images/boom/boom02.png","images/boom/boom03.png","images/boom/boom04.png","images/boom/boom05.png","images/boom/boom06.png",]for i in bombimages :self.screen.blit(pygame.image.load(i),(self.rect.x,self.rect.y))time.sleep(0.005)pygame.display.update()self.kill()class BulletHelp(Sprite):                               #补给品def __init__(self,imagepath,rect,pos,speed,screen,movetype):    #self,图片路径,创建位置,背景,移动方式super().__init__()self.image = pygame.image.load(imagepath)self.image = self.image.subsurface(rect)self.rect = self.image.get_rect()self.rect.x = pos[0]self.rect.y = pos[1]self.speed = speedself.xspeed = random.randint(0, speed) * movetypeself.screen = screendef update(self):self.rect.y += self.speedself.rect.x += self.xspeedif self.rect.x > 400:self.xspeed *= -1self.rect.x += self.xspeedif self.rect.x < 0:self.xspeed *= -1self.rect.x += self.xspeedif self.rect.y > 800:self.kill()class BulletSprite(Sprite):                             #玩家子弹精灵租def __init__(self,imagepath,pos,speed,direction):    #self,图片路径,位置,速度,方向super().__init__()self.image = pygame.image.load(imagepath)# self.image = self.image.subsurface(rect)self.rect = self.image.get_rect()self.rect.x = pos[0]self.rect.y = pos[1]self.speed = speedself.dir = directiondef move(self):if self.dir == 0:self.rect.y -= self.speedelif self.dir > 0 :self.rect.y -= self.speed * math.sin(self.dir)self.rect.x -= self.speed * math.cos(self.dir+90)else:self.rect.y -= self.speed * math.sin(self.dir * -1 )self.rect.x += self.speed * math.cos(self.dir * -1 + 90)if self.rect.y < -50:self.kill()def update(self):self.move()class EnemyBulletSprite(Sprite):                             #敌机子弹精灵组def __init__(self,imagepath,pos,speed,direction):     #self,图片路径,位置,速度,方向super().__init__()self.image = pygame.image.load(imagepath)# self.image = self.image.subsurface(rect)self.rect = self.image.get_rect()self.rect.x = pos[0]self.rect.y = pos[1]self.speed = speedself.dir = directiondef move(self):self.rect.y -= self.speedif self.rect.y < -50:self.kill()def move2(self):self.rect.y -= self.speedself.rect.x -= self.speed/2if self.rect.y < -50:self.kill()def move3(self):self.rect.y -= self.speedself.rect.x += self.speed/2if self.rect.y < -50:self.kill()def update(self):       #子弹发射方向if self.dir == 0:self.move()elif self.dir == -1:self.move2()elif self.dir == 1:self.move3()class PlayerSprite(Sprite):                                 #玩家战机def __init__(self,imagepath,rect,pos,speed,screen):     #self,图片路径,位置,速度,背景super().__init__()self.image = pygame.image.load(imagepath)self.image = self.image.subsurface(rect)self.rect = self.image.get_rect()self.rect.x = pos[0]self.rect.y = pos[1]self.speed = speedself.screen = screenself.bulletgroup = Group()self.score = 0self.hp = 10000self.timer = 0# self.canfire = Falseself.bullettype = 0self.bulletnum = 10def move(self):keys = pygame.key.get_pressed()if keys[locals.K_RIGHT]:self.rect.x += self.speedif self.rect.x > 412:self.rect.x = 412if keys[locals.K_LEFT]:self.rect.x -= self.speedif self.rect.x < 0:self.rect.x = 0if keys[locals.K_UP]:self.rect.y -= self.speedif self.rect.y < 0:self.rect.y = 0if keys[locals.K_DOWN]:self.rect.y += self.speedif self.rect.y > 702:self.rect.y = 702def fire(self):self.timer += 1if self.timer > 50:self.timer = 0if self.bullettype == 0:bullet = BulletSprite("images/bullet/bullet1.png",  [self.rect.x + 45, self.rect.y - 20], 6,0)self.bulletgroup.add(bullet)elif self.bullettype == 1:bullet = BulletSprite("images/bullet/bullet1.png", [self.rect.x + 30, self.rect.y - 20], 6,0)bullet2 = BulletSprite("images/bullet/bullet1.png", [self.rect.x + 60, self.rect.y - 20], 6,0)self.bulletgroup.add(bullet)self.bulletgroup.add(bullet2)elif self.bullettype == 2:bullet = BulletSprite("images/bullet/bullet1.png", [self.rect.x + 30, self.rect.y - 20], 6,0)bullet2 = BulletSprite("images/bullet/bullet1.png", [self.rect.x + 60, self.rect.y - 20], 6,0)bullet3 = BulletSprite("images/bullet/bullet3r.png", [self.rect.x + 60, self.rect.y - 20], 6,15)bullet4 = BulletSprite("images/bullet/bullet3l.png", [self.rect.x + 28, self.rect.y - 20], 6,-15.076)bullet5 = BulletSprite("images/bullet/bullet4r.png", [self.rect.x + 60, self.rect.y - 20], 6,45.5)bullet6 = BulletSprite("images/bullet/bullet4l.png", [self.rect.x + 25, self.rect.y - 20], 6,-45.5)self.bulletgroup.add(bullet)self.bulletgroup.add(bullet2)self.bulletgroup.add(bullet3)self.bulletgroup.add(bullet4)self.bulletgroup.add(bullet5)self.bulletgroup.add(bullet6)self.bulletnum -= 1if self.bulletnum <= 0:self.bullettype = 1def update(self):self.bulletgroup.update()self.bulletgroup.draw(self.screen)self.move()self.fire()CREATE_ENEMY = locals.USEREVENT + 1        #加入用户事件
CREATE_BOSS = locals.USEREVENT + 2
CREATE_BULLET_HELP = locals.USEREVENT + 3def main():pygame.init()pygame.display.set_caption("雷霆战机")           #标题名称screen = pygame.display.set_mode((512,768))     #背景大小bggroup = Group()bgsprite = BGSprite("images/bg/bg0.jpg")bggroup.add(bgsprite)playergroup = Group()playersprite = PlayerSprite("images/hero/hero_b_03.png",[0,0,122,105],[bgsprite.rect.w/2 - 61 ,600],5,screen)        #玩家战机playergroup.add(playersprite)enemygroup = Group()         #敌机精灵组bullethelpgroup = Group()    #子弹精灵组bossgroup = Group()          #boss精灵组pygame.time.set_timer(CREATE_ENEMY,random.randint(500,1000))        #事件触发计时器pygame.time.set_timer(CREATE_BOSS, random.randint(3000, 5000))pygame.time.set_timer(CREATE_BULLET_HELP,random.randint(3000,5000))font = pygame.font.Font(None,32)        #字体设置while True:bggroup.update()        #保持更新bggroup.draw(screen)playergroup.update()playergroup.draw(screen)enemygroup.update()enemygroup.draw(screen)bossgroup.update()bossgroup.draw(screen)bullethelpgroup.update()bullethelpgroup.draw(screen)screen.blit(font.render("Score:"+str(playersprite.score) , True,(255, 0, 0)),(20,20))    #显示分数screen.blit(font.render("HP:" + str(playersprite.hp), True, (255, 0, 0)), (20, 60))        #显示血量pygame.display.update()check1 = pygame.sprite.groupcollide(playersprite.bulletgroup,enemygroup,True,False)#碰撞检测 销毁子弹 销毁敌机if check1:enemy.hp -= 1        #如果检测到碰撞,hp-1if enemy.hp <= 0:list(check1.values())[0][0].bomb()    #当hp<=0时,播放爆炸动画,玩家分数+100playersprite.score += 100check11 = pygame.sprite.groupcollide(playersprite.bulletgroup, bossgroup, True, False)  # 碰撞检测 销毁子弹 销毁敌机if check11:boss.hp -= 1if boss.hp <= 0:list(check11.values())[0][0].bomb()playersprite.score += 500check2 = pygame.sprite.groupcollide(playergroup,enemygroup,False,True)if check2:list(check2.values())[0][0].bomb()playersprite.hp -= 1check22 = pygame.sprite.groupcollide(playergroup, bossgroup, False, True)if check22:list(check22.values())[0][0].bomb()playersprite.hp -= 1#print(enemygroup)for enemy in enemygroup:check3 = pygame.sprite.groupcollide(playergroup,enemy.bulletgroup,False,True)if check3:playersprite.hp -= 1#playersprite.bullettype = 0for boss in bossgroup:check33 = pygame.sprite.groupcollide(playergroup,boss.bulletgroup,False,True)if check33:playersprite.hp -= 1#playersprite.bullettype = 0if playersprite.hp <= 0:playersprite.kill()check4 = pygame.sprite.groupcollide(playergroup,bullethelpgroup,False,True)if check4:if playersprite.bullettype <= 1:playersprite.bullettype += 1playersprite.bulletnum = 10else:playersprite.bulletnum = 10playersprite.fire()for event in pygame.event.get():# if event.type == locals.KEYUP:#     if event.key == locals.K_SPACE:#         playersprite.fire()if event.type == locals.QUIT:pygame.quit()exit()if event.type == CREATE_ENEMY:    #创建普通敌机enemy = EnemySprite("images/enemy/enemy2.png",[random.randint(0,410),-50],2,screen,random.randrange(-1,1,2),0,1)enemygroup.add(enemy)    #加入敌机精灵组if event.type == CREATE_BOSS:     #创建BOSS敌机boss = EnemySprite("images/enemy/boss.png",  [random.randint(0, 410), -50], 1,screen, 0,1,15)bossgroup.add(boss)      #加入boss精灵组if event.type == CREATE_BULLET_HELP:    #创建补给道具bullethelp = BulletHelp("images/hero/supply.png",[0,0,40,41],[random.randint(0,410),-50],2,screen,random.randrange(-1,1,2))bullethelpgroup.add(bullethelp)    #加入补给精灵组if __name__ == '__main__':main()

 


http://www.ppmy.cn/news/594541.html

相关文章

什么是AOP?

目录 一、AOP简介 1、AOP简介和作用 2、AOP的概念 二、AOP的基本实现 三、AOP工作流程 1 、AOP工作流程 2、AOP核心概念 四、AOP切入点表达式 1、语法格式 2、通配符 五、AOP通知类型 1、AOP通知分类 2、AOP通知详解 &#xff08;1&#xff09;前置通知 &#xf…

FZU 1685 跑跑卡丁车

跑跑卡丁车 Time Limit:1sMemory limit:32MAccepted Submit:214Total Submit:586 cigam在宿舍闲着没事&#xff0c;又玩起了跑跑卡丁车&#xff0c;为了到达终点&#xff0c;他需要通过m段路&#xff0c;在通过每段路时&#xff0c;他可以利用加速器来加快速度&#xff0c;每段…

游戏浅谈1-传奇,跑跑卡丁车

自己也玩过很多游戏&#xff0c;当做一个总结&#xff0c;和简单分析。 最开始接触的的游戏&#xff0c;当属 传奇私服&#xff1a; 简介&#xff1a; 1&#xff0c; 3种职业&#xff0c;法师&#xff0c;道士&#xff0c;武士&#xff0c;3种职业各种都有多种技能&#x…

【软考网络管理员】2023年软考网管初级常见知识考点(19)-防火墙与入侵检测系统IDS

涉及知识点 防火墙有哪些及其功能&#xff0c;防火墙的区域划分及工作模式&#xff0c;IDS是什么及其作用&#xff1f;入侵检测系统的分类及原理&#xff0c;软考网络管理员常考知识点&#xff0c;软考网络管理员网络安全&#xff0c;网络管理员考点汇总。 原创于&#xff1a;…

2023上半年软考系统分析师科目一整理-04

2023上半年软考系统分析师科目一整理-04 企业信息化 企业信息化 企业信息化工程是将( A )相结合&#xff0c;改善企业的经营、管理、产品开发和生产等各个环节&#xff0c;提高生产效率、产品质量和企业的创新能力&#xff0c;从而实现产品设计制造和企业管理的信息化、生产过…

Hystrix快速上手

Hystrix熔断器快速上手 简介 Hystrix是Spring Cloud提供的一种带有熔断机制的框架&#xff0c;由于在微服务系统中同一个操作会由多个不同的微服务来共同完成&#xff0c;所以微服务与微服务之间会由很多相互的调用&#xff0c;由于在分布式环境中经常会出现某个微服务节点故…

街机游戏集

原文发布时间为&#xff1a;2010-06-10 —— 来源于本人的百度文章 [由搬家工具导入]《Kawaks 1.45 最终中文典藏版ROM游戏集》(Kawaks 1.45)[压缩包] www.verycd.com/topics/20336/ 中 文名称: Kawaks 1.45 最终中文典藏版ROM游戏集 英文名称: Kawaks 1.45 游戏类型: ETC 其它…

详解volatile关键字、线程的可见性、有序性

0、引言 在juc多线程并发编程中&#xff0c;常常需要关注线程的“可见性”与“有序性”。本文将详细介绍这两部分内容&#xff0c;以及volatile关键字的使用。 阅读本文前需要一些jvm运行时内存、进程与线程、共享内存、锁等相关知识。 1、可见性 1.1 定义 定义&…