问一下为什么这个游戏里的飞船动不了,而且一中止运行就是报错(截图)
import sys
import pygame
from settings import Settings
from ship import Ship
class AlienInvasion:
"""管理游戏资源和行为的类"""
def __init__(self):
"""初始化游戏并创建游戏资源"""
pygame.init()
self.settings = Settings()
self.screen = pygame.display.set_mode((self.settings.screen_width,self.settings.screen_height))
pygame.display.set_caption("Alien Invasion")
self.ship = Ship(self)
def run_game(self):
"""考试游戏的主循环"""
while True:
self._check_events()
self.ship.updata()
self._updata_screen()
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)
elif event.type == pygame.K_q:
sys.exit()
def _check_keydown_events(self,event):
"""响应按键"""
if event.type == pygame.K_RIGHT:
# 向右移动飞船
self.ship.moving_right = True
elif event.type == pygame.K_LEFT:
self.ship.moving_left = True
def _check_keyup_events(self,event):
"""相应松开"""
if event.type == pygame.K_RIGHT:
self.ship.moving_right = False
elif event.type == pygame.K_LEFT:
self.ship.moving_left = False
def _updata_screen(self):
# 每次循环时都会重绘屏幕
self.screen.fill(self.settings.bg_color)
self.ship.biltme()
# 让最近绘制的屏幕可见
pygame.display.flip()
if __name__ == '__main__':
# 创建游戏实列并运行游戏
ai = AlienInvasion()
ai.run_game()
import pygame
class Ship:
"""管理飞船的类"""
def __init__(self,ai_game):
"""初始化飞船位置"""
self.screen = ai_game.screen
self.settings=ai_game.settings
self.screen_rect = ai_game.screen.get_rect()
# 加载飞船图像并获取其外接矩形
self.image=pygame.image.load('images/rocket-g5b8b31cef_640(1).png')
self.rect = self.image.get_rect()
# 对于每艘新的飞船,都将其放在屏幕底部的中央
self.rect.midbottom = self.screen_rect.midbottom
# 在飞船的属性x中存储小数值
self.x = float(self.rect.x)
# 移动标志
self.moving_right = False
self.moving_left=False
def updata(self):
"""根据移动标志调整飞船的位置"""
# 更新飞船而不是rect对象的x值
if self.moving_right:
self.x += self.settings.ship_speed
if self.moving_left:
self.x -= self.settings.ship_speed
# 根据self.x更新rect对象
self.rect.x = self.x
def biltme(self):
"""在指定位置绘制飞船"""
self.screen.blit(self.image,self.rect)
class Settings:
"""存储游戏《外星人入侵》中所有设置的类"""
def __init__(self):
"""初始化游戏设置"""
# 屏幕设置
self.screen_width=1200
self.screen_height=800
self.bg_color=(230,230,230)
self.ship_speed=3.0