import pygame
from settings import Settings
from ship import Ship
import game_functions as gf
def run_game():
#初始化背景设置
pygame.init()
ai_settings = Settings()
#显示窗口大小
screen = pygame.display.set_mode(
(ai_settings.screen_width, ai_settings.screen_height))
#标题
pygame.display.set_caption("Alien Invasion")
ship = Ship(screen)
while True:
gf.check_events(ship)
ship.update()
gf.update_screen(ai_settings, screen, ship)
run_game()
class Settings():
def __init__(self):
self.screen_width = 800
self.screen_height = 600
self.bg_color = (230, 230, 230)
import pygame
class Ship():
def __init__(self, screen):
self.screen = screen
self.image = pygame.image.load('images/ship.bmp')
self.rect = self.image.get_rect()
self.screen_rect = screen.get_rect()
self.rect.centerx = self.screen_rect.centerx
self.rect.bottom = self.screen_rect.bottom
self.moving_right = False
def update(self):
if self.moving_right:
self.rect.centerx += 1
def blitme(self):
self.screen.blit(self.image, self.rect)
import sys
import pygame
def check_events(ship):
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.type == pygame.K_RIGHT:
ship.moving_right = True
elif event.type == pygame.KEYUP:
if event.type == pygame.K_RIGHT:
ship.moving_right = False
def update_screen(ai_settings, screen, ship):
screen.fill(ai_settings.bg_color)
ship.blitme()
pygame.display.flip()
from settings import Settings
from ship import Ship
import game_functions as gf
def run_game():
#初始化背景设置
pygame.init()
ai_settings = Settings()
#显示窗口大小
screen = pygame.display.set_mode(
(ai_settings.screen_width, ai_settings.screen_height))
#标题
pygame.display.set_caption("Alien Invasion")
ship = Ship(screen)
while True:
gf.check_events(ship)
ship.update()
gf.update_screen(ai_settings, screen, ship)
run_game()
class Settings():
def __init__(self):
self.screen_width = 800
self.screen_height = 600
self.bg_color = (230, 230, 230)
import pygame
class Ship():
def __init__(self, screen):
self.screen = screen
self.image = pygame.image.load('images/ship.bmp')
self.rect = self.image.get_rect()
self.screen_rect = screen.get_rect()
self.rect.centerx = self.screen_rect.centerx
self.rect.bottom = self.screen_rect.bottom
self.moving_right = False
def update(self):
if self.moving_right:
self.rect.centerx += 1
def blitme(self):
self.screen.blit(self.image, self.rect)
import sys
import pygame
def check_events(ship):
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.type == pygame.K_RIGHT:
ship.moving_right = True
elif event.type == pygame.KEYUP:
if event.type == pygame.K_RIGHT:
ship.moving_right = False
def update_screen(ai_settings, screen, ship):
screen.fill(ai_settings.bg_color)
ship.blitme()
pygame.display.flip()