-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
202 lines (144 loc) · 5.55 KB
/
main.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
import random
import pygame
# ========== Pygame Config ==========
WIDTH = 400
HEIGHT = 400
screen_size = [WIDTH, HEIGHT]
# Define the colors we will use in RGB format
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# Initialize the game engine
pygame.init()
# Set the height and width of the screen
screen = pygame.display.set_mode(screen_size)
pygame.display.set_caption("Ohad's Snake")
# Set text fonts
score_font = pygame.font.SysFont("monospace", 15)
# ========== Functions ==============
def p5_map(n, start1, stop1, start2, stop2):
return ((n - start1) / (stop1 - start1)) * (stop2 - start2) + start2
# ========== Classes =====================
class Position:
def __init__(self, x, y):
self.x = x
self.y = y
def coordinates(self):
return self.x, self.y
def is_clear(self, game_):
for pos in game_.snake.positions:
if pos.coordinates == self.coordinates():
return False
return True
class Vector:
def __init__(self, x, y):
self.x = x
self.y = y
def __invert__(self):
return Vector(-self.x, -self.y)
VECTOR_SIZE = 5
RIGHT = Vector(VECTOR_SIZE, 0)
LEFT = Vector(-VECTOR_SIZE, 0)
UP = Vector(0, -VECTOR_SIZE)
DOWN = Vector(0, VECTOR_SIZE)
class Food:
def __init__(self, game_):
self.game = game_
self.position = self.random_position()
def random_position(self):
x = random.randint(1, WIDTH - 1)
y = random.randint(1, HEIGHT - 1)
p = Position(x, y)
if p.is_clear(self.game):
return p
return self.random_position()
def show(self):
pygame.draw.circle(self.game.screen, BLUE, self.position.coordinates(), 5)
class Snake:
def __init__(self, game_):
self.game = game_
self.positions = [Position(WIDTH / 2, HEIGHT / 2)]
self.vector = RIGHT
def new_pos(self):
new_x = self.positions[0].x + self.vector.x
new_y = self.positions[0].y + self.vector.y
return Position(new_x, new_y)
def update(self, eat=False):
self.positions.insert(0, self.new_pos())
if eat is False:
self.positions.pop()
def eat(self):
self.update(True)
self.game.food.position = self.game.food.random_position()
def is_out_of_boundaries(self):
return self.positions[0].x == 0 or self.positions[0].x == WIDTH or self.positions[0].y == 0 or self.positions[0].y == HEIGHT
def is_self_colliding(self):
for position in self.positions[1:]:
if self.positions[0].coordinates() == position.coordinates():
return True
return False
def is_colliding(self):
return self.is_out_of_boundaries() or self.is_self_colliding()
def show(self):
for position in self.positions:
pygame.draw.circle(self.game.screen, RED, position.coordinates(), 5)
def encounters(self, other, padding):
if abs(self.positions[0].x - other.position.x) <= padding:
if abs(self.positions[0].y - other.position.y) <= padding:
return True
return False
class Game:
def __init__(self, screen_, object_padding=10, frame_rate=50):
self.screen = screen_
self.snake = Snake(self)
self.food = Food(self)
self.object_padding = object_padding
self.frame_rate = frame_rate
def run(self):
# Loop until the user clicks the close button.
done = False
clock = pygame.time.Clock()
# Mainloop
while not done:
# This limits the while loop to a max of 10 times per second.
# Leave this out and we will use all CPU we can.
clock.tick(self.frame_rate)
# Update HUD
score_label = score_font.render("Score: {}".format(len(self.snake.positions)), 1, BLACK)
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done = True # Flag that we are done so we exit this loop
elif event.type == pygame.KEYDOWN:
prev_vector = self.snake.vector
if event.key == pygame.K_RIGHT:
self.snake.vector = RIGHT
elif event.key == pygame.K_LEFT:
self.snake.vector = LEFT
if event.key == pygame.K_UP:
self.snake.vector = UP
elif event.key == pygame.K_DOWN:
self.snake.vector = DOWN
if prev_vector.__invert__().__dict__ == self.snake.vector.__dict__:
self.snake.vector = prev_vector
if self.snake.is_colliding():
done = True
if self.snake.encounters(self.food, self.object_padding):
self.snake.eat()
# Clear the screen and set the screen background
self.screen.fill(WHITE)
# ===========> UPDATE POSITIONS HERE <========
self.snake.update()
# ===========> START DRAWING HERE <===========
self.snake.show()
self.food.show()
self.screen.blit(score_label, (15, HEIGHT - 25))
# ===========> END DRAWING HERE <=============
# Go ahead and update the screen with what we've drawn.
# This MUST happen after all the other drawing commands.
pygame.display.flip()
if __name__ == '__main__':
while True:
game = Game(screen)
game.run()