我无法通过按下 KEYUP 和 KEYDOWN 键让 pygame 中的这艘船左右移动

I am having trouble getting this ship in pygame to move right and left with KEYUP and KEYDOWN presses

目前,我正在学习 Eric Matthes 的 Python 速成课程介绍书。我在关于使用 Pygame 制作游戏的部分。他们第二次添加 keyup 和 keydown 我的代码停止让我左右移动船。事先我可以在没有附加功能的情况下移动飞船。


我试过在网上找。我试过弄乱代码以适应这种情况。然而,我还没有尝试过另一个 IDE。如果格式不正确,我提前道歉,我是这个网站的菜鸟。谢谢!

===================

alien_invaion.py

import pygame

from settings import Settings
from ship import Ship
import game_functions as gf


def run_game():
# Initialize pygame, settings, and screen object.
    pygame.init()
    ai_settings = Settings()
    screen = pygame.display.set_mode(
        (ai_settings.screen_width, ai_settings. screen_height))
    pygame.display.set_caption("Alien Invasion")

    # Make a ship.
    ship = Ship(ai_settings, screen)

    # Start the main loop of the game.
    while True:
        gf.check_events(ship)
        ship.update
        gf.update_screen(ai_settings, screen, ship)


run_game()

ship.py

import pygame


class Ship():

    def __init__(self, ai_settings, screen):
        """Initialize the ship and set its starting position."""
        self.screen = screen
        self.ai_settings = ai_settings

        # Load the ship image and get is rect.
        self.image = pygame.image.load('images/ship.bmp')
        self.rect = self.image.get_rect()
        self.screen_rect = screen.get_rect()

        # Start each new ship at the bottom center of the screen.
        self.rect.centerx = self.screen_rect.centerx
        self.rect.bottom = self.screen_rect.bottom

        # Store a decimal value for the ship's center.


        # Movement flags
        self.moving_right = False

    def update(self):
        """Update the ship's position based on the movement flag."""
        # Update the ship's center value, not rect.
        if self.moving_right:
            self.rect.centerx += 1
        # Update rect object from self.center.

    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 = (0, 23, 233)

     # Ship settings

game_functions.py

import sys

import pygame



def check_events(ship):
    """respond to keypresss's 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:
                ship.moving_right = True

        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_RIGHT:
                ship.moving_right = False


def update_screen(ai_settings, screen, ship):
    """Update images on the screen and flip to the new screen."""
    # Redraw the screen during each pass through the loop.
    screen.fill(ai_settings.bg_color)
    ship.blitme()
    


# Make the most recently drawn screen visible.
    pygame.display.flip()

没有错误消息...我希望当我按住左右键时飞船能够移动,而当我松开这些键时飞船会停止移动。

您在调用 update 方法时遗漏了括号:

ship.update

ship.update()