python--飞机大战
发布时间:2023-02-16 14:09:11 所属栏目:Python 来源:互联网
导读:实现功能: 1:飞机的移动,发射子弹,手雷,生命值,生命条 2:敌飞机有3种形态(小,中,大)不同的飞机大小不一样,生命值不一样,爆炸动画也不一样 3:背景音乐,子弹击中敌机会发生碰撞的爆炸动画和音效 4:随机产生补给(双射子弹和全屏炸弹) 5:难度
self.image = pygame.image.load("images/bullet1.png").convert_alpha() self.rect = self.image.get_rect() self.rect.left,self.rect.top = position self.speed = 12 self.active = True self.mask = pygame.mask.from_surface(self.image) def move(self): self.rect.top -= self.speed if self.rect.top < 0: self.active = False def reset(self,position): self.rect.left,self.rect.top = position self.active = True class Bullet2(pygame.sprite.Sprite): def __init__(self,position): super(Bullet2,self).__init__() self.image = pygame.image.load("images/bullet2.png").convert_alpha() self.rect = self.image.get_rect() self.rect.left,self.rect.top = position self.speed = 14 self.active = False self.mask = pygame.mask.from_surface(self.image) def move(self): self.rect.top -= self.speed if self.rect.top < 0: self.active = False def reset(self,self.rect.top = position self.active = True # 主程序的实现 :# 主程序的实现 :# 主程序的实现 :# 主程序的实现 :# 主程序的实现 def add_small_enemies(group1,group2,num): for i in range(num): e1 = SmallEnemy(bg_size) group1.add(e1) group2.add(e1) def add_mid_enemies(group1,num): for i in range(num): e2 = MidEnemy(bg_size) group1.add(e2) group2.add(e2) def add_big_enemies(group1,num): for i in range(num): e3 = BigEnemy(bg_size) group1.add(e3) group2.add(e3) def inc_speed(target,inc): for each in target: each.speed += inc def main(): # 播放音乐 pygame.mixer.music.play(-1) # 实例化我方飞机 me =MyPlane(bg_size=bg_size) # 实例化敌方飞机 enemies = pygame.sprite.Group() # 实例化敌方小型飞机 small_enemies = pygame.sprite.Group() add_small_enemies(small_enemies,enemies,15) # 实例化敌方中型飞机 mid_enemies = pygame.sprite.Group() add_mid_enemies(mid_enemies,4) # 实例化敌方大型飞机 big_enemies = pygame.sprite.Group() add_big_enemies(big_enemies,2) # 实例化普通子弹 bullet1 = [] bullet1_index = 0 BULLET1_NUM = 4 for i in range(BULLET1_NUM): bullet1.append(Bullet1((me.rect.centerx-10,me.rect.centery))) # 实例超级子弹 bullet2 = [] bullet2_index = 0 BULLET2_NUM = 8 for i in range(BULLET2_NUM // 2): bullet2.append(Bullet2((me.rect.centerx - 33,me.rect.centery))) bullet2.append(Bullet2((me.rect.centerx + 30,me.rect.centery))) # 中弹图片索引 e1_destroy_index = 0 e2_destroy_index = 0 e3_destroy_index = 0 me_destroy_index = 0 # 统计得分 score = 0 score_font = pygame.font.Font("font/font1.ttf",36) # 标志是否暂停游戏 paused = False paused_nor_image = pygame.image.load("images/pause_nor.png").convert_alpha() paused_pressed_image = pygame.image.load( "images/pause_pressed.png").convert_alpha() resume_nor_image = pygame.image.load('images/resume_nor.png').convert_alpha() resume_pressed_image = pygame.image.load('images/resume_pressed.png').convert_alpha() paused_rect = paused_nor_image.get_rect() paused_rect.left,paused_rect.top = width - paused_rect.width - 10,10 paused_image = paused_nor_image # 设置难度 level = 1 # 全屏炸弹 bomb_image = pygame.image.load('images/bomb.png').convert_alpha() bomb_rect = bomb_image.get_rect() bomb_font = pygame.font.Font("font/font1.ttf",48) bomb_num = 3 # 每20秒发放一个补给包 bullet_supply = Bullet_Supply(bg_size) bomb_supply = Bomb_Supply(bg_size) SUPPLY_TIME = USEREVENT pygame.time.set_timer(SUPPLY_TIME,20 * 1000) DOUBLE_BULLTET_TIME = USEREVENT + 1 INVINCIBLE_TIME = USEREVENT + 2 # 标志是否使用超级子弹 is_double_bullet = False # 生命数量 life_image = pygame.image.load('images/life.png').convert_alpha() life_rect = life_image.get_rect() life_num = 3 # 用于切换我方飞机图片 switch_plane = True # 游戏结束画面 gameover_font = pygame.font.Font("font/font1.TTF",48) again_image = pygame.image.load("images/again.png").convert_alpha() (编辑:甘南站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |