Python Turtle game, Check not working?
Question: 
    import turtle
    
    # Make the play screen
    wn = turtle.Screen()
    wn.bgcolor("red")
    
    # Make the play field
    mypen = turtle.Turtle()
    mypen.penup()
    mypen.setposition(-300,-300)
    mypen.pendown()
    mypen.pensize(5)
    for side in range(4):
        mypen.forward(600)
        mypen.left(90)
    mypen.hideturtle()
    
    # Make the object
    player = turtle.Turtle()
    player.color("black")
    player.shape("circle")
    player.penup()
    
    
    # define directions( East, West , South , Nord )
    
    def west():
        player.setheading(180)
    
    def east():
        player.setheading(0)
    
    def north():
        player.setheading(90)
    
    def south():
        player.setheading(270)
    
    # define forward
    
    def forward():
        player.forward(20)
    
    # Wait for input
    turtle.listen()
    turtle.onkey(west, "a")
    turtle.onkey(east, "d")
    turtle.onkey(forward,"w")
    turtle.onkey(north,"q")
    turtle.onkey(south,"s")
    
    if player.xcor() > 300 or player.xcor() < -300:
        print("Game over")   
    if player.ycor() > 300 or player.ycor() < -300:
        print("Game over")
    

So everything is working fine, till the If statements. When i go trough the
play field it should give me a print " Game over ". The coordinates are right
but it doesnt check the coordinates! What am i doing wrong ?


Answer: The problem is that your logic to test if the player has gone out of bounds is
at the top level of your code -- it doesn't belong there. You should turn
control over to the turtle listener, via `mainloop()` and handle the bounds
detection in one of your callback methods, namely `forward()`.

A demonstration of the above in a rework of your code:

    
    
    import turtle
    
    QUADRANT = 250
    
    # Make the play screen
    screen = turtle.Screen()
    screen.bgcolor("red")
    
    # Make the play field
    play_pen = turtle.Turtle()
    play_pen.pensize(5)
    play_pen.speed("fastest")
    play_pen.penup()
    play_pen.setposition(-QUADRANT, -QUADRANT)
    play_pen.pendown()
    for _ in range(4):
        play_pen.forward(QUADRANT * 2)
        play_pen.left(90)
    play_pen.hideturtle()
    
    # Make the object
    player = turtle.Turtle()
    player.color("black")
    player.shape("circle")
    player.penup()
    
    # define forward
    
    def forward():
        player.forward(20)
    
        if player.xcor() > QUADRANT or player.xcor() < -QUADRANT or player.ycor() > QUADRANT or player.ycor() < -QUADRANT:
            player.hideturtle()
            player.setposition((0, 0))
            player.write("Game over", False, align="center", font=("Arial", 24, "normal"))
    
    # define directions(East, West, North, South)
    
    turtle.onkey(lambda: player.setheading(180), "a")  # west
    turtle.onkey(lambda: player.setheading(0), "d")  # east
    turtle.onkey(lambda: player.setheading(90), "q")  # north
    turtle.onkey(lambda: player.setheading(270), "s")  # south
    
    turtle.onkey(forward, "w")
    
    # Wait for input
    
    turtle.listen()
    
    turtle.mainloop()
    

