mirror of
https://github.com/csd4ni3l/connect-the-current.git
synced 2026-01-01 04:13:41 +01:00
Switched to Linux and it changed the line encoding, probably, cause every file is showing as changed.
This commit is contained in:
118
game/cell.py
118
game/cell.py
@@ -1,60 +1,60 @@
|
||||
import arcade, arcade.gui
|
||||
|
||||
from utils.constants import ROTATIONS, NEIGHBOURS
|
||||
from utils.preload import TEXTURE_MAP, wire_sound_effect
|
||||
|
||||
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 Cell(arcade.Sprite):
|
||||
def __init__(self, cell_type, x, y, left_neighbour, top_neighbour):
|
||||
super().__init__(TEXTURE_MAP[cell_type, ROTATIONS[cell_type][0] if cell_type in ROTATIONS else "cross", cell_type == "power_source"], center_x=x, center_y=y)
|
||||
|
||||
self.rotation = ROTATIONS[cell_type][0] if cell_type in ROTATIONS else "cross"
|
||||
self.cell_type = cell_type
|
||||
self.powered = False
|
||||
self.left_neighbour, self.top_neighbour = left_neighbour, top_neighbour
|
||||
self.right_neighbour, self.bottom_neighbour = None, None
|
||||
|
||||
def get_neighbour(self, name):
|
||||
if name == "l":
|
||||
return self.left_neighbour
|
||||
elif name == "r":
|
||||
return self.right_neighbour
|
||||
elif name == "b":
|
||||
return self.bottom_neighbour
|
||||
elif name == "t":
|
||||
return self.top_neighbour
|
||||
|
||||
def get_connected_neighbours(self, include_houses=False):
|
||||
return [
|
||||
self.get_neighbour(neighbour_direction) for neighbour_direction in NEIGHBOURS[self.rotation]
|
||||
if (
|
||||
self.get_neighbour(neighbour_direction) and
|
||||
(include_houses or self.get_neighbour(neighbour_direction).cell_type != "house") and
|
||||
get_opposite(neighbour_direction) in NEIGHBOURS[self.get_neighbour(neighbour_direction).rotation]
|
||||
)
|
||||
]
|
||||
|
||||
def update_visual(self):
|
||||
self.texture = TEXTURE_MAP[(self.cell_type, self.rotation, self.powered)]
|
||||
|
||||
def next_rotation(self, sfx, sfx_volume):
|
||||
if sfx:
|
||||
wire_sound_effect.play(volume=sfx_volume / 50)
|
||||
|
||||
current_index = ROTATIONS[self.cell_type].index(self.rotation)
|
||||
|
||||
if current_index + 1 == len(ROTATIONS[self.cell_type]):
|
||||
self.rotation = ROTATIONS[self.cell_type][0]
|
||||
else:
|
||||
self.rotation = ROTATIONS[self.cell_type][current_index + 1]
|
||||
|
||||
import arcade, arcade.gui
|
||||
|
||||
from utils.constants import ROTATIONS, NEIGHBOURS
|
||||
from utils.preload import TEXTURE_MAP, wire_sound_effect
|
||||
|
||||
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 Cell(arcade.Sprite):
|
||||
def __init__(self, cell_type, x, y, left_neighbour, top_neighbour):
|
||||
super().__init__(TEXTURE_MAP[cell_type, ROTATIONS[cell_type][0] if cell_type in ROTATIONS else "cross", cell_type == "power_source"], center_x=x, center_y=y)
|
||||
|
||||
self.rotation = ROTATIONS[cell_type][0] if cell_type in ROTATIONS else "cross"
|
||||
self.cell_type = cell_type
|
||||
self.powered = False
|
||||
self.left_neighbour, self.top_neighbour = left_neighbour, top_neighbour
|
||||
self.right_neighbour, self.bottom_neighbour = None, None
|
||||
|
||||
def get_neighbour(self, name):
|
||||
if name == "l":
|
||||
return self.left_neighbour
|
||||
elif name == "r":
|
||||
return self.right_neighbour
|
||||
elif name == "b":
|
||||
return self.bottom_neighbour
|
||||
elif name == "t":
|
||||
return self.top_neighbour
|
||||
|
||||
def get_connected_neighbours(self, include_houses=False):
|
||||
return [
|
||||
self.get_neighbour(neighbour_direction) for neighbour_direction in NEIGHBOURS[self.rotation]
|
||||
if (
|
||||
self.get_neighbour(neighbour_direction) and
|
||||
(include_houses or self.get_neighbour(neighbour_direction).cell_type != "house") and
|
||||
get_opposite(neighbour_direction) in NEIGHBOURS[self.get_neighbour(neighbour_direction).rotation]
|
||||
)
|
||||
]
|
||||
|
||||
def update_visual(self):
|
||||
self.texture = TEXTURE_MAP[(self.cell_type, self.rotation, self.powered)]
|
||||
|
||||
def next_rotation(self, sfx, sfx_volume):
|
||||
if sfx:
|
||||
wire_sound_effect.play(volume=sfx_volume / 50)
|
||||
|
||||
current_index = ROTATIONS[self.cell_type].index(self.rotation)
|
||||
|
||||
if current_index + 1 == len(ROTATIONS[self.cell_type]):
|
||||
self.rotation = ROTATIONS[self.cell_type][0]
|
||||
else:
|
||||
self.rotation = ROTATIONS[self.cell_type][current_index + 1]
|
||||
|
||||
self.update_visual()
|
||||
@@ -1,17 +1,17 @@
|
||||
from game.cell import Cell
|
||||
|
||||
class House(Cell):
|
||||
def __init__(self, x, y, left_neighbour, top_neighbour):
|
||||
super().__init__("house", x, y, left_neighbour, top_neighbour)
|
||||
|
||||
class PowerSource(Cell):
|
||||
def __init__(self, x, y, left_neighbour, top_neighbour):
|
||||
super().__init__("power_source", x, y, left_neighbour, top_neighbour)
|
||||
self.on_click = lambda e: self.next_rotation()
|
||||
|
||||
class PowerLine(Cell):
|
||||
def __init__(self, cell_type, x, y, left_neighbour, top_neighbour):
|
||||
super().__init__(cell_type, x, y, left_neighbour, top_neighbour)
|
||||
|
||||
if not cell_type == "cross":
|
||||
from game.cell import Cell
|
||||
|
||||
class House(Cell):
|
||||
def __init__(self, x, y, left_neighbour, top_neighbour):
|
||||
super().__init__("house", x, y, left_neighbour, top_neighbour)
|
||||
|
||||
class PowerSource(Cell):
|
||||
def __init__(self, x, y, left_neighbour, top_neighbour):
|
||||
super().__init__("power_source", x, y, left_neighbour, top_neighbour)
|
||||
self.on_click = lambda e: self.next_rotation()
|
||||
|
||||
class PowerLine(Cell):
|
||||
def __init__(self, cell_type, x, y, left_neighbour, top_neighbour):
|
||||
super().__init__(cell_type, x, y, left_neighbour, top_neighbour)
|
||||
|
||||
if not cell_type == "cross":
|
||||
self.on_click = lambda e: self.next_rotation()
|
||||
@@ -1,130 +1,130 @@
|
||||
import random
|
||||
from utils.constants import ROTATIONS, NEIGHBOURS, DIRECTIONS
|
||||
|
||||
def in_bounds(x, y, size):
|
||||
return 0 <= x < size and 0 <= y < size
|
||||
|
||||
def classify_tile(conns):
|
||||
for rotation, connections in NEIGHBOURS.items():
|
||||
if conns == connections:
|
||||
for cell_type, rotations in ROTATIONS.items():
|
||||
if rotation in rotations:
|
||||
return cell_type
|
||||
|
||||
print(f"Unknown: {conns}")
|
||||
return "cross"
|
||||
|
||||
def add_cycles(conns, num_cycles):
|
||||
size = len(conns)
|
||||
added = 0
|
||||
attempts = 0
|
||||
max_attempts = num_cycles * 20
|
||||
|
||||
while added < num_cycles and attempts < max_attempts:
|
||||
attempts += 1
|
||||
x, y = random.randint(0, size-1), random.randint(0, size-1)
|
||||
|
||||
dirs = list(DIRECTIONS.items())
|
||||
random.shuffle(dirs)
|
||||
|
||||
for d, (dx, dy, opposite) in dirs:
|
||||
nx, ny = x + dx, y + dy
|
||||
|
||||
if in_bounds(nx, ny, size) and d not in conns[y][x]:
|
||||
conns[y][x].add(d)
|
||||
conns[ny][nx].add(opposite)
|
||||
added += 1
|
||||
break
|
||||
|
||||
return conns
|
||||
|
||||
def pick_random_cells(size, count, avoid=None):
|
||||
all_cells = [(x, y) for y in range(size) for x in range(size)]
|
||||
|
||||
if avoid:
|
||||
all_cells = [c for c in all_cells if c not in avoid]
|
||||
|
||||
random.shuffle(all_cells)
|
||||
return all_cells[:count]
|
||||
|
||||
def generate_spanning_tree_with_dead_ends(size, num_dead_ends):
|
||||
if num_dead_ends > size * size - 1:
|
||||
num_dead_ends = size * size - 1
|
||||
|
||||
grid = [[set() for _ in range(size)] for _ in range(size)]
|
||||
all_cells = [(x, y) for y in range(size) for x in range(size)]
|
||||
random.shuffle(all_cells)
|
||||
|
||||
start = all_cells[0]
|
||||
stack = [start]
|
||||
visited = {start}
|
||||
leaf_candidates = []
|
||||
|
||||
while len(visited) < size * size:
|
||||
if not stack:
|
||||
unvisited = [c for c in all_cells if c not in visited]
|
||||
if unvisited:
|
||||
stack.append(unvisited[0])
|
||||
visited.add(unvisited[0])
|
||||
|
||||
x, y = stack[-1]
|
||||
dirs = list(DIRECTIONS.items())
|
||||
random.shuffle(dirs)
|
||||
|
||||
found = False
|
||||
for d, (dx, dy, opposite) in dirs:
|
||||
nx, ny = x + dx, y + dy
|
||||
if in_bounds(nx, ny, size) and (nx, ny) not in visited:
|
||||
grid[y][x].add(d)
|
||||
grid[ny][nx].add(opposite)
|
||||
visited.add((nx, ny))
|
||||
stack.append((nx, ny))
|
||||
found = True
|
||||
break
|
||||
|
||||
if not found:
|
||||
if len(grid[y][x]) == 1:
|
||||
leaf_candidates.append((x, y))
|
||||
stack.pop()
|
||||
|
||||
leaf_nodes = leaf_candidates[:num_dead_ends]
|
||||
|
||||
for y in range(size):
|
||||
for x in range(size):
|
||||
if (x, y) not in leaf_nodes and len(grid[y][x]) < 2:
|
||||
dirs = list(DIRECTIONS.items())
|
||||
random.shuffle(dirs)
|
||||
|
||||
for d, (dx, dy, opposite) in dirs:
|
||||
nx, ny = x + dx, y + dy
|
||||
|
||||
if in_bounds(nx, ny, size) and d not in grid[y][x]:
|
||||
grid[y][x].add(d)
|
||||
grid[ny][nx].add(opposite)
|
||||
if len(grid[y][x]) >= 2:
|
||||
break
|
||||
|
||||
return grid, leaf_nodes
|
||||
|
||||
def generate_map(size, source_count, house_count, cycles=15):
|
||||
conns, dead_ends = generate_spanning_tree_with_dead_ends(size, house_count)
|
||||
conns = add_cycles(conns, cycles)
|
||||
|
||||
houses = dead_ends[:house_count]
|
||||
available_cells = [(x, y) for y in range(size) for x in range(size) if (x, y) not in houses]
|
||||
random.shuffle(available_cells)
|
||||
sources = available_cells[:source_count]
|
||||
|
||||
grid = []
|
||||
for y in range(size):
|
||||
grid.append([])
|
||||
|
||||
for x in range(size):
|
||||
if (x, y) in sources:
|
||||
grid[-1].append("power_source")
|
||||
elif (x, y) in houses:
|
||||
grid[-1].append("house")
|
||||
else:
|
||||
grid[-1].append(classify_tile(conns[y][x]))
|
||||
|
||||
import random
|
||||
from utils.constants import ROTATIONS, NEIGHBOURS, DIRECTIONS
|
||||
|
||||
def in_bounds(x, y, size):
|
||||
return 0 <= x < size and 0 <= y < size
|
||||
|
||||
def classify_tile(conns):
|
||||
for rotation, connections in NEIGHBOURS.items():
|
||||
if conns == connections:
|
||||
for cell_type, rotations in ROTATIONS.items():
|
||||
if rotation in rotations:
|
||||
return cell_type
|
||||
|
||||
print(f"Unknown: {conns}")
|
||||
return "cross"
|
||||
|
||||
def add_cycles(conns, num_cycles):
|
||||
size = len(conns)
|
||||
added = 0
|
||||
attempts = 0
|
||||
max_attempts = num_cycles * 20
|
||||
|
||||
while added < num_cycles and attempts < max_attempts:
|
||||
attempts += 1
|
||||
x, y = random.randint(0, size-1), random.randint(0, size-1)
|
||||
|
||||
dirs = list(DIRECTIONS.items())
|
||||
random.shuffle(dirs)
|
||||
|
||||
for d, (dx, dy, opposite) in dirs:
|
||||
nx, ny = x + dx, y + dy
|
||||
|
||||
if in_bounds(nx, ny, size) and d not in conns[y][x]:
|
||||
conns[y][x].add(d)
|
||||
conns[ny][nx].add(opposite)
|
||||
added += 1
|
||||
break
|
||||
|
||||
return conns
|
||||
|
||||
def pick_random_cells(size, count, avoid=None):
|
||||
all_cells = [(x, y) for y in range(size) for x in range(size)]
|
||||
|
||||
if avoid:
|
||||
all_cells = [c for c in all_cells if c not in avoid]
|
||||
|
||||
random.shuffle(all_cells)
|
||||
return all_cells[:count]
|
||||
|
||||
def generate_spanning_tree_with_dead_ends(size, num_dead_ends):
|
||||
if num_dead_ends > size * size - 1:
|
||||
num_dead_ends = size * size - 1
|
||||
|
||||
grid = [[set() for _ in range(size)] for _ in range(size)]
|
||||
all_cells = [(x, y) for y in range(size) for x in range(size)]
|
||||
random.shuffle(all_cells)
|
||||
|
||||
start = all_cells[0]
|
||||
stack = [start]
|
||||
visited = {start}
|
||||
leaf_candidates = []
|
||||
|
||||
while len(visited) < size * size:
|
||||
if not stack:
|
||||
unvisited = [c for c in all_cells if c not in visited]
|
||||
if unvisited:
|
||||
stack.append(unvisited[0])
|
||||
visited.add(unvisited[0])
|
||||
|
||||
x, y = stack[-1]
|
||||
dirs = list(DIRECTIONS.items())
|
||||
random.shuffle(dirs)
|
||||
|
||||
found = False
|
||||
for d, (dx, dy, opposite) in dirs:
|
||||
nx, ny = x + dx, y + dy
|
||||
if in_bounds(nx, ny, size) and (nx, ny) not in visited:
|
||||
grid[y][x].add(d)
|
||||
grid[ny][nx].add(opposite)
|
||||
visited.add((nx, ny))
|
||||
stack.append((nx, ny))
|
||||
found = True
|
||||
break
|
||||
|
||||
if not found:
|
||||
if len(grid[y][x]) == 1:
|
||||
leaf_candidates.append((x, y))
|
||||
stack.pop()
|
||||
|
||||
leaf_nodes = leaf_candidates[:num_dead_ends]
|
||||
|
||||
for y in range(size):
|
||||
for x in range(size):
|
||||
if (x, y) not in leaf_nodes and len(grid[y][x]) < 2:
|
||||
dirs = list(DIRECTIONS.items())
|
||||
random.shuffle(dirs)
|
||||
|
||||
for d, (dx, dy, opposite) in dirs:
|
||||
nx, ny = x + dx, y + dy
|
||||
|
||||
if in_bounds(nx, ny, size) and d not in grid[y][x]:
|
||||
grid[y][x].add(d)
|
||||
grid[ny][nx].add(opposite)
|
||||
if len(grid[y][x]) >= 2:
|
||||
break
|
||||
|
||||
return grid, leaf_nodes
|
||||
|
||||
def generate_map(size, source_count, house_count, cycles=15):
|
||||
conns, dead_ends = generate_spanning_tree_with_dead_ends(size, house_count)
|
||||
conns = add_cycles(conns, cycles)
|
||||
|
||||
houses = dead_ends[:house_count]
|
||||
available_cells = [(x, y) for y in range(size) for x in range(size) if (x, y) not in houses]
|
||||
random.shuffle(available_cells)
|
||||
sources = available_cells[:source_count]
|
||||
|
||||
grid = []
|
||||
for y in range(size):
|
||||
grid.append([])
|
||||
|
||||
for x in range(size):
|
||||
if (x, y) in sources:
|
||||
grid[-1].append("power_source")
|
||||
elif (x, y) in houses:
|
||||
grid[-1].append("house")
|
||||
else:
|
||||
grid[-1].append(classify_tile(conns[y][x]))
|
||||
|
||||
return grid
|
||||
298
game/play.py
298
game/play.py
@@ -1,150 +1,150 @@
|
||||
import arcade, arcade.gui, json, time
|
||||
|
||||
from utils.constants import button_style, NEIGHBOURS
|
||||
from utils.preload import button_texture, button_hovered_texture
|
||||
|
||||
from collections import deque
|
||||
|
||||
from game.level_generator import generate_map
|
||||
from game.cells import *
|
||||
|
||||
class Game(arcade.gui.UIView):
|
||||
def __init__(self, pypresence_client, grid_size, source_count=None, house_count=None):
|
||||
super().__init__()
|
||||
|
||||
self.pypresence_client = pypresence_client
|
||||
self.pypresence_client.update(state='In Game', start=self.pypresence_client.start_time)
|
||||
|
||||
self.grid_size = grid_size
|
||||
self.source_count = source_count
|
||||
self.house_count = house_count
|
||||
|
||||
self.start = time.perf_counter()
|
||||
self.wire_rotations = 0
|
||||
self.cells = []
|
||||
self.power_sources = []
|
||||
self.houses = []
|
||||
|
||||
self.anchor = self.add_widget(arcade.gui.UIAnchorLayout(size_hint=(1, 1)))
|
||||
self.map = generate_map(self.grid_size, int((self.grid_size * self.grid_size) / 10) if not source_count else source_count, int((self.grid_size * self.grid_size) / 5) if not house_count else house_count)
|
||||
|
||||
self.spritelist = arcade.SpriteList()
|
||||
|
||||
with open("settings.json", "r") as file:
|
||||
self.settings = json.load(file)
|
||||
|
||||
def on_show_view(self):
|
||||
super().on_show_view()
|
||||
|
||||
self.back_button = arcade.gui.UITextureButton(texture=button_texture, texture_hovered=button_hovered_texture, text='<--', style=button_style, width=100, height=50)
|
||||
self.back_button.on_click = lambda event: self.main_exit()
|
||||
self.anchor.add(self.back_button, anchor_x="left", anchor_y="top", align_x=5, align_y=-5)
|
||||
|
||||
self.won_label = self.anchor.add(arcade.gui.UILabel(text="You won!", font_size=48), anchor_x="center", anchor_y="center")
|
||||
self.won_label.visible = False
|
||||
|
||||
self.info_label = self.anchor.add(arcade.gui.UILabel("Time spent: 0s Wire Rotations: 0", font_size=24), anchor_x="center", anchor_y="top")
|
||||
|
||||
x = (self.window.width / 2) - (self.grid_size * 64) / 2
|
||||
y = (self.window.height / 2) + (self.grid_size * 64) / 2
|
||||
|
||||
for row in range(self.grid_size):
|
||||
self.cells.append([])
|
||||
|
||||
for col in range(self.grid_size):
|
||||
left_neighbour = self.cells[row][col - 1] if col > 0 else None
|
||||
top_neighbour = self.cells[row - 1][col] if row > 0 else None
|
||||
|
||||
cell_type = self.map[row][col]
|
||||
|
||||
if cell_type in ["line", "corner", "t_junction", "cross"]:
|
||||
cell = PowerLine(cell_type, x, y, left_neighbour, top_neighbour)
|
||||
elif cell_type == "power_source":
|
||||
cell = PowerSource(x, y, left_neighbour, top_neighbour)
|
||||
self.power_sources.append(cell)
|
||||
elif cell_type == "house":
|
||||
cell = House(x, y, left_neighbour, top_neighbour)
|
||||
self.houses.append(cell)
|
||||
|
||||
self.spritelist.append(cell)
|
||||
self.cells[row].append(cell)
|
||||
|
||||
if left_neighbour:
|
||||
left_neighbour.right_neighbour = cell
|
||||
if top_neighbour:
|
||||
top_neighbour.bottom_neighbour = cell
|
||||
|
||||
x += 64
|
||||
x = (self.window.width / 2) - (self.grid_size * 64) / 2
|
||||
y -= 64
|
||||
|
||||
arcade.schedule(self.update_grid, 1 / 8)
|
||||
|
||||
def update_grid(self, _):
|
||||
for row in self.cells:
|
||||
for power_line in row:
|
||||
if power_line.cell_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.cells:
|
||||
for cell in row:
|
||||
cell.update_visual()
|
||||
|
||||
self.check_win()
|
||||
|
||||
def check_win(self):
|
||||
for row in self.cells:
|
||||
for cell in row:
|
||||
if cell.cell_type == "power_source":
|
||||
continue
|
||||
elif cell.cell_type == "house":
|
||||
if not len(cell.get_connected_neighbours()) >= 1:
|
||||
return
|
||||
else:
|
||||
continue
|
||||
|
||||
if len(cell.get_connected_neighbours(True)) != len(NEIGHBOURS[cell.rotation]):
|
||||
return
|
||||
|
||||
self.won_label.visible = True
|
||||
self.spritelist.visible = False
|
||||
|
||||
arcade.unschedule(self.update_grid)
|
||||
|
||||
def on_mouse_press(self, x, y, button, modifiers):
|
||||
for row in self.cells:
|
||||
for cell in row:
|
||||
if cell.cell_type in ["house", "power_source"]:
|
||||
continue
|
||||
|
||||
if cell.rect.point_in_rect((x, y)):
|
||||
self.wire_rotations += 1
|
||||
cell.next_rotation(self.settings["sfx"], self.settings.get("sfx_volume", 50))
|
||||
|
||||
def on_draw(self):
|
||||
super().on_draw()
|
||||
self.spritelist.draw()
|
||||
|
||||
def on_update(self, delta_time):
|
||||
self.info_label.text = f"Time left: {int(time.perf_counter() - self.start)}s Wire Rotations: {self.wire_rotations}"
|
||||
|
||||
def main_exit(self):
|
||||
from menus.main import Main
|
||||
import arcade, arcade.gui, json, time
|
||||
|
||||
from utils.constants import button_style, NEIGHBOURS
|
||||
from utils.preload import button_texture, button_hovered_texture
|
||||
|
||||
from collections import deque
|
||||
|
||||
from game.level_generator import generate_map
|
||||
from game.cells import *
|
||||
|
||||
class Game(arcade.gui.UIView):
|
||||
def __init__(self, pypresence_client, grid_size, source_count=None, house_count=None):
|
||||
super().__init__()
|
||||
|
||||
self.pypresence_client = pypresence_client
|
||||
self.pypresence_client.update(state='In Game', start=self.pypresence_client.start_time)
|
||||
|
||||
self.grid_size = grid_size
|
||||
self.source_count = source_count
|
||||
self.house_count = house_count
|
||||
|
||||
self.start = time.perf_counter()
|
||||
self.wire_rotations = 0
|
||||
self.cells = []
|
||||
self.power_sources = []
|
||||
self.houses = []
|
||||
|
||||
self.anchor = self.add_widget(arcade.gui.UIAnchorLayout(size_hint=(1, 1)))
|
||||
self.map = generate_map(self.grid_size, int((self.grid_size * self.grid_size) / 10) if not source_count else source_count, int((self.grid_size * self.grid_size) / 5) if not house_count else house_count)
|
||||
|
||||
self.spritelist = arcade.SpriteList()
|
||||
|
||||
with open("settings.json", "r") as file:
|
||||
self.settings = json.load(file)
|
||||
|
||||
def on_show_view(self):
|
||||
super().on_show_view()
|
||||
|
||||
self.back_button = arcade.gui.UITextureButton(texture=button_texture, texture_hovered=button_hovered_texture, text='<--', style=button_style, width=100, height=50)
|
||||
self.back_button.on_click = lambda event: self.main_exit()
|
||||
self.anchor.add(self.back_button, anchor_x="left", anchor_y="top", align_x=5, align_y=-5)
|
||||
|
||||
self.won_label = self.anchor.add(arcade.gui.UILabel(text="You won!", font_size=48), anchor_x="center", anchor_y="center")
|
||||
self.won_label.visible = False
|
||||
|
||||
self.info_label = self.anchor.add(arcade.gui.UILabel("Time spent: 0s Wire Rotations: 0", font_size=24), anchor_x="center", anchor_y="top")
|
||||
|
||||
x = (self.window.width / 2) - (self.grid_size * 64) / 2
|
||||
y = (self.window.height / 2) + (self.grid_size * 64) / 2
|
||||
|
||||
for row in range(self.grid_size):
|
||||
self.cells.append([])
|
||||
|
||||
for col in range(self.grid_size):
|
||||
left_neighbour = self.cells[row][col - 1] if col > 0 else None
|
||||
top_neighbour = self.cells[row - 1][col] if row > 0 else None
|
||||
|
||||
cell_type = self.map[row][col]
|
||||
|
||||
if cell_type in ["line", "corner", "t_junction", "cross"]:
|
||||
cell = PowerLine(cell_type, x, y, left_neighbour, top_neighbour)
|
||||
elif cell_type == "power_source":
|
||||
cell = PowerSource(x, y, left_neighbour, top_neighbour)
|
||||
self.power_sources.append(cell)
|
||||
elif cell_type == "house":
|
||||
cell = House(x, y, left_neighbour, top_neighbour)
|
||||
self.houses.append(cell)
|
||||
|
||||
self.spritelist.append(cell)
|
||||
self.cells[row].append(cell)
|
||||
|
||||
if left_neighbour:
|
||||
left_neighbour.right_neighbour = cell
|
||||
if top_neighbour:
|
||||
top_neighbour.bottom_neighbour = cell
|
||||
|
||||
x += 64
|
||||
x = (self.window.width / 2) - (self.grid_size * 64) / 2
|
||||
y -= 64
|
||||
|
||||
arcade.schedule(self.update_grid, 1 / 8)
|
||||
|
||||
def update_grid(self, _):
|
||||
for row in self.cells:
|
||||
for power_line in row:
|
||||
if power_line.cell_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.cells:
|
||||
for cell in row:
|
||||
cell.update_visual()
|
||||
|
||||
self.check_win()
|
||||
|
||||
def check_win(self):
|
||||
for row in self.cells:
|
||||
for cell in row:
|
||||
if cell.cell_type == "power_source":
|
||||
continue
|
||||
elif cell.cell_type == "house":
|
||||
if not len(cell.get_connected_neighbours()) >= 1:
|
||||
return
|
||||
else:
|
||||
continue
|
||||
|
||||
if len(cell.get_connected_neighbours(True)) != len(NEIGHBOURS[cell.rotation]):
|
||||
return
|
||||
|
||||
self.won_label.visible = True
|
||||
self.spritelist.visible = False
|
||||
|
||||
arcade.unschedule(self.update_grid)
|
||||
|
||||
def on_mouse_press(self, x, y, button, modifiers):
|
||||
for row in self.cells:
|
||||
for cell in row:
|
||||
if cell.cell_type in ["house", "power_source"]:
|
||||
continue
|
||||
|
||||
if cell.rect.point_in_rect((x, y)):
|
||||
self.wire_rotations += 1
|
||||
cell.next_rotation(self.settings["sfx"], self.settings.get("sfx_volume", 50))
|
||||
|
||||
def on_draw(self):
|
||||
super().on_draw()
|
||||
self.spritelist.draw()
|
||||
|
||||
def on_update(self, delta_time):
|
||||
self.info_label.text = f"Time left: {int(time.perf_counter() - self.start)}s Wire Rotations: {self.wire_rotations}"
|
||||
|
||||
def main_exit(self):
|
||||
from menus.main import Main
|
||||
self.window.show_view(Main(self.pypresence_client))
|
||||
Reference in New Issue
Block a user