首页 > 解决方案 > 宇宙飞船图像无法移动。我的代码有错误吗?

问题描述

我正在使用 Eric Matthes 的一本名为 Python Crash Course 的书。本书的一部分涉及 python 代码项目,我开始为 Alien Invasion Game 编写代码。我附上了到目前为止为游戏工作而编写的代码,但是每当我运行代码时,无论我按下键盘的右键还是左键,宇宙飞船都不会移动。我已经多次运行代码,但我的编辑器并没有指出有错误。我的代码中有什么需要修复的吗?如果有人知道如何解决这个问题,我将不胜感激。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)

设置.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)

标签: pythonimagepython-2.7keyboardatom-editor

解决方案


我得到了代码工作,这是一个拼写错误。

    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

该代码现在正在运行,将继续在该项目上工作。


推荐阅读