Python Turtle game, Check not working?
<pre><code>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() &gt; 300 or player.xcor() &lt; -300:
    print("Game over")   
if player.ycor() &gt; 300 or player.ycor() &lt; -300:
    print("Game over")
</code></pre>

<p>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 ? </p>

<p>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 <code>mainloop()</code> and handle the bounds detection in one of your callback methods, namely <code>forward()</code>.</p>

<p>A demonstration of the above in a rework of your code:</p>

<pre><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() &gt; QUADRANT or player.xcor() &lt; -QUADRANT or player.ycor() &gt; QUADRANT or player.ycor() &lt; -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()
</code></pre>

python visual-studio if-statement turtle-graphics