-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonium.py
More file actions
191 lines (160 loc) · 7.01 KB
/
Copy pathpythonium.py
File metadata and controls
191 lines (160 loc) · 7.01 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
import pygame
import random
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
WIDTH = 1000
HEIGHT = 600
SIZE = 20
FPS_BASE = 15
SPEED_BASE = 1
HIGH_SCORE = 0
class Snake:
def __init__(self, speed, fps):
self.body = [(WIDTH // 2, HEIGHT // 2)]
self.direction = "RIGHT"
self.change_direction = self.direction
self.score = 0
self.alive = True
self.speed = speed
self.fps = fps
def move(self):
if self.alive:
if self.change_direction == "RIGHT":
self.body.insert(0, (self.body[0][0] + SIZE, self.body[0][1]))
elif self.change_direction == "LEFT":
self.body.insert(0, (self.body[0][0] - SIZE, self.body[0][1]))
elif self.change_direction == "UP":
self.body.insert(0, (self.body[0][0], self.body[0][1] - SIZE))
elif self.change_direction == "DOWN":
self.body.insert(0, (self.body[0][0], self.body[0][1] + SIZE))
self.body.pop()
def draw(self, screen):
for x, y in self.body:
pygame.draw.rect(screen, GREEN, (x, y, SIZE, SIZE))
def check_collision(self):
if self.body[0][0] < 0 or self.body[0][0] >= WIDTH or self.body[0][1] < 0 or self.body[0][1] >= HEIGHT:
self.alive = False
return True
if self.check_self_collision():
self.alive = False
return True
return False
def eat(self, food):
if self.body[0] == food.pos:
self.score += 1
food.new_pos()
self.body.append(self.body[-1])
return True
return False
def reset(self):
self.body = [(WIDTH // 2, HEIGHT // 2)]
self.direction = "RIGHT"
self.change_direction = self.direction
self.score = 0
self.alive = True
def check_self_collision(self):
head = self.body[0]
for i in range(1, len(self.body)):
if head == self.body[i]:
return True
return False
class Food:
def __init__(self):
self.new_pos()
def new_pos(self):
self.pos = (random.randrange(0, WIDTH, SIZE), random.randrange(0, HEIGHT, SIZE))
def draw(self, screen):
pygame.draw.rect(screen, RED, (self.pos[0], self.pos[1], SIZE, SIZE))
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Pythonium")
clock = pygame.time.Clock()
font = pygame.font.Font(None, 36)
def show_menu():
screen.fill(BLACK)
text = font.render("Pythonium", True, WHITE)
screen.blit(text, (WIDTH // 2 - 50, HEIGHT // 2 - 100))
text = font.render("Легкий [1]", True, WHITE)
screen.blit(text, (WIDTH // 2 - 50, HEIGHT // 2 - 50))
text = font.render("Средний [2]", True, WHITE)
screen.blit(text, (WIDTH // 2 - 50, HEIGHT // 2))
text = font.render("Сложный [3]", True, WHITE)
screen.blit(text, (WIDTH // 2 - 50, HEIGHT // 2 + 50))
pygame.display.flip()
running = True
while running:
show_menu()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_1:
SPEED_BASE = 1
FPS_BASE = 15
elif event.key == pygame.K_2:
SPEED_BASE = 1.5
FPS_BASE = 20
elif event.key == pygame.K_3:
SPEED_BASE = 2
FPS_BASE = 25
elif event.key == pygame.K_ESCAPE:
running = False
if event.key in (pygame.K_1, pygame.K_2, pygame.K_3):
snake = Snake(SPEED_BASE, FPS_BASE)
food = Food()
game_running = True
while game_running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
game_running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT and snake.change_direction != "RIGHT":
snake.change_direction = "LEFT"
elif event.key == pygame.K_RIGHT and snake.change_direction != "LEFT":
snake.change_direction = "RIGHT"
elif event.key == pygame.K_UP and snake.change_direction != "DOWN":
snake.change_direction = "UP"
elif event.key == pygame.K_DOWN and snake.change_direction != "UP":
snake.change_direction = "DOWN"
elif event.key in (pygame.K_SPACE, pygame.K_RETURN):
snake.reset()
elif event.key == pygame.K_ESCAPE:
running = False
game_running = False
if snake.alive:
snake.move()
if snake.check_collision():
font = pygame.font.Font(None, 72)
text = font.render("Игра окончена!", True, WHITE)
screen.blit(text, (WIDTH // 2 - 200, HEIGHT // 2 - 50))
text = font.render("Нажмите пробел или Enter, чтобы начать заново", True, WHITE)
screen.blit(text, (WIDTH // 2 - 300, HEIGHT // 2 + 50))
pygame.display.flip()
while not snake.alive:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key in (pygame.K_SPACE, pygame.K_RETURN):
snake.reset()
elif event.key == pygame.K_ESCAPE:
running = False
game_running = False
break
if snake.eat(food):
snake.speed += 0.1
snake.fps += 1
if snake.score > HIGH_SCORE:
HIGH_SCORE = snake.score
screen.fill(BLACK)
snake.draw(screen)
food.draw(screen)
font = pygame.font.Font(None, 36)
text = font.render("Счёт: {}".format(snake.score), True, WHITE)
screen.blit(text, (10, 10))
text = font.render("Рекорд: {}".format(HIGH_SCORE), True, WHITE)
screen.blit(text, (10, 50))
pygame.display.flip()
clock.tick(snake.fps)
pygame.quit()