-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPython_snake_game.py
368 lines (305 loc) · 11 KB
/
Python_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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
from pygame import *
import random
import string
from tkinter import *
import pymysql
connection = pymysql.connect(host="localhost",user="root",password="root")
cursor=connection.cursor()
g=0
sc=0
name=" "
i=0
class fpage:
def fpage(self):
self.top=Tk()
self.top.title("Snakes")
self.name=""
self.l1=Label(self.top,text="Your Name")
self.l1.pack()
#creating an entry box
self.v1=StringVar()
self.ebox1=Entry(self.top,textvariable=self.v1)
self.ebox1.pack()
#insertin second label
self.l2=Label(self.top,text="Select your Difficulty")
self.l2.pack()
#creating a radio box
self.v2=IntVar()
self.rbutton1=Radiobutton(self.top,text="Amateur",variable=self.v2,value=1,command=self.check1)
self.rbutton1.pack()
self.rbutton2=Radiobutton(self.top,text="Semi-pro",variable=self.v2,value=2,command=self.check1)
self.rbutton2.pack()
self.rbutton3=Radiobutton(self.top,text="Professional",variable=self.v2,value=3,command=self.check1)
#self.v2.set(1)
self.rbutton3.pack()
#reading the entry box
self.name=self.v1.get()
self.name=str(self.name)
def strt():
if g!=0 :
self.top.destroy()
main()
else:
self.l3.pack()
self.b=Button(self.top,text="Lets Play ",command=strt)
self.b.pack()
self.l3=Label(self.top,text="Please Enter a Difficulty Level")
def check1(self):
global g
if self.v2.get()==1:
g=1
if self.v2.get()==2:
g=2
if self.v2.get()==3:
g=3
#self.ll3.pack()
global name
name=self.v1.get()
f=fpage()
f.fpage()
# Specify the snake & the characters it uses
HEAD_CHAR = "#"
FOOD_CHARS = "*"
# Application
class Application(fpage):
# Basic setup variables
TITLE = "Snake"
SIZE = 300, 300
fobj=fpage
def __init__(self, master):
# Initializing the variables
self.master = master
self.head = None
self.head_position = None
self.segments = []
self.segments_positions = []
self.food = None
self.food_position = None
self.direction = None
self.ads="#"
self.moved = True
self.running = False
# Run the init function
self.init()
def init(self):
self.master.title(self.TITLE)
# Creating the canvas
self.canvas = Canvas(self.master)
self.canvas.grid(sticky=NSEW)
# Creating the start button
self.start_button = Button(self.master, text="Start", command=self.do)
self.start_button.grid(sticky=EW)
# Bind the movements keys to the canvas
self.master.bind('<Up>', self.on_up)
self.master.bind('<Left>', self.on_left)
self.master.bind('<Down>', self.on_down)
self.master.bind('<Right>', self.on_right)
# Configure the size of the canvas
self.master.columnconfigure(0, weight=1)
self.master.rowconfigure(0, weight=1)
self.master.resizable(width=False, height=False)
self.master.geometry("%dx%d" % self.SIZE)
# When start button is clicked
def on_start(self):
# Reset Everything
self.reset()
#self.ebox1.set("")
# Check if the game is already running
if self.running:
self.running = False
# Changing the text displayed on the button
self.start_button.configure(text="Start")
else:
self.running = True
# Changing the text displayed on the button
self.start_button.configure(text="Stop")
# Starting the game
self.start()
def do(self):
self.play()
self.on_start()
def play(self):
mixer.music.play()
def stop(self):
mixer.music.stop()
def amateur(self):
spd = 100
if len(self.segments) > 5:
spd = 75
if len(self.segments) > 10:
spd = 60
if len(self.segments) > 20:
spd = 45
return spd
def semipro(self):
spd = 75
if len(self.segments) > 5:
spd = 60
if len(self.segments) > 10:
spd = 45
if len(self.segments) > 20:
spd = 35
return spd
def pro(self):
spd = 60
if len(self.segments) > 5:
spd = 45
if len(self.segments) > 10:
spd = 35
if len(self.segments) > 20:
spd = 25
return spd
# Reset function for the game
def reset(self):
# Delete all the snake's body
del self.segments[:]
del self.segments_positions[:]
self.canvas.delete(ALL)
# Start function for the game
def start(self):
# Taking in the info of the canvas (width & height)
width = self.canvas.winfo_width()
height = self.canvas.winfo_height()
# Draw the game screen
self.canvas.create_rectangle(10, 10, width - 10, height - 10)
self.direction = random.choice('wasd')
head_position = [round(width / 2, -1), round(height / 2, -1)]
self.head = self.canvas.create_text(tuple(head_position), text=HEAD_CHAR)
self.head_position = head_position
# Calling the functions to start the game - spawning food & updating
self.spawn_food()
self.tick()
# Function for spawning the food
def spawn_food(self):
# get the width & height of canvas
width = self.canvas.winfo_width()
height = self.canvas.winfo_height()
# check if the food is spawned on the snake's body
positions = [tuple(self.head_position), self.food_position] + self.segments_positions
position = round(random.randint(20, width - 20), -1), round(random.randint(20, height - 20), -1)
# if the newly generated food is overlapping, generate until it is not
while position in positions:
position = round(random.randint(20, width - 20), -1), round(random.randint(20, height - 20), -1)
# pick a character to be generated
character = "*"
self.food = self.canvas.create_text(position, text=character)
# store the previously generated character
self.food_position = position
self.food_character = character
# When the timer ticks (updating the game)
def tick(self):
# get the canvas' width & height
width = self.canvas.winfo_width()
height = self.canvas.winfo_height()
previous_head_position = tuple(self.head_position)
# move the snake
if self.direction == "w":
self.head_position[1] -= 10
elif self.direction == "a":
self.head_position[0] -= 10
elif self.direction == "s":
self.head_position[1] += 10
elif self.direction == "d":
self.head_position[0] += 10
# check if the game is over
head_position = tuple(self.head_position)
if(head_position[0] < 10 or head_position[0] >= width - 10 or head_position[1] < 10 or head_position[1] >= height - 10 or any(segments_position == head_position for segments_position in self.segments_positions)):
self.game_over()
self.db()
return
# Check if snake eats the food
if head_position == self.food_position:
self.canvas.coords(self.food, previous_head_position)
self.segments.append(self.food)
self.segments_positions.append(previous_head_position)
self.spawn_food()
mixer.Sound('Munch.wav').play()
# Make the food following the snake's head
if self.segments:
previous_position = previous_head_position
for index, (segment, position) in enumerate(zip(self.segments, self.segments_positions)):
self.canvas.coords(segment, previous_position)
self.segments_positions[index] = previous_position
previous_position = position
# Put the new head's position into head_position
self.canvas.coords(self.head, head_position)
self.moved = True
# change level (level up according to length of snake)
if g==0:
speed=self.amateur()
if g==1:
speed=self.amateur()
if g==2:
speed=self.semipro()
if g==3:
speed=self.pro()
# Call the tick function to update again after a certain time
if self.running:
self.canvas.after(speed, self.tick)
display_speed = 10000 / speed
self.start_button.configure(text = "Speed: %d" %display_speed)
# Function for game over screen
def game_over(self):
# get the canvas' width & height
width = self.canvas.winfo_width()
height = self.canvas.winfo_height()
mixer.Sound('GameOver.wav').play()
self.stop()
# stop the game from running
self.running = False
# change the button's text to "start"
self.start_button.configure(text="Start")
global i
i=i+1
# display the game over message & show the score
global sc
score = len(self.segments) * 10
self.canvas.create_text(round(width/2), round(height/2), text="Game Over! Your score is: %d" %score)
sc=score
def db(self):
global i
global name
global sc
global g
if g==1:
ln="Amateur"
elif g==2:
ln="Semi-pro"
else :
ln="Professional"
try:
cursor.execute("create database players;")
except:
cursor.execute("use players;")
try:
cursor.execute("create table player_info(Pno int,Pname varchar(20),score int,level varchar(20));")
except:
print("table exists")
cursor.execute("insert into player_info values(%s,%s,%s,%s)",(i,name,sc,ln))
connection.commit()
# Function for 4 inputs
# Cannot move opposite direction e.g.Moving down cannot move up
def on_up(self, event):
if self.moved and not self.direction == "s":
self.direction = "w"
self.moved = False
def on_down(self,event):
if self.moved and not self.direction == "w":
self.direction = "s"
self.moved = False
def on_left(self, event):
if self.moved and not self.direction == "d":
self.direction = "a"
self.moved = False
def on_right(self, event):
if self.moved and not self.direction == "a":
self.direction = "d"
self.moved = False
# Declaring the main loop (outside of any classes)
def main():
music_file = 'ThemeTrack.mp3'
mixer.init()
mixer.music.load(music_file)
root = Tk()
Application(root)
root.mainloop()