Python:有没有更有效的方法来反复检查鼠标?

时间:2016-01-15 03:47:39

标签: python performance python-2.7 python-3.x

我目前正在Mac上运行Python 3.5,而我似乎无法找到一种方法来提高我的代码效率。

总而言之,我目前正在创造一个财富游戏(或基本上是刽子手),我拥有它,以便当用户点击一个字母时,它会检查字母是否在单词等中。

有没有比每次编写26 if语句和检查坐标更好的方法?是否有更有效的方法使用if语句检查坐标?

while True:
    mouse = win.getMouse()
    if 35 < mouse.x < 65 and 85 < mouse.y < 115:
        ans = "a"
        break
    elif 65 < mouse.x < 95 and 85 < mouse.y < 115:
        ans = "b"
        break
    elif 95 < mouse.x < 125 and 85 < mouse.y < 115:
        ans = "c"
        break
    elif 125 < mouse.x < 155 and 85 < mouse.y < 115:
        ans = "d"
        break
    elif 155 < mouse.x < 185 and 85 < mouse.y < 115:
        ans = "e"
        break
    elif 185 < mouse.x < 215 and 85 < mouse.y < 115:
        ans = "f"
        break
    elif 215 < mouse.x < 245 and 85 < mouse.y < 115:
        ans = "g"
        break
    elif 245 < mouse.x < 275 and 85 < mouse.y < 115:
        ans = "h"
        break
    elif 275 < mouse.x < 305 and 85 < mouse.y < 115:
        ans = "i"
        break
    elif 305 < mouse.x < 335 and 85 < mouse.y < 115:
        ans = "j"
        break
    elif 35 < mouse.x < 65 and 115 < mouse.y < 145:
        ans = "k"
        break
    elif 65 < mouse.x < 95 and 115< mouse.y < 145:
        ans = "l"
        break
    elif 95 < mouse.x < 125 and 115 < mouse.y < 145:
        ans = "m"
        break
    elif 125 < mouse.x < 155 and 115 < mouse.y < 145:
        ans = "n"
        break
    elif 155 < mouse.x < 185 and 115 < mouse.y < 145:
        ans = "o"
        break
    elif 185 < mouse.x < 215 and 115 < mouse.y < 145:
        ans = "p"
        break
    elif 215 < mouse.x < 245 and 115 < mouse.y < 145:
        ans = "q"
        break
    elif 245 < mouse.x < 275 and 115 < mouse.y < 145:
        ans = "r"
        break
    elif 275 < mouse.x < 305 and 115 < mouse.y < 145:
        ans = "s"
        break
    elif 305 < mouse.x < 335 and 115 < mouse.y < 145:
        ans = "t"
        break
    elif 35 < mouse.x < 65 and 145 < mouse.y < 175:
        ans = "u"
        break
    elif 65 < mouse.x < 95 and 145 < mouse.y < 175:
        ans = "v"
        break
    elif 95 < mouse.x < 125 and 145 < mouse.y < 175:
        ans = "w"
        break
    elif 125 < mouse.x < 155 and 145 < mouse.y < 175:
        ans = "x"
        break
    elif 155 < mouse.x < 185 and 145 < mouse.y < 175:
        ans = "y"
        break
    elif 185 < mouse.x < 215 and 145 < mouse.y < 175:
        ans = "z"
        break

3 个答案:

答案 0 :(得分:2)

使用计算与多个if语句的简单解决方案:

import string

while True:
    mouse = win.getMouse()
    x, xr = divmod(mouse.x-35, 30)
    y, yr = divmod(mouse.y-85, 30) 
    if not(0 <= x < 10 and 0 <= y < 3):   # Guard values
        continue
    if xr == 0 or yr == 0:                # Gridlines?
        continue
    try:
        ans = string.ascii_lowercase[10*y+x]
        break
    except IndexError:
        pass

e.g。鼠标(x = 102,y = 143)将返回&#39; m&#39;

答案 1 :(得分:0)

您可以使用以下代码来避免使用大的if语句。

def check_pos(x, y):
    table = 'abcdefghijklmnopqrstuvwxyz'

    idx = (x - (35-30)) / 30
    idy = (y - (85-30)) / 30

    # each row has 10 cells
    offset = idx + (idy -1) * 10
    if 1 <= offset <= 26:
        return table[offset-1]

答案 2 :(得分:0)

import string
alpha=string.ascii_lowercase
mouse = win.getMouse()
for idx,i in enumerate(range(35,335,30)):
    if mouse.x>i and mouse.x<i+30:
        if mouse.y>85 and mouse.y<115:
            ans=alpha[idx]
            break
        elif mouse.y>115 and mouse.y<145:
            ans=alpha[idx + 10]
            break
        elif mouse.y>145 and mouse.y<175:
            ans=alpha[idx+20]
            break