mirror of
https://github.com/csd4ni3l/connect-the-current.git
synced 2026-01-01 04:13:41 +01:00
improve performance by using arcade Sprites instead of buttons, add win check and display winning, small fixes and improvements
This commit is contained in:
62
game/play.py
62
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
|
||||
|
||||
Reference in New Issue
Block a user