import sys
import pygame
from settings import Settings
from ship import Ship
class AlienInvasion:
"""管理游戏资源和行为的类"""
def __init__(self):
""""初始化游戏并创建游戏资源"""
pygame.init()
self.setting = Settings()
self.screen = pygame.display.set_mode(
(0,0),pygame.FULLSCREEN)
self.setting.screen_width = self.screen.get_rect().width
self.setting.screen_height = self.screen.get_rect().height
pygame.display.set_caption("外星人入侵")
self.ship = Ship(self)
def run_game(self):
"""开始游戏主循环"""
while True:
self._check_events()
self._update_screen()
self.ship.update()
def _check_events(self):
"""" 监视键盘和鼠标事件 """
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
self._check_keydown_events(event)
elif event.type == pygame.KEYUP:
self._check_keyup_events(event)
def _check_keydown_events(self,event):
""""响应按键"""
if event.key == pygame.K_d:
self.ship.moving_right = True
elif event.key == pygame.K_a:
self.ship.moving_left = True
elif event.key == pygame.K_w:
self.moving_up = True
elif event.key == pygame.K_s:
self.moving_down = True
elif event.key == pygame.K_q:
sys.exit()
def _check_keyup_events(self,event):
"""响应松开"""
if event.key == pygame.K_d:
self.ship.moving_right = False
elif event.key == pygame.K_a:
self.ship.moving_left = False
elif event.key == pygame.K_w:
self.moving_up = False
elif event.key == pygame.K_s:
self.moving_down = False
def _update_screen(self):
""" 更新屏幕上的图像,并切换到新屏幕 """
self.screen.fill(self.setting.bg_color)
self.ship.blitme()
pygame.display.flip()
if __name__ == '__main__':
# 创建游戏实例并运行游戏
ai = AlienInvasion()
ai.run_game()
class Settings:
"""储存游戏中所有设置的类"""
def __init__(self):
"""初始化游戏的设置"""
# 屏幕设置
self.screen_width = 1200
self.screen_height = 800
self.bg_color = (230,230,230)
# 飞船设置
self.ship_speed = 1.5
import pygame
class Ship:
"""A class to manage the ship."""
def __init__(self, ai_game):
"""Initialize the ship and set its starting position."""
self.screen = ai_game.screen
self.settings = ai_game.setting
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.center
# 在飞船的属性X中储存小数值
self.x = float(self.rect.x)
self.y = float(self.rect.y)
# 移动标志
self.moving_right = False
self.moving_left = False
self.moving_up = False
self.moving_down = False
def update(self):
"""根据移动标志调整飞船的位置"""
if self.moving_right and self.rect.right < self.screen_rect.right:
self.x += self.settings.ship_speed
if self.moving_left and self.rect.left > self.screen_rect.left:
self.x -= self.settings.ship_speed
if self.moving_up and self.rect.top > 0:
self.y -= self.settings.ship_speed
if self.moving_down and self.rect.bottom > self.screen_rect.top:
self.y += self.settings.ship_speed
# 根据self.x更新self.rect.x
self.rect.x = self.x
self.rect.y = self.y
def blitme(self):
"""Draw the ship at its current location."""
self.screen.blit(self.image, self.rect)
测试了一下,左右能动,上下不行。
""""响应按键"""
if event.key == pygame.K_d:
self.ship.moving_right = True
elif event.key == pygame.K_a:
self.ship.moving_left = True
elif event.key == pygame.K_w:
self.ship.moving_up = True
elif event.key == pygame.K_s:
self.ship.moving_down = True
elif event.key == pygame.K_q:
sys.exit()
这儿少了.ship