41
41
42
42
43
43
# ----------- Snake Class ----------------
44
+ # self.history[0][0] is the location of the head of the snake
45
+
44
46
class Snake :
45
47
def __init__ (self , x_start , y_start ):
46
48
self .x = x_start
@@ -61,14 +63,16 @@ def reset(self):
61
63
self .y_dir = 0
62
64
self .history = [[self .x , self .y ]]
63
65
self .length = 1
64
-
66
+
67
+ #function to show the body of snake
65
68
def show (self ):
66
69
for i in range (self .length ):
67
70
if not i == 0 :
68
71
pygame .draw .rect (display , snake_colour , (self .history [i ][0 ], self .history [i ][1 ], self .w , self .h ))
69
72
else :
70
73
pygame .draw .rect (display , snake_head , (self .history [i ][0 ], self .history [i ][1 ], self .w , self .h ))
71
74
75
+
72
76
def check_eaten (self ):
73
77
if abs (self .history [0 ][0 ] - food_x ) < scale and abs (self .history [0 ][1 ] - food_y ) < scale :
74
78
return True
@@ -91,6 +95,80 @@ def update(self):
91
95
i -= 1
92
96
self .history [0 ][0 ] += self .x_dir * scale
93
97
self .history [0 ][1 ] += self .y_dir * scale
98
+
99
+ def autoplay (self ):
100
+
101
+ if abs (food_x - self .history [0 ][0 ]) < 10 and abs (food_y - self .history [0 ][1 ]) < 10 :
102
+ # if self.check_eaten():
103
+ # food.new_location()
104
+ # score += 1
105
+ # self.grow()
106
+ print ("" )
107
+ elif abs (food_x - self .history [0 ][0 ]) < 10 :
108
+
109
+ # if self.y_dir==1 or self.y_dir==-1:
110
+ # self.y_dir=0
111
+ # self.x_dir=1
112
+ if self .x_dir == 1 or self .x_dir == - 1 :
113
+ if food_y > self .history [0 ][1 ]:
114
+ self .y_dir = 1
115
+ else :
116
+ self .y_dir = - 1
117
+ self .x_dir = 0
118
+ elif abs (food_y - self .history [0 ][1 ]) < 10 :
119
+
120
+ # if self.x_dir==1 or self.x_dir==-1:
121
+ # self.x_dir=0
122
+ # self.y_dir=1
123
+ if self .y_dir == 1 or self .y_dir == - 1 :
124
+ self .y_dir = 0
125
+ if food_x > self .history [0 ][0 ]:
126
+ self .x_dir = 1
127
+ else :
128
+ self .x_dir = - 1
129
+
130
+
131
+ elif food_x - self .history [0 ][0 ] >= 10 and food_y - self .history [0 ][1 ] >= 10 :
132
+
133
+ if self .x_dir == - 1 :
134
+ self .y_dir = 1
135
+ self .x_dir = 0
136
+ elif self .y_dir == - 1 :
137
+ self .y_dir = 0
138
+ self .x_dir = 1
139
+ elif self .history [0 ][0 ]- food_x >= 10 and food_y - self .history [0 ][1 ] >= 10 :
140
+
141
+ if self .x_dir == 1 :
142
+ self .y_dir = 1
143
+ self .x_dir = 0
144
+ elif self .y_dir == 1 :
145
+ self .y_dir = 0
146
+ self .x_dir = - 1
147
+ elif self .history [0 ][0 ]- food_x >= 10 and self .history [0 ][1 ]- food_y >= 10 :
148
+
149
+ if self .x_dir == 1 :
150
+ self .y_dir = - 1
151
+ self .x_dir = 0
152
+ elif self .y_dir == 1 :
153
+ self .y_dir = 0
154
+ self .x_dir = - 1
155
+
156
+ elif food_x - self .history [0 ][0 ] >= 10 and self .history [0 ][1 ]- food_y >= 10 :
157
+
158
+ if self .x_dir == - 1 :
159
+ self .y_dir = - 1
160
+ self .x_dir = 0
161
+ elif self .y_dir == 1 :
162
+ self .y_dir = 0
163
+ self .x_dir = 1
164
+
165
+ self .update ()
166
+
167
+
168
+
169
+
170
+
171
+
94
172
95
173
96
174
# ----------- Food Class --------------
@@ -116,47 +194,61 @@ def gameLoop():
116
194
117
195
global score
118
196
119
- snake = Snake (width / 2 , height / 2 )
197
+ snake = Snake (width / 2 , height / 2 ) #starting from mid of grid
120
198
food = Food ()
121
199
food .new_location ()
200
+ ap = False
122
201
123
202
while loop :
203
+
204
+ display .fill (background )
205
+ snake .show ()
206
+ food .show ()
207
+ show_score ()
208
+
124
209
for event in pygame .event .get ():
125
210
if event .type == pygame .QUIT :
126
211
pygame .quit ()
127
212
sys .exit ()
213
+
128
214
if event .type == pygame .KEYDOWN :
129
215
if event .key == pygame .K_q :
130
216
pygame .quit ()
131
217
sys .exit ()
132
- if snake .y_dir == 0 :
133
- if event .key == pygame .K_UP :
134
- snake .x_dir = 0
135
- snake .y_dir = - 1
136
- if event .key == pygame .K_DOWN :
137
- snake .x_dir = 0
138
- snake .y_dir = 1
139
-
140
- if snake .x_dir == 0 :
141
- if event .key == pygame .K_LEFT :
142
- snake .x_dir = - 1
143
- snake .y_dir = 0
144
- if event .key == pygame .K_RIGHT :
145
- snake .x_dir = 1
146
- snake .y_dir = 0
147
-
148
- display .fill (background )
149
-
150
- snake .show ()
151
- snake .update ()
152
- food .show ()
153
- show_score ()
218
+ if event .key == pygame .K_SPACE : #autoplay start
219
+ ap = True
220
+ if event .key == pygame .K_TAB : #autoplay end
221
+ ap = False
222
+ else :
223
+ if snake .y_dir == 0 :
224
+ if event .key == pygame .K_UP :
225
+ snake .x_dir = 0
226
+ snake .y_dir = - 1
227
+ if event .key == pygame .K_DOWN :
228
+ snake .x_dir = 0
229
+ snake .y_dir = 1
230
+
231
+ if snake .x_dir == 0 :
232
+ if event .key == pygame .K_LEFT :
233
+ snake .x_dir = - 1
234
+ snake .y_dir = 0
235
+ if event .key == pygame .K_RIGHT :
236
+ snake .x_dir = 1
237
+ snake .y_dir = 0
238
+
239
+
240
+ if ap :
241
+ snake .autoplay ()
242
+ else :
243
+ snake .update ()
244
+
154
245
155
246
if snake .check_eaten ():
156
247
food .new_location ()
157
248
score += 1
158
249
snake .grow ()
159
250
251
+
160
252
if snake .death ():
161
253
score = 0
162
254
font = pygame .font .SysFont ("Copperplate Gothic Bold" , 50 )
@@ -166,6 +258,8 @@ def gameLoop():
166
258
time .sleep (3 )
167
259
snake .reset ()
168
260
261
+
262
+ #updating the values if snake goes out of board
169
263
if snake .history [0 ][0 ] > width :
170
264
snake .history [0 ][0 ] = 0
171
265
if snake .history [0 ][0 ] < 0 :
@@ -177,6 +271,6 @@ def gameLoop():
177
271
snake .history [0 ][1 ] = height
178
272
179
273
pygame .display .update ()
180
- clock .tick (10 )
274
+ clock .tick (10 ) #at most 10 frames should pass in 1 sec, it is used to control the speed of snake
181
275
182
276
gameLoop ()
0 commit comments