-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
311 lines (274 loc) · 10.5 KB
/
game.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
"""Naming rules
x, y - mouse coordinates (px)
cell_x, cell_y - cell coordinates (0..N-1)
"""
import pygame
import random
import typing
from button import Button
from const import (
CELL_VALUES,
MUSIC_MINUS,
MUSIC_PLUS,
SOUND_MINUS,
SOUND_PLUS,
WINDOW_SIZE,
INDENT_SIZE,
)
from graphics import (
MINUS_BUTTON,
MUSIC_ICON,
PLUS_BUTTON,
SOUND_ICON,
WINDOW,
BACKGROUND,
BACKGROUND_IMAGE,
WIN_LABEL,
STEP_COUNTER,
DIGIT_IMAGE,
get_cell_values_images,
# fade_out,
# grow_image,
)
from sound import (
TURN_SOUND,
CONGRATULATIONS_SOUND,
change_sound_volume,
change_music_volume,
)
class EXIT_CODES:
QUIT = 0
NEW = 1
class Game:
def __init__(self, rows: int, difficulty_level: int):
if difficulty_level > rows * rows:
raise Exception('Difficulty level cannot be so high')
self.window_size = WINDOW_SIZE
self.rows = rows
self.difficulty_level = difficulty_level
self.cell_size = self.window_size // self.rows
self.window = WINDOW
self.images = get_cell_values_images(self.cell_size)
self.buttons = {
SOUND_MINUS: None,
SOUND_PLUS: None,
MUSIC_MINUS: None,
MUSIC_PLUS: None,
}
self.field = [[CELL_VALUES.HORIZONTAL] * self.rows for _ in range(self.rows)]
self.cur_step = 0
def start(self):
pygame.display.set_caption(f'Croco game. Level: {self.difficulty_level}')
self.shuffle(self.difficulty_level)
self.draw_field()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return EXIT_CODES.QUIT
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
if self.is_end_game():
return EXIT_CODES.NEW
mouse_pos = pygame.mouse.get_pos()
self.process_buttons(mouse_pos)
self.change_cell_cross_by_mouse(mouse_pos)
self.draw_field()
if self.is_end_game():
self.draw_win_label()
pygame.mixer.Sound.stop(TURN_SOUND)
pygame.mixer.Sound.play(CONGRATULATIONS_SOUND)
pygame.display.update()
def increase_step(self):
self.cur_step += 1
def are_cell_coords_correct(self, cell_x, cell_y):
return 0 <= cell_x < self.rows and 0 <= cell_y < self.rows
def change_cell_cross_by_mouse(self, mouse_pos):
cur_cell_x, cur_cell_y = self.coordinates_to_cell(*mouse_pos)
if self.are_cell_coords_correct(cur_cell_x, cur_cell_y):
self.change_cell_cross(cur_cell_x, cur_cell_y)
self.increase_step()
pygame.mixer.Sound.play(TURN_SOUND)
def change_cell_cross(self, cur_cell_x: int, cur_cell_y: int):
for cell_x in range(0, self.rows):
if cell_x == cur_cell_x:
continue
self.toggle_cell(cell_x, cur_cell_y)
for cell_y in range(0, self.rows):
if cell_y == cur_cell_y:
continue
self.toggle_cell(cur_cell_x, cell_y)
self.toggle_cell(cur_cell_x, cur_cell_y)
def toggle_cell(self, cell_x: int, cell_y: int):
value = self.field[cell_x][cell_y]
self.field[cell_x][cell_y] = (value + 1) % 2
def coordinates_to_cell(self, x: int, y: int) -> (int, int):
cell_x = (x - INDENT_SIZE) // self.cell_size
cell_y = (y - INDENT_SIZE - STEP_COUNTER.height) // self.cell_size
return cell_x, cell_y
def cell_image_coordinates(self, cell_x: int, cell_y: int) -> (int, int):
x = self.cell_size * cell_x + INDENT_SIZE
y = self.cell_size * cell_y + INDENT_SIZE + STEP_COUNTER.height
return x, y
def fill_cell(self, cell_x: int, cell_y: int):
x, y = self.cell_image_coordinates(cell_x, cell_y)
value = self.field[cell_x][cell_y]
self.window.blit(self.images[value], (x, y))
def process_buttons(self, mouse_pos):
if self.buttons[SOUND_MINUS].is_over(mouse_pos):
change_sound_volume(False)
pygame.mixer.Sound.play(TURN_SOUND)
if self.buttons[SOUND_PLUS].is_over(mouse_pos):
change_sound_volume(True)
pygame.mixer.Sound.play(TURN_SOUND)
if self.buttons[MUSIC_MINUS].is_over(mouse_pos):
change_music_volume(False)
if self.buttons[MUSIC_PLUS].is_over(mouse_pos):
change_music_volume(True)
@staticmethod
def _get_digit_list(num: int) -> typing.List[int]:
if num == 0:
return [0]
digits = []
while num > 0:
digits.append(num % 10)
num //= 10
return digits[::-1]
def _draw_digit(self, digit: int, digit_offset: int, step_counter_right_offset: int):
x = self.window_size + INDENT_SIZE - step_counter_right_offset + digit_offset
y = INDENT_SIZE
self.window.blit(DIGIT_IMAGE.images[digit], (x, y))
def draw_step_counter(self):
digit_list = self._get_digit_list(self.cur_step)
step_counter_right_offset = DIGIT_IMAGE.width * len(digit_list)
x = self.window_size - STEP_COUNTER.width - step_counter_right_offset
y = INDENT_SIZE
self.window.blit(STEP_COUNTER.image, (x, y))
for i, digit in enumerate(digit_list):
self._draw_digit(digit, i * DIGIT_IMAGE.width, step_counter_right_offset)
def draw_buttons(self):
sound_minus_btn = Button(
self.window,
INDENT_SIZE,
INDENT_SIZE,
MINUS_BUTTON.width,
MINUS_BUTTON.height,
MINUS_BUTTON.image,
)
sound_minus_btn.draw()
self.buttons[SOUND_MINUS] = sound_minus_btn
sound_btn = Button(
self.window,
sound_minus_btn.x + sound_minus_btn.width + INDENT_SIZE,
INDENT_SIZE,
SOUND_ICON.width,
SOUND_ICON.height,
SOUND_ICON.image,
)
sound_btn.draw()
sound_plus_btn = self.buttons[SOUND_PLUS] = Button(
self.window,
sound_btn.x + sound_btn.width + INDENT_SIZE,
INDENT_SIZE,
PLUS_BUTTON.width,
PLUS_BUTTON.height,
PLUS_BUTTON.image,
)
sound_plus_btn.draw()
self.buttons[SOUND_PLUS] = sound_plus_btn
music_minus_btn = Button(
self.window,
sound_plus_btn.x + sound_plus_btn.width * 2 + INDENT_SIZE * 2,
INDENT_SIZE,
MINUS_BUTTON.width,
MINUS_BUTTON.height,
MINUS_BUTTON.image,
)
music_minus_btn.draw()
self.buttons[MUSIC_MINUS] = music_minus_btn
music_btn = Button(
self.window,
music_minus_btn.x + music_minus_btn.width + INDENT_SIZE,
INDENT_SIZE,
MUSIC_ICON.width,
MUSIC_ICON.height,
MUSIC_ICON.image,
)
music_btn.draw()
music_plus_btn = self.buttons[MUSIC_PLUS] = Button(
self.window,
music_btn.x + music_btn.width + INDENT_SIZE,
INDENT_SIZE,
PLUS_BUTTON.width,
PLUS_BUTTON.height,
PLUS_BUTTON.image,
)
music_plus_btn.draw()
self.buttons[MUSIC_PLUS] = music_plus_btn
def draw_background(self):
self.window.blit(BACKGROUND_IMAGE, (0, 0))
def draw_field(self):
"""Draw everything in the correct order"""
pygame.draw.rect(self.window, BACKGROUND, (
0, 0, self.window_size + 2 * INDENT_SIZE, self.window_size + 2 * INDENT_SIZE + STEP_COUNTER.height
))
self.draw_background()
self.draw_step_counter()
self.draw_buttons()
for x in range(self.rows):
for y in range(len(self.field[x])):
self.fill_cell(x, y)
def shuffle(self, steps: int):
while self.field == [[0] * self.rows] * self.rows:
cell_set = set()
for _ in range(steps):
cell_x = random.randint(0, self.rows - 1)
cell_y = random.randint(0, self.rows - 1)
while (cell_x, cell_y) in cell_set:
cell_x = random.randint(0, self.rows - 1)
cell_y = random.randint(0, self.rows - 1)
self.change_cell_cross(cell_x, cell_y)
cell_set.add((cell_x, cell_y))
def is_end_game(self) -> bool:
for row in self.field:
for cell in row:
if cell == CELL_VALUES.VERTICAL:
return False
return True
def draw_win_label(self):
x = int((self.window_size + 2 * INDENT_SIZE - WIN_LABEL.width) / 2)
# Custom label output on the first odd levels
if self.rows == 1:
y = int(self.window_size * 0.4 + STEP_COUNTER.height)
elif self.rows == 3:
y = int(-self.window_size * 0.16 + STEP_COUNTER.height)
elif self.rows == 5:
y = int(-self.window_size * 0.08 + STEP_COUNTER.height)
else:
y = int((self.window_size - WIN_LABEL.height) / 2) + STEP_COUNTER.height + INDENT_SIZE
# print(x, y)
# fade_out(self.window, 2) # fade out over 2 seconds
# grow_image(self.window, WIN_LABEL.image, (x, y), 0.5) # fade out over 2 seconds
if self.rows >= 11:
num_lines_to_fill = self.rows // 5
num_lines_to_fill = (num_lines_to_fill + (self.rows + 1) % 2) // 2 * 2 + self.rows % 2
pygame.draw.rect(self.window, BACKGROUND, (
0,
(self.rows - num_lines_to_fill) // 2 * self.cell_size + INDENT_SIZE + STEP_COUNTER.height,
self.window_size + 2 * INDENT_SIZE,
self.cell_size * num_lines_to_fill,
))
elif self.rows >= 7 and self.rows % 2:
pygame.draw.rect(self.window, BACKGROUND, (
0,
(self.rows - 1) // 2 * self.cell_size + INDENT_SIZE + STEP_COUNTER.height,
self.window_size + 2 * INDENT_SIZE,
self.cell_size,
))
elif self.rows >= 8 and not self.rows % 2:
pygame.draw.rect(self.window, BACKGROUND, (
0,
(self.rows // 2 - 1) * self.cell_size + INDENT_SIZE + STEP_COUNTER.height,
self.window_size + 2 * INDENT_SIZE,
self.cell_size * 2,
))
self.window.blit(WIN_LABEL.image, (x, y))