Welcome folks today in this blog post we will be building a arcade snake game
in tkinter using pillow
library. All the full source code of the application is shown below.
Get Started
In order to get started you need to install the following library using the pip
command as shown below
pip install pillow
After installing this library you need to make an snake_game.py
file and copy paste the following code
snake_game.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
import tkinter as tk from random import randint from PIL import ImageTk, Image MOVE_INCREMENT = 20 MOVES_PER_SECOND = 15 GAME_SPEED = 250 class Snake(tk.Canvas): #init function to load the starting state of the game def __init__(self): super().__init__( width=600, height=620, background="black", highlightthickness=0 ) self.score=0 self.snake_positions = [(100,100),(80,100),(60,100)] self.food_position = self.set_new_food_position() self.direction = "Right" self.bind_all("<Key>",self.on_key_press) self.load_assets() self.create_objects() self.pack() self.after(GAME_SPEED, self.perform_actions) #loading our assets - snake and food on the Canvas def load_assets(self): try: self.snake_body_image = Image.open("./assets/snake.png") self.snake_body = ImageTk.PhotoImage(self.snake_body_image) self.food_image = Image.open("./assets/food.png") self.food = ImageTk.PhotoImage(self.food_image) except IOError as error: print(error) root.destroy() #rendering the objects, score box and play area on the Canvas def create_objects(self): self.create_text( 40,12,text=f"Score: {self.score}",tag="score",fill="#fff",font=10 ) for x_pos, y_pos in self.snake_positions: self.create_image( x_pos, y_pos, image=self.snake_body, tag="snake" ) self.create_image(*self.food_position, image=self.food, tag="food") self.create_rectangle(7,27,593,613,outline="#525d69") #make our snake work for his food def set_new_food_position(self): while True: x_position = randint(1,29)*MOVE_INCREMENT y_position = randint(3,30)*MOVE_INCREMENT food_position = (x_position,y_position) if food_position not in self.snake_positions: return food_position # feeding our snake some snacks def check_food_collision(self): if self.snake_positions[0] == self.food_position: self.score +=1 self.snake_positions.append(self.snake_positions[-1]) self.create_image( *self.snake_positions[-1], image=self.snake_body, tag="snake" ) self.food_position = self.set_new_food_position() self.coords(self.find_withtag("food"), *self.food_position) score = self.find_withtag("score") self.itemconfigure(score, text=f"Score: {self.score}",tag="score") #checking the conditions for game to end def check_collisions(self): head_x_pos, head_y_pos = self.snake_positions[0] return( head_x_pos in(0,600) or head_y_pos in (20,620) or (head_x_pos,head_y_pos) in self.snake_positions[1:] ) #checking the users input def on_key_press(self,e): new_direction = e.keysym all_directions = ("Up", "Down", "Left", "Right") opposites = ({"Up","Down"}, {"Left", "Right"}) if(new_direction in all_directions and {new_direction,self.direction} not in opposites): self.direction = new_direction #animating our snake :3 def move_snake(self): head_x_pos, head_y_pos = self.snake_positions[0] if self.direction == "Left": new_head_positon = (head_x_pos-MOVE_INCREMENT, head_y_pos) elif self.direction == "Right": new_head_positon = (head_x_pos+MOVE_INCREMENT, head_y_pos) elif self.direction == "Down": new_head_positon = (head_x_pos, head_y_pos+MOVE_INCREMENT) elif self.direction == "Up": new_head_positon = (head_x_pos, head_y_pos-MOVE_INCREMENT) self.snake_positions = [new_head_positon] + self.snake_positions[:-1] for segment, position in zip(self.find_withtag("snake"), self.snake_positions): self.coords(segment,position) #performing actions based on user input def perform_actions(self): if self.check_collisions(): self.end_game() self.check_food_collision() self.move_snake() self.after(GAME_SPEED, self.perform_actions) #end game def end_game(self): self.delete(tk.ALL) self.create_text( self.winfo_width() / 2, self.winfo_height() / 2, text=f"Game Over! You Scored {self.score}!", fill="#fff", font=("",10) ) root=tk.Tk() root.title("Snake Arcade") root.resizable(False,False) root.tk.call("tk","scaling",4.0) board = Snake() root.mainloop() |
Now just download the entire github
repository from below
DOWNLOAD SOURCE CODE
After downloading the github repository the code directory structure will look something like this
Now just run the snake_game.py
file by the below command
python snake_game.py