本代码学习于黑马程序员,开发环境基于IDEA,pygame模块。图片资源私博主~
先上效果图
plane_main.py
import pygame from plane_sprites import * class PlaneGame(object): def __init__(self): print("游戏初始化") # 创建游戏窗口 self.scree = pygame.display.set_mode(SCREE_RECT.size) # 创建游戏时钟 self.clock = pygame.time.Clock() self.__reate_sprites() # 定时出现敌机 pygame.time.set_timer(CREATE_ENEMY_EVENT, 1000) pygame.time.set_timer(HERO_FIRE_EVENT, 400) def __reate_sprites(self): # 创建背景 bg1 = Background() bg2 = Background(True) self.back_group = pygame.sprite.Group(bg1, bg2) self.enemy_group = pygame.sprite.Group() self.hero = Hero() self.hero_group = pygame.sprite.Group(self.hero) # 创建敌机精灵组 def start_game(self): while True: self.clock.tick(FRAME_PER_SEC) self.__event_handler() self.__check_collide() self.__update_sprites() pygame.display.update() # 设置创新帧率 def __event_handler(self): for event in pygame.event.get(): if event.type == pygame.QUIT: PlaneGame.__game_over() elif event.type == CREATE_ENEMY_EVENT: enemy = Enemy() self.enemy_group.add(enemy) elif event.type ==HERO_FIRE_EVENT: self.hero.fire() pass # elif event.type==pygame.KEYDOWN and event.key==pygame.K_RIGHT: # print("向右移动") key_pressed = pygame.key.get_pressed() if key_pressed[pygame.K_RIGHT]: self.hero.speed = 2 elif key_pressed[pygame.K_LEFT]: self.hero.speed = -2 else: self.hero.speed = 0 def __check_collide(self): # 子弹打敌机 pygame.sprite.groupcollide(self.hero.Bullets,self.enemy_group,True,True) # 飞机相撞 enemies=pygame.sprite.spritecollide(self.hero,self.enemy_group,True) if len(enemies)>0: self.hero.kill() PlaneGame.__game_over() def __update_sprites(self): self.back_group.update() self.back_group.draw(self.scree) self.enemy_group.update() self.enemy_group.draw(self.scree) self.hero_group.update() self.hero_group.draw(self.scree) self.hero.Bullets.update() self.hero.Bullets.draw(self.scree) @staticmethod def __game_over(): pygame.quit() print("游戏结束") exit() if __name__ == '__main__': game = PlaneGame() game.start_game()
2.plane_sprites.py
import random import pygame # 屏幕大小常量 SCREE_RECT = pygame.Rect(0, 0, 480, 700) # 刷新的帧率 FRAME_PER_SEC = 60 # 敌机定时器常量 CREATE_ENEMY_EVENT = pygame.USEREVENT # 英雄发射子弹事件 HERO_FIRE_EVENT = pygame.USEREVENT + 1 class Gamesprite(pygame.sprite.Sprite): """飞机大战精灵""" def __init__(self, image_name, speed=1): super().__init__() self.image = pygame.image.load(image_name) self.rect = self.image.get_rect() self.speed = speed def update(self): self.rect.y += self.speed class Background(Gamesprite): def __init__(self, is_alt=False): super().__init__('./images/background.png') if is_alt == True: self.rect.y = -self.rect.height def update(self): super().update() # 判断是否移出屏幕 if self.rect.y >= SCREE_RECT.height: self.rect.y = -self.rect.height pass class Enemy(Gamesprite): def __init__(self): # 创建敌机精灵 super().__init__('./images/enemy1.png') # 初试位置,速度 self.speed = random.randint(1, 4) self.rect.bottom = 0 self.rect.x = random.randint(0, SCREE_RECT.width - self.rect.width) def update(self): # 保持垂直飞行,飞出屏幕则删除 super().update() if self.rect.y >= SCREE_RECT.height: self.kill() pass class Hero(Gamesprite): def __init__(self): # super().__init__('./images/me1.png', 0) self.rect.centerx = SCREE_RECT.centerx self.rect.bottom = SCREE_RECT.bottom - 120 self.Bullets =pygame.sprite.Group() def update(self): self.rect.x += self.speed if self.rect.x < 0: self.rect.x = 0 elif self.rect.right > SCREE_RECT.right: self.rect.right = SCREE_RECT.right def fire(self): for i in (0,1,2): bullet1 =Bullet() bullet1.rect.bottom=self.rect.y-i*20 bullet1.rect.centerx=self.rect.centerx self.Bullets.add(bullet1) class Bullet(Gamesprite): def __init__(self): super().__init__('./images/bullet1.png', -3) def update(self): super().update() if self.rect.bottom < 0: self.kill()