improve performance by using arcade Sprites instead of buttons, add win check and display winning, small fixes and improvements

This commit is contained in:
2025-11-08 15:48:24 +01:00
parent 96ebdef4e3
commit 5973d1143f
4 changed files with 66 additions and 25 deletions

View File

@@ -13,9 +13,9 @@ def get_opposite(direction):
elif direction == "b": elif direction == "b":
return "t" return "t"
class Cell(arcade.gui.UITextureButton): class Cell(arcade.Sprite):
def __init__(self, cell_type, left_neighbour, top_neighbour): def __init__(self, cell_type, x, y, 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"]) 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.rotation = ROTATIONS[cell_type][0] if cell_type in ROTATIONS else "cross"
self.cell_type = cell_type self.cell_type = cell_type
@@ -33,21 +33,18 @@ class Cell(arcade.gui.UITextureButton):
elif name == "t": elif name == "t":
return self.top_neighbour return self.top_neighbour
def get_connected_neighbours(self): def get_connected_neighbours(self, include_houses=False):
return [ return [
self.get_neighbour(neighbour_direction) for neighbour_direction in NEIGHBOURS[self.rotation] self.get_neighbour(neighbour_direction) for neighbour_direction in NEIGHBOURS[self.rotation]
if ( if (
self.get_neighbour(neighbour_direction) and 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] get_opposite(neighbour_direction) in NEIGHBOURS[self.get_neighbour(neighbour_direction).rotation]
) )
] ]
def update_visual(self): def update_visual(self):
self.texture = TEXTURE_MAP[(self.cell_type, self.rotation, self.powered)] 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): def next_rotation(self):
current_index = ROTATIONS[self.cell_type].index(self.rotation) current_index = ROTATIONS[self.cell_type].index(self.rotation)

View File

@@ -1,17 +1,17 @@
from game.cell import Cell from game.cell import Cell
class House(Cell): class House(Cell):
def __init__(self, left_neighbour, top_neighbour): def __init__(self, x, y, left_neighbour, top_neighbour):
super().__init__("house", left_neighbour, top_neighbour) super().__init__("house", x, y, left_neighbour, top_neighbour)
class PowerSource(Cell): class PowerSource(Cell):
def __init__(self, left_neighbour, top_neighbour): def __init__(self, x, y, left_neighbour, top_neighbour):
super().__init__("power_source", left_neighbour, top_neighbour) super().__init__("power_source", x, y, left_neighbour, top_neighbour)
self.on_click = lambda e: self.next_rotation() self.on_click = lambda e: self.next_rotation()
class PowerLine(Cell): class PowerLine(Cell):
def __init__(self, cell_type, left_neighbour, top_neighbour): def __init__(self, cell_type, x, y, left_neighbour, top_neighbour):
super().__init__(cell_type, left_neighbour, top_neighbour) super().__init__(cell_type, x, y, left_neighbour, top_neighbour)
if not cell_type == "cross": if not cell_type == "cross":
self.on_click = lambda e: self.next_rotation() self.on_click = lambda e: self.next_rotation()

View File

@@ -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 utils.preload import button_texture, button_hovered_texture
from collections import deque 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.anchor = self.add_widget(arcade.gui.UIAnchorLayout(size_hint=(1, 1)))
self.grid_size = int(difficulty.split("x")[0]) 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): def on_show_view(self):
super().on_show_view() super().on_show_view()
@@ -33,24 +33,31 @@ class Game(arcade.gui.UIView):
self.back_button.on_click = lambda event: self.main_exit() 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.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): for row in range(self.grid_size):
self.cells.append([]) self.cells.append([])
for col in range(self.grid_size): for col in range(self.grid_size):
left_neighbour = self.cells[row][col - 1] if col > 0 else None left_neighbour = self.cells[row][col - 1] if col > 0 else None
top_neighbour = self.cells[row - 1][col] if row > 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"]: 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": elif cell_type == "power_source":
cell = PowerSource(left_neighbour, top_neighbour) cell = PowerSource(x, y, left_neighbour, top_neighbour)
self.power_sources.append(cell) self.power_sources.append(cell)
elif cell_type == "house": elif cell_type == "house":
cell = House(left_neighbour, top_neighbour) cell = House(x, y, left_neighbour, top_neighbour)
self.houses.append(cell) self.houses.append(cell)
self.power_grid.add(cell, row=row, column=col) self.spritelist.append(cell)
self.cells[row].append(cell) self.cells[row].append(cell)
if left_neighbour: if left_neighbour:
@@ -58,6 +65,10 @@ class Game(arcade.gui.UIView):
if top_neighbour: if top_neighbour:
top_neighbour.bottom_neighbour = cell 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) arcade.schedule(self.update_grid, 1 / 8)
def update_grid(self, _): def update_grid(self, _):
@@ -87,6 +98,39 @@ class Game(arcade.gui.UIView):
for cell in row: for cell in row:
cell.update_visual() 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): def main_exit(self):
from menus.main import Main from menus.main import Main

View File

@@ -39,6 +39,6 @@ TEXTURE_MAP = {
("power_source", "cross", True): arcade.load_texture("assets/graphics/power_source.png"), ("power_source", "cross", True): arcade.load_texture("assets/graphics/power_source.png"),
("house", "cross", False): button_texture, ("house", "cross", True): arcade.load_texture("assets/graphics/powered_lines/cross/cross.png"),
("house", "cross", True): button_texture, ("house", "cross", False): arcade.load_texture("assets/graphics/unpowered_lines/cross/cross.png"),
} }