1
+ import turtle
2
+ import random
3
+ import time
4
+
5
+
6
+ def game ():
7
+
8
+ delay = 0.1
9
+ score = 0
10
+ highstscore = 0
11
+
12
+ #snake body
13
+ bodies = []
14
+
15
+ #getting a screen
16
+ s = turtle .Screen ()
17
+ s .title ("Snake Game" )
18
+ s .bgcolor ("black" )
19
+ s .setup (width = 600 ,height = 600 )
20
+ s .cv ._rootwindow .resizable (False , False )
21
+
22
+ #create head of Snake
23
+ head = turtle .Turtle ()
24
+ head .speed (0 )
25
+ head .shape ("circle" )
26
+ head .color ("#79891A" )
27
+ head .fillcolor ("#0CB968" )
28
+ head .penup ()
29
+ head .goto (0 ,0 )
30
+ head .direction = "stop"
31
+
32
+ #Food of Snake
33
+ food = turtle .Turtle ()
34
+ food .speed (0 )
35
+ food .shape ("turtle" )
36
+ food .color ("yellow" )
37
+ food .fillcolor ("#4ACB61" )
38
+ food .penup ()
39
+ food .ht ()
40
+ food .goto (0 ,200 )
41
+ food .st ()
42
+
43
+ #Game Score board
44
+ sb = turtle .Turtle ()
45
+ sb .shape ("square" )
46
+ sb .color ("gray" )
47
+ sb .fillcolor ("gray" )
48
+ sb .penup ()
49
+ sb .ht ()
50
+ sb .goto (- 250 ,- 250 )
51
+ sb .write ("Score:0 | Highest Score:0" )
52
+
53
+
54
+ def moveup ():
55
+ if head .direction != "down" :
56
+ head .direction = "up"
57
+
58
+ def movedown ():
59
+ if head .direction != "up" :
60
+ head .direction = "down"
61
+ def moveleft ():
62
+ if head .direction != "right" :
63
+ head .direction = "left"
64
+ def moveright ():
65
+ if head .direction != "left" :
66
+ head .direction = "right"
67
+
68
+ def move ():
69
+ if head .direction == "up" :
70
+ y = head .ycor ()
71
+ head .sety (y + 20 )
72
+ if head .direction == "down" :
73
+ y = head .ycor ()
74
+ head .sety (y - 20 )
75
+ if head .direction == "left" :
76
+ x = head .xcor ()
77
+ head .setx (x - 20 )
78
+ if head .direction == "right" :
79
+ x = head .xcor ()
80
+ head .setx (x + 20 )
81
+
82
+ #Game Keyboard
83
+ s .listen ()
84
+ s .onkey (moveup ,"Up" )
85
+ s .onkey (movedown ,"Down" )
86
+ s .onkey (moveleft ,"Left" )
87
+ s .onkey (moveright ,"Right" )
88
+
89
+
90
+ #main loop
91
+
92
+ while True :
93
+ s .update () #this is to update the screen
94
+ #Handle collission with border
95
+ if head .xcor ()> 290 :
96
+ head .setx (- 290 )
97
+ if head .xcor ()< - 290 :
98
+ head .setx (290 )
99
+ if head .ycor ()> 290 :
100
+ head .sety (- 290 )
101
+ if head .ycor ()< - 290 :
102
+ head .sety (290 )
103
+
104
+ #chechk for collission with food
105
+ if head .distance (food )< 20 :
106
+ x = random .randint (- 290 ,290 )
107
+ y = random .randint (- 290 ,290 )
108
+ food .goto (x ,y )
109
+
110
+ #increase the length of snake
111
+ body = turtle .Turtle ()
112
+ body .speed (0 )
113
+ body .penup ()
114
+ body .shape ("circle" )
115
+ body .color ("#79891A" )
116
+ body .fillcolor ("#0CB968" )
117
+ bodies .append (body ) #append new body
118
+
119
+ #increase the score
120
+ score += 5
121
+
122
+ #change delay
123
+ delay -= 0.001
124
+
125
+ #update the highestscore
126
+ if score > highstscore :
127
+ highstscore = score
128
+ sb .clear ()
129
+ sb .write (f"Score: { score } | Highest Score: { highstscore } " )
130
+
131
+ #move the snake bodies
132
+ for index in range (len (bodies )- 1 ,0 ,- 1 ):
133
+ x = bodies [index - 1 ].xcor ()
134
+ y = bodies [index - 1 ].ycor ()
135
+ bodies [index ].goto (x ,y )
136
+
137
+ if len (bodies )> 0 :
138
+ x = head .xcor ()
139
+ y = head .ycor ()
140
+ bodies [0 ].goto (x ,y )
141
+ move ()
142
+
143
+ #check for collision with snake body
144
+ for body in bodies :
145
+ if body .distance (head )< 20 :
146
+ time .sleep (1 )
147
+ head .goto (0 ,0 )
148
+ head .direction = "stop"
149
+
150
+ #hide bodies
151
+ for body in bodies :
152
+ body .ht ()
153
+ bodies .clear ()
154
+
155
+
156
+ score = 0
157
+ delay = 0.1
158
+
159
+ #update the score board
160
+ sb .clear ()
161
+ sb .write (f"Score: { score } | Highest Score: { highstscore } " )
162
+ s .bye ()
163
+ time .sleep (delay )
164
+ s .mainloop ()
165
+
166
+
167
+ if __name__ == '__main__' :
168
+ game ()
0 commit comments