1
+ import turtle
2
+ import time
3
+ import random
4
+
5
+ delay = 0.1
6
+
7
+ # Score
8
+ score = 0
9
+ high_score = 0
10
+
11
+ # Setting up the screen
12
+ wn = turtle .Screen ()
13
+ wn .title ("Snake Game in Python" )
14
+ wn .bgcolor ("black" )
15
+ wn .setup (width = 600 , height = 600 )
16
+ wn .tracer (0 ) # Turns off the screen updates
17
+
18
+ # Snake head
19
+ head = turtle .Turtle ()
20
+ head .speed (0 )
21
+ head .shape ("square" )
22
+ head .color ("red" )
23
+ head .penup ()
24
+ head .goto (0 , 0 )
25
+ head .direction = "stop"
26
+
27
+ # Snake food
28
+ food = turtle .Turtle ()
29
+ food .speed (0 )
30
+ food .shape ("circle" )
31
+ food .color ("green" )
32
+ food .penup ()
33
+ food .goto (0 , 100 )
34
+
35
+ segments = []
36
+
37
+ # Pen
38
+ pen = turtle .Turtle ()
39
+ pen .speed (0 )
40
+ pen .shape ("square" )
41
+ pen .color ("white" )
42
+ pen .penup ()
43
+ pen .hideturtle ()
44
+ pen .goto (0 , 260 )
45
+ pen .write ("Score: 0 High Score: 0" , align = "center" , font = ("Verdana" , 24 , "normal" ))
46
+
47
+
48
+ # Functions
49
+ def go_up ():
50
+ if head .direction != "down" :
51
+ head .direction = "up"
52
+
53
+
54
+ def go_down ():
55
+ if head .direction != "up" :
56
+ head .direction = "down"
57
+
58
+
59
+ def go_left ():
60
+ if head .direction != "right" :
61
+ head .direction = "left"
62
+
63
+
64
+ def go_right ():
65
+ if head .direction != "left" :
66
+ head .direction = "right"
67
+
68
+
69
+ def move ():
70
+ if head .direction == "up" :
71
+ y = head .ycor ()
72
+ head .sety (y + 20 )
73
+
74
+ if head .direction == "down" :
75
+ y = head .ycor ()
76
+ head .sety (y - 20 )
77
+
78
+ if head .direction == "left" :
79
+ x = head .xcor ()
80
+ head .setx (x - 20 )
81
+
82
+ if head .direction == "right" :
83
+ x = head .xcor ()
84
+ head .setx (x + 20 )
85
+
86
+
87
+ # Keyboard bindings
88
+ wn .listen ()
89
+ wn .onkeypress (go_up , "w" )
90
+ wn .onkeypress (go_down , "s" )
91
+ wn .onkeypress (go_left , "a" )
92
+ wn .onkeypress (go_right , "d" )
93
+
94
+ # Main game loop
95
+ while True :
96
+ wn .update ()
97
+
98
+ # Check for a collision with the border
99
+ if head .xcor () > 290 or head .xcor () < - 290 or head .ycor () > 290 or head .ycor () < - 290 :
100
+ time .sleep (1 )
101
+ head .goto (0 , 0 )
102
+ head .direction = "stop"
103
+
104
+ # Hide the segments
105
+ for segment in segments :
106
+ segment .goto (1000 , 1000 )
107
+
108
+ # Clear the segments list
109
+ segments .clear ()
110
+
111
+ # Reset the score
112
+ score = 0
113
+
114
+ # Reset the delay
115
+ delay = 0.1
116
+
117
+ pen .clear ()
118
+ pen .write ("Score: {} High Score: {}" .format (score , high_score ), align = "center" , font = ("Verdana" , 24 , "normal" ))
119
+
120
+ # Check for a collision with the food
121
+ if head .distance (food ) < 20 :
122
+ # Move the food to a random spot
123
+ x = random .randint (- 290 , 290 )
124
+ y = random .randint (- 290 , 290 )
125
+ food .goto (x , y )
126
+
127
+ # Add a segment
128
+ new_segment = turtle .Turtle ()
129
+ new_segment .speed (0 )
130
+ new_segment .shape ("square" )
131
+ new_segment .color ("orange" )
132
+ new_segment .penup ()
133
+ segments .append (new_segment )
134
+
135
+ # Shorten the delay
136
+ delay -= 0.001
137
+
138
+ # Increase the score
139
+ score += 10
140
+
141
+ if score > high_score :
142
+ high_score = score
143
+
144
+ pen .clear ()
145
+ pen .write ("Score: {} High Score: {}" .format (score , high_score ), align = "center" , font = ("Courier" , 24 , "normal" ))
146
+
147
+ # Move the end segments first in reverse order
148
+ for index in range (len (segments ) - 1 , 0 , - 1 ):
149
+ x = segments [index - 1 ].xcor ()
150
+ y = segments [index - 1 ].ycor ()
151
+ segments [index ].goto (x , y )
152
+
153
+ # Move segment 0 to the location of head
154
+ if len (segments ) > 0 :
155
+ x = head .xcor ()
156
+ y = head .ycor ()
157
+ segments [0 ].goto (x , y )
158
+
159
+ move ()
160
+
161
+ # Check for head collision with the body segments
162
+ for segment in segments :
163
+ if segment .distance (head ) < 20 :
164
+ time .sleep (1 )
165
+ head .goto (0 , 0 )
166
+ head .direction = "stop"
167
+
168
+ # Hide the segments
169
+ for segment in segments :
170
+ segment .goto (1000 , 1000 )
171
+
172
+ # Clear the segments list
173
+ segments .clear ()
174
+
175
+ # Reset the score
176
+ score = 0
177
+
178
+ # Reset the delay
179
+ delay = 0.1
180
+
181
+ # Update the score display
182
+ pen .clear ()
183
+ pen .write ("Score: {} High Score: {}" .format (score , high_score ), align = "center" ,
184
+ font = ("Verdana" , 24 , "normal" ))
185
+
186
+ time .sleep (delay )
187
+
188
+ wn .mainloop ()
0 commit comments