-
Notifications
You must be signed in to change notification settings - Fork 54
/
snake2.py
226 lines (176 loc) · 6.04 KB
/
snake2.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
import pygame
import sys
import time
import random
from pygame.locals import *
snap_time = 0
#rainbow_berry_effects = 0
FPS = 15
pygame.init()
fpsClock=pygame.time.Clock()
SCREEN_WIDTH, SCREEN_HEIGHT = 800, 800
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), 0, 32)
surface = pygame.Surface(screen.get_size())
surface = surface.convert()
surface.fill((255,255,255))
clock = pygame.time.Clock()
pygame.key.set_repeat(1, 40)
GRIDSIZE=10
GRID_WIDTH = SCREEN_WIDTH / GRIDSIZE
GRID_HEIGHT = SCREEN_HEIGHT / GRIDSIZE
UP = (0, -1)
DOWN = (0, 1)
LEFT = (-1, 0)
RIGHT = (1, 0)
BERRY_TYPES = 5
screen.blit(surface, (0,0))
def draw_box(surf, color, pos):
r = pygame.Rect((pos[0], pos[1]), (GRIDSIZE, GRIDSIZE))
pygame.draw.rect(surf, color, r)
class Snake(object):
def __init__(self):
self.lose()
self.color = (0,0,0)
self.snap_time = 0
def get_head_position(self):
return self.positions[0]
def lose(self):
print('You have lost. The game will restart shortly. Press "q" to quit')
time.sleep(2.5)
self.length = 1
self.positions = [((SCREEN_WIDTH / 2), (SCREEN_HEIGHT / 2))]
self.direction = random.choice([UP, DOWN, LEFT, RIGHT])
def point(self, pt):
if self.length > 1 and (pt[0] * -1, pt[1] * -1) == self.direction:
return
else:
self.direction = pt
def move(self):
cur = self.positions[0]
x, y = self.direction
# if timer expires, set in_boost_snap to false
cur_speed_time = time.time()
if cur_speed_time - self.snap_time >= 5:
speed = 1
# print("timer is off " + str(cur_time) + " " + str(self.snap_time))
self.snap_time = 0
else:
speed = 2
# print("timer is on " + str(cur_time) + str(self.snap_time))
new = (((cur[0]+(x*speed*GRIDSIZE)) % SCREEN_WIDTH), (cur[1]+(y*speed*GRIDSIZE)) % SCREEN_HEIGHT)
if len(self.positions) > 2 and new in self.positions[2:]:
self.lose()
else:
self.positions.insert(0, new)
if len(self.positions) > self.length:
self.positions.pop()
def draw(self, surf):
for p in self.positions:
draw_box(surf, self.color, p)
class Apple(object):
def __init__(self):
self.position = (0,0)
self.color = (255,0,0)
self.randomize()
def randomize(self):
self.position = (random.randint(0, GRID_WIDTH-1) * GRIDSIZE, random.randint(0, GRID_HEIGHT-1) * GRIDSIZE)
def draw(self, surf):
draw_box(surf, self.color, self.position)
def check_eat_apple(snake, apple):
if snake.get_head_position() == apple.position:
snake.length += 1
apple.randomize()
class Blueberry(object):
def __init__(self):
self.position = (0,0)
self.color = (0,0,255)
self.randomize()
def randomize(self):
self.position = (random.randint(0, GRID_WIDTH-1) * GRIDSIZE, random.randint(0, GRID_HEIGHT-1) * GRIDSIZE)
def draw(self, surf):
draw_box(surf, self.color, self.position)
def check_eat_blueberry(snake, Blueberry):
if snake.get_head_position() == blueberry.position:
#start timer for 5-10 seconds
snake.snap_time = time.time()
print("""You have eaten a blueberry.
Your speed will be doubled for the next 5 seconds.
_______________________________________________________""")
time.sleep(1)
snake.length += 5
Blueberry.randomize()
class Rock (object):
def __init__(self):
self.position = (0,0)
self.color = (160,80,30)
self.randomize()
def randomize(self):
self.position = (random.randint(0, GRID_WIDTH-1) * GRIDSIZE,
random.randint(0, GRID_HEIGHT-1) * GRIDSIZE)
def draw(self, surf):
draw_box(surf, self.color, self.position)
def check_smash_rock(snake, rock):
if snake.get_head_position() == rock.position:
snake.lose()
class Thorns (object):
def __init__(self):
self.position = (0,0)
self.color = (50,135,50)
self.randomize()
def randomize(self):
self.position = (random.randint(0, GRID_WIDTH-1) * GRIDSIZE,
random.randint(0, GRID_HEIGHT-1) * GRIDSIZE)
def draw(self, surf):
draw_box(surf, self.color, self.position)
def check_smash_thorns(snake, thorns):
if snake.get_head_position() == thorns.position:
snake.lose()
if __name__ == '__main__':
#snake
snake = Snake()
#fruits
apple = Apple()
blueberry = Blueberry()
#obstacles
rock = Rock()
thorns = Thorns()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
if event.key == K_UP or event.key == K_w:
snake.point(UP)
elif event.key == K_DOWN or event.key == K_s:
snake.point(DOWN)
elif event.key == K_LEFT or event.key == K_a:
snake.point(LEFT)
elif event.key == K_RIGHT or event.key == K_d:
snake.point(RIGHT)
elif event.key == K_q:
sys.exit()
surface.fill((255,255,255))
snake.move()
#checking for collision
check_eat_apple(snake, apple)
check_eat_blueberry(snake, blueberry)
check_smash_thorns(snake, thorns)
check_smash_rock(snake, rock)
#drawing everything
snake.draw(surface)
apple.draw(surface)
blueberry.draw(surface)
rock.draw(surface)
thorns.draw(surface)
#displaying the score
font = pygame.font.Font(None, 36)
text = font.render(str(snake.length), 1, (10, 10, 10))
textpos = text.get_rect()
textpos.centerx = 20
surface.blit(text, textpos)
screen.blit(surface, (0,0))
#updating the screen
pygame.display.flip()
pygame.display.update()
fpsClock.tick(FPS + snake.length/3)