我正在尝试按住一个键并自动移动方块。我试图将pygame.key.get.pressed()
更改为pygame.key.get.focused()
,但仍然没有。
import pygame
pygame.init()
screen = pygame.display.set_mode((400,300))
pygame.display.set_caption("shield hacking")
JogoAtivo = True
GAME_BEGIN = False
# Speed in pixels per frame
x_speed = 0
y_speed = 0
cordX = 10
cordY = 100
def desenha():
screen.fill((0, 0, 0))
quadrado = pygame.draw.rect(screen, (255, 0, 0), (cordX, cordY ,50, 52))
pygame.display.flip();
while JogoAtivo:
for evento in pygame.event.get():
print(evento)
#verifica se o evento que veio eh para fechar a janela
pressed_keys = pygame.key.get_pressed()
if evento.type == pygame.QUIT:
JogoAtivo = False
pygame.quit();
if pressed_keys[pygame.K_SPACE]:
print('GAME BEGIN')
GAME_BEGIN = True
desenha();
if pressed_keys[pygame.K_LEFT] and GAME_BEGIN:
speedX=-3
cordX+=speedX
desenha()
if pressed_keys[pygame.K_RIGHT] and GAME_BEGIN:
speedX=3
cordX+=speedX
desenha()
更新了代码,但仍存在同样的问题(包括KEYDOWN事件)。
import pygame
pygame.init()
screen = pygame.display.set_mode((400,300))
pygame.display.set_caption("shield hacking")
JogoAtivo = True
GAME_BEGIN = False
# Speed in pixels per frame
x_speed = 0
y_speed = 0
cordX = 10
cordY = 100
def desenha():
screen.fill((0, 0, 0))
quadrado = pygame.draw.rect(screen, (255, 0, 0), (cordX, cordY ,50, 52))
pygame.display.flip();
while JogoAtivo:
for evento in pygame.event.get():
print(evento)
#verifica se o evento que veio eh para fechar a janela
if evento.type == pygame.QUIT:
JogoAtivo = False
pygame.quit();
if evento.type == pygame.KEYDOWN:
if evento.key == pygame.K_SPACE:
print('GAME BEGIN')
GAME_BEGIN = True
desenha();
if evento.type == pygame.KEYDOWN:
if evento.key == pygame.K_LEFT:
speedX=-3
cordX+=speedX
desenha()
if evento.type == pygame.KEYDOWN:
if evento.key == pygame.K_RIGHT:
speedX=3
cordX+=speedX
desenha()
答案 0 :(得分:0)
您无法在get_pressed()
循环中使用for evento
,因为当您按住键时,键不会生成事件,pygame.event.get()
会返回空列表,因此for
不执行任何操作
当您开始按键时,系统会生成单个偶数KEYDOWN
,当您停止按键时,系统会生成单个偶数KEYUP
,但系统不会在这两个时刻之间生成KEYDOW
个事件(按住键时)。
您必须在get_pressed()
循环后使用for
(及其他代码)。
for evento in pygame.event.get():
print(evento)
if evento.type == pygame.QUIT:
JogoAtivo = False
pygame.quit();
# after for loop
pressed_keys = pygame.key.get_pressed()
if pressed_keys[pygame.K_SPACE]:
print('GAME BEGIN')
GAME_BEGIN = True
desenha();
if pressed_keys[pygame.K_LEFT] and GAME_BEGIN:
speedX=-3
cordX+=speedX
desenha()
if pressed_keys[pygame.K_RIGHT] and GAME_BEGIN:
speedX=3
cordX+=speedX
desenha()
或(或多或少)
for evento in pygame.event.get():
print(evento)
#verifica se o evento que veio eh para fechar a janela
if evento.type == pygame.QUIT:
JogoAtivo = False
pygame.quit();
elif evento.type == pygame.KEYDOWN:
if evento.key == pygame.K_SPACE:
print('GAME BEGIN')
GAME_BEGIN = True
elif evento.key == pygame.K_LEFT:
speedX = -3
elif evento.key == pygame.K_RIGHT:
speedX = 3
elif evento.type == pygame.KEYUP:
if evento.key in (pygame.K_LEFT, pygame.K_RIGHT):
speedX = 0
# after loop
if GAME_BEGIN:
cordX += speedX
desenha()
答案 1 :(得分:0)
根据我的理解(纠正我,如果我错了,我可能不太了解你的问题),但看起来你要做的就是按一下键,按住键,当你按住键时,让pygame不断处理KEYDOWN事件。在pygame中,这不起作用,但是可以以不同的方式处理按键。试想一下:如果你开始按住某个键,它会创建一个KEYDOWN事件。当您放开该键时,它会生成一个KEYUP事件。因此,在按下键之后按下键,然后 按下。以下代码通过示例解释了该概念:
Rails.application.config.to_prepare do # to_prepare ensures that the monkey patching happens before the first request
Devise::OmniauthCallbacksController.class_eval do # reopen the class
def failure # redefine the failure method
set_flash_message! :alert, :failure, kind: OmniAuth::Utils.camelize(failed_strategy.name), reason: failure_message
redirect_to after_omniauth_failure_path_for(resource_name)
end
end
end
我还没有能够测试代码(使用学校计算机),但是当你持有" A"键,每秒大约5次(这是一种限制fps的懒惰方式)。这同样适用于所有键,也适用于鼠标拖动。