在按下键盘上的某个键之前,如何返回“false”为“true”的变量?

问题描述

我和乌龟做了一个贪吃蛇游戏。我想在游戏中制作不同的作弊模式。例如,当我按下键盘上的“c”键时,我希望蛇在碰到边缘或自身时不会死亡。所以,我写这个:

cheat_mode = True
...
if not cheat_mode:
     #check the edge and body

但是,我无法将作弊模式定义为键盘按钮。

解决方法

为您的密钥设置处理程序:

turtle.onkey(cheat_on,'c')

激活对海龟的关注:

turtle.listen()

并定义一个 cheat_on 函数:

def cheat_on():
    global cheat_mode
    cheat_mode = True