gpt4 book ai didi

python - pygame.key.set_repeat 用于操纵杆

转载 作者:行者123 更新时间:2023-11-30 23:37:25 25 4
gpt4 key购买 nike

我正在使用 pygame 构建一个菜单,我想使用特定的游戏 handle 使其可导航。理想情况下,我希望能够反复按住方向键上的“*down”,或者在键盘上得到类似的效果,在重复输入相同的字符之前,第一次按下按钮会有延迟(看起来)。

我正在尝试模拟操纵杆的pygame.key.set_repeat(...) 函数。到目前为止我的方法是

pygame.time.set_timer(pygame.USEREVENT, 10)
DELAY_TIME = 0.250 #ms
y_delay = True
while not done:
for event in pygame.event.get():
y_axis = gamepad.get_axis(1)
if y_axis > 0.5: # pushing down
main_menu.move_down()
redraw() #redraw everything on the surface before sleeping
if y_delay:
time.sleep(DELAY_TIME)
y_delay = False #don't delay the next time the y axis is used


elif y_axis < -0.5: #pushing up
# repetitive I know, but I'm still working on it
main_menu.move_up()
redraw()
if y_delay:
time.sleep(DELAY_TIME)
y_delay = False

else:
y_delay = True # delay the next time

我的问题是,如果有人向上或向下点击的速度超过 DELAY_TIME,他们就会在 DELAY_TIME 内再次移动。此外,如果有人在 time.sleep 间隔内释放并按下向上/向下按钮,Python 根本不会发现它已被释放,并且不允许延迟。

也许有一种方法可以使用事件或以某种方式将操纵杆映射到按键来做到这一点? qjoypad 不适合我,joy2keys 就是垃圾。我需要在 python 程序中进行映射。

最佳答案

Sleep 会导致程序停止执行,因此这不是一个可行的选择。您也可以在不使用 set_timer 和事件的情况下执行此操作。我did it使用几个标志和 pygame.time 的 get_ticks

import pygame
from pygame.locals import *

def main():
pygame.init()
pygame.display.set_mode((480, 360))
gamepad = pygame.joystick.Joystick(0)
gamepad.init()

delay = 1000
neutral = True
pressed = 0
last_update = pygame.time.get_ticks()

while True:
for event in pygame.event.get():
if event.type == QUIT:
return

move = False
if gamepad.get_axis(1) == 0:
neutral = True
pressed = 0
else:
if neutral:
move = True
neutral = False
else:
pressed += pygame.time.get_ticks() - last_update
if pressed > delay:
move = True
pressed -= delay
if move:
print "move"
last_update = pygame.time.get_ticks()

if __name__ == "__main__":
main()
pygame.quit()

get_axis指示没有运动时,中立标志被设置,按下的计时器被重置,导致移动标志保持未设置状态。当中立标志未设置时,如果新设置,则设置移动标志。如果不是新设置的,则按下的计时器会增加,并且仅当按下的计时器大于延迟时才设置移动。

关于python - pygame.key.set_repeat 用于操纵杆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15592671/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com