How to make a snake on Python
Make a snake game in Python with this step-by-step guide. Follow the example & create a classic game in no time!
Making a Snake on Python
Python is a powerful programming language and offers a wide variety of libraries for creating games. To make a snake game with Python, you will need to use the Turtle Graphics library. Turtle is a library that allows you to draw shapes and other graphics onto a canvas, which can then be used to create your game. Let's take a look at how to make a snake game with Python using Turtle.
The first step is to import the Turtle library. To do this, you will need to include the following code at the beginning of your program:
import turtle
Once you have imported the library, you will need to create a canvas for the game. To do this, you will need to create a turtle object and set its screen size. To do this, you will need to use the following code:
wn = turtle.Screen()
wn.setup(width=600, height=600)
Now that you have created your canvas, you will need to create the snake. To do this, you will need to create a turtle object and set its shape and color. To do this, you will need to use the following code:
snake = turtle.Turtle()
snake.shape("square")
snake.color("green")
Once you have created the snake, you will need to set its speed and initial position. To do this, you will need to use the following code:
snake.speed(5)
snake.penup()
snake.goto(0,0)
snake.direction = "stop"
Now that you have set up the snake, you will need to write the code for the game loop. The game loop will need to check for user input and move the snake accordingly. To do this, you will need to use the following code:
while True:
wn.update()
if snake.direction == "up":
y = snake.ycor()
snake.sety(y + 20)
if snake.direction == "down":
y = snake.ycor()
snake.sety(y - 20)
if snake.direction == "left":
x = snake.xcor()
snake.setx(x - 20)
if snake.direction == "right":
x = snake.xcor()
snake.setx(x + 20)
Now that you have written the game loop, you will need to write the code for user input. To do this, you will need to use the following code:
def go_up():
if snake.direction != "down":
snake.direction = "up"
def go_down():
if snake.direction != "up":
snake.direction = "down"
def go_left():
if snake.direction != "right":
snake.direction = "left"
def go_right():
if snake.direction != "left":
snake.direction = "right"
wn.listen()
wn.onkeypress(go_up, "Up")
wn.onkeypress(go_down, "Down")
wn.onkeypress(go_left, "Left")
wn.onkeypress(go_right, "Right")
Finally, you will need to create a game over condition and a score system. To do this, you will need to use the following code:
if snake.xcor()>290 or snake.xcor()<-290 or snake.ycor()>290 or snake.ycor()<-290:
snake.goto(0,0)
snake.direction = "stop"
score = 0
pen.clear()
pen.write("Score: {} High Score: {}".format(score, high_score), align="center", font=("Courier", 24, "normal"))
That's it! You now have all the code you need to create a simple snake game with Python. You can now start building your own version of the game and adding more features. Good luck!