-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathturtlesnake.py
446 lines (396 loc) · 11.5 KB
/
turtlesnake.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
# Python Snake Game by Steven Weinstein on 8-8-2022. Version available in version.txt
# import required modules
TK_SILENCE_DEPRECATION = 1
from platform import python_version
import json
import threading as thr
import snakeconfig
from random import randint, choice
from time import sleep
import turtle
from pathlib import Path
script_path = Path(__file__, '..').resolve()
# checking if python version is compatible
pyversion = python_version()
print("Your python version is:")
print(pyversion)
if (
"3.6" in pyversion or
"3.7" in pyversion or
"3.8" in pyversion or
"3.9" in pyversion or
"3.10" in pyversion or
"3.11" in pyversion
):
print("Python version check pass")
else:
print("v"*20)
print("Please upgrade your python to be version 3.6 or newer, terminating process")
print("^"*20)
quit()
foodnum = 1
global changedcolor
changedcolor = False
# default options:
prefs = {
"highscore": 0,
"bgcolor": "dark green",
"foodcolor": "navy",
"foodshape": "square",
"foodnum": 2,
"mutesound": True,
"headcolor": "white",
"tailcolor": "blue"
}
try:
from pygame import mixer
noplaysound = False
mixer.init()
except:
noplaysound = True
global high_score
global mutesound
with open(script_path.joinpath("prefs.json"), "r") as read_file:
prefs = json.load(read_file)
high_score = prefs["highscore"]
bgcolor = prefs["bgcolor"]
foodcolor = prefs["foodcolor"]
foodshape = prefs["foodshape"]
foodnum = prefs["foodnum"]
mutesound = prefs["mutesound"]
tailcolor = prefs["tailcolor"]
headcolor = prefs["headcolor"]
# defining important vars
error = False
delay = 0.025
movepertick = 5
score = 0
APIon = False
# opening files
datadoc = open(script_path.joinpath("data.txt"), "a")
foodnum = int(foodnum)
if foodnum == 2:
APIdata = [None, None, None]
elif foodnum == 1:
APIdata = [None]
try:
high_score = int(high_score)
except ValueError:
high_score = 0
error = True
print("Error: last highscore is not an integer.")
wn = turtle.Screen()
wn.title("Snake Game Project v2.0.0")
wn.bgcolor(bgcolor)
# the width and height can be put as user's choice
wn.setup(width=600, height=600)
wn.tracer(0)
# head of the snake
head = turtle.Turtle()
head.shape("square")
try:
head.color(headcolor)
except:
snakeconfig.snakereset()
quit()
head.penup()
head.goto(0, 0)
head.direction = "Stop"
# food in the game
food = turtle.Turtle()
#colors = colist[1]
colors = foodcolor
shapes = foodshape
food.speed(0)
food.shape(shapes)
food.color(colors)
food.penup()
food.goto(0, 100)
if foodnum == 2:
food2 = turtle.Turtle()
colors2 = foodcolor
shapes2 = foodshape
food2.speed(0)
food2.shape(shapes2)
food2.color(colors2)
food2.penup()
food2.goto(0, -200)
pen = turtle.Turtle()
pen.speed(0)
pen.shapesize(24)
pen.shape("square")
pen.color("white")
pen.penup()
pen.hideturtle()
pen.goto(0, 250)
pen.write(f"Score: 0 High Score: {high_score}", align="center",
font=("candara", 24, "bold"))
# assigning key directions
def goup():
if head.direction != "down":
head.direction = "up"
def godown():
if head.direction != "up":
head.direction = "down"
def goleft():
if head.direction != "right":
head.direction = "left"
def goright():
if head.direction != "left":
head.direction = "right"
def move():
if head.direction == "up":
y = head.ycor()
head.sety(y+movepertick)
if head.direction == "down":
y = head.ycor()
head.sety(y-movepertick)
if head.direction == "left":
x = head.xcor()
head.setx(x-movepertick)
if head.direction == "right":
x = head.xcor()
head.setx(x+movepertick)
def DEVTOOLRESET():
head.direction = "Stop"
head.goto(0, 0)
global high_score
high_score = 0
global prefs
prefs = {
"highscore": 0,
}
with open(script_path.joinpath("prefs.json"), "w") as write_file:
json.dump(prefs, write_file)
quit()
def colorconfig():
snakeconfig.config()
def snakerecolor():
snakeconfig.snakecolor()
def APItoggle():
global APIon
if APIon:
APIon = False
else:
APIon = True
def APIproc():
dist1 = head.distance(food)
APIdata[0] = dist1
if foodnum == 2:
dist2 = head.distance(food2)
print(APIdata)
APIdata[1] = dist2
try:
APIdata[2] = (int(APIdata[0]) + int(APIdata[1]))/2
except IndexError:
APIdata.append((int(APIdata[0]) + int(APIdata[1]))/2)
datadoc.write(
f"({round(APIdata[0], 2)}),({round(APIdata[1], 2)}),({round(APIdata[2], 3)})\n")
elif foodnum == 1:
datadoc.write(f"({round(APIdata[0], 2)})\n")
def playswallow():
if not noplaysound:
mixer.music.load(script_path.joinpath("extrafiles/swallow.mp3"))
mixer.music.play()
def playsmash():
if not noplaysound:
mixer.music.load(script_path.joinpath("extrafiles/smash.mp3"))
mixer.music.play()
def togglemute():
global mutesound
if mutesound:
mutesound = False
else:
mutesound = True
def config():
prefs = {
"highscore": 0,
"bgcolor": "dark green",
"foodcolor": "navy",
"foodshape": "square",
"foodnum": 2,
"mutesound": True
}
print('''Color Configurator
enter a hex code or an accepted color name from this list: https://trinket.io/docs/colors NO RGB VALUES''')
prefs["bgcolor"] = input("What color would you like the background: ")
prefs["foodcolor"] = input("What color would you like the food: ")
prefs["foodshape"] = input("Food shape: circle square triange or turtle: ")
prefs["foodnum"] = input("Would you like to have 1 or 2 foods: ")
with open(script_path.joinpath("prefs.json"), "w") as write_file:
json.dump(prefs, write_file)
# writing to text document
print("Succesfuly configured!\nYou will have to restart the game for changes to take effect.")
global changedcolor
changedcolor = True
return
def snakecolor():
prefs = {
"highscore": 0,
"bgcolor": "dark green",
"foodcolor": "navy",
"foodshape": "square",
"foodnum": 2,
"mutesound": True
}
print('''Color Configurator
enter a hex code or an accepted color name from this list: https://trinket.io/docs/colors NO RGB VALUES''')
prefs["headcolor"] = input("What color would you like the snake head: ")
prefs["tailcolor"] = input("What color would you like the snake tail: ")
with open(script_path.joinpath("prefs.json"), "w") as write_file:
json.dump(prefs, write_file)
# writing to text document
print("Succesfuly configured!\nYou will have to restart the game for changes to take effect.")
global changedcolor
changedcolor = True
return
def exit():
wn.bye()
wn.listen()
wn.onkeypress(goup, "w")
wn.onkeypress(godown, "s")
wn.onkeypress(goleft, "a")
wn.onkeypress(goright, "d")
wn.onkeypress(goup, "W")
wn.onkeypress(godown, "S")
wn.onkeypress(goleft, "A")
wn.onkeypress(goright, "D")
wn.onkeypress(goup, "Up")
wn.onkeypress(godown, "Down")
wn.onkeypress(goleft, "Left")
wn.onkeypress(goright, "Right")
wn.onkeypress(DEVTOOLRESET, "/")
wn.onkeypress(DEVTOOLRESET, "r")
wn.onkeypress(DEVTOOLRESET, "?")
wn.onkeypress(DEVTOOLRESET, "R")
wn.onkeypress(APItoggle, "0")
wn.onkeypress(config, "9")
wn.onkeypress(snakecolor, "8")
wn.onkeypress(togglemute, "m")
wn.onkeypress(togglemute, "M")
wn.onkeypress(exit, "Escape")
segments = []
prefs = {}
# Main Gameplay
while True:
wn.update()
if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290:
if not mutesound:
playsmash()
sleep(1)
head.goto(0, 0)
head.direction = "Stop"
for segment in segments:
segment.goto(1000, 1000)
segments.clear()
score = 0
delay = 0.025
pen.clear()
pen.write(f"Score: {score} High Score: {high_score} ",
align="center", font=("candara", 24, "bold"))
if head.distance(food) < 20:
x = round(randint(-270, 270), 24)
y = round(randint(-270, 270), 24)
if mutesound == False:
if __name__ == '__main__':
# creating thread
soundt = thr.Thread(target=playswallow, args=())
soundt.start()
food.goto(x, y)
# Adding segment
new_segment = turtle.Turtle()
new_segment.speed(0)
new_segment.shape("square")
try:
new_segment.color(tailcolor)
except:
snakeconfig.snakereset(script_path)
quit()
new_segment.penup()
segments.append(new_segment)
score += 1
if error == True:
high_score = score
else:
if score > int(high_score):
high_score = score
pen.clear()
pen.write("Score: {} High Score: {} ".format(
score, high_score), align="center", font=("candara", 24, "bold"))
if foodnum == 2:
if head.distance(food2) < 20:
x2 = round(randint(-270, 270), 24)
y2 = round(randint(-270, 270), 24)
if mutesound == False:
playswallow()
food2.goto(x2, y2)
# Adding segment
new_segment = turtle.Turtle()
new_segment.speed(0)
new_segment.shape("square")
try:
new_segment.color(tailcolor)
except:
snakeconfig.snakereset()
quit()
new_segment.penup()
segments.append(new_segment)
score += 1
if error == True:
high_score = score
else:
if score > int(high_score):
high_score = score
pen.clear()
pen.write("Score: {} High Score: {} ".format(
score, high_score), align="center", font=("candara", 24, "bold"))
# Checking for head collisions with body segments
for index in range(len(segments)-1, 0, -1):
x = segments[index-1].xcor()
y = segments[index-1].ycor()
segments[index].goto(x, y)
if len(segments) > 0:
x = head.xcor()
y = head.ycor()
segments[0].goto(x, y)
move()
for segment in segments:
if segment.distance(head) < movepertick:
if mutesound == False:
playsmash()
head.goto(0, 600)
sleep(delay)
sleep(1)
head.goto(0, 0)
head.direction = "Stop"
colors = choice(['red', 'blue', 'green'])
shapes = choice(['square', 'circle'])
for segment in segments:
segment.goto(1000, 1000)
segments = []
score = 0
delay = 0.025
pen.clear()
pen.write(f"Score: {score} High Score: {high_score} ",
align="center", font=("candara", 24, "bold"))
if APIon == True:
if __name__ == "__main__":
# creating thread
t1 = thr.Thread(target=APIproc, args=())
t1.start()
prefs = {
"highscore": high_score,
"bgcolor": bgcolor,
"foodcolor": foodcolor,
"foodshape": foodshape,
"foodnum": foodnum,
"mutesound": mutesound,
"headcolor": headcolor,
"tailcolor": tailcolor
}
if changedcolor == False:
with open(script_path.joinpath("prefs.json"), "w") as write_file:
json.dump(prefs, write_file)
else:
quit()
sleep(delay)