pygame几乎可以识别任意外接游戏操纵设备。
游戏手柄上的每个操作都会形成一个电信号被joystick类对象捕获到, joystick把这个信号归一化到[-1,1]区间,或者离散化为{0,1}。
以下程序创建一个弹出窗口,实时显示joystick捕获到的信号数值,便于查看joystick捕获到的信号对应于游戏手柄的哪个按钮/操作。
代码参考博客:Pygame详解(十七):joystick 模块_pygame joystick-优快云博客
进行了注释解读。
import pygame
# Define some colors ## 定义颜色
BLACK = ( 0, 0, 0)
WHITE = ( 255, 255, 255)
# 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, 用于帮助我们把joystick的信息打印到屏幕上,便于查看哪个按键对应哪个变量。
## 这个类不对 joystick 做任何操作,仅仅打印它的信息。
class TextPrint:
def __init__(self):
self.reset()
self.font = pygame.font.Font(None, 20)
def print(self, screen, textString):
textBitmap = self.font.render(textString, True, BLACK) ## 设置屏幕中显示的文字,并设置显示颜色
screen.blit(textBitmap, [self.x, self.y]) ## 在screen 的指定位置打印文字。
self.y += self.line_height ## 光标的位置下移一个行高的高度
def reset(self):
self.x = 10 ## 设置打印文字的初始 x 位置
self.y = 10 ## 设置打印文字的初始 y 位置
self.line_height = 15 ## 设置行高为15像素
def indent(self): ## 光标位置右移10个空格
self.x += 10
def unindent(self): ## 光标位置左移10个空格

最低0.47元/天 解锁文章
3898

被折叠的 条评论
为什么被折叠?



