-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSNAKE GAME_TKINTER
289 lines (243 loc) · 10.4 KB
/
SNAKE GAME_TKINTER
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# SNAKE GAME
import tkinter as tk
from tkinter import *
from tkinter import messagebox
import random
root = tk.Tk()
root.title("Snake Game")
root.configure(bg="light green")
def start():
global variable, apple, score
#for arrow precision
variable = 0
#game variables
score = 0
grid_limitx=40
grid_limity=25
speed = 100
#root.maxsize(width=grid_limitx*26, height=grid_limity*30)
#root.minsize(width=grid_limitx*26, height=grid_limity*30)
#grid system visualization
for i in range(grid_limitx):
l = Button(root, text=i, width=2, height=1, bg="grey")
l.grid(row=grid_limity, column=i)
l = Button(root, text=i, width=2, height=1, bg="grey")
l.grid(row=0, column=i)
for i in range(grid_limity):
l = Button(root, text=i, width=2, height=1, bg="grey")
l.grid(row=i, column=0)
l = Button(root, text=i+1, width=2, height=1, bg="grey")
l.grid(row=i+1, column=grid_limitx)
'''
for i in range(len(word)):
if i%2==0:
l = Button(root, text=word[i], width=2, height=1, bg="dark green")
l.grid(row=i+1, column=grid_limit, sticky=E)
else:
l = Button(root, text=word[i], width=2, height=1, bg="green")
l.grid(row=i+1, column=11, sticky=E)
'''
#starting btn
btn = Button(root, text=" :", bg="dark green", width=2)
btn.grid(row=grid_limity//2, column=grid_limitx//2)
def score_update():
global scr_l
root.title("Snake Game"+" "*grid_limitx+"Score: "+str(score))
'''
scr_l = Label(root, text="Score: "+str(score), bg="yellow")
scr_l.grid(row=0, column=11)
'''
score_update()
#apple poition
apple_pos = [(i, j) for i in range(1, grid_limity-1) for j in range(1, grid_limitx-1)]
random.shuffle(apple_pos)
#lists
bodies = [] #snake bodies
body_pos = [] #position for bodies
current = [] #current apple position
#apple
apple = Button(root, text="", bg="red", width=2)
apple.grid(row=apple_pos[0][0], column=apple_pos[0][1])
current.append((apple.grid_info()["row"], apple.grid_info()["column"]))
#messagebox.showinfo("Notification", "Snake Game\nBy Allen J")
#eat
def eat():
global apple,score
#if snake head position in current apple position
if (btn.grid_info()["row"], btn.grid_info()["column"]) in current:
apple.destroy()
score+=1
#scr_l.destroy()
score_update()
current.clear()
#adding a body
if len(bodies)%2==0:
btn1 = Button(root, text=" ", bg="green", width=2)
btn1.grid(row=body_pos[0][0], column=body_pos[0][1])
bodies.append(btn1)
else:
btn1 = Button(root, text=" ", bg="dark green", width=2)
btn1.grid(row=body_pos[0][0], column=body_pos[0][1])
bodies.append(btn1)
print(len(bodies))
#random apple position
apple = Button(root, text="", bg="red", width=2)
random.shuffle(apple_pos)
#to check if apple position in snake body position
def check():
if (apple_pos[0][0], apple_pos[0][1]) in body_pos:
random.shuffle(apple_pos)
check()
else:
apple.grid(row=apple_pos[0][0], column=apple_pos[0][1])
check()
current.append((apple.grid_info()["row"], apple.grid_info()["column"]))
else:
#removing the unnecessary body position(snake body wont use this)
body_pos.pop(0)
#left arrow
def left_arrow(event):
global variable
variable=1
def left():
if variable==1:
print("left")
btn.configure(text=": ")
#if snake head not touching border and snake body
if btn.grid_info()["column"]-1>=1 and (btn.grid_info()["row"], btn.grid_info()["column"]-1) not in body_pos:
btn.grid_configure(row=btn.grid_info()["row"], column=btn.grid_info()["column"]-1)
body_pos.append((btn.grid_info()["row"], btn.grid_info()["column"]))
#each snake body to move in previous position of previous snake body
for i in range(len(bodies)):
bodies[i].grid_configure(row=body_pos[-(i+2)][0], column=body_pos[ -(i+2)][1])
print(body_pos)
#if left arrow pressed, cannot press again, cannot press right arrow, can only press up and down arrows
if variable==1:
root.bind('<Down>', down_arrow)
root.bind('<Up>', up_arrow)
root.unbind('<Left>')
root.unbind('<Right>')
root.after(speed, left)
else:
root.bind('<Left>', left_arrow)
root.bind('<Right>', right_arrow)
else:
print("Lose!")
restart()
eat()
left()
#right arrow
def right_arrow(event):
global variable
variable=2
def right():
if variable==2:
print("right")
btn.configure(text=" :")
#if snake head not touching border and snake body
if btn.grid_info()["column"]+1<=grid_limitx-1 and (btn.grid_info()["row"], btn.grid_info()["column"]+1) not in body_pos:
btn.grid_configure(row=btn.grid_info()["row"], column=btn.grid_info()["column"]+1)
body_pos.append((btn.grid_info()["row"], btn.grid_info()["column"]))
#each snake body to move in previous position of previous snake body
for i in range(len(bodies)):
bodies[i].grid_configure(row=body_pos[-(i+2)][0], column=body_pos[-(i+2)][1])
print(body_pos)
#if right arrow pressed, cannot press again, cannot press left arrow, can only press up and down arrows
if variable==2:
root.bind('<Down>', down_arrow)
root.bind('<Up>', up_arrow)
root.unbind('<Right>')
root.unbind('<Left>')
root.after(speed, right)
else:
root.bind('<Right>', right_arrow)
root.bind('<Left>', left_arrow)
else:
print("Lose!")
restart()
eat()
right()
#up arrow
def up_arrow(event):
global variable
variable=3
def up():
if variable==3:
print("up")
btn.configure(text="..")
#if snake head not touching border and snake body
if btn.grid_info()["row"]-1>=1 and (btn.grid_info()["row"]-1, btn.grid_info()["column"]) not in body_pos:
btn.grid_configure(row=btn.grid_info()["row"]-1, column=btn.grid_info()["column"])
body_pos.append((btn.grid_info()["row"], btn.grid_info()["column"]))
#each snake body to move in previous position of previous snake body
for i in range(len(bodies)):
bodies[i].grid_configure(row=body_pos[-(i+2)][0], column=body_pos[-(i+2)][1])
#if up arrow pressed, cannot press again, cannot press down arrow, can only press left and right arrows
if variable==3:
root.unbind('<Up>')
root.unbind('<Down>')
root.bind('<Left>', left_arrow)
root.bind('<Right>', right_arrow)
root.after(speed, up)
else:
root.bind('<Up>', up_arrow)
root.bind('<Down>', down_arrow)
print(body_pos)
else:
print("Lose!")
restart()
eat()
up()
#down arrow
def down_arrow(event):
global variable
variable=4
def down():
if variable==4:
print("down")
btn.configure(text="..")
#if snake head not touching border and snake body
if btn.grid_info()["row"]+1<=grid_limity-1 and (btn.grid_info()["row"]+1, btn.grid_info()["column"]) not in body_pos:
btn.grid_configure(row=btn.grid_info()["row"]+1, column=btn.grid_info()["column"])
body_pos.append((btn.grid_info()["row"], btn.grid_info()["column"]))
#each snake body to move in previous position of previous snake body
for i in range(len(bodies)):
bodies[i].grid_configure(row=body_pos[-(i+2)][0], column=body_pos[-(i+2)][1])
#if down arrow pressed, cannot press again, cannot press up arrow, can only press left and right arrows
if variable==4:
root.unbind('<Down>')
root.unbind('<Up>')
root.bind('<Left>', left_arrow)
root.bind('<Right>', right_arrow)
root.after(speed, down)
else:
root.bind('<Down>', down_arrow)
root.bind('<Up>', up_arrow)
print(body_pos)
else:
print("Lose!")
restart()
eat()
down()
def restart():
messagebox.showinfo("Notification", "Game Over \nScore: "+str(score))
btn.destroy()
apple.destroy()
for body in bodies:
body.destroy()
bodies.clear()
body_pos.clear()
current.clear()
start()
def pause(event):
global variable
variable=0
messagebox.showinfo("Notification", "Game Paused")
#binding root to the arrow keys
root.bind('<Left>', left_arrow)
root.bind('<Right>', right_arrow)
root.bind('<Up>', up_arrow)
root.bind('<Down>', down_arrow)
root.bind('<p>', pause)
start()
root.mainloop()