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).
Cause our code gets longer and longer, I won't repeat the last part again
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()
.
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.
# 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:
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.
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 usetime
to slow down the head movement.
There's a method in time module which issleep
.
Actuallysleep()
suspends execution for the given number of seconds.
Take a look that how we can usetime
module withsleep()
method.
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.
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.
# 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:
# 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)
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 ๐
๐ ๐ ๐ ๐ ๐
Top comments (1)
love this ๐