-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnake.py
294 lines (195 loc) · 6.17 KB
/
Snake.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
import pygame
import time
import math
from random import randrange
pygame.init()
WIDTH = 600
HEIGHT= 500
black= (0,0,0)
white= (255,255,255)
lightgreen = (200,255,255)
red= (255,0,0)
green= (0,255,0)
blue= (0,0,255)
show= pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('SNAKE(Ok4s 1st)')
clock= pygame.time.Clock()
show.fill(lightgreen)
global score
score = 0
global continuegame
continuegame= True
#global f
#f = open('highscore7.txt', 'a')
class box:
def __init__(self,x,y):
self.x= x
self.y= y
self.size= 20
#self.boxlist = [box(x,20), box(x,20), box(x,20)]
#x= (self.boxlist.index(b)+1)*i
def draw(self):
p= (self.x, self.y, self.size, self.size)
pygame.draw.rect(show, blue, p)
def isoutside(self):
if self.x < 0:
self.x = WIDTH
if self.y < 0:
self.y = HEIGHT
if self.x > WIDTH:
self.x = 0
if self.y > HEIGHT:
self.y = 0
def getcenter(self):
a= self.x + self.size/2
b= self.y + self.size/2
return a,b
def collide(self, x, y):
cx,cy = self.getcenter()
if (((x-(cx))**2 + (y-(cy))**2)**0.5) < (self.size):
return True
return False
#def poss(b):
#v= (self.boxlist.index(b)+1)*i
#return v
class snake:
def __init__(self, x,y,dx= 1, dy= 0):
self.boxsize= 22
self.x= x
self.y= y
self.dx =dx
self.dy = dy
self.poslistx= [20, 42, 64]
self.boxlist = [box(64,20), box(42,20), box(20,20)]
self.grow= False
#self.poss= b
def move(self):
fbox= self.boxlist[0]
newx= fbox.x+ (self.dx * self.boxsize)
newy= fbox.y+ (self.dy * self.boxsize)
b= box(newx, newy)
self.boxlist.insert(0, b)
if self.grow == False:
self.boxlist.remove(self.boxlist[-1])
self.grow= False
##self.poslistx[-1]+=22
#print(self.poslist)
##self.boxlist.insert(-1, box(self.poslistx[-1], 20))
##del self.boxlist[0]
#for i in self.boxlist:
#u= poss(i)
#return u
#self.x+= self.dx
#self.y+= self.dy
def draw(self):
for i in self.boxlist:
i.draw()
i.isoutside()
def gameover(self):
global continuegame
x,y= self.boxlist[0].getcenter()
for i in self.boxlist[1:-1]:
if i.collide(x,y):
continuegame= False
class apple:
def __init__ (self, x, y):
self.box= box(x,y)
self.box.size = 25
def draw(self):
a= (self.box.x, self.box.y)
pygame.draw.circle(show, red, a, int(self.box.size/2))
#def animate(self):
#self.size+=1
#self.size-=1
#def collide(self, spriteGroup):
#if pygame.sprite.spritecollide(self, spriteGroup, False):
#print('ok')
def collide(self, x2,y2, s):
global score
if self.box.collide(x2,y2):
self.box.x= randrange(50, WIDTH-50)
self.box.y= randrange(50, HEIGHT-50)
s.grow = True
score= score + 1
def drawscore():
font= pygame.font.SysFont(None, 25)
text= font.render("Score:" + str(score), True, black)
show.blit(text, (0,0))
def rungame():
global score
score= 0
global continuegame
continuegame = True
a= apple(250,200)
direction= 'left'
s= snake(45,87)
while continuegame:
show.fill(lightgreen)
drawscore()
s.draw()
s.move()
a.draw()
i= s.boxlist[0]
x,y= i.getcenter()
a.collide(x, y, s)
#a.size+= 1
#time.sleep(1)
#a.size-= 1
#a.animate()
s.gameover()
#gethighscore()
#print(hi)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP or event.key == ord('w'):
if direction != "down":
s.dx = 0
s.dy = -1
direction = 'up'
if event.key == pygame.K_DOWN or event.key == ord('s'):
if direction != "up":
s.dx = 0
s.dy = 1
direction = 'down'
if event.key == pygame.K_RIGHT or event.key == ord('d'):
if direction != 'left':
s.dx = 1
s.dy = 0
direction = 'right'
if event.key == pygame.K_LEFT or event.key == ord('a'):
if direction != 'right':
s.dx = -1
s.dy = 0
direction = 'left'
pygame.display.update()
clock.tick(15)
# GAme Over
#highscore = 0
#f = open('highscore8.txt', 'a')
#f.write(str(highscore))
#if score > highscore:
# f = open('highscore8.txt', 'a')
# f.truncate(0)
# f.close()
# highscore = score
# f = open('highscore8.txt', 'a')
# f.write(str(highscore))
# f.close
#f = open('highscore8.txt', 'r')
#hi = f.read()
#print(hi)
def message_display():
font= pygame.font.SysFont(None, 75)
text= font.render("Game Over Bro", True, black)
show.blit(text, (int(WIDTH/5),int(HEIGHT/3)))
pygame.display.update()
pygame.time.wait(10000)
def main():
while True:
rungame()
message_display()
#gethighscore()
main()