[心缘地方]同学录
首页 | 功能说明 | 站长通知 | 编码查看转换 | 代码下载 | 常见问题及讨论 | Python游戏编程讨论 | 《深入解析ASP核心技术》 | Python游戏编程教程 | HEIC转JPG工具
登录系统:用户名: 密码: 如果要讨论问题,请先注册。

[备忘]pygame使用摇杆/十字方向键做选卡的触发问题

上一篇:[备忘]pygame里十字方向键,单次触发问题
下一篇:[备忘]角度较小(<15°),sin ≈ tan

添加日期:2026/6/26 9:59:14 快速返回   返回列表 阅读197次
也就是实现类似key_down单次触发。


for event in events:
    if event.type == pygame.KEYDOWN:
        if event.key in [pygame.K_LEFT, pygame.K_a]:
            xxxx()
        elif event.key in [pygame.K_RIGHT, pygame.K_d]:
            xxxx()
    elif event.type == pygame.JOYHATMOTION:

        # 按下左方向,再松开,会触发两次JOYHATMOTION事件,分别为(-1, 0)和(0, 0)
        hat_x, hat_y = event.value

        # 如果摇杆回到中心(x=0),重置所有状态
        if hat_x == 0:
            locked_direction = None
        else:
            # 方向名称
            direction = getHatDirection(event.value)
            print(direction)

            isDoSelect = False
            # 第一次按下方向,左或右
            if locked_direction is None:
                locked_direction = direction
                isDoSelect = True
            elif direction != locked_direction:
                isDoSelect = True
            else:
                # 方向跟上次一样,什么都不做(忽略左→左下→左)
                pass

            # 触发方向切换
            if isDoSelect:
                if direction == "right":
                    xxxx()
                elif direction == "left":
                    xxxx()

    elif event.type == pygame.JOYAXISMOTION:
        # 手柄摇杆 - 只在"推过阈值"的瞬间触发
        if event.axis == 0:  # 左摇杆X轴

            # 检测从"中心"到"推动"的瞬间
            # 0,0.11,0.21,0.31,0.41,0.51,0.61……大概这样
            if abs(event.value) > 0.5 and last_axis_value < 0.5:
                if not axis_triggered:
                    if event.value > 0:
                        #右边
                        xxxx()
                    else:
                        xxxx()
                    axis_triggered = True
            else:
                # 摇杆回到中心,解锁
                axis_triggered = False

            # 记录上次x轴的值
            # 这个值只在选卡界面时更新,选卡后松开会变为0,会导致下次选卡第一次进来last_axis_value < 0.5条件成立,导致跳了一下。
            # 所以,在upgrade_phase切换为1时,需要设置last_axis_value值为1.
            last_axis_value = abs(event.value)
    elif event.type == pygame.JOYBUTTONDOWN:
        if event.button==0:
            xxxx()

 

评论 COMMENTS
没有评论 No Comments.

添加评论 Add new comment.
昵称 Name:
评论内容 Comment:
验证码(不区分大小写)
Validation Code:
(not case sensitive)
看不清?点这里换一张!(Change it here!)
 
评论由管理员查看后才能显示。the comment will be showed after it is checked by admin.
CopyRight © 心缘地方 2005-2999. All Rights Reserved