-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake_game.py
238 lines (215 loc) · 7.28 KB
/
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
'''
Requirements
# pip install tk
# pip install numpy
'''
from tkinter import *
import random
import time
import numpy as np
from PIL import ImageTk,Image
size_of_board = 600
rows = 10
cols = 10
DELAY = 100
snake_initial_length = 3
symbol_size = (size_of_board / 3 - size_of_board / 8) / 2
symbol_thickness = 2
RED_COLOR = "red"
BLUE_COLOR = "blue"
GREEN_COLOR = "green"
BLUE_COLOR_LIGHT = '#67B0CF'
RED_COLOR_LIGHT = '#EE7E77'
class SnakeGame:
def __init__(self):
self.window = Tk()
self.window.title("Snake Game")
self.canvas = Canvas(self.window, width=size_of_board, height=size_of_board)
self.canvas.pack()
self.window.bind("<Key>", self.key_input)
self.window.bind("<Button-1>", self.mouse_input)
self.play_again()
self.begin = False
def initialize_board(self):
self.board = []
self.food_obj = []
self.old_food_cell = []
for i in range(rows):
for j in range(cols):
self.board.append((i, j))
for i in range(rows):
self.canvas.create_line(
i * size_of_board / rows, 0, i * size_of_board / rows, size_of_board,
)
for i in range(cols):
self.canvas.create_line(
0, i * size_of_board / cols, size_of_board, i * size_of_board / cols,
)
def initialize_snake(self):
self.snake = []
self.crashed = False
self.snake_heading = "Right"
self.last_key = self.snake_heading
self.forbidden_actions = {}
self.forbidden_actions["Right"] = "Left"
self.forbidden_actions["Left"] = "Right"
self.forbidden_actions["Up"] = "Down"
self.forbidden_actions["Down"] = "Up"
self.snake_objects = []
for i in range(snake_initial_length):
self.snake.append((i, 0))
def play_again(self):
self.canvas.delete("all")
self.initialize_board()
self.initialize_snake()
self.place_food()
self.display_snake(mode="complete")
self.begin_time = time.time()
def mainloop(self):
while True:
self.window.update()
if self.begin:
if not self.crashed:
self.window.after(DELAY, self.update_snake(self.last_key))
else:
self.begin = False
self.display_gameover()
def display_gameover(self):
score = len(self.snake)
self.canvas.delete("all")
score_text = "Scores \n"
self.canvas.create_text(
size_of_board / 2,
3 * size_of_board / 8,
font="cmr 40 bold",
fill=GREEN_COLOR,
text=score_text,
)
score_text = str(score)
self.canvas.create_text(
size_of_board / 2,
1 * size_of_board / 2,
font="cmr 50 bold",
fill=BLUE_COLOR,
text=score_text,
)
time_spent = str(np.round(time.time() - self.begin_time, 1)) + 'sec'
self.canvas.create_text(
size_of_board / 2,
3 * size_of_board / 4,
font="cmr 20 bold",
fill=BLUE_COLOR,
text=time_spent,
)
score_text = "Click to play again! \n"
self.canvas.create_text(
size_of_board / 2,
15 * size_of_board / 16,
font="cmr 20 bold",
fill="gray",
text=score_text,
)
def place_food(self):
unoccupied_cels = set(self.board) - set(self.snake)
self.food_cell = random.choice(list(unoccupied_cels))
row_h = int(size_of_board / rows)
col_w = int(size_of_board / cols)
x1 = self.food_cell[0] * row_h
y1 = self.food_cell[1] * col_w
x2 = x1 + row_h
y2 = y1 + col_w
self.food_obj = self.canvas.create_rectangle(
x1, y1, x2, y2, fill=RED_COLOR_LIGHT, outline=BLUE_COLOR,
)
def display_snake(self, mode=""):
if self.snake_objects != []:
self.canvas.delete(self.snake_objects.pop(0))
if mode == "complete":
for i, cell in enumerate(self.snake):
row_h = int(size_of_board / rows)
col_w = int(size_of_board / cols)
x1 = cell[0] * row_h
y1 = cell[1] * col_w
x2 = x1 + row_h
y2 = y1 + col_w
self.snake_objects.append(
self.canvas.create_rectangle(
x1, y1, x2, y2, fill=BLUE_COLOR, outline=BLUE_COLOR,
)
)
else:
cell = self.snake[-1]
row_h = int(size_of_board / rows)
col_w = int(size_of_board / cols)
x1 = cell[0] * row_h
y1 = cell[1] * col_w
x2 = x1 + row_h
y2 = y1 + col_w
self.snake_objects.append(
self.canvas.create_rectangle(
x1, y1, x2, y2, fill=BLUE_COLOR, outline=RED_COLOR,
)
)
if self.snake[0] == self.old_food_cell:
self.snake.insert(0, self.old_food_cell)
self.old_food_cell = []
tail = self.snake[0]
row_h = int(size_of_board / rows)
col_w = int(size_of_board / cols)
x1 = tail[0] * row_h
y1 = tail[1] * col_w
x2 = x1 + row_h
y2 = y1 + col_w
self.snake_objects.insert(
0,
self.canvas.create_rectangle(
x1, y1, x2, y2, fill=BLUE_COLOR, outline=RED_COLOR
),
)
self.window.update()
def update_snake(self, key):
tail = self.snake[0]
head = self.snake[-1]
if tail != self.old_food_cell:
self.snake.pop(0)
if key == "Left":
self.snake.append((head[0] - 1, head[1]))
elif key == "Right":
self.snake.append((head[0] + 1, head[1]))
elif key == "Up":
self.snake.append((head[0], head[1] - 1))
elif key == "Down":
self.snake.append((head[0], head[1] + 1))
head = self.snake[-1]
if (
head[0] > cols - 1
or head[0] < 0
or head[1] > rows - 1
or head[1] < 0
or len(set(self.snake)) != len(self.snake)
):
self.crashed = True
elif self.food_cell == head:
self.old_food_cell = self.food_cell
self.canvas.delete(self.food_obj)
self.place_food()
self.display_snake()
else:
self.snake_heading = key
self.display_snake()
def check_if_key_valid(self, key):
valid_keys = ["Up", "Down", "Left", "Right"]
if key in valid_keys and self.forbidden_actions[self.snake_heading] != key:
return True
else:
return False
def mouse_input(self, event):
self.play_again()
def key_input(self, event):
if not self.crashed:
key_pressed = event.keysym
if self.check_if_key_valid(key_pressed):
self.begin = True
self.last_key = key_pressed
game_instance = SnakeGame()
game_instance.mainloop()