【问题标题】:How to get correct logic truth table results with two inputs in pygame without having to press both inputs at exactly the right time如何在pygame中使用两个输入获得正确的逻辑真值表结果,而不必在正确的时间按下两个输入
【发布时间】:2020-06-11 18:33:00
【问题描述】:

我计划通过使用不同的开关组合组合来制作一个仅使用 5 个键的键盘,但有一件事我不知道如何解决,所以我想尝试重新创建一个基本的板子示例python 使用键盘按键来模拟现实生活中的按钮。

假设我有一个红色按钮和一个蓝色按钮。当我只按下红色按钮时,假设我应该得到“A”的输出。当我只按下蓝色按钮时,我应该得到“B”的输出,当我同时按下两个按钮时,我应该得到“C”的输出。

问题是我必须同时按下红色和蓝色按钮,计算机才能理解我想要输出“C”,如果我在另一个之前按下一个,我会输出两个字符而不是一个。

我还没有真正为它编写任何代码,但我希望有人能帮助我解决这个问题,以便我可以将它实现到我的 PyGame 程序中,然后再实现到 Arduino 上。

【问题讨论】:

    标签: python pygame boolean logic


    【解决方案1】:

    可能像基于字典的查找表这样的快速方法会起作用。不过,在 Arduino 上,我会在速度+空间高效的数据结构中使用 2 字节查找表。

    ### Chord key mapping table
    key_table = {  "a"  : "a",
                   "s"  : "e",
                   "d"  : "i",
                   "f"  : "o",
                   "g"  : "u",
                   "as" : "s",
                   "ad" : "t",
                   "af" : "n"   }
    

    在主循环中通过查看pygame.key.get_pressed() 元组来构建映射键:

    keys = pygame.key.get_pressed()
    # print( str( keys ) )
    
    chord_pressed = ''
    if ( keys[pygame.K_a] ):   # note: the order the chord is made 
        chord_pressed += 'a'   #       is significant
    if ( keys[pygame.K_s] ):
        chord_pressed += 's'
    if ( keys[pygame.K_d] ):
        chord_pressed += 'd'
    if ( keys[pygame.K_f] ):
        chord_pressed += 'f'
    if ( keys[pygame.K_g] ):
        chord_pressed += 'g'
    
    if ( chord_pressed in key_table.keys() ):
        print( "[%s] → [%s]" % ( chord_pressed, key_table[ chord_pressed ] ) )
    

    似乎工作正常:

    [a] → [a]
    [a] → [a]
    [as] → [s]
    [as] → [s]
    [as] → [s]

    【讨论】:

      猜你喜欢
      • 2019-01-12
      • 1970-01-01
      • 2020-07-31
      • 1970-01-01
      • 1970-01-01
      • 2016-10-11
      • 1970-01-01
      • 1970-01-01
      • 2020-08-03
      相关资源
      最近更新 更多