Rename connected to powered, fix power not propagating correctly(spelling errors), add temporary power source texture and power sources themselves, fix power line textures being misaligned

This commit is contained in:
csd4ni3l
2025-11-06 22:53:32 +01:00
parent b246648099
commit 344c07038c
24 changed files with 121 additions and 60 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 167 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

View File

Before

Width:  |  Height:  |  Size: 203 B

After

Width:  |  Height:  |  Size: 203 B

View File

Before

Width:  |  Height:  |  Size: 187 B

After

Width:  |  Height:  |  Size: 187 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

View File

Before

Width:  |  Height:  |  Size: 191 B

After

Width:  |  Height:  |  Size: 191 B

View File

Before

Width:  |  Height:  |  Size: 181 B

After

Width:  |  Height:  |  Size: 181 B

View File

@@ -3,6 +3,8 @@ import arcade, arcade.gui, random
from utils.constants import button_style from utils.constants import button_style
from utils.preload import button_texture, button_hovered_texture from utils.preload import button_texture, button_hovered_texture
from collections import deque
from game.power_line import PowerLine from game.power_line import PowerLine
class Game(arcade.gui.UIView): class Game(arcade.gui.UIView):
@@ -14,6 +16,7 @@ class Game(arcade.gui.UIView):
self.difficulty = difficulty self.difficulty = difficulty
self.power_lines = [] self.power_lines = []
self.power_sources = []
self.anchor = self.add_widget(arcade.gui.UIAnchorLayout(size_hint=(1, 1))) self.anchor = self.add_widget(arcade.gui.UIAnchorLayout(size_hint=(1, 1)))
self.grid_size = list(map(int, difficulty.split("x"))) self.grid_size = list(map(int, difficulty.split("x")))
@@ -29,17 +32,51 @@ class Game(arcade.gui.UIView):
for row in range(self.grid_size[0]): for row in range(self.grid_size[0]):
self.power_lines.append([]) self.power_lines.append([])
for col in range(self.grid_size[1]): for col in range(self.grid_size[1]):
left_neighbor = self.power_lines[row][col - 1] if col > 0 else None left_neighbour = self.power_lines[row][col - 1] if col > 0 else None
top_neighbor = self.power_lines[row - 1][col] if row > 0 else None top_neighbour = self.power_lines[row - 1][col] if row > 0 else None
line_type = random.choice(["line", "corner", "power_source"])
power_line = PowerLine(line_type, left_neighbour, top_neighbour)
if line_type == "power_source":
self.power_sources.append(power_line)
power_line = PowerLine(random.choice(["line", "corner"]), left_neighbor, top_neighbor)
self.power_grid.add(power_line, row=row, column=col) self.power_grid.add(power_line, row=row, column=col)
self.power_lines[row].append(power_line) self.power_lines[row].append(power_line)
if left_neighbor: if left_neighbour:
left_neighbor.right_neighbor = power_line left_neighbour.right_neighbour = power_line
if top_neighbor: if top_neighbour:
top_neighbor.bottom_neighbor = power_line top_neighbour.bottom_neighbour = power_line
arcade.schedule(self.update_grid, 1 / 10)
def update_grid(self, _):
for row in self.power_lines:
for power_line in row:
if power_line.line_type != "power_source":
power_line.powered = False
queue = deque(self.power_sources)
visited = set()
while queue:
current = queue.popleft()
if id(current) in visited:
continue
visited.add(id(current))
current.powered = True
for connected_neighbour in current.get_connected_neighbours():
if id(connected_neighbour) not in visited:
queue.append(connected_neighbour)
for row in self.power_lines:
for power_line in row:
power_line.update_visual()
def main_exit(self): def main_exit(self):
from menus.main import Main from menus.main import Main

View File

