python - 我无法让这艘船在 pygame 中通过 KEYUP 和 KEYDOWN 按键左右移动
问题描述
目前,我正在学习 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()
推荐阅读
- regex - 如何验证 Yup 中的非必需文件
- node.js - 尝试代理到 localhost:5000 时发生 Docker 错误
- sql-server - 无法将 Windows 组添加为 SQL Server 登录
- javascript - 如何使用 Angular 5 翻译带有参数的通知消息?
- xamarin.forms - Xamarin 表单:打开选项卡时调用相邻选项卡
- mysql - 如何在 MySQL 服务器的“where”中使用案例
- python - 训练和验证分数很高,但测试准确性很差
- sql-server-2016 - 如何使用openjson递归解析JSON字符串
- sql - 在创建 SQL 时需要帮助
- javascript - 使用数据表中的偏移量和限制调用下一个 api