-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
257 lines (216 loc) · 6.76 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
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
import pygame
import random
import time
pygame.init()
def main_menu():
menu = True
selected = "start"
while menu:
sr.fill(DarkGreen)
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
selected = "start"
if event.key == pygame.K_DOWN:
selected = "quit"
if event.key == pygame.K_RETURN:
if selected == "start":
return True
else:
return False
f1 = pygame.font.SysFont("Comic Sans MS", 90)
title = f1.render("Snake game", True, Yellow)
f2 = pygame.font.SysFont("Comic Sans MS", 75)
if selected == "start":
start = f2.render("START", True, Black)
else:
start = f2.render("START", True, Yellow)
if selected == "quit":
quit = f2.render("QUIT", True, Black)
else:
quit = f2.render("QUIT", True, Yellow)
sr.blit(title, (WIDTH/2 - 300,80))
sr.blit(start, (WIDTH / 2, 200))
sr.blit(quit, (WIDTH / 2, 300))
pygame.display.update()
clock.tick(fps)
class Snake:
def __init__(self, x, y, color, speed, size):
self.x = x
self.y = y
self.color = Red
self.speed = speed
self.size = size
self.dir_x = 0 # -1 0 1
self.dir_y = 0
self.count = 1
self.heads = [] # 0 1
self.add_head()
def change_color(self):
colors = [White, Red, Green, DarkGreen, Blue, Yellow]
self.color = random.choice(colors)
def add_head(self):
self.heads.append(Snake_head(self.x, self.y, self.color, self.speed, self.size))
def remove_head(self):
if len(self.heads) > self.count:
self.heads.pop(0)
def draw(self, screen):
for head in self.heads:
head.draw(screen)
def move(self):
if self.dir_x == 1:
self.x += self.speed
if self.dir_x == -1:
self.x -= self.speed
if self.dir_y == 1:
self.y += speed
if self.dir_y == -1:
self.y -= speed
self.add_head()
self.remove_head()
def move_right(self):
if self.count == 1:
self.dir_x = 1
self.dir_y = 0
else:
if self.dir_y:
self.dir_x = 1
self.dir_y = 0
def move_left(self):
if self.count == 1:
self.dir_x = -1
self.dir_y = 0
else:
if self.dir_y:
self.dir_x = -1
self.dir_y = 0
def move_down(self):
if self.count == 1:
self.dir_x = 0
self.dir_y = 1
else:
if self.dir_x:
self.dir_x = 0
self.dir_y = 1
def move_up(self):
if self.count == 1:
self.dir_x = 0
self.dir_y = -1
else:
if self.dir_x:
self.dir_x = 0
self.dir_y = -1
def check_walls(self):
if self.x <= 0 or self.y <= 0 or self.y >= HEIGHT - self.size or self.x >= WIDTH - self.size:
return False
return True
def check_snake(self): # 0 0 1 len 3
for i in range(len(self.heads)):
if i != len(self.heads) - 1:
if self.x == self.heads[i].x and self.y == self.heads[i].y:
return False
return True
def check_food(self, food_x, food_y):
if self.x == food_x and self.y == food_y:
self.count += 1
return True
return False
class Snake_head:
def __init__(self, x, y, color, speed, size):
self.x = x
self.y = y
self.color = color
self.speed = speed
self.size = size
self.dir_x = 0 # -1 0 1
self.dir_y = 0
def draw(self, screen):
pygame.draw.rect(screen, self.color, (self.x, self.y, self.size, self.size))
WIDTH = 720
HEIGHT = 480
Black = (0,0,0)
White = (255,255,255)
Red = (255,0,0)
Green = (0,255,0)
DarkGreen = (0,100,0)
Blue = (0,0,255)
Yellow = (255,255,0)
sr = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption('SkySmart Game')
image = pygame.image.load('skyicon.PNG')
pygame.display.set_icon(image)
fps = 10 # frames per second
clock = pygame.time.Clock()
is_key_right = False
is_key_left = False
is_key_down = False
is_key_top = False
speed = 15 # Скорость должна быть равной размеру
size = 15
width_hero = 15
height_hero = 15
food_x = 150
food_y = 150
is_eat = True
f1 = pygame.font.Font(None, 36)
game_over_text = f1.render("Game over", True, Red)
snake = Snake(3 * speed, 3 * speed, Red, speed, size)
is_game_active = main_menu()
while is_game_active:
sr.fill(Black)
f2 = pygame.font.Font(None, 36)
score_text = f2.render(str(snake.count), True, Green)
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
is_key_right = True
if event.key == pygame.K_LEFT:
is_key_left = True
if event.key == pygame.K_UP:
is_key_top = True
if event.key == pygame.K_DOWN:
is_key_down = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
is_key_right = False
if event.key == pygame.K_LEFT:
is_key_left = False
if event.key == pygame.K_UP:
is_key_top = False
if event.key == pygame.K_DOWN:
is_key_down = False
if is_key_right:
snake.move_right()
if is_key_left:
snake.move_left()
if is_key_top:
snake.move_up()
if is_key_down:
snake.move_down()
snake.move()
is_game_active1 = snake.check_walls()
is_game_active2 = snake.check_snake()
is_game_active = is_game_active1 and is_game_active2
is_eat = snake.check_food(food_x, food_y)
snake.draw(sr)
if is_eat:
snake.change_color()
is_repeat = True
while is_repeat:
is_repeat = False
food_x = random.randint(0, WIDTH) * speed % WIDTH
food_y = random.randint(0, HEIGHT) * speed % HEIGHT
for snake_head in snake.heads:
if food_x == snake_head.x and food_y == snake_head.y:
is_repeat = True
sr.blit(score_text, (0, 0))
pygame.draw.rect(sr, Blue, (food_x, food_y, size, size))
pygame.display.update()
clock.tick(fps)
sr.blit(game_over_text, (250, 200))
pygame.display.update()
time.sleep(10)