gpt4 book ai didi

python - 是否可以使键盘模块与 pygame 和线程一起使用

转载 作者:行者123 更新时间:2023-12-02 09:53:53 25 4
gpt4 key购买 nike

我的 friend 要求我制作一个小程序,允许用户按下 Xbox Controller 上的按钮并让它在键盘上执行多种功能(如 reWASD)。我以为我几乎做到了最后,发现拥有keyboard.press()和keyboard.release()不会执行它的功能。有什么可能的方法让它做我希望它做的事情。抱歉英语不好,我是新生,现在是凌晨 1 点,我是 stakoverflow 格式的新手。

这是我的代码。

import keyboard  # using module keyboard
import pygame
import threading
pygame.init()
pygame.joystick.init()
clock = pygame.time.Clock()

BLACK = pygame.Color('black')
WHITE = pygame.Color('white')
stall = 0

def stall1():
global stall
while stall == 1:
keyboard.press('a')
keyboard.release('a')
stall = 0



# This is a simple class that will help us print to the screen.
# It has nothing to do with the joysticks, just outputting the
# information.
class TextPrint(object):
def __init__(self):
self.reset()
self.font = pygame.font.Font(None, 20)

def tprint(self, screen, textString):
textBitmap = self.font.render(textString, True, BLACK)
screen.blit(textBitmap, (self.x, self.y))
self.y += self.line_height

def reset(self):
self.x = 10
self.y = 10
self.line_height = 15

def indent(self):
self.x += 10

def unindent(self):
self.x -= 10

screen = pygame.display.set_mode((500, 700))

pygame.display.set_caption("My Game")

textPrint = TextPrint()


while True: # making a loo
t = threading.Thread(target=stall1())

screen.fill(WHITE)
textPrint.reset()

# Get count of joysticks.
joystick_count = pygame.joystick.get_count()

textPrint.tprint(screen, "Number of joysticks: {}".format(joystick_count))
events = pygame.event.get()
for event in events:
if event.type == pygame.JOYBUTTONDOWN:
print("Button Pressed")
if joystick.get_button(0):
stall = 1
# Control Left Motor using L2
elif joystick.get_button(2):
# Control Right Motor using R2
print('yote')
elif event.type == pygame.JOYBUTTONUP:
print("Button Released")

for i in range(joystick_count):
joystick = pygame.joystick.Joystick(i)
joystick.init()

# Get the name from the OS for the controller/joystick.
name = joystick.get_name()

# Usually axis run in pairs, up/down for one, and left/right for
# the other.
axes = joystick.get_numaxes()
pygame.display.flip()

# Limit to 20 frames per second.
clock.tick(20)

# Close the window and quit.
# If you forget this line, the program will 'hang'
# on exit if running from IDLE.
pygame.quit()

这样我就可以尝试简单地解释一下。我想按一个按钮并让它输入一些内容。使用线程、pygame 和键盘。抱歉,我刚开始在 stackoverflow 上进行编码和格式化。

最佳答案

声明

t = threading.Thread(target=stall1())

不执行您期望的操作,因为 stall1() 是对函数 stall1 的调用。这意味着该函数将立即在主线程中调用,并且该函数的返回值将传递给关键字参数 target (在本例中为 None)。

您必须将函数对象 (stall1) 传递给参数:

t = threading.Thread(target=stall1)

更改函数stall1,以便只要设置了状态thread_running,它就会运行:

stall = 0
thread_running = True
def stall1():
global stall
while thread_running:
if stall == 1:
stall = 0
keyboard.press('a')
keyboard.release('a')

在主应用程序循环之前启动线程并初始化操纵杆:

t = threading.Thread(target=stall1)
t.start()

joystick_count = pygame.joystick.get_count()
if joystick_count > 0:
joystick = pygame.joystick.Joystick(0)
joystick.init()

run = True
while run:

screen.fill(WHITE)
textPrint.reset()
textPrint.tprint(screen, "Number of joysticks: {}".format(joystick_count))

events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
thread_running = False
run = False

if event.type == pygame.KEYDOWN:
print(chr(event.key)) # print key (triggered from 'stall1')

if event.type == pygame.JOYBUTTONDOWN:
print("Button Pressed")
if joystick.get_button(0):
stall = 1
elif joystick.get_button(2):
print('yote')
elif event.type == pygame.JOYBUTTONUP:
print("Button Released")

pygame.display.flip()
clock.tick(20)

关于python - 是否可以使键盘模块与 pygame 和线程一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59606826/

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