diff --git a/game/cell.py b/game/cell.py index f427d02..5e1dd5f 100644 --- a/game/cell.py +++ b/game/cell.py @@ -13,9 +13,9 @@ def get_opposite(direction): elif direction == "b": return "t" -class Cell(arcade.gui.UITextureButton): - def __init__(self, cell_type, left_neighbour, top_neighbour): - super().__init__(texture=TEXTURE_MAP[cell_type, ROTATIONS[cell_type][0] if cell_type in ROTATIONS else "cross", cell_type == "power_source"]) +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 @@ -33,21 +33,18 @@ class Cell(arcade.gui.UITextureButton): elif name == "t": return self.top_neighbour - def get_connected_neighbours(self): + 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 - self.get_neighbour(neighbour_direction).cell_type != "house" 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)] - self.texture_hovered = TEXTURE_MAP[(self.cell_type, self.rotation, self.powered)] - self.texture_pressed = TEXTURE_MAP[(self.cell_type, self.rotation, self.powered)] - self._requires_render = True def next_rotation(self): current_index = ROTATIONS[self.cell_type].index(self.rotation) diff --git a/game/cells.py b/game/cells.py index 08f2495..94cab49 100644 --- a/game/cells.py +++ b/game/cells.py @@ -1,17 +1,17 @@ from game.cell import Cell class House(Cell): - def __init__(self, left_neighbour, top_neighbour): - super().__init__("house", left_neighbour, top_neighbour) + def __init__(self, x, y, left_neighbour, top_neighbour): + super().__init__("house", x, y, left_neighbour, top_neighbour) class PowerSource(Cell): - def __init__(self, left_neighbour, top_neighbour): - super().__init__("power_source", left_neighbour, top_neighbour) + 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, left_neighbour, top_neighbour): - super().__init__(cell_type, left_neighbour, top_neighbour) + 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() \ No newline at end of file diff --git a/game/play.py b/game/play.py index f234bfc..be9632d 100644 --- a/game/play.py +++ b/game/play.py @@ -1,6 +1,6 @@ -import arcade, arcade.gui, random +import arcade, arcade.gui -from utils.constants import button_style +from utils.constants import button_style, NEIGHBOURS from utils.preload import button_texture, button_hovered_texture from collections import deque @@ -22,9 +22,9 @@ class Game(arcade.gui.UIView): self.anchor = self.add_widget(arcade.gui.UIAnchorLayout(size_hint=(1, 1))) self.grid_size = int(difficulty.split("x")[0]) - self.grid = generate_map(self.grid_size, int((self.grid_size * self.grid_size) / 10), int((self.grid_size * self.grid_size) / 5)) + self.map = generate_map(self.grid_size, int((self.grid_size * self.grid_size) / 10), int((self.grid_size * self.grid_size) / 5)) - self.power_grid = self.anchor.add(arcade.gui.UIGridLayout(horizontal_spacing=0, vertical_spacing=0, row_count=self.grid_size, column_count=self.grid_size)) + self.spritelist = arcade.SpriteList() def on_show_view(self): super().on_show_view() @@ -33,30 +33,41 @@ class Game(arcade.gui.UIView): 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 + + 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.grid[row][col] + cell_type = self.map[row][col] if cell_type in ["line", "corner", "t_junction", "cross"]: - cell = PowerLine(cell_type, left_neighbour, top_neighbour) + cell = PowerLine(cell_type, x, y, left_neighbour, top_neighbour) elif cell_type == "power_source": - cell = PowerSource(left_neighbour, top_neighbour) + cell = PowerSource(x, y, left_neighbour, top_neighbour) self.power_sources.append(cell) elif cell_type == "house": - cell = House(left_neighbour, top_neighbour) + cell = House(x, y, left_neighbour, top_neighbour) self.houses.append(cell) - self.power_grid.add(cell, row=row, column=col) + 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) @@ -87,6 +98,39 @@ class Game(arcade.gui.UIView): 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)): + cell.next_rotation() + + def on_draw(self): + super().on_draw() + self.spritelist.draw() def main_exit(self): from menus.main import Main diff --git a/utils/preload.py b/utils/preload.py index 84721a6..a7fd095 100644 --- a/utils/preload.py +++ b/utils/preload.py @@ -39,6 +39,6 @@ TEXTURE_MAP = { ("power_source", "cross", True): arcade.load_texture("assets/graphics/power_source.png"), - ("house", "cross", False): button_texture, - ("house", "cross", True): button_texture, + ("house", "cross", True): arcade.load_texture("assets/graphics/powered_lines/cross/cross.png"), + ("house", "cross", False): arcade.load_texture("assets/graphics/unpowered_lines/cross/cross.png"), }