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

时间:2020-01-06 04:49:12

标签: python python-3.x 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和键盘。抱歉,我不熟悉Stackoverflow上的编码和格式化。

1 个答案:

答案 0 :(得分:3)

声明

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)