Repository navigation
Expand file tree
/
Copy pathgame.py
More file actions
183 lines (157 loc) · 5.45 KB
/
Copy pathgame.py
File metadata and controls
183 lines (157 loc) · 5.45 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
from __future__ import annotations
import enum
import random
from typing import Callable
from pygame import Surface
from consts import GRID_SIZE
from tile import Tile, TileColor
from ui_object import UIObject
from visual import animation
from visual.animation import Animation
from visual.text import GameFonts
def create_board() -> list[list[Tile | None]]:
return [[Tile(random.choice(list(TileColor)).value) for _ in range(GRID_SIZE)] for _ in range(GRID_SIZE)]
class GameState(enum.Enum):
MAIN_MENU = 0
LEVEL_SELECTION = 1
IN_GAME = 2
PAUSE_MENU = 3
class Game:
def set_animation_speed(self, speed: float) -> None:
self._ANIMATION_SPEED = speed
_ANIMATION_SPEED: float = 2.5
ANIM_SPEED_MULT: float = 1
EXPLOSION_SPEED: float = 2
FALL_SPEED: float = 6.25
ANIMATION_SPEED: float = property(lambda self: self._ANIMATION_SPEED * self.ANIM_SPEED_MULT, set_animation_speed)
UNMODIFIED_ANIMATION_SPEED: float = property(lambda self: self._ANIMATION_SPEED)
first_tile: tuple[int, int] | None = None
board: list[list[Tile | None]] = create_board()
no_draw: set[tuple] = set()
animations: list[animation.TileAnimation]
FPS = 120
score = 0
steps_left = 0
input_locked: bool = False
lock_timeout: int = 0
current_state = GameState.MAIN_MENU
screen: Surface
mouse_pos: tuple[float, float]
chain_size: int = 0
ui_objects: list[UIObject]
game_fonts: GameFonts
star_score: list[int]
def __init__(self):
self.animations: list[animation.Animation] = []
self.ui_objects: list[UIObject] = []
self.winning_condition: Callable[[Game], bool] = lambda game: False
def add_score(self, score: int):
self.score += score
def set_state(self, state: GameState):
self.current_state = state
def add_anim(self, anim: animation.Animation):
self.animations.append(anim)
self.animations.sort(key=lambda a: a.priority)
def remove_anim(self, anim: animation.TileAnimation):
if anim in self.animations:
self.animations.remove(anim)
def pick_random_tile(self) -> tuple[int, int]:
# Pick a random tile. If it is None, pick a new one until it is not None
while True:
x = random.randint(0, GRID_SIZE - 1)
y = random.randint(0, GRID_SIZE - 1)
if self.board[x][y] is not None:
return x, y
def dec_steps(self):
self.steps_left -= 1
def remove_combinations(self):
combs = self.find_combinations()
while len(combs) > 0:
combs = self.find_combinations()
for comb in combs:
self.board[comb[1][0]][comb[1][1]].color = random.choice(list(TileColor)).value
def find_combinations(self) -> list[list[tuple[int, int]]]:
combinations = []
# check vertical combinations
for j in range(GRID_SIZE):
for i in range(GRID_SIZE - 2):
tile1: Tile = self.board[i][j]
tile1_color = tile1.color if tile1 else None
if ((i, j) in self.no_draw) or (tile1 is None) or not tile1.can_combine():
continue
comb_size = 1
# go through each piece in the row to check for same color
for n in range(1, GRID_SIZE - i):
tile2 = self.board[i + n][j]
tile2_color = tile2.color if tile2 else None
if tile1_color == tile2_color and ((i + n, j) not in self.no_draw) and tile2.can_combine():
comb_size += 1
else:
break
# append if combination is 3 or more
if comb_size >= 3:
combinations.append([(k, j) for k in range(i, i + comb_size)])
i += comb_size - 1
# check horizontal combinations
for i in range(GRID_SIZE):
for j in range(GRID_SIZE - 2):
tile1: Tile = self.board[i][j]
tile1_color = tile1.color if tile1 else None
if ((i, j) in self.no_draw) or (tile1 is None) or not tile1.can_combine():
continue
comb_size = 1
# go through each piece in the row to check for same color
for n in range(1, GRID_SIZE - j):
tile2 = self.board[i][j + n]
tile2_color = tile2.color if tile2 else None
if tile1_color == tile2_color and ((i, j + n) not in self.no_draw) and tile2 and tile2.can_combine():
comb_size += 1
else:
break
# append if combination is 3 or more
if comb_size >= 3:
combinations.append([(i, k) for k in range(j, j + comb_size)])
j += comb_size - 1
# combine combinations that match up on edges
for combination in combinations.copy():
for comb in combinations.copy():
if combination == comb or comb not in combinations or combination not in combinations:
continue
c1f = combination[0]
c1l = combination[-1]
c2f = comb[0]
c2l = comb[-1]
if c1f == c2f or c1l == c2l or c1f == c2l or c1l == c2f:
combinations.remove(comb)
combinations.remove(combination)
combinations.append(list(set(combination).union(set(comb))))
combinations.sort(key=lambda x: len(x), reverse=True)
return combinations
def end_level(self):
self.set_state(GameState.LEVEL_SELECTION)
self.no_draw.clear()
self.animations.clear()
self.ANIM_SPEED_MULT = 1
def can_start(self, anim: Animation) -> bool:
if anim.starting_condition and not anim.starting_condition():
return False
if anim.delay > 0:
anim.delay -= (1 / self.FPS)
return False
anim.is_running = True
return True
def run_animations(game: Game):
for anim in game.animations.copy():
if not game.can_start(anim) and not anim.is_running:
continue
if anim.progress == 0:
if anim.on_start:
anim.on_start()
# calculate current attributes
anim.update(anim.get_anim_type_progress())
# anim.draw(game.screen)
if anim.progress > 1:
if anim.on_finish:
anim.on_finish()
game.animations.remove(anim)
anim.progress += anim.speed * (1 / game.FPS)