diff --git a/assets/graphics/connected_lines/corner/left_bottom.png b/assets/graphics/connected_lines/corner/left_bottom.png new file mode 100644 index 0000000..6bfef33 Binary files /dev/null and b/assets/graphics/connected_lines/corner/left_bottom.png differ diff --git a/assets/graphics/connected_lines/corner/left_top.png b/assets/graphics/connected_lines/corner/left_top.png new file mode 100644 index 0000000..1be071b Binary files /dev/null and b/assets/graphics/connected_lines/corner/left_top.png differ diff --git a/assets/graphics/connected_lines/corner/right_bottom.png b/assets/graphics/connected_lines/corner/right_bottom.png new file mode 100644 index 0000000..322b432 Binary files /dev/null and b/assets/graphics/connected_lines/corner/right_bottom.png differ diff --git a/assets/graphics/connected_lines/corner/right_top.png b/assets/graphics/connected_lines/corner/right_top.png new file mode 100644 index 0000000..a2ded3f Binary files /dev/null and b/assets/graphics/connected_lines/corner/right_top.png differ diff --git a/assets/graphics/connected_lines/horizontal.png b/assets/graphics/connected_lines/horizontal.png deleted file mode 100644 index 7056e7e..0000000 Binary files a/assets/graphics/connected_lines/horizontal.png and /dev/null differ diff --git a/assets/graphics/connected_lines/line/horizontal.png b/assets/graphics/connected_lines/line/horizontal.png new file mode 100644 index 0000000..02b01b7 Binary files /dev/null and b/assets/graphics/connected_lines/line/horizontal.png differ diff --git a/assets/graphics/connected_lines/line/vertical.png b/assets/graphics/connected_lines/line/vertical.png new file mode 100644 index 0000000..571d4e1 Binary files /dev/null and b/assets/graphics/connected_lines/line/vertical.png differ diff --git a/assets/graphics/connected_lines/vertical.png b/assets/graphics/connected_lines/vertical.png deleted file mode 100644 index 681597d..0000000 Binary files a/assets/graphics/connected_lines/vertical.png and /dev/null differ diff --git a/assets/graphics/unconnected_lines/corner/left_bottom.png b/assets/graphics/unconnected_lines/corner/left_bottom.png new file mode 100644 index 0000000..b3734ce Binary files /dev/null and b/assets/graphics/unconnected_lines/corner/left_bottom.png differ diff --git a/assets/graphics/unconnected_lines/corner/left_top.png b/assets/graphics/unconnected_lines/corner/left_top.png new file mode 100644 index 0000000..37147ed Binary files /dev/null and b/assets/graphics/unconnected_lines/corner/left_top.png differ diff --git a/assets/graphics/unconnected_lines/corner/right_bottom.png b/assets/graphics/unconnected_lines/corner/right_bottom.png new file mode 100644 index 0000000..34f7fc3 Binary files /dev/null and b/assets/graphics/unconnected_lines/corner/right_bottom.png differ diff --git a/assets/graphics/unconnected_lines/corner/right_top.png b/assets/graphics/unconnected_lines/corner/right_top.png new file mode 100644 index 0000000..71d9c12 Binary files /dev/null and b/assets/graphics/unconnected_lines/corner/right_top.png differ diff --git a/assets/graphics/unconnected_lines/horizontal.png b/assets/graphics/unconnected_lines/horizontal.png deleted file mode 100644 index 5d30953..0000000 Binary files a/assets/graphics/unconnected_lines/horizontal.png and /dev/null differ diff --git a/assets/graphics/unconnected_lines/line/horizontal.png b/assets/graphics/unconnected_lines/line/horizontal.png new file mode 100644 index 0000000..b565670 Binary files /dev/null and b/assets/graphics/unconnected_lines/line/horizontal.png differ diff --git a/assets/graphics/unconnected_lines/line/vertical.png b/assets/graphics/unconnected_lines/line/vertical.png new file mode 100644 index 0000000..0092de7 Binary files /dev/null and b/assets/graphics/unconnected_lines/line/vertical.png differ diff --git a/assets/graphics/unconnected_lines/vertical.png b/assets/graphics/unconnected_lines/vertical.png deleted file mode 100644 index b05ccd5..0000000 Binary files a/assets/graphics/unconnected_lines/vertical.png and /dev/null differ diff --git a/game/play.py b/game/play.py index 02cc741..da3dd6c 100644 --- a/game/play.py +++ b/game/play.py @@ -1,4 +1,4 @@ -import arcade, arcade.gui +import arcade, arcade.gui, random from utils.constants import button_style from utils.preload import button_texture, button_hovered_texture @@ -13,6 +13,7 @@ class Game(arcade.gui.UIView): self.pypresence_client.update(state='In Game', start=self.pypresence_client.start_time) self.difficulty = difficulty + self.power_lines = [] self.anchor = self.add_widget(arcade.gui.UIAnchorLayout(size_hint=(1, 1))) self.grid_size = list(map(int, difficulty.split("x"))) @@ -26,8 +27,19 @@ class Game(arcade.gui.UIView): self.anchor.add(self.back_button, anchor_x="left", anchor_y="top", align_x=5, align_y=-5) for row in range(self.grid_size[0]): + self.power_lines.append([]) for col in range(self.grid_size[1]): - self.power_grid.add(PowerLine(), row=row, column=col) + left_neighbor = self.power_lines[row][col - 1] if col > 0 else None + top_neighbor = self.power_lines[row - 1][col] if row > 0 else None + + power_line = PowerLine(random.choice(["line", "corner"]), left_neighbor, top_neighbor) + self.power_grid.add(power_line, row=row, column=col) + self.power_lines[row].append(power_line) + + if left_neighbor: + left_neighbor.right_neighbor = power_line + if top_neighbor: + top_neighbor.bottom_neighbor = power_line def main_exit(self): from menus.main import Main diff --git a/game/power_line.py b/game/power_line.py index 55cd131..963a9d1 100644 --- a/game/power_line.py +++ b/game/power_line.py @@ -1,26 +1,81 @@ import arcade, arcade.gui -from utils.preload import vertical_connected, vertical_unconnected, horizontal_connected, horizontal_unconnected +from utils.preload import * -from typing import Literal +TEXTURE_MAP = { + ("vertical", True): vertical_connected, + ("vertical", False): vertical_unconnected, + + ("horizontal", True): horizontal_connected, + ("horizontal", False): horizontal_unconnected, + + ("left_bottom", True): left_bottom_connected, + ("left_bottom", False): left_bottom_unconnected, + + ("left_top", True): left_top_connected, + ("left_top", False): left_top_unconnected, + + ("right_bottom", True): right_bottom_connected, + ("right_bottom", False): right_bottom_unconnected, + + ("right_top", True): right_top_connected, + ("right_top", False): right_top_unconnected, +} + +ROTATIONS = { + "line": ["vertical", "horizontal"], + "corner": ["left_top", "right_top", "right_bottom", "left_bottom"] +} + +NEIGHBOURS = { + "vertical": lambda l, r, t, b: bool((b and b.connected) or (t and t.connected)), + "horizontal": lambda l, r, t, b: bool((l and l.connected) or (r and r.connected)), + "left_bottom": lambda l, r, t, b: bool(((l and l.connected) or (b and b.connected))), + "right_bottom": lambda l, r, t, b: bool(((r and r.connected) or (b and b.connected))), + "left_top": lambda l, r, t, b: bool(((l and l.connected) or (t and t.connected))), + "right_top": lambda l, r, t, b: bool(((r and r.connected) or (t and t.connected))) +} class PowerLine(arcade.gui.UITextureButton): - def __init__(self): - super().__init__(texture=vertical_unconnected) + def __init__(self, line_type, left_neighbor, top_neighbour): + super().__init__(texture=TEXTURE_MAP[ROTATIONS[line_type][0], False]) - self.rotation: Literal["vertical", "horizontal"] = "vertical" + self.line_type = line_type + self.rotation = ROTATIONS[line_type][0] self.connected = False + self.left_neighbour, self.top_neighbour = left_neighbor, top_neighbour + self.right_neighbour, self.bottom_neighbour = None, None + self.on_click = lambda e: self.next_rotation() - def next_rotation(self): - if self.rotation == "vertical": - self.rotation = "horizontal" - self.texture = horizontal_connected if self.connected else horizontal_unconnected - self.texture_hovered = horizontal_connected if self.connected else horizontal_unconnected - elif self.rotation == "horizontal": - self.rotation = "vertical" - self.texture = vertical_connected if self.connected else vertical_unconnected - self.texture_hovered = vertical_connected if self.connected else vertical_unconnected + self.update() + def next_rotation(self): + current_index = ROTATIONS[self.line_type].index(self.rotation) + + if current_index + 1 == len(ROTATIONS[self.line_type]) - 1: + self.rotation = ROTATIONS[self.line_type][0] + else: + self.rotation = ROTATIONS[self.line_type][current_index + 1] + + self.update() + + def update_neighbours(self): + if self.rotation == "horizontal": + self.left_neighbour.update() if self.left_neighbour else None + self.right_neighbour.update() if self.right_neighbour else None + elif self.rotation == "vertical": + self.top_neighbour.update() if self.top_neighbour else None + self.bottom_neighbour.update() if self.bottom_neighbour else None + + def update(self): + if not self.connected: + old_connected = self.connected + self.connected = NEIGHBOURS[self.rotation](self.left_neighbour, self.right_neighbour, self.top_neighbour, self.bottom_neighbour) + if self.connected != old_connected: + self.update_neighbours() + + self.texture = TEXTURE_MAP[(self.rotation, self.connected)] + self.texture_hovered = TEXTURE_MAP[(self.rotation, self.connected)] self._requires_render = True \ No newline at end of file diff --git a/utils/preload.py b/utils/preload.py index ac065b3..77487cd 100644 --- a/utils/preload.py +++ b/utils/preload.py @@ -3,8 +3,16 @@ import arcade.gui, arcade button_texture = arcade.gui.NinePatchTexture(64 // 4, 64 // 4, 64 // 4, 64 // 4, arcade.load_texture("assets/graphics/button.png")) button_hovered_texture = arcade.gui.NinePatchTexture(64 // 4, 64 // 4, 64 // 4, 64 // 4, arcade.load_texture("assets/graphics/button_hovered.png")) -vertical_connected = arcade.load_texture("assets/graphics/connected_lines/vertical.png") -horizontal_connected = arcade.load_texture("assets/graphics/connected_lines/horizontal.png") +vertical_connected = arcade.load_texture("assets/graphics/connected_lines/line/vertical.png") +horizontal_connected = arcade.load_texture("assets/graphics/connected_lines/line/horizontal.png") +left_bottom_connected = arcade.load_texture("assets/graphics/connected_lines/corner/left_bottom.png") +left_top_connected = arcade.load_texture("assets/graphics/connected_lines/corner/left_top.png") +right_bottom_connected = arcade.load_texture("assets/graphics/connected_lines/corner/right_bottom.png") +right_top_connected = arcade.load_texture("assets/graphics/connected_lines/corner/right_top.png") -vertical_unconnected = arcade.load_texture("assets/graphics/unconnected_lines/vertical.png") -horizontal_unconnected = arcade.load_texture("assets/graphics/unconnected_lines/horizontal.png") \ No newline at end of file +vertical_unconnected = arcade.load_texture("assets/graphics/unconnected_lines/line/vertical.png") +horizontal_unconnected = arcade.load_texture("assets/graphics/unconnected_lines/line/horizontal.png") +left_bottom_unconnected = arcade.load_texture("assets/graphics/unconnected_lines/corner/left_bottom.png") +left_top_unconnected = arcade.load_texture("assets/graphics/unconnected_lines/corner/left_top.png") +right_bottom_unconnected = arcade.load_texture("assets/graphics/unconnected_lines/corner/right_bottom.png") +right_top_unconnected = arcade.load_texture("assets/graphics/unconnected_lines/corner/right_top.png") \ No newline at end of file