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



我的朋友挑战我制作一个小程序,允许用户按下Xbox控制器上的按钮,并让它在键盘上执行多种功能(如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()

只是为了让我可以尝试简单地解释。我想按 a 按钮并让它输入一些东西。使用线程、pygame 和键盘。抱歉,我不熟悉堆栈溢出的编码和格式化。

语句

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)

最新更新