当 onkeypress() 不满足所需条件时,如何使 onkeypress() 不对特定键做出反应?

发布于 2025-01-11 11:16:47 字数 848 浏览 0 评论 0原文

所以我用海龟做了一个简单的游戏 (title 和 title2 是变量) onkeypress() 可以停止我不想要的 Secret() ,如果我向 main() 添加更多内容,再次按空格键将重置 main() 中的所有内容。

import turtle

TS = turtle.Screen()
TS.title('Cookie Collector')  # Window title
TS.bgcolor('lightyellow')  # Window bg color
TS.setup(width=900, height=700)  # Window size pixels
TS.tracer(0)  # Window updates


def main():
    title.clear()
    title2.clear()
    TS.bgcolor('lightgreen')


def secret():
    title.clear()
    title2.clear()
    TS.bgcolor('black')
    title.color('white')
    title2.color('white')
    title.write("haha epic secret", align='center', font=('Kristen ITC', 40, 'normal'))
    title2.write("oOoooOoooOOoOOOOOoooOoOO", align='center', font=('Kristen ITC', 40, 'normal'))

TS.listen()
TS.onkeypress(main, 'space')
TS.onkeypress(secret, 'Up')

while True:
    TS.update()

so im making a simple game in turtle
(title and title2 are variables)
onkeypress() makes it possible to stop secret() which I don't want, and if I would add more to main() pressing the spacebar again would reset everything in main().

import turtle

TS = turtle.Screen()
TS.title('Cookie Collector')  # Window title
TS.bgcolor('lightyellow')  # Window bg color
TS.setup(width=900, height=700)  # Window size pixels
TS.tracer(0)  # Window updates


def main():
    title.clear()
    title2.clear()
    TS.bgcolor('lightgreen')


def secret():
    title.clear()
    title2.clear()
    TS.bgcolor('black')
    title.color('white')
    title2.color('white')
    title.write("haha epic secret", align='center', font=('Kristen ITC', 40, 'normal'))
    title2.write("oOoooOoooOOoOOOOOoooOoOO", align='center', font=('Kristen ITC', 40, 'normal'))

TS.listen()
TS.onkeypress(main, 'space')
TS.onkeypress(secret, 'Up')

while True:
    TS.update()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薯片软お妹 2025-01-18 11:16:47

我从你的问题描述中猜测你想任意打开和关闭关键事件。您可以使用以下方法关闭它们:

screen.onkeypress(None, 'Up')

然后按照您最初启用它们的方式将它们重新打开:

from turtle import Screen, Turtle

def main():
    screen.onkeypress(None, 'space')  # disable handler while inside handler

    title.clear()
    title2.clear()
    screen.bgcolor('lightgreen')

    screen.update()
    screen.onkeypress(secret, 'Up')  # enable up arrow

def secret():
    screen.onkeypress(None, 'Up')  # disable handler while inside handler

    title.clear()
    title2.clear()
    screen.bgcolor('black')

    title.write("haha epic secret", align='center', font=('Kristen ITC', 40, 'normal'))
    title2.write("oOoooOoooOOoOOOOOoooOoOO", align='center', font=('Kristen ITC', 40, 'normal'))

    screen.update()
    screen.onkeypress(main, 'space')  # enable space bar

screen = Screen()
screen.title('Cookie Collector')
screen.bgcolor('lightyellow')
screen.setup(width=900, height=700)
screen.tracer(0)  # Enable explicit window updates

title = Turtle()
title.hideturtle()
title.color('white')
title.penup()
title.sety(100)

title2 = title.clone()
title.sety(-100)

screen.onkeypress(main, 'space')
screen.listen()

screen.mainloop()

I'm guessing from your problem description that you want to arbitrarily turn on and off key events. You can turn them off using:

screen.onkeypress(None, 'Up')

And then turn them back on the way you enabled them in the first place:

from turtle import Screen, Turtle

def main():
    screen.onkeypress(None, 'space')  # disable handler while inside handler

    title.clear()
    title2.clear()
    screen.bgcolor('lightgreen')

    screen.update()
    screen.onkeypress(secret, 'Up')  # enable up arrow

def secret():
    screen.onkeypress(None, 'Up')  # disable handler while inside handler

    title.clear()
    title2.clear()
    screen.bgcolor('black')

    title.write("haha epic secret", align='center', font=('Kristen ITC', 40, 'normal'))
    title2.write("oOoooOoooOOoOOOOOoooOoOO", align='center', font=('Kristen ITC', 40, 'normal'))

    screen.update()
    screen.onkeypress(main, 'space')  # enable space bar

screen = Screen()
screen.title('Cookie Collector')
screen.bgcolor('lightyellow')
screen.setup(width=900, height=700)
screen.tracer(0)  # Enable explicit window updates

title = Turtle()
title.hideturtle()
title.color('white')
title.penup()
title.sety(100)

title2 = title.clone()
title.sety(-100)

screen.onkeypress(main, 'space')
screen.listen()

screen.mainloop()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文