-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
216 lines (163 loc) · 5.7 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import time
import pygame
import random
pygame.init()
window_width = 420
window_height = 420
window_game = pygame.display.set_mode((window_width, window_height))
score = 0
speed = 5
game_over = False
pygame.display.set_caption('Snake')
all_coordinates = []
for i in range(0, 401, 20):
for j in range(0, 401, 20):
if j == 0:
pass
else:
all_coordinates.append([i, j])
class Snake:
def __init__(self, x, y):
self.x = x
self.y = y
self.w = 20
self.h = 20
def draw(self):
return pygame.draw.rect(window_game, '#823FF3', [self.x, self.y, self.w, self.h], 2)
class Apple:
def __init__(self, x, y):
self.x = x
self.y = y
self.color = '#201926'
self.w = 20
self.h = 20
def draw(self):
pygame.draw.rect(window_game, self.color, [self.x,
self.y,
self.w,
self.h])
class Text:
def __init__(self, name, size, text, color, bkg_color, coordinates):
self.name = name
self.size = size
self.text = text
self.color = color
self.bkg_color = bkg_color
self.coordinates = coordinates
def show_score(self):
font = pygame.font.Font(self.name, self.size)
text = font.render(self.text + str(score), True, self.color, self.bkg_color)
rect = text.get_rect()
rect.center = self.coordinates
window_game.blit(text, rect)
def show(self):
font = pygame.font.Font(self.name, self.size)
text = font.render(self.text, True, self.color, self.bkg_color)
rect = text.get_rect()
rect.center = self.coordinates
window_game.blit(text, rect)
game_over_text = Text('freesansbold.ttf', 30, 'Game Over!', '#392A46', '#FDF7FF',
((window_width - 20) // 2, (window_height - 20) // 2))
score_text = Text('freesansbold.ttf', 15, 'Score: ', '#392A46', '#FDF7FF', (210, 10))
quit_text = Text('freesansbold.ttf', 20, 'or type Q to quit', '#FDF7FF', '#392A46',
((window_width - 20) // 2, (window_height + 145) // 2))
restart_text = Text('freesansbold.ttf', 20, 'Type R to restart the game', '#FDF7FF', '#392A46',
((window_width - 20) // 2, (window_height + 100) // 2))
def on_game_over():
global game_over
game_over = True
def init_game():
global snake_body
global apple
global score
global direction
global change_dir
global game_over
global speed
snake_body = [snake_body_element_1, snake_body_element_2, snake_body_element_3]
direction = 'Right'
change_dir = direction
score = 0
speed = 5
game_over = False
apple = Apple(80, 100)
snake_body_element_1 = Snake(20, 40)
snake_body_element_2 = Snake(40, 40)
snake_body_element_3 = Snake(60, 40)
snake_body = [snake_body_element_1, snake_body_element_2, snake_body_element_3]
clock = pygame.time.Clock()
dx = 20
dy = 0
direction = 'Right'
change_dir = direction
running = True
while running:
clock.tick(speed)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
change_dir = 'Right'
if event.key == pygame.K_LEFT:
change_dir = 'Left'
if event.key == pygame.K_UP:
change_dir = 'Up'
if event.key == pygame.K_DOWN:
change_dir = 'Down'
if event.key == pygame.K_r:
init_game()
if event.key == pygame.K_q:
pygame.quit()
if change_dir == 'Right' and direction != 'Left':
direction = 'Right'
if change_dir == 'Left' and direction != 'Right':
direction = 'Left'
if change_dir == 'Up' and direction != 'Down':
direction = 'Up'
if change_dir == 'Down' and direction != 'Up':
direction = 'Down'
if direction == 'Right':
dx = 20
dy = 0
if direction == 'Left':
dx = -20
dy = 0
if direction == 'Up':
dy = -20
dx = 0
if direction == 'Down':
dy = 20
dx = 0
window_game.fill((255, 255, 255))
if game_over:
game_over_text.show()
restart_text.show()
quit_text.show()
else:
snake_head = Snake(snake_body[-1].x + dx, snake_body[-1].y + dy)
snake_body.append(snake_head)
snake_body.pop(0)
draw_snake = list(map(lambda snake_body_element: snake_body_element.draw(), snake_body))
eat_apple = snake_body[-1].x == apple.x and snake_body[-1].y == apple.y
if eat_apple:
available_apple_coordinates = all_coordinates
for i in snake_body:
if [i.x, i.y] in available_apple_coordinates:
available_apple_coordinates.remove([i.x, i.y])
random_apple = available_apple_coordinates[random.randrange(0, len(available_apple_coordinates))]
apple.x, apple.y = random_apple[0], random_apple[1]
snake_tail = Snake(snake_body[0].x, snake_body[0].y)
snake_body.insert(0, snake_tail)
score += 1
speed += 0.25
out_of_border = snake_body[-1].x < 0 or snake_body[-1].x > 400 or snake_body[-1].y < 20 or snake_body[-1].y > 400
if out_of_border:
on_game_over()
eat_snake = list(filter(lambda sbe: snake_body[-1].x == sbe.x and snake_body[-1].y == sbe.y, snake_body[:-1]))
if eat_snake:
on_game_over()
apple.draw()
score_text.show_score()
pygame.display.flip()
pygame.quit()