本文主要介绍飞机大战的思路,代码会在文章末尾给出。
分为两个文件:plane_main和plane_spirite。
此游戏不是很难。在设计父类时,我将父类定义为一个抽象类,抽象方法是update(self)。
图片是在B站上黑马教程下面的链接中找到的。
import pygamefrom flying_war_2022.plane_spirite import *class Game: enemy_str = ["AirPlane", "BigAirPlane", "SuperAirPlane"] def __init__(self): pygame.init() self.clock = pygame.time.Clock() self.screen = pygame.display.set_mode(SCREEN_RECT.size) self.__create_sky() self.__create_air_planes() self.__create_hero() self.__create_bullets() self.__create_dead_planes() Game.__set_time() @staticmethod def __set_time(): pygame.time.set_timer(AIR_PLANE, CREATE_TIME) pygame.time.set_timer(FIRE, FIRE_TIME) def __create_sky(self): sky_01 = Sky() sky_02 = Sky(True) self.skys = pygame.sprite.Group(sky_01, sky_02) def __create_air_planes(self): self.air_plane_groups = pygame.sprite.Group() def __create_hero(self): self.hero = Hero() self.hero_group = pygame.sprite.Group(self.hero) def __create_bullets(self): self.bullets_group = pygame.sprite.Group() def __create_dead_planes(self): self.dead_planes_group = pygame.sprite.Group() def start(self): print(f'游戏开始了') while True: self.clock.tick(FPS) self.__update() self.__event_detection() self.__collision_detection() pygame.display.update() def __collision_detection(self): for bullet in self.bullets_group: for enemy in self.air_plane_groups: if pygame.sprite.collide_mask(bullet, enemy): enemy.is_alive = True self.dead_planes_group.add(enemy) def __update(self): self.skys.update() self.skys.draw(self.screen) self.air_plane_groups.update() self.air_plane_groups.draw(self.screen) self.hero_group.update() self.hero_group.draw(self.screen) self.bullets_group.update() self.bullets_group.draw(self.screen) self.dead_planes_group.update() self.dead_planes_group.draw(self.screen) def __event_detection(self): keys_pressed = pygame.key.get_pressed() if keys_pressed[pygame.K_RIGHT]: self.hero.rect.x += self.hero.speed elif keys_pressed[pygame.K_LEFT]: self.hero.rect.x -= self.hero.speed elif keys_pressed[pygame.K_UP]: self.hero.rect.y -= self.hero.speed elif keys_pressed[pygame.K_DOWN]: self.hero.rect.y += self.hero.speed for event in pygame.event.get(): if event.type == pygame.QUIT: Game.__end() break elif event.type == AIR_PLANE: enemy = random.choice(Game.enemy_str) self.air_plane_groups.add(eval(enemy)()) elif event.type == FIRE: bullet = self.hero.fire() self.bullets_group.add(bullet) @staticmethod def __end(): print(f'游戏结束') pygame.quit()if __name__ == '__main__': game = Game() game.start()
from abc import ABCmeta, abstractmethodimport pygameimport randomSCREEN_RECT = pygame.Rect(0, 0, 480, 700)SKY_SPEED = 3FPS = 60CREATE_TIME = 1000AIR_PLANE = pygame.USEREVENTFIRE = pygame.USEREVENT + 1FIRE_TIME = 200class Flying(pygame.sprite.Sprite, metaclass=ABCmeta): # 继承精灵类 def __init__(self): super().__init__() # 这个不能少 self.speed = 0 def image_process(self, image_name): self.image = pygame.image.load("../images/" + image_name + ".png") self.rect = self.image.get_rect() @abstractmethod def update(self): passclass Hero(Flying): def __init__(self): super().__init__() super().image_process("me1") self.index = 0 self.speed = 5 self.rect.x = SCREEN_RECT.centerx self.rect.y = SCREEN_RECT.height - 200 def update(self): if self.rect.x <= 0: self.rect.x = 0 elif self.rect.right >= SCREEN_RECT.width: self.rect.right = SCREEN_RECT.width self.__image_change() def __image_change(self): self.index += 1 if self.index % 16 == 0: self.image = pygame.image.load("../images/me1.png") elif self.index % 16 == 8: self.image = pygame.image.load("../images/me2.png") def fire(self): bullet = Bullet() bullet.rect.x = self.rect.centerx bullet.rect.y = self.rect.y - 20 return bulletclass Bullet(Flying): def __init__(self): super().__init__() super().image_process("bullet2") self.speed = 6 def update(self): self.rect.y -= self.speed if self.rect.y <= 0: self.kill()class Sky(Flying): def __init__(self, judgment_flag=False): super().__init__() super().image_process("background") self.speed = SKY_SPEED if judgment_flag: self.rect.y = -self.rect.height def update(self): self.rect.y += self.speed if self.rect.y >= self.rect.height: self.rect.y = -self.rect.heightclass AirPlane(Flying): def __init__(self): super().__init__() super().image_process("enemy1") self.config() self.is_alive = False self.index = 0 self.dead_index = 0 def config(self): self.speed = random.randint(2, 12) self.rect.x = random.randint(0, SCREEN_RECT.width - self.rect.width) self.rect.y = -self.rect.height def update(self, dead_plane_model="enemy1_down", num_of_pictures_of_death=4): self.__dead(dead_plane_model, num_of_pictures_of_death) self.rect.y += self.speed if self.rect.y >= SCREEN_RECT.height: self.kill() def __dead(self, dead_plane_model, num_of_pictures_of_death): if self.is_alive: self.speed = 0 self.images = [pygame.image.load("../images/" + dead_plane_model + str(i) + ".png") for i in range(1, num_of_pictures_of_death + 1)] self.__image_change(num_of_pictures_of_death) def __image_change(self, num_of_pictures_of_death): self.index += 1 if self.index % 16 == 8: self.image = self.images[self.dead_index] self.dead_index += 1 if self.dead_index >= num_of_pictures_of_death: self.kill()class BigAirPlane(AirPlane): def __init__(self): super().__init__() super().image_process("enemy2") super().config() def update(self, dead_plane_model="enemy2_down", num_of_pictures_of_death=4): super().update(dead_plane_model=dead_plane_model, num_of_pictures_of_death=num_of_pictures_of_death)class SuperAirPlane(AirPlane): def __init__(self): super().__init__() super().image_process("enemy3_n1") super().config() def update(self, dead_plane_model="enemy3_down", num_of_pictures_of_death=6): super().update(dead_plane_model=dead_plane_model, num_of_pictures_of_death=num_of_pictures_of_death)