Question: Code:

```
import turtle
import random
import time

s = turtle.getscreen()
turtle.screensize(canvwidth=400, canvheight=400)
t = turtle.Turtle()
t.pensize(0)
t.shape('square')
t.color("black")
t.speed(0)
t.penup()

def moveu(num):
    t.setheading(num)
    t.forward(20)

s.onkey(lambda : moveu(90), 'w')
s.onkey(lambda : moveu(270), 's')
s.onkey(lambda : moveu(180), 'a')
s.onkey(lambda : moveu(0), 'd')

s.listen()

```

I am not close to done with this project but I have run into some problems. I want to create a game in the `turtle` module. But I don't know how to prevent the block from moving backward. I have seen other people use `t.direction` or something. But I have tried that and it didn't really work, maybe I'm just stupid and I did something wrong. How can I prevent the square from moving in the opposite direction?

Answer: 
You can add the condition `if (t.heading() + 180) % 360 != num:`, meaning if the opposite direction of the turtle's current heading direction isn't the direction the number passed into the function, then proceed:

```
import turtle
import random
import time

s = turtle.getscreen()
turtle.screensize(canvwidth=400, canvheight=400)
t = turtle.Turtle()
t.pensize(0)
t.shape('square')
t.color("black")
t.speed(0)
t.penup()

def moveu(num):
    if (t.heading() + 180) % 360 != num:
        t.setheading(num)
        t.forward(20)

s.onkey(lambda : moveu(90), 'w')
s.onkey(lambda : moveu(270), 's')
s.onkey(lambda : moveu(180), 'a')
s.onkey(lambda : moveu(0), 'd')

s.listen()
turtle.mainloop()

```
Solution
========

So, first issue is that I did not see `turtle.mainloop()` in the code. This means that it would not stay open. So, that is the first addition to the code.

Secondly, using a simple `if` statement might just be your answer. It is way simpler than you thought.

But, first, let's look at the directions.

```

             90
              ↑
              |
              |
180  ←---- 'turtle' ----→ 0
              |
              |
              ↓
             270

```

As, I am presuming, **180** degrees is the direction you consider to be backwards. If it isn't, just swap in your desired angle in the final code.

---

The `if` statement goes as follows. I have presented it within your `moveu` function.

```

def moveu(num):
    if num != 180:
        t.setheading(num)
        t.forward(20)

```

This means that, if the angle is not **180**, it will move. Therefore, it will not move back.

---

### Final Code

```

import turtle
import random
import time

s = turtle.getscreen()
turtle.screensize(canvwidth=400, canvheight=400)
t = turtle.Turtle()
t.pensize(0)
t.shape('square')
t.color('black')
t.speed(0)
t.penup()

def moveu(num):
    if num != 180:
        t.setheading(num)
        t.forward(20)

s.onkey(lambda : moveu(90), 'w')
s.onkey(lambda : moveu(270), 's')
s.onkey(lambda : moveu(180), 'a')
s.onkey(lambda : moveu(0), 'd')

s.listen()
s.mainloop()

```

As you can see, I have added `turtle.mainloop()` and the `if` statement.