#game.py
#主程序
import pygame
from settings import Settings
from game_function import update_screen
from game_function import check_events
from ship import Ship
def run_game():
pygame.init()
my_set = Settings()
screen = pygame.display.set_mode((my_set.width,my_set.height))
ship = Ship(screen)
pygame.display.set_caption(my_set.caption)
while True:
check_events()
update_screen(my_set,screen,ship)
run_game()
#settings.py
import pygame
import sys
class Settings():
def __init__(self):
self.color = (0,0,255)
self.width = 1000
self.height = 800
self.caption = 'alien invasion'
#ship.py
import pygame
class Ship():
def __init__(self,screen):
self.screen = screen
self.screen_rect = self.screen.get_rect()
self.image = pygame.image.load('ship.bmp')
self.rect = self.image.get_rect()
self.rect.centerx = self.screen_rect.centerx
self.rect.bottom = self.screen_rect.bottom
def blitme(self):
self.screen.blit(self.image,self.rect)
#game_function.py
import pygame
import sys
from settings import Settings
def check_events():
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
def update_screen(settings,screen,ship):
"""circle the ship in screen"""
"""fill the color """
"""flip the screen"""
screen.fill(settings.color)
ship.blitme()
pygame.display.flip()
本文所用到的ship.bmp和alien.bmp的下载 链接: https://pan.baidu.com/s/1Dn5yaQGZLIsN0BohM8PzoA 密码: 38ie
满足上述4个源代码文件以及保证文件夹下放置ship.bmp情况下,运行代码:
ubuntu运行命令:
python3 game.py
win系统运行命令:
python game.py
运行结果: