簡體   English   中英

我無法讓這艘船在 pygame 中通過 KEYUP 和 KEYDOWN 按鍵左右移動

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

目前,我正在學習 Eric Matthes 的 Python Crash Course Intro Book。 我在關於用 Pygame 制作游戲的部分。 他們添加 keyup 和 keydown 的那一刻,我的代碼就停止讓我左右移動船。 事先我能夠在沒有附加功能的情況下移動這艘船。


我試過在網上查找。 我試圖搞亂代碼以適應這種情況。 但是,我還沒有嘗試過其他 IDE。 如果格式不正確,我提前道歉,我是這個網站的菜鳥。 謝謝!

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

外星人入侵.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()

運送.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)

設置.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

游戲功能.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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM