Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python外星人入侵运动速成班_Python - Fatal编程技术网

Python外星人入侵运动速成班

Python外星人入侵运动速成班,python,Python,运行代码时没有错误,但飞船无法向左或向右移动,我确信我做的一切都是正确的,所以下面是所有代码。船显示正确,但根本不移动 import pygame from ship import Ship import game_functions as gf from settings import Settings def run_game(): pygame.init() game_settings = Settings() screen = pygame.display.s

运行代码时没有错误,但飞船无法向左或向右移动,我确信我做的一切都是正确的,所以下面是所有代码。船显示正确,但根本不移动

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


def run_game():
    pygame.init()
    game_settings = Settings()
    screen = pygame.display.set_mode((game_settings.screen_width, game_settings.screen_height))
    pygame.display.set_caption("Alien Invasion")
    space_ship = Ship(screen)

    while True:
        gf.check_events(space_ship)
        gf.update_screen(game_settings, screen, space_ship)
        space_ship.update()


run_game()
设置.py

class Settings:
    def __init__(self):
        self.screen_width = 800
        self.screen_height = 600
        self.bg_colour = (0, 0, 255)
ship.py

import pygame


class Ship:
    def __init__(self, screen):
        # Initializing the ship
        self.screen = screen

        # Load the ship and get the rect attribute
        self.image = pygame.image.load("spaceship.png")
        self.rect = self.image.get_rect()
        self.screen_rect = screen.get_rect()

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

        # Movement flag
        self.moving_right = False
        self.moving_left = False


    def blitme(self):
        self.screen.blit(self.image, self.rect)

    def update(self):
        if self.moving_right:
            self.rect.centerx += 1
        if self.moving_left:
            self.rect.centerx -= 1
game_functions.py

import sys
import pygame


def check_events(spaceship):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.type == pygame.K_RIGHT:
                spaceship.moving_right = True
            elif event.type == pygame.K_LEFT:
                spaceship.moving_left = True
        elif event.type == pygame.KEYUP:
            if event.type == pygame.K_RIGHT:
                spaceship.moving_right = False
            elif event.type == pygame.K_LEFT:
                spaceship.moving_left = False


def update_screen(game_settings, screen, spaceship):
    screen.fill(game_settings.bg_colour)
    spaceship.blitme()
    pygame.display.flip()

这就是我能做的所有代码。有人能解决这个问题吗?

在pygame中,使用
事件。键入
检查关键事件。使用
event.key
检查按下了哪个键

def check_events(spaceship):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:  # event.type
            if event.key == pygame.K_RIGHT:  # event.key
                spaceship.moving_right = True
            elif event.key == pygame.K_LEFT:
                spaceship.moving_left = True
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_RIGHT:
                spaceship.moving_right = False
            elif event.key == pygame.K_LEFT:
                spaceship.moving_left = False

非常感谢@Mike67