0

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()

1 Answers1

0

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()
cdlane
  • 40,441
  • 5
  • 32
  • 81