1 # -*- coding:utf-8 -*- 2 # Author:Sure Feng 3 4 import pygame 5 from plane_sprites import * 6 7 class PlaneGame(object): 8 """飞机大战游戏""" 9 10 def __init__(self): 11 """初始化游戏""" 12 # 创建游戏窗口对象 13 self.screen = pygame.display.set_mode(SCREEN_RECT.size) 14 # 创建时钟对象 15 self.clock = pygame.time.Clock() 16 # 调用creat_sprites(),创建游戏精灵 17 self.creat_sprites() 18 # 创建敌机的计时器 19 pygame.time.set_timer(CREATE_ENEMY_SEC, 1000) 20 # 发射子弹的计时器 21 pygame.time.set_timer(FIRE_BULLET_SEC, 500) 22 23 24 def creat_sprites(self): 25 """创建游戏精灵""" 26 # 创建背景精灵、精灵组 27 bg1 = Background() 28 bg2 = Background(True) 29 self.bg_group = pygame.sprite.Group(bg1, bg2) 30 # 创建敌机精灵组 31 self.enemy_group = pygame.sprite.Group() 32 # 创建英雄精灵、精灵组 33 self.hero = Hero() 34 self.hero_group = pygame.sprite.Group(self.hero) 35 36 # 开始游戏 37 def start_game(self): 38 while True: 39 # 设置游戏刷新帧率 40 self.clock.tick(REFRESH_PER_SEC) 41 # 事件监听方法 42 self.event_handler() 43 # 监测精灵碰撞 44 self.sprite_collide() 45 # 调用sprite_update(),刷新精灵状态 46 self.sprite_update() 47 48 # 刷新游戏画面 49 pygame.display.update() 50 51 # 事件监听方法 52 def event_handler(self): 53 # 游戏窗口关闭监听 54 for event in pygame.event.get(): 55 if event.type == pygame.QUIT: 56 self.game_over() 57 elif event.type == CREATE_ENEMY_SEC: 58 enemy = Enemy() 59 self.enemy_group.add(enemy) 60 elif event.type == FIRE_BULLET_SEC: 61 self.hero.fire() 62 63 key_pressed = pygame.key.get_pressed() 64 if key_pressed[pygame.K_RIGHT]: 65 self.hero.rect.x += 3 66 elif key_pressed[pygame.K_LEFT]: 67 self.hero.rect.x -= 3 68 else: 69 self.hero.rect.x += 0 70 71 72 def sprite_collide(self): 73 """监测精灵碰撞""" 74 # 子弹精灵碰撞敌机,两者均销毁 75 pygame.sprite.groupcollide(self.hero.bullet_group, self.enemy_group, True, True) 76 # 英雄精灵碰撞敌机,两者均销毁 77 enemys = pygame.sprite.spritecollide(self.hero, self.enemy_group, True) 78 if len(enemys) > 0: 79 self.hero.kill() 80 self.game_over() 81 82 def sprite_update(self): 83 """刷新精灵状态""" 84 85 self.bg_group.update() 86 self.bg_group.draw(self.screen) 87 88 self.enemy_group.update() 89 self.enemy_group.draw(self.screen) 90 91 self.hero_group.update() 92 self.hero_group.draw(self.screen) 93 94 self.hero.bullet_group.update() 95 self.hero.bullet_group.draw(self.screen) 96 97 @staticmethod 98 def game_over(): 99 """游戏结束方法""" 100 print("GAME OVER") 101 pygame.quit() 102 exit() 103 104 # 启动游戏 105 if __name__ == '__main__': 106 107 # 创建飞机大战游戏对象 108 game = PlaneGame() 109 # 调用start_game()开始游戏 110 game.start_game()
1 # -*- coding:utf-8 -*- 2 # Author:Sure Feng 3 4 import random 5 import pygame 6 7 # 游戏窗口rect 8 SCREEN_RECT = pygame.Rect(0, 0, 480, 700) 9 # 刷新帧率秒数 10 REFRESH_PER_SEC = 60 11 # 敌机出现事件 12 CREATE_ENEMY_SEC = pygame.USEREVENT 13 # 发射子弹间隔 14 FIRE_BULLET_SEC = pygame.USEREVENT + 1 15 # 背景图像 16 IMAGE_BACKGROUND = "./images/background.png" 17 # 敌机精灵图像 18 IMAGE_ENEMY = "./images/enemy1.png" 19 # 英雄飞机图像 20 IMAGE_HERO = "./images/me1.png" 21 # 子弹图像 22 IMAGE_BULLET = "./images/bullet1.png" 23 24 25 class GameSprite(pygame.sprite.Sprite): 26 """飞机大战游戏精灵""" 27 def __init__(self, image_address, speed=1): 28 # 初始化父类方法 29 super().__init__() 30 # 创建图像对象 31 self.image = pygame.image.load(image_address) 32 # 创建rect对象 33 self.rect = self.image.get_rect() 34 self.speed = speed 35 36 def update(self): 37 # 修改坐标,移动精灵 38 self.rect.y += self.speed 39 40 41 class Background(GameSprite): 42 """背景精灵""" 43 def __init__(self, is_alt=False): 44 # 调用父类初始化方法 45 super().__init__(IMAGE_BACKGROUND) 46 47 # 设置替换图像初始位置 48 if is_alt: 49 self.rect.bottom = 0 50 51 def update(self): 52 super().update() 53 if self.rect.y > SCREEN_RECT.height: 54 # 错误点:self.rect.y > SCREEN_RECT.height,因为一直成立, 55 # 所以rect.bottom = 0,图像都处于游戏窗口上方,画面成黑色 56 self.rect.bottom = 0 57 58 59 class Enemy(GameSprite): 60 """敌机精灵""" 61 def __init__(self): 62 super().__init__(IMAGE_ENEMY) 63 64 max_x = SCREEN_RECT.width - self.rect.width 65 self.rect.x = random.randint(0, max_x) 66 self.speed = random.randint(2, 4) 67 68 def update(self): 69 super().update() 70 71 if self.rect.y > SCREEN_RECT.height: 72 self.kill() 73 74 75 class Hero(GameSprite): 76 """英雄精灵""" 77 def __init__(self): 78 super().__init__(IMAGE_HERO, 0) 79 80 self.rect.centerx = SCREEN_RECT.centerx 81 self.rect.bottom = SCREEN_RECT.height -120 82 83 self.bullet_group = pygame.sprite.Group() 84 85 def update(self): 86 super().update() 87 88 if self.rect.x < 0: 89 self.rect.x = 0 90 elif self.rect.right > SCREEN_RECT.right: 91 self.rect.right = SCREEN_RECT.right 92 93 def fire(self): 94 95 for i in (1, 2, 3): 96 self.bullet = Bullet() 97 self.bullet.rect.centerx = self.rect.centerx 98 self.bullet.rect.bottom = self.rect.y - i * 20 99 100 self.bullet_group.add(self.bullet) 101 102 103 104 105 106 class Bullet(GameSprite): 107 """子弹精灵""" 108 def __init__(self): 109 super().__init__(IMAGE_BULLET, -3) 110 111 def update(self): 112 super().update() 113 114 if self.rect.bottom < 0: 115 self.kill()