Intrduction
Hello Guys,Most of us have once overhear in their life about the famous snake game and many of us love playing it. Today lets we learn how to code this classic game using the python programming language!
Before start coding of hungry snake game we first need to install the turtle module. The turtle module is a library of Python Language that allows the user to make pictures and shapes by providing them with a virtual canvas moreover the second library which we will be using is random library the random library is used to generate random numbers.
Step#1
If have not installed the library, Install the library using pip in command prompt or ShellC:\Users\Admin>pip install turtle
C:\Users\Admin>pip install random2
Step#2
First we need to open source where we can write code VScode,Pycharm,Jupyter etc or any source which you want to use. Pycharm supports extra features for python I'll prefare you to use Pycharm for python.Step#3
- We need to import turtle and random Library which we have installed in our PC in python path-First we are initializing values in different variables and stores keys and values in offsets as dictionary
import turtle
import random
w = 500
h = 500
food_size = 10
delay = 100
offsets = {
"up": (0, 20),
"down": (0, -20),
"left": (-20, 0),
"right": (20, 0)
}
Step#4
We are making global variables snake,snake_dir,food_position, pen and making nested list as snake.def reset():
global snake, snake_dir, food_position, pen
snake = [[0, 0], [0, 20], [0, 40], [0, 60], [0, 80]]
snake_dir = "up"
food_position = get_random_food_position()
food.goto(food_position)
move_snake()
Step#5
Here we are coding how to move snake. We see in game if a hungry snake catches its food its size increases so here we're doing this thing.def move_snake():
global snake_dir
new_head = snake[-1].copy()
new_head[0] = snake[-1][0] + offsets[snake_dir][0]
new_head[1] = snake[-1][1] + offsets[snake_dir][1]
if new_head in snake[:-1]:
reset()
else:
snake.append(new_head)
if not food_collision():
snake.pop(0)
if snake[-1][0] > w / 2:
snake[-1][0] -= w
elif snake[-1][0] < - w / 2:
snake[-1][0] += w
elif snake[-1][1] > h / 2:
snake[-1][1] -= h
elif snake[-1][1] < -h / 2:
snake[-1][1] += h
pen.clearstamps()
for segment in snake:
pen.goto(segment[0], segment[1])
pen.stamp()
screen.update()
turtle.ontimer(move_snake, delay)
Step#6
In this step we're coding that the food of hungry snake wouldn't collide.def food_collision():
global food_position
if get_distance(snake[-1], food_position) < 20:
food_position = get_random_food_position()
food.goto(food_position)
return True
return False
Step#7
In this step we''re getting random postition of food like as we saw in game if snake eat a bite the other piece will be place at different position.def get_random_food_position():
x = random.randint(- w / 2 + food_size, w / 2 - food_size)
y = random.randint(- h / 2 + food_size, h / 2 - food_size)
return (x, y)
def get_distance(pos1, pos2):
x1, y1 = pos1
x2, y2 = pos2
distance = ((y2 - y1) ** 2 + (x2 - x1) ** 2) ** 0.5
Step#8
In this step we're making directions of hungry snake whether to go up, down , right or leftdef go_up():
global snake_dir
if snake_dir != "down":
snake_dir = "up"
def go_right():
global snake_dir
if snake_dir != "left":
snake_dir = "right"
def go_down():
global snake_dir
if snake_dir!= "up":
snake_dir = "down"
def go_left():
global snake_dir
if snake_dir != "right":
snake_dir = "left"
Driver Code
This is the final step of our code we're running code in this step.screen = turtle.Screen()
screen.setup(w, h)
screen.title("Snake")
screen.bgcolor("blue")
screen.setup(500, 500)
screen.tracer(0)
pen = turtle.Turtle("square")
pen.penup()
food = turtle.Turtle()
food.shape("square")
food.color("yellow")
food.shapesize(food_size / 20)
food.penup()
screen.listen()
screen.onkey(go_up, "Up")
screen.onkey(go_right, "Right")
screen.onkey(go_down, "Down")
screen.onkey(go_left, "Left")
reset()
turtle.done()
Output
Conclusion
This is most interesting game and interest more developed when it comes to coding I practiced to code this game hope I get better response from community. Please take care of indentation while using this code ThankyouI am using #club5050 tag because I will convert 50% of this payout to powerup my steem as mentioned by @steemcurator01 and I will post very next to it where I will show I powered up my steem.
Special Thanks:
@steemit-pak
@haidermehdi
@event-horizon
@vvarishayy
50 SP | 100 SP | 200 SP | 300 SP | 400 SP | 500 SP |
---|
1000 SP | 1500 SP | 2000 SP | 3000 SP | 4000 SP | 5000 SP |
---|