@@ -3,79 +3,101 @@ import arcade, arcade.gui
from utils.preload import * from utils.preload import *
TEXTURE_MAP = { TEXTURE_MAP = {
("vertical", True): vertical_connected, ("line", "vertical", True): vertical_powered,
("vertical", False): vertical_unconnected, ("line", "vertical", False): vertical_unpowered,
("horizontal", True): horizontal_connected, ("line", "horizontal", True): horizontal_powered,
("horizontal", False): horizontal_unconnected, ("line", "horizontal", False): horizontal_unpowered,
("left_bottom", True): left_bottom_connected, ("corner", "left_bottom", True): left_bottom_powered,
("left_bottom", False): left_bottom_unconnected, ("corner", "left_bottom", False): left_bottom_unpowered,
("left_top", True): left_top_connected, ("corner", "left_top", True): left_top_powered,
("left_top", False): left_top_unconnected, ("corner", "left_top", False): left_top_unpowered,
("right_bottom", True): right_bottom_connected, ("corner", "right_bottom", True): right_bottom_powered,
("right_bottom", False): right_bottom_unconnected, ("corner", "right_bottom", False): right_bottom_unpowered,
("right_top", True): right_top_connected, ("corner", "right_top", True): right_top_powered,
("right_top", False): right_top_unconnected, ("corner", "right_top", False): right_top_unpowered,
("power_source", "all", True): power_source,
} }
ROTATIONS = { ROTATIONS = {
"line": ["vertical", "horizontal"], "line": ["vertical", "horizontal"],
"corner": ["left_top", "right_top", "right_bottom", "left_bottom"] "corner": ["right_bottom", "left_bottom", "left_top", "right_top"],
"power_source": ["all"]
} }
NEIGHBOURS = { NEIGHBOURS = {
"vertical": lambda l, r, t, b: bool((b and b.connected) or (t and t.connected)), "vertical": ["b", "t"],
"horizontal": lambda l, r, t, b: bool((l and l.connected) or (r and r.connected)), "horizontal": ["l", "r"],
"left_bottom": lambda l, r, t, b: bool(((l and l.connected) or (b and b.connected))), "left_bottom": ["l", "b"],
"right_bottom": lambda l, r, t, b: bool(((r and r.connected) or (b and b.connected))), "right_bottom": ["r", "b"],
"left_top": lambda l, r, t, b: bool(((l and l.connected) or (t and t.connected))), "left_top": ["l", "t"],
"right_top": lambda l, r, t, b: bool(((r and r.connected) or (t and t.connected))) "right_top": ["r", "t"],
"all": ["l", "r", "t", "b"]
} }
def get_opposite(direction):
if direction == "l":
return "r"
elif direction == "r":
return "l"
elif direction == "t":
return "b"
elif direction == "b":
return "t"
class PowerLine(arcade.gui.UITextureButton): class PowerLine(arcade.gui.UITextureButton):
def __init__(self, line_type, left_neighbor, top_neighbour): def __init__(self, line_type, left_neighbour, top_neighbour):
super().__init__(texture=TEXTURE_MAP[ROTATIONS[line_type][0], False]) super().__init__(texture=TEXTURE_MAP[line_type, ROTATIONS[line_type][0], line_type == "power_source"])
self.line_type = line_type self.line_type = line_type
self.rotation = ROTATIONS[line_type][0] self.rotation = ROTATIONS[line_type][0]
self.connected = False self.powered = self.line_type == "power_source"
self.left_neighbour, self.top_neighbour = left_neighbor, top_neighbour self.left_neighbour, self.top_neighbour = left_neighbour, top_neighbour
self.right_neighbour, self.bottom_neighbour = None, None self.right_neighbour, self.bottom_neighbour = None, None
self.on_click = lambda e: self.next_rotation() self.on_click = lambda e: self.next_rotation()
self.update() self.update_visual()
def next_rotation(self): def next_rotation(self):
current_index = ROTATIONS[self.line_type].index(self.rotation) current_index = ROTATIONS[self.line_type].index(self.rotation)
if current_index + 1 == len(ROTATIONS[self.line_type]) - 1: if current_index + 1 == len(ROTATIONS[self.line_type]):
self.rotation = ROTATIONS[self.line_type][0] self.rotation = ROTATIONS[self.line_type][0]
else: else:
self.rotation = ROTATIONS[self.line_type][current_index + 1] self.rotation = ROTATIONS[self.line_type][current_index + 1]
self.update() self.update_visual()
def update_neighbours(self): def get_neighbour(self, name):
if self.rotation == "horizontal": if name == "l":
self.left_neighbour.update() if self.left_neighbour else None return self.left_neighbour
self.right_neighbour.update() if self.right_neighbour else None elif name == "r":
elif self.rotation == "vertical": return self.right_neighbour
self.top_neighbour.update() if self.top_neighbour else None elif name == "b":
self.bottom_neighbour.update() if self.bottom_neighbour else None return self.bottom_neighbour
elif name == "t":
return self.top_neighbour
def update(self): def get_connected_neighbours(self):
if not self.connected: return [
old_connected = self.connected self.get_neighbour(neighbour_direction) for neighbour_direction in NEIGHBOURS[self.rotation]
self.connected = NEIGHBOURS[self.rotation](self.left_neighbour, self.right_neighbour, self.top_neighbour, self.bottom_neighbour) if (
if self.connected != old_connected: self.get_neighbour(neighbour_direction) and
self.update_neighbours() get_opposite(neighbour_direction) in NEIGHBOURS[self.get_neighbour(neighbour_direction).rotation]
)
]
self.texture = TEXTURE_MAP[(self.rotation, self.connected)] def update_value(self):
self.texture_hovered = TEXTURE_MAP[(self.rotation, self.connected)] self.powered = any([neighbour.powered for neighbour in self.get_connected_neighbours()])
def update_visual(self):
self.texture = TEXTURE_MAP[(self.line_type, self.rotation, self.powered)]
self.texture_hovered = TEXTURE_MAP[(self.line_type, self.rotation, self.powered)]
self._requires_render = True self._requires_render = True

