Search code examples
pythonpython-turtle

How do I make it so that pressing Enter only sends me to the character selection when I am on the title screen?


When I start my game it first shows the title screen and when I press Enter it goes to the character selection screen. This is intended, however at anytime if I press Enter, whether after the title screen or not, it will go to character selection.

Is there a way I can make it so that the onkeypress only works when I am on the title screen?

I have tried to move the onkeypress into my function that opens the title screen and I have also tried to make an if statement that says "if it is on the title screen then my onkeypress", however neither solution worked.

How can I do this correctly? I am working in replit.

import turtle as trtl
wn = trtl.Screen()
wn.setup(1280,720)

#the turtles
why = trtl.Turtle()
why2 = trtl.Turtle()
why3 = trtl.Turtle()

wn.bgcolor("dim grey")

#characters
closed = "mouthclosed.gif"
opened = "mouthopen.gif"
thumb = "thumb.gif"
cclosed = "mouthclosedc.gif"
copened = "mouthopenc.gif"
thumbc = "thumbc.gif"
#backgrounds
bikini = "bikini.gif"


wn.addshape(closed)
wn.addshape(opened)
wn.addshape(cclosed)
wn.addshape(copened)
wn.addshape(thumb)
wn.addshape(thumbc)


def title():
  why.pu()
  why2.pu()
  why3.pu()
  why.goto(0,300)
  why.color("rebecca purple")
  why.write("mouth pop game !", align="center",font=("Courier", 40, "bold"))
  why.goto(0,250)
  why.color("royal blue")
  why.write("Press enter to start", align="center",font=("Courier", 40, "bold"))
title()



def character_select():
  why.clear()
  why.goto(0,300)
  why.color("royal blue")
  why.write("Choose your Character", align="center",font=("Courier", 40, "bold"))
  why.goto(-200,0)
  why.shape(thumb)
  why2.goto(200,0)
  why2.shape(thumbc)


def godspeed():
  why.shape(opened)
def ohdear():
  why.shape(closed)
def bikinimc():
  wn.bgpic(bikini)

wn.onkeypress(character_select,"Return")

wn.onkeypress(godspeed,"a")
wn.onkeypress(ohdear, "s")
wn.onkeypress(bikinimc,"c")
wn.listen()
wn.mainloop()

I tried to solve it by adding a variable called a that equals 1 at the beginning and it adds one to a at the end of character_select:

if a == 1:
  wn.onkeypress(character_select, "Return")

Solution

  • The check whether a is equal to 1 needs to be done every time the Enter key is pressed.

    Your attempt

    if a == 1:
      wn.onkeypress(character_select, "Return")
    

    was not effective, because this is only executed once during the program.

    Instead, move the check inside the character_select function. Either as a positive check:

    def character_select():
        global a
        if a == 1:
            # ... code for going to character selection screen ...
            a += 1
    

    or as a negative check with an "early return":

    def character_select():
        global a
        if a != 1:
            return  # do NOT go to character selection screen
    
        # ... code for going to character selection screen ...
        a += 1
    

    Notice that the code for going to the character selection screen must not be indented to be inside the if statement in this case.

    Also notice that in order to assign to the a variable from inside character_select you need to mark it as global.