-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest.py
More file actions
266 lines (213 loc) · 8.44 KB
/
Test.py
File metadata and controls
266 lines (213 loc) · 8.44 KB
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
import pygame
import random
from enum import Enum
from collections import namedtuple
from sortedcollections import OrderedSet
import numpy as np
pygame.init()
font = pygame.font.Font('arial.ttf', 25)
class Direction(Enum):
RIGHT = 1
LEFT = 2
UP = 3
DOWN = 4
Point = namedtuple('Point', 'x, y')
# rgb colors
WHITE = (255, 255, 255)
RED = (200,0,0)
BLUE1 = (0, 0, 255)
BLUE2 = (0, 100, 255)
BLACK = (0,0,0)
APPLE = pygame.image.load('snake_apple.png')
RealApple = pygame.transform.scale(APPLE, (40,40))
bg = pygame.image.load("Board.png")
realbg = pygame.transform.scale(bg, (760,680))
HS = pygame.image.load('HighScore.png')
realHS = pygame.transform.scale(HS, (40,40))
head = pygame.image.load('Head.png')
realhead = pygame.transform.scale(head, (40,40))
body = pygame.image.load('Body.png')
realbody = pygame.transform.scale(body, (40,40))
tail = pygame.image.load('Tail.png')
realtail = pygame.transform.scale(tail, (40,40))
eating = pygame.image.load('Eating.png')
RE = pygame.transform.scale(eating, (40,40))
BLOCK_SIZE = 40
SPEED = 10
class SnakeGame:
def __init__(self, w=760, h=680):
self.w = w
self.h = h
# init display
self.display = pygame.display.set_mode((self.w, self.h))
pygame.display.set_caption('SneK')
self.clock = pygame.time.Clock()
self.highscore = 0
# init game state
self.reset()
def findborder(self):
border = [self.snake[0]]
for i in self.snake[1:]:
if i == self.snake[0]:
break
border.append(i)
return border
def findInner(self,border):
Inner = []
minX = min([i.x for i in border])
maxX = max([i.x for i in border])
minY = min([i.y for i in border])
maxY = max([i.y for i in border])
for row in range(minX,maxX,40):
count = 0
for column in range(minY, maxY,40):
temp = Point(row,column)
if count == 1:
Inner.append(temp)
if temp in border:
count += 1
if(count == 2):
Inner.pop()
continue
for change in Inner:
print(change.x," ", change.y)
def reset(self):
self.newgame = True
self.direction = Direction.RIGHT
self.currdirect = 0
self.head = Point(160, 320)
self.snake = [self.head,
Point(self.head.x-BLOCK_SIZE, self.head.y),
Point(self.head.x-(2*BLOCK_SIZE), self.head.y)]
self.directions = [0,0,0]
self.score = 0
self.food = None
self._place_food()
def _place_food(self):
x = random.randint(1, (self.w-(BLOCK_SIZE * 2))//BLOCK_SIZE )*BLOCK_SIZE
y = random.randint(1, (self.h-(BLOCK_SIZE * 2))//BLOCK_SIZE )*BLOCK_SIZE
self.food = Point(x, y)
if self.food in self.snake:
self._place_food()
def play_step(self):
# 1. collect user input
for event in pygame.event.get():
if event.type == pygame.QUIT:
print(self.highscore)
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT and self.currdirect != 0:
self.currdirect = 2
self.direction = Direction.LEFT
if event.key == pygame.K_RIGHT and self.currdirect != 2:
self.currdirect = 0
self.direction = Direction.RIGHT
elif event.key == pygame.K_UP and self.currdirect != 3:
self.currdirect = 1
self.direction = Direction.UP
elif event.key == pygame.K_DOWN and self.currdirect != 1:
self.currdirect = 3
self.direction = Direction.DOWN
# 2. move
self._move(self.direction) # update the head
self.snake.insert(0, self.head)
self.directions.insert(0, self.currdirect)
# 3. check if game over
game_over = False
if self.is_collision():
# init game state
if self.highscore < self.score:
self.highscore = self.score
#self.reset()
return game_over, self.score
# 4. place new food or just move
if self.head == self.food:
self.score += 1
self._place_food()
else:
self.snake.pop()
self.directions.pop()
# 5. update ui and clock
self._update_ui()
self.clock.tick(SPEED)
if self.newgame == True:
self.newgame = False
Pressed = False
while True:
pygame.time.wait(10)
for event in pygame.event.get():
if event.type == pygame.QUIT:
print(self.highscore)
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT or event.key == pygame.K_UP or event.key == pygame.K_DOWN:
Pressed = True
if event.key == pygame.K_RIGHT and self.currdirect != 2:
self.currdirect = 0
self.direction = Direction.RIGHT
elif event.key == pygame.K_UP and self.currdirect != 3:
self.currdirect = 1
self.direction = Direction.UP
elif event.key == pygame.K_DOWN and self.currdirect != 1:
self.currdirect = 3
self.direction = Direction.DOWN
if Pressed:
break
# 6. return game over and score
return game_over, self.score
def is_collision(self, pt = None):
if pt == None:
pt = self.head
# hits boundary
if pt.x > (self.w - (BLOCK_SIZE * 2)) or pt.x < BLOCK_SIZE or pt.y > (self.h - (BLOCK_SIZE * 2)) or pt.y < BLOCK_SIZE:
return True
# hits itself
if pt in self.snake[1:]:
self.findInner(self.findborder())
return True
return False
def _update_ui(self):
self.display.blit(realbg, (0,0))
for pt in range(0,len(self.snake)):
if pt == 0:
if ( ((self.food.x + (BLOCK_SIZE * 2)) > (self.snake[pt]).x) and ((self.food.x - (BLOCK_SIZE * 2)) < (self.snake[pt]).x) and ((self.food.y - (BLOCK_SIZE * 2)) < (self.snake[pt]).y) and ((self.food.y + (BLOCK_SIZE * 2)) > (self.snake[pt]).y)):
temp = RE
else:
temp = realhead
newhead = pygame.transform.rotate(temp, (self.currdirect * 90))
self.display.blit(newhead, ((self.snake[pt]).x, (self.snake[pt]).y))
elif pt == len(self.snake) - 1:
newtail = pygame.transform.rotate(realtail, ((self.directions[pt]) * 90))
self.display.blit(newtail, ((self.snake[pt]).x, (self.snake[pt]).y))
else:
self.display.blit(realbody, ((self.snake[pt]).x, (self.snake[pt]).y))
self.display.blit(RealApple, (self.food.x, self.food.y))
text = font.render("Score: " + str(self.score), True, WHITE)
text1 = font.render("HighScore: " + str(self.highscore), True, WHITE)
self.display.blit(RealApple, [10,0])
self.display.blit(text, [50, 5])
self.display.blit(realHS, [160,0])
self.display.blit(text1, [200,5])
pygame.display.flip()
def _move(self, direction):
x = self.head.x
y = self.head.y
if direction == Direction.RIGHT:
x += BLOCK_SIZE
elif direction == Direction.LEFT:
x -= BLOCK_SIZE
elif direction == Direction.DOWN:
y += BLOCK_SIZE
elif direction == Direction.UP:
y -= BLOCK_SIZE
self.head = Point(x, y)
if __name__ == '__main__':
game = SnakeGame()
# game loop
while True:
game_over, score = game.play_step()
if game_over == True:
break
print('Final Score', score)