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