我一直在跟踪Python崩溃课程中的pygame项目。'复制确切的代码,因为我对python很新。当我运行我的代码时,游戏窗口打开,图像显示在正确的初始位置。我无法让图像像预期的那样移动。当我使用在while循环中注释掉的代码来移动图像时,它可以工作。但是,当我继续按照本书中的步骤操作时,它会将该功能移动到导入主文件的单独文件中。我完全复制了它,但图像不会移动。
import sys
import pygame
from settings import Settings
from ship import Ship
import game_functions as gf
def run_game():
# main game loop
ai_settings = Settings()
pygame.init()
screen = pygame.display.set_mode((ai_settings.screen_width,
ai_settings.screen_height))
ship = Ship(screen)
pygame.display.set_caption("Alien Invasion!")
while True:
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.rect.centerx += 1
elif event == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
ship.moving_right = True
elif event == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
ship.moving_right = False
gf.check_events(ship)
ship.update()
gf.update_screen(ai_settings, ship, screen)
run_game()