效果图:
完整素材源码打包: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()