-
Notifications
You must be signed in to change notification settings - Fork 0
/
Turtle-game.py
608 lines (458 loc) · 20.7 KB
/
Turtle-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
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
import turtle
import random
import time
import sqlite3
class Game:
def __init__(self):
#Game status variable
self.game_status = True
#heading variable (stores the current direction of the player)
self.current_heading = "right"
#The Variable that is used to toggle the pause the game
self.is_paused = False
self.pause_message = turtle.Turtle()
self.pause_message.hideturtle()
self.pause_on_screen = False
self.pause_time = 0
self.pause_current_heading = "right"
#Wait Variables to hold the movement checks and updates
self.is_waiting = False
self.is_waiting_after_game = False
#Timer Variables
self.previous_time = 0
### FOOD DETAILS ###
#The variable that stores the number of food on the field
self.food_count = 0
self.max_food_count = 5
self.food_data = []
#Variables that govern Speed of the turtle
self.player_speed = 2
self.max_player_speed = 8
self.speed_variable = 0
#Variable that stores game score
self.game_score = 0
#Variable that stores lives
self.turtle_lives = 1
#Fonts
self.score_font = ("Courier", 20, "")
self.screen_font = ("Courier", 30, "bold")
#Double Points Variables (Violet Pellet)
self.double_points = False
self.double_points_time = 0
self.points_multiplier = 1
#Speed Bost Variables (Blue Pellet)
self.speed_boost = False
self.speed_boost_time = 0
self.speed_multiplier = 1
#Speed Bost Variables (Purple Pellet)
self.invincibility = False
self.invincibility_time = 0
#Game Reset Variables
self.reset = False
#player details
self.player_name = ""
# Function to Spawn A food pellet
def spawn_food(self):
#Creating a new pellet
food_turtle = turtle.Turtle()
food_turtle.shape("circle")
food_turtle.color("lime")
food_turtle.type = "lime"
food_turtle.time = 0
food_turtle.timeout = random.randint(5,10)
food_turtle.speed(0)
food_turtle.penup()
# Spawning pellet at random position
position_x = random.randint(-220,200)
position_y = random.randint(-210,170)
food_turtle.goto(position_x,position_y)
#Storing pellet in a list to be used later
self.food_data.append(food_turtle)
#function to check food timeout
def food_timeout(self):
#checking timeout for all food turtles
for food in self.food_data:
#checking if the food is timed out
if food.time > food.timeout:
#respawning food if timed out
self.respawn(food)
# Function that moves the food to random loaction
def respawn(self,food_turtle):
#Getting Random Chance Variable
random_chance = int(random.random()*100)
if food_turtle.type == "white":
self.lives_increase(1)
#If the value of chance variable is 0 then spawn extra life food pellet
if random_chance < 1:
# Making the life Turtle pellet
food_turtle.shape("turtle")
food_turtle.color("white")
food_turtle.type = "white"
#respawning the turtle
position_x = random.randint(-220,200)
position_y = random.randint(-210,170)
food_turtle.goto(position_x,position_y)
#If the value of chance variable is 5 or lower but above 0 then spawn ability food pellet
elif random_chance < 6:
ability_chance = int(random.random()*100)
if ability_chance < 26:
# Making the gold Turtle (50 pointer)
food_turtle.shape("circle")
food_turtle.color("yellow")
food_turtle.type = "gold"
elif ability_chance < 51:
# Making the violet Turtle (points boost)
food_turtle.shape("square")
food_turtle.color("violet")
food_turtle.type = "violet"
elif ability_chance < 76:
# Making the blue Turtle (speed boost)
food_turtle.shape("square")
food_turtle.color("blue")
food_turtle.type = "blue"
else:
# Making the purple Turtle (invincibility)
food_turtle.shape("triangle")
food_turtle.color("purple")
food_turtle.type = "purple"
position_x = random.randint(-220,220)
position_y = random.randint(-220,220)
food_turtle.goto(position_x,position_y)
else:
ability_chance = int(random.random()*100)
if ability_chance <=10:
#making the red pellets
food_turtle.shape("circle")
food_turtle.color("red")
food_turtle.type = "red"
else:
#making the regular food
food_turtle.shape("circle")
food_turtle.color("lime")
food_turtle.type = "lime"
#repositioning
position_x = random.randint(-220,220)
position_y = random.randint(-220,220)
food_turtle.goto(position_x,position_y)
food_turtle.time = 0
food_turtle.timeout = random.randint(5,10)
#Function to increase Game score and Game speed Variable
def score_increase(self, points):
self.game_score += points
self.speed_variable += points
self.scoreboard.clear()
self.scoreboard.write("Score:{}".format(self.game_score) , align="left", font=self.score_font)
#function to check for border collisions
def check_border(self):
if self.player.xcor() > 250 or self.player.ycor() > 250 or self.player.ycor() < -250 or self.player.xcor() < -250:
self.lives_increase(-1)
#Function to increase Game score and Game speed Variable
def lives_increase(self, points):
self.turtle_lives += points
#Ending the game if necessary
if self.turtle_lives <= 0:
self.game_over()
#Checking a life has been lost
if self.turtle_lives > 0:
self.is_waiting = True
if points < 0:
self.player.goto(0,0)
for i in range(3,0,-1):
self.pause_message.color("red")
self.pause_message.write("{}!".format(i), align="center", font=self.screen_font)
time.sleep(0.5)
self.pause_message.clear()
time.sleep(0.35)
self.is_waiting = False
self.go_up()
self.lives.clear()
self.lives.write("Lives:{}".format(self.turtle_lives) , align="right", font=self.score_font)
#Function to add score to database
def insert_into_db(self):
self.c.execute("insert into game_scores(player_name,player_score) values(\"{}\",{})".format(self.player_name,self.game_score))
self.conn.commit()
#function to get top 3 score from database
def get_from_db(self):
data = self.c.execute("select * from game_scores order by player_score desc limit 5")
return data
#end's the game
def game_over(self):
#inserting score into the db
self.insert_into_db()
#getting the top 3 score
data = self.get_from_db()
#printing the Game over sign
self.is_waiting = True
for i in range(3):
self.pause_message.color("red")
self.pause_message.write("{}!".format("Game Over"), align="center", font=self.screen_font)
time.sleep(0.5)
self.pause_message.clear()
time.sleep(0.35)
self.is_waiting = False
self.go_up()
#printing the Highscores
name_x,name_y = -100,100
score_x,score_y = 100,100
#write on screen
self.scoreboard.pu()
self.scoreboard.goto(name_x,name_y)
self.scoreboard.write("Player", align="center", font=self.score_font)
self.lives.pu()
self.lives.goto(score_x,score_y)
self.lives.write("Score", align="center", font=self.score_font)
#printing the scorebord
for i in data:
name_y -= 30
self.scoreboard.goto(name_x,name_y)
self.scoreboard.write("{}".format(i[0]), align="center", font=self.score_font)
score_y -= 30
self.lives.goto(score_x,score_y)
self.lives.write("{}".format(i[1]), align="center", font=self.score_font)
time.sleep(5)
#Terminate's main game loop
self.game_status = False
#function that incresases player speed
def speed_increase(self,amount):
if self.player_speed < self.max_player_speed:
self.player_speed += amount
# Function that checks if a food pellet has been eaten
def has_consumed(self):
for food in self.food_data:
if food.distance(self.player) < 25:
#Increase Game Score
if food.type == "lime":
self.score_increase(10*self.points_multiplier)
elif food.type == "gold":
self.score_increase(50*self.points_multiplier)
elif food.type == "red":
self.score_increase(-20)
else:
self.score_increase(20*self.points_multiplier)
if food.type == "violet":
if not self.double_points:
self.double_points = True
self.points_multiplier = 2
self.score_increase(20)
self.double_points_time = int(time.time())
if food.type == "blue":
if not self.speed_boost:
self.speed_boost = True
self.speed_multiplier = 1.25
self.speed_boost_time = int(time.time())
if food.type == "purple":
if not self.invincibility:
self.invincibility = True
self.invincibility_time = int(time.time())
if food.type == "red":
#Respawn Food
self.respawn(food)
self.lives_increase(-1)
#Respawn Food
self.respawn(food)
#Increase speed
if self.speed_variable >= 100:
self.speed_increase(1)
self.speed_variable = 0
#Function to check for all the power-ups
def check_ups(self):
# When the violet power up is active
if self.double_points:
# If the 10 second timer is done then disable the powerup
if self.double_points_time >= 10:
self.double_points = False
self.points_multiplier = 1
self.double_points_time = 0
if self.speed_boost:
# If the 10 second timer is done then disable the powerup
if self.speed_boost_time >= 10:
self.speed_boost = False
self.speed_multiplier = 1
self.speed_boost_time = 0
if self.invincibility:
# If the 10 second timer is done then disable the powerup
if self.invincibility_time >= 10:
self.invincibility = False
self.invincibility_time = 0
# function that starts the game loop
def start(self):
#larger turtle polygon
turtle_x2=((0,32), (-4,28), (-2,20), (-8,14),(-14,18), (-18,16),
(-12,10), (-14,2), (-10,-6), (-16,-12),
(-12,-16), (-8,-5), (0,-14), (8,-10), (12,-16), (16,-12),
(10,-6), (14,2), (12,10), (18,16), (14,18), (8,14), (2,20),(4,28))
#setting up the canvas
self.wn = turtle.Screen()
self.wn.cv._rootwindow.resizable(False, False)
self.wn.title("Turtle Game")
self.wn.setup(500,500)
self.wn.bgpic("assets/background.gif")
#getting player name from the user
self.player_name = turtle.textinput("Who's Playing","Player Name")
if self.player_name == "":
self.player_name = "Guest"
#creating database if there is no database or table
self.conn = sqlite3.connect("Score.db")
self.c = self.conn.cursor()
self.c.execute("create table if not exists game_scores(player_name varchar(50) not null, player_score integer not null)")
#Points Scoreboard
self.scoreboard = turtle.Turtle()
self.scoreboard.hideturtle()
self.scoreboard.color("white")
self.scoreboard.up()
self.scoreboard.goto(-240,220)
self.scoreboard.write("Score:{}".format(self.game_score), align="left", font=self.score_font)
#Life Scoreboard
self.lives = turtle.Turtle()
self.lives.hideturtle()
self.lives.up()
self.lives.color("white")
self.lives.goto(240,220)
self.lives.write("Lives:{}".format(self.turtle_lives), align="right", font=self.score_font)
#setting up the player character
self.player = turtle.Turtle()
turtle.register_shape("assets/turtle/turtle-up.gif")
turtle.register_shape("assets/turtle/turtle-down.gif")
turtle.register_shape("assets/turtle/turtle-left.gif")
turtle.register_shape("assets/turtle/turtle-right.gif")
self.player.shape("assets/turtle/turtle-right.gif")
self.player.color("white")
self.player.penup()
self.player.speed(0)
#binding the player charter movement to w,a,s and d
self.wn.onkey(self.go_up, "w")
self.wn.onkey(self.go_left, "a")
self.wn.onkey(self.go_down, "s")
self.wn.onkey(self.go_right, "d")
self.wn.onkey(self.pause, "p")
self.previous_time = time.time()
#main loop which keeps the player alive
while self.game_status:
try:
#updating turtle
turtle.update()
#pushing the player forward if game is not paused
if not self.is_paused:
self.player.forward(self.player_speed*self.speed_multiplier)
if not self.is_paused:
self.food_timer()
#spawning food on the field if there are none
if self.max_food_count > self.food_count:
self.spawn_food()
self.food_count += 1
#Checking if a food pellet has been consumed
self.has_consumed()
#chechink for border Collision
self.check_border()
# Checking for power ups and time outs
self.check_ups()
#checking if food timesout
self.food_timeout()
# Listening for key contrls and updating
self.wn.listen()
self.wn.update()
except:
break
turtle.bye()
#function to update the timer of the food per second
def food_timer(self):
#checking if one second has passed
if int(time.time() - self.previous_time) >=1:
#updating the time for each elements
for food in self.food_data:
food.time += 1
#Updating the time variable for the power up
self.double_points_time += 1
self.speed_boost_time += 1
self.invincibility_time += 1
#updating the previous time for
self.previous_time = time.time()
#Function that changes the player character's heading to up
def go_up(self):
#Checking if the game is paused
if not self.is_paused and not self.is_waiting:
#setting the current turtle image
self.player.shape("assets/turtle/turtle-up.gif")
#setting up the movement of the player based on current heading
if self.current_heading == "right":
self.player.left(90)
elif self.current_heading == "left":
self.player.left(-90)
elif self.current_heading == "down":
self.player.left(180)
#updating the universal heading variable
self.current_heading = "up"
#Function that changes the player character's heading to left
def go_left(self):
#Checking if the game is paused
if not self.is_paused and not self.is_waiting:
#setting the current turtle image
self.player.shape("assets/turtle/turtle-left.gif")
#setting up the movement of the player based on current heading
if self.current_heading == "right":
self.player.left(180)
elif self.current_heading == "up":
self.player.left(90)
elif self.current_heading == "down":
self.player.left(-90)
#updating the universal heading variable
self.current_heading = "left"
#Function that changes the player character's heading to right
def go_right(self):
#Checking if the game is paused
if not self.is_paused and not self.is_waiting:
#setting the current turtle image
self.player.shape("assets/turtle/turtle-right.gif")
#setting up the movement of the player based on current heading
if self.current_heading == "left":
self.player.left(180)
elif self.current_heading == "down":
self.player.left(90)
elif self.current_heading == "up":
self.player.left(-90)
#updating the universal heading variable
self.current_heading = "right"
#Function that changes the player character's heading to down
def go_down(self):
#Checking if the game is paused
if not self.is_paused and not self.is_waiting:
#setting the current turtle image
self.player.shape("assets/turtle/turtle-down.gif")
#setting up the movement of the player based on current heading
if self.current_heading == "left":
self.player.left(90)
elif self.current_heading == "right":
self.player.left(-90)
elif self.current_heading == "up":
self.player.left(180)
#updating the universal heading variable
self.current_heading = "down"
#function to toggle the game from being paused
def pause(self):
# Changing Pause Variable
self.is_paused = not self.is_paused
#storing current heading of the turtle
if self.is_paused:
self.pause_current_heading = self.current_heading
self.pause_time = time.time()
#when the game is resumed changing heading to the right heading
if not self.is_paused:
if self.pause_current_heading == "right":
self.go_right()
elif self.pause_current_heading == "left":
self.go_left()
elif self.pause_current_heading == "up":
self.go_up()
elif self.pause_current_heading == "down":
self.go_down()
# Displaying Paused Text on screen
if self.pause_on_screen:
self.pause_message.clear()
self.pause_on_screen = False
else:
self.pause_message.write("Paused!", align="center", font=self.screen_font)
self.pause_on_screen = True
#initial startup
if __name__ == '__main__':
obj = Game()
obj.start()