DEV Community

loading...
Cover image for How to make a Snake game with Python? | Part 1 (making snake head)

How to make a Snake game with Python? | Part 1 (making snake head)

banji220 profile image Banji Updated on ・4 min read

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
Just Keep Moving forward!

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.

import turtle
Enter fullscreen mode Exit fullscreen mode

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.

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)
Enter fullscreen mode Exit fullscreen mode
  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:


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()

Enter fullscreen mode Exit fullscreen mode

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.

# Snake Head
head = turtle.Turtle()
Enter fullscreen mode Exit fullscreen mode

Then we're going to use some of the Turtle() methods that I'll explain you what they do in our codes.

# Snake Head
head = turtle.Turtle()

head.speed(0)
head.shape("square")
head.color("black")
head.penup()
head.goto(0, 0)
head.direction = "stop"
Enter fullscreen mode Exit fullscreen mode
  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 slowestCheck it out.

  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:


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()
Enter fullscreen mode Exit fullscreen mode

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!

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()

Enter fullscreen mode Exit fullscreen mode

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 💛

🅑🅐🅝🅙🅘

Discussion (2)

pic
Editor guide
Collapse
gagandureja profile image
Gagan • Edited

Error: Tkinter module not found on Ubuntu
For those who face the same problem:
try- sudo apt-get install python3-tk

Collapse
banji220 profile image
Banji Author

Thank you bro 🎈