View File

@@ -3,16 +3,18 @@ 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_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")) 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/line/vertical.png") vertical_powered = arcade.load_texture("assets/graphics/powered_lines/line/vertical.png")
horizontal_connected = arcade.load_texture("assets/graphics/connected_lines/line/horizontal.png") horizontal_powered = arcade.load_texture("assets/graphics/powered_lines/line/horizontal.png")
left_bottom_connected = arcade.load_texture("assets/graphics/connected_lines/corner/left_bottom.png") left_bottom_powered = arcade.load_texture("assets/graphics/powered_lines/corner/left_bottom.png")
left_top_connected = arcade.load_texture("assets/graphics/connected_lines/corner/left_top.png") left_top_powered = arcade.load_texture("assets/graphics/powered_lines/corner/left_top.png")
right_bottom_connected = arcade.load_texture("assets/graphics/connected_lines/corner/right_bottom.png") right_bottom_powered = arcade.load_texture("assets/graphics/powered_lines/corner/right_bottom.png")
right_top_connected = arcade.load_texture("assets/graphics/connected_lines/corner/right_top.png") right_top_powered = arcade.load_texture("assets/graphics/powered_lines/corner/right_top.png")
vertical_unconnected = arcade.load_texture("assets/graphics/unconnected_lines/line/vertical.png") vertical_unpowered = arcade.load_texture("assets/graphics/unpowered_lines/line/vertical.png")
horizontal_unconnected = arcade.load_texture("assets/graphics/unconnected_lines/line/horizontal.png") horizontal_unpowered = arcade.load_texture("assets/graphics/unpowered_lines/line/horizontal.png")
left_bottom_unconnected = arcade.load_texture("assets/graphics/unconnected_lines/corner/left_bottom.png") left_bottom_unpowered = arcade.load_texture("assets/graphics/unpowered_lines/corner/left_bottom.png")
left_top_unconnected = arcade.load_texture("assets/graphics/unconnected_lines/corner/left_top.png") left_top_unpowered = arcade.load_texture("assets/graphics/unpowered_lines/corner/left_top.png")
right_bottom_unconnected = arcade.load_texture("assets/graphics/unconnected_lines/corner/right_bottom.png") right_bottom_unpowered = arcade.load_texture("assets/graphics/unpowered_lines/corner/right_bottom.png")
right_top_unconnected = arcade.load_texture("assets/graphics/unconnected_lines/corner/right_top.png") right_top_unpowered = arcade.load_texture("assets/graphics/unpowered_lines/corner/right_top.png")
power_source = arcade.load_texture("assets/graphics/power_source.png")