我的朋友向我提出挑战,让我制作一个小程序,允许用户按下Xbox控制器上的按钮,让它在键盘上执行多种功能(如reWASD)。我想我差不多完成了,发现有键盘。press()和keyboard.release()不能完成它的功能。是否有任何可能的方法让它做我希望它做的事。对不起,我是一年级新生,英语很差,现在是凌晨1点,我是斯塔科弗流格式的新手
这是我的密码
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的编码和格式还不熟悉
声明
不会执行您期望它执行的操作,因为
stall1()
是对函数stall1
的调用。这意味着函数将在主线程中立即调用,函数的返回值将传递给关键字参数target
(None
)必须将函数对象(
stall1
)传递给参数:更改函数
stall1
,使其在设置状态thread_running
时运行:在主应用程序循环之前启动线程并初始化操纵杆:
相关问题 更多 >
编程相关推荐