Pygame: joystick 模块使用示例

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个空格
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值