Python 速成指南 5(项目-异形入侵 )

115 阅读1分钟

ps: 完整可跑🏃代码 请看仓库

初始化

projects/py_game/alien_attack.py

import sys
import pygame


class AlienAttack:

    def __init__(self):

        pygame.init()

        self.screen = pygame.display.set_mode((1200, 800))

        pygame.display.set_caption('Alien Attack')

    def start_game(self):

        while True:

            for event in pygame.event.get():

                if event.type == pygame.QUIT:

                    sys.exit()

            pygame.display.flip()


if __name__ == '__main__':

    ai = AlienAttack()

    ai.start_game()

背景和时钟

projects/py_game/alien_attack.py

image.png

settins

projects/py_game/settings.py

class Settings:
    def __init__(self):
        self.screen_width = 1200
        self.screen_height = 800
        self.bg_color = (230, 230, 230)
  • 使用

image.png

加一个船

projects/py_game/ship.py

import pygame







class Ship:

    def __init__(self, ai_game):

        """Initialize the ship and set its starting position."""

        self.screen = ai_game.screen

        self.screen_rect = ai_game.screen.get_rect()




        # Load the ship image and get its rect.

        self.image = pygame.image.load('images/ship.bmp')

        self.rect = self.image.get_rect()




        # Start each new ship at the bottom center of the screen.

        self.rect.midbottom = self.screen_rect.midbottom




    def blitme(self):

        """Draw the ship at its current location."""

        self.screen.blit(self.image, self.rect)

雏形样子

de0e69e3-182a-451d-a860-8586d281f640.png

仓库

github.com/huanhunmao/…