How to make a Snake game with Python? | Part 1 (making snake head)
Tags: tutorial, python, beginners, challenge
Published: 2020-08-12 09:14:17

Hey my friends ;)

I'm here to share my experience and what I learned about making a game which was so cool and helpful in my journey.
I believe if you share what you learned something with someone-else it helps you to understand more in depth.
So yeah, I wan to show you how to make __Snake-Game__ in this post series.
In this tutorial I'll use a built-in module which is __Turtle__, it's for create 2D shapes and drawing and I love it cause it's so __beginner friendly__!


![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/61rbl3j9fj24z06d5lu4.jpg)<figcaption>__Just Keep Moving forward!__</figcaption>

If you are a __Beginner__ like me and want to learn more and improve your coding skill just keep reading my posts.

*****************
First of all we need to import `turtle` module to our _snake_game.py_ file.
```python
import turtle
```
Now we can use all of the classes and methods from turtle module.
The first and foremost thing that you need to set for every game is setting up the _screen_!

I'll write codes for setting up the screen then I'll explain you what's happening in our codes.
```python
import turtle
# 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)
```
1. __windows = turtle.Screen()__
>In the first line we instantiated the `Screen()` class.
So we can use `Screen()` methods.  

2.  __windows.title("Snake Game by Banji")__
>As you can see above after we instantiated the `Screen()` class, we used `title()` method to define our game title above of the game windows.(It is so amazing, isn't it?)

3. **windows.bgcolor("Pink")**
>here you can define what `BackGround Color` you want for your snake game.

4. **windows.setup(width = 1000, height = 700)**
>It will the size of your game windows. it has 4 parameter which are _(width, height, startx and starty)_ .
startx and starty will be the starting position of your windows and if you leave it alone your game-windows will open from the center!
5. __windows.tracer(0)__
> Turn the animation off on the screen!(Based On document!)
(I can't explain this one in depth, so if you think you can explain it more clear and better just leave a comment below, I'll add your explanation to this post)

If you run this program, you'll see something like pop up and the windows will close after a second!
for this problem you need to add a function to keep the windows open  and it's called `mainloop()`.
> __💡__ __Note: You Must add this function at the end of your codes!__

Now let's take a look to our codes:
```python

import turtle
# 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)

windows.mainloop()

```
Now if you run this code in your editor you'll see the windows will pop up on your computer screen which is cool!

*****************
After setting up the screen we'll move to code the snake head.
What we need to do first is to instantiate the `Turtle()` class to use its methods.
```python
# Snake Head
head = turtle.Turtle()
```

Then we're going to use some of the `Turtle()` methods that I'll explain you what they do in our codes.
```python
# Snake Head
head = turtle.Turtle()

head.speed(0)
head.shape("square")
head.color("black")
head.penup()
head.goto(0, 0)
head.direction = "stop"
```
1. **head.speed(0)**
>Defining the speed of animation for line drawing and turtle turning. Based on turtle docs the fastet is 0 and 1 is the slowest![Check it out](https://docs.python.org/3/library/turtle.html?highlight=speed#turtle.speed).

2. **head.shape()**
> You can choose the shape of your snake head, I chose `"square"` but there's other shapes like: `"triangle", "circle", "turtle", "classis"`, so you can pick up one of them.

3. **head.penup()**
> There's `penup()` and `pendown()`!
If you don't want your snake to draw lines in your game, just set it up to `penup()` so it will not draw lines anymore but if you want to play around with drawing just leave it alone! Cause the default is `pendown()`(Give it a shot!).
4. **head.goto()**
> Set the absolute position for snake head.
If you want to start from center you have to set like this: `head.goto(0, 0)`.

So our final codes would be like this:
```python 

import turtle



# 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"


windows.mainloop()
```
If you run this program you can't see the snake head!

* What's the problem?

You need to update the TurtleScreen everytime.
How? 
Using while loop!
```python
import turtle



# 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"

while True:
    windows.update()

windows.mainloop()

```
OMG!
Now if you run the program You can see our snake head and our pink screen(together)!
Isn't it so cool?
Ofcourse it is!
That was the first part of our snake game with python.
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 __💛__

__🅑🅐🅝🅙🅘__