How to make a Snake game with Python? | Part 2 (moving the head)
Tags: python, beginners, tutorial, challenge
Published: 2020-08-13 11:10:25

Heya I'm Banji with another tutorial for __beginners__ like myself!
This is the second part of making _Snake Game_.
In this part we are trying to make a function to move the snake head around on the screen.
Let's dive into coding and see what will happen...

If you follow this series of tutorial you have to remember the last part of our codes.[(How to make a Snake game with Python? | Part 1)](https://dev.to/banji220/how-to-make-a-snake-game-with-python-part-1-making-snake-head-26io).
Cause our code gets longer and longer, I won't repeat the last part again


![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/apwqs60i8h9vctfkdkov.jpg)<figcaption>__Let's be a beginner!__</figcaption>

So don't be afraid to be a beginner, just keep moving forward and I promise that we will learn a lot together, __FOREVER__!

*************
For moving the snake head we'll make a new function and call it `move()`.

```python
def move():
    pass
```

we want to take some action on it, so let's see what we can do and after that I'll explain you what happened.
```python
# Move Function
def move():
    # Move Up 
    if head.direction == "Up":
        y = head.ycor()
        head.sety(y + 10)
``` 

Put this part of code above the __while loop__.
Actually what is happening here is:
> If the head direction is equals to up, turn the head in y coordinate. we set 10, but it's totally up to you just play around with it!

If you run this code, nothing will happen!
Because you need to put move function in the loop!
So it will be like this:
```python
import turtle
import time


# Set up the screen
windows = turtle.Screen()
windows.title("Snake Game by Banji")
windows.bgcolor("Pink")
windows.setup(width = 1000, height = 700)
windows.tracer(0)

# Snake Head
head = turtle.Turtle()
head.speed(0)
head.shape("square")
head.color("black")
head.penup()
head.goto(0, 0)
head.direction = "stop"


def move():
    # Move Up 
    if head.direction == "Up":
        y = head.ycor()
        head.sety(y + 10)

# Main Game Loop
while True:
    windows.update()

    # putting the move  function in the loop 
    move()
windows.mainloop()



```
if you run this code again nothing will happen, do you know why?
>Because the `head.direction()` should be the same!
So let's change the `"stop"` to the `"Up"` an run the codes again.

```python
import turtle
import time



windows = turtle.Screen()
windows.title("Snake Game by Banji")
windows.bgcolor("Pink")
windows.setup(width = 1000, height = 700)
windows.tracer(0)


head = turtle.Turtle()
head.speed(0)
head.shape("square")
head.color("black")
head.penup()
head.goto(0, 0)



# You have to change this part
head.direction = "Up"



def move():

    if head.direction == "Up":
        y = head.ycor()
        head.sety(y + 10)


while True:
    windows.update()

    # move  function in the loop 
    move()
windows.mainloop()



```
Run the program agin...
This time you can't see the snake head!
Damn God :(
The reason of this problem is:
>Cause Snake head is moving soooo fast so you can't see the head!
For solving this issue we are going to use `time` to slow down the head movement.
There's a method in time module which is `sleep`.
Actually `sleep()` suspends execution for the given number of seconds.
Take a look that how we can use `time` module with `sleep()` method.
```python

import turtle
import time


# Set up the screen
windows = turtle.Screen()
windows.title("Snake Game by Banji")
windows.bgcolor("Pink")
windows.setup(width = 1000, height = 700)
windows.tracer(0)

# Snake Head
head = turtle.Turtle()
head.speed(0)
head.shape("square")
head.color("black")
head.penup()
# head.goto(0, 0)
head.direction = "Up"


def move():
    # Move Up 
    if head.direction == "Up":
        y = head.ycor()
        head.sety(y + 10)

# Main Game Loop
while True:
    windows.update()
     
    move()


    #Adding Time Module and sleep method to our Main Loop
    time.sleep(0.1)



windows.mainloop()



```

Cause we have 4 directions we need to add other directions to our `move()` function.

```python
def move():
    # Move Up 
    if head.direction == "Up":
        y = head.ycor()
        head.sety(y + 10)
        
    # Move Down 
    if head.direction == "Down":
        y = head.ycor()
        head.sety(y - 10)
    
    # Move Left 
    if head.direction == "Left":
        x = head.xcor()
        head.setx(x - 10)
    
    # Move Right 
    if head.direction == "Right":
        x = head.xcor()
        head.setx(x + 10)
```

So if you want to stop moving, just set `head.direction = "Up"` to `head.direction = "stop"`.
But what if we want to move the snake head with keyboard?
We need to define functions for every direction.

```python
# Go Functions
def go_up():
    head.direction = "Up"
 
def go_down():
    head.direction = "Down"

def go_left():
    head.direction = "Left"  

def go_right():
    head.direction = "Right"
```
Put this functions above the `move()` function.
Then for the next step, there's a method `.listen()` to collect key-events.
After that we need to define another method which is `.onkeypress(function, key)`!

Let's take a look:
```python
# Keyboard Binding
windows.listen()
windows.onkeypress(go_up, key = "w")
windows.onkeypress(go_down, key = "s")
windows.onkeypress(go_left, key = "a")
windows.onkeypress(go_right, key = "d")
```
Put this piece of code above the `while loop`.
If you want to check out the complete codes or if your are so __LAZY__ to write a lot of codes, you can check out my gitlab.
[Part 2 | How to move Snake Head?(complete codes)](https://gitlab.com/banji220/snake-game/-/blob/master/snake_head_moving.py)


Now, if you run the program you can see the head is moving on the screen, which is sooooo __COOL!
I love it, just keep learning.
********

If you found any __Bugs__ or problem in codes or if you have any suggestion feel free to leave a __Comment__ below.
Thank you for reading my tutorials and articles. 




Keep Moving Forward __ツ__

Enjoy Your Journey 👊

Code with __💛__

__🅑🅐🅝🅙🅘__