Python飛機大戰

筆尖下的沉默發表於2021-12-04

先用pip裝上pygame
左下角開啟終端,輸入pip install pygame
在這裡插入圖片描述
安裝成功
Pygamed的官方文件
測試一下好不好使(注意直譯器要選裝了pygame的那個)

import pygame

print(pygame.__version__)

結果如下:
在這裡插入圖片描述
程式碼如下:
plane_main.py

import pygame
# 從plane_sprites裡面匯入每個類。不需要用句點表示
from plane_sprites import *


# 繼承object
class PlaneGame(object):
    # python三引號允許一個字串跨多行,寫多行註釋用
    """飛機大戰主遊戲"""

    def __init__(self):
        print("遊戲初始化")
        # create window
        self.screen = pygame.display.set_mode(SCREEN_RECT.size)
        # create clock
        self.clock = pygame.time.Clock()
        # create sprites and groups
        self.__create_sprites()
        # create enemy
        pygame.time.set_timer(CREATE_ENEMY_EVENT, 700)
        pygame.time.set_timer(HERO_FIRE_EVENT, 500)

    def __create_sprites(self):
        # create bg sprites and groups
        bg1 = Background()
        bg2 = Background(True)

        self.back_group = pygame.sprite.Group(bg1, bg2)

        # create enemy group
        self.enemy_group = pygame.sprite.Group()

        # create hero sprites and groups
        self.hero = Hero()
        self.hero_group = pygame.sprite.Group(self.hero)

    def start_game(self):
        print("game starts!")

        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()

        keys_pressed = pygame.key.get_pressed()
        if keys_pressed[pygame.K_RIGHT]:
            self.hero.speed = 10
        elif keys_pressed[pygame.K_LEFT]:
            self.hero.speed = -10
        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.screen)

        self.enemy_group.update()
        self.enemy_group.draw(self.screen)

        self.hero_group.update()
        self.hero_group.draw(self.screen)

        self.hero.bullets.update()
        self.hero.bullets.draw(self.screen)

    @staticmethod
    def __game_over():
        print("game over")
        pygame.quit()
        exit()


if __name__ == '__main__':
    game = PlaneGame()

    game.start_game()

plane_sprites.py

import random
import pygame

# 螢幕大小
SCREEN_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):
        # use father class method
        super().__init__()

        self.image = pygame.image.load(image_name)
        self.rect = self.image.get_rect()
        self.speed = speed

    def update(self):
        # move up
        self.rect.y += self.speed


class Background(GameSprite):

    def __init__(self, is_alt=False):
        super().__init__("./images/background.png")

        # judge weather is alternative image
        if is_alt:
            self.rect.y = -self.rect.height

    def update(self):

        super().update()

        # if is out of screen, move
        if self.rect.y >= SCREEN_RECT.height:
            self.rect.y = -self.rect.height


class Enemy(GameSprite):

    def __init__(self):
        super().__init__("./images/enemy1.png")

        self.speed = random.randint(1, 5)

        self.rect.bottom = 0

        max_x = SCREEN_RECT.width - self.rect.width
        self.rect.x = random.randint(0, max_x)

    def update(self):
        super().update()

        if self.rect.y >= SCREEN_RECT.height:
            print("game over")
            pygame.quit()
            exit()

    def __del__(self):
        print("killed!")


class Hero(GameSprite):

    def __init__(self):

        super().__init__("./images/me1.png", 0)

        self.rect.centerx = SCREEN_RECT.centerx
        self.rect.bottom = SCREEN_RECT.bottom - 120

        self.bullets = pygame.sprite.Group()

    def update(self):

        self.rect.x += self.speed

        # Hero can not be out of screen
        if self.rect.x < 0:
            self.rect.x = 0
        elif self.rect.right > SCREEN_RECT.right:
            self.rect.right = SCREEN_RECT.right

    def fire(self):
        print("firing")

        for i in (1, 2, 3):
            bullet = Bullet()
            bullet.rect.bottom = self.rect.y - i * 20
            bullet.rect.centerx = self.rect.centerx

            self.bullets.add(bullet)


class Bullet(GameSprite):

    def __init__(self):
        super().__init__("./images/bullet1.png", -2)

    def update(self):
        super().update()

        if self.rect.bottom < 0:
            self.kill()

    def __del__(self):
        print("bullet is destroyed")

這個還有若干可以改進的點,例如生命值,子彈手動發射,子彈數,子彈種類,擊毀動畫,敵機種類,敵機炸彈,補血,暫停介面

相關文章