我正在使用Eric Matthes写的一本叫做《 Python速成班》的书。本书的一部分涉及python代码项目,我开始为Alien Invasion游戏编写代码。我附带了到目前为止编写的代码,以使游戏能够正常工作,但是无论何时运行代码,无论按下键盘的向右键还是向左键,飞船都不会移动。我已经运行了几次代码,但是我的编辑器没有指出有错误。我需要在代码中修复某些问题吗?如果有人知道如何解决此问题,我将不胜感激。 Alien_invasion.py代码:
import sys
import pygame
from settings import Settings
from ship import Ship
class AlienInvasion:
"""Overall class to manage game assets and behavior."""
def __init__(self):
"""Initialize the game, and create game resources."""
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)
# Set the background color.
self.bg_color = (230, 230, 230)
def run_game(self):
"""Start the main loop for the game."""
while True:
self._check_events()
self.ship.update()
self._update_screen()
# Redraw the screen during each pass through the loop.
def _check_events(self):
"""Respond to keypresses and mouse events."""
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
self.ship.moving_right = True
elif event.key == pygame.K_LEFT:
self.ship.moving_left = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
self.ship.moving_right = False
elif event.key == pygame.K_LEFT:
self.ship.moving_left = False
# Move the ship to the right.
self.ship.rect.x += 1
def _update_screen(self):
"""Update images on the screen, and flip to the new screen."""
self.screen.fill(self.settings.bg_color)
self.ship.blitme()
pygame.display.flip()
if __name__ == '__main__':
# Make a game instance, and run the game.
ai = AlienInvasion()
ai.run_game()
Ship.py代码:
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.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()
# Movement flags
self.moving_right = False
self.moving_left = False
def update(self):
"""Update the ship's position based on the movement flag."""
if self.moving_right:
self.rect.x += 1
if self.moving_left:
self.rect.x -= 1
# 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)
Settings.py代码:
class Settings:
"""A class to store all settings for Alien Invasion."""
def __init__(self):
"""Initialize the game's settings."""
# Screen settings
self.screen_width = 1200
self.screen_height = 800
self.bg_color = (230, 230, 230)
答案 0 :(得分:0)
我让代码正常工作,这是一个错字错误。
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if **event.key** == pygame.K_RIGHT:
self.ship.moving_right = True
elif **event.key** == pygame.K_LEFT:
self.ship.moving_left = True
elif event.type == pygame.KEYUP:
if **event.key** == pygame.K_RIGHT:
self.ship.moving_right = False
elif **event.key** == pygame.K_LEFT:
self.ship.moving_left = False
我以前编码的是event.type而不是event.key
代码正在运行,将继续在项目上工作。