[英]I am having trouble getting this ship in pygame to move right and left with KEYUP and KEYDOWN presses
At the moment, I am learning from the Python Crash Course Intro Book By Eric Matthes.目前,我正在学习 Eric Matthes 的 Python Crash Course Intro Book。 I am on the section about making a game with Pygame.
我在关于用 Pygame 制作游戏的部分。 The second they added keyup and keydown my code stopped letting me move the ship right and left.
他们添加 keyup 和 keydown 的那一刻,我的代码就停止让我左右移动船。 Beforehand I was able to move the ship without the additional functions.
事先我能够在没有附加功能的情况下移动这艘船。
I have tried looking on the web.我试过在网上查找。 I have tried messing around with the code to fit the situation.
我试图搞乱代码以适应这种情况。 I, however, have not tried another IDE.
但是,我还没有尝试过其他 IDE。 I apologize in advance if this is not properly formatted, I am a noob to this website.
如果格式不正确,我提前道歉,我是这个网站的菜鸟。 Thanks!
谢谢!
=================== ====================
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()
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)
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
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()
No error messages... I expect the ship to be able to move when I hold the left and right keys down and the ship to stop moving when I release said keys.没有错误消息......我希望当我按住左右键时船能够移动,而当我释放所述键时船会停止移动。
调用update
方法时您错过了括号:
ship.update
ship.update()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.