mirror of
https://github.com/csd4ni3l/logical-signals.git
synced 2025-11-05 05:58:09 +01:00
Add 25 levels, make Outputs None by default, add DIY mode, auto evaluate on changes, difficulty indication in level selector, fix task label overflowing
This commit is contained in:
49
game/play.py
49
game/play.py
@@ -20,7 +20,7 @@ class LogicalGate(arcade.gui.UIBoxLayout):
|
||||
self.input_add_button = self.add(arcade.gui.UIFlatButton(text="+", style=dropdown_style, height=30, width=30))
|
||||
self.input_add_button.visible = not self.gate_type == "INPUT"
|
||||
|
||||
self.gate_button = self.add(arcade.gui.UIFlatButton(text=f"{gate_type} ({self.value})", style=dropdown_style, height=30, width=100))
|
||||
self.gate_button = self.add(arcade.gui.UIFlatButton(text=f"{gate_type} ({self.value})", style=dropdown_style, height=30, width=120))
|
||||
|
||||
self.output_add_button = self.add(arcade.gui.UIFlatButton(text="+", style=dropdown_style, height=30, width=30))
|
||||
self.output_add_button.visible = not self.gate_type == "OUTPUT"
|
||||
@@ -29,10 +29,14 @@ class LogicalGate(arcade.gui.UIBoxLayout):
|
||||
self.output: LogicalGate | None = None
|
||||
|
||||
def calculate_value(self):
|
||||
if self.gate_type == "OUTPUT":
|
||||
if self.gate_type == "OUTPUT" and self.input:
|
||||
self.value = self.input[0].calculate_value()
|
||||
elif self.gate_type == "INPUT": # dont set INPUT to None
|
||||
pass
|
||||
elif len(self.input) == 2:
|
||||
self.value = int(LOGICAL_GATES[self.gate_type](self.input[0].calculate_value(), self.input[1].calculate_value())) # have to convert to int cause it might return boolean
|
||||
else:
|
||||
self.value = None
|
||||
|
||||
self.gate_button.text = f"{self.gate_type} ({self.value})"
|
||||
return self.value
|
||||
@@ -62,23 +66,25 @@ class Game(arcade.gui.UIView):
|
||||
self.anchor = self.add_widget(arcade.gui.UIAnchorLayout(size_hint=(1, 1)))
|
||||
self.tools_box = self.anchor.add(arcade.gui.UIBoxLayout(space_between=5), anchor_x="right", anchor_y="bottom", align_x=-5, align_y=20)
|
||||
|
||||
self.task_label = self.anchor.add(arcade.gui.UILabel(text=generate_task_text(LEVELS[level_num]), font_size=24), anchor_x="center", anchor_y="top")
|
||||
gate_names = list(LOGICAL_GATES.keys())
|
||||
|
||||
for gate in LOGICAL_GATES.keys():
|
||||
if not level_num == -1:
|
||||
self.task_label = self.anchor.add(arcade.gui.UILabel(text=generate_task_text(LEVELS[level_num]), font_size=20, multiline=True), anchor_x="center", anchor_y="top")
|
||||
for requirement in LEVELS[level_num]:
|
||||
if requirement[1] == "INPUT":
|
||||
for _ in range(requirement[0]):
|
||||
self.add_gate(random.randint(0, self.window.width - 300), random.randint(200, self.window.height - 100), "INPUT", requirement[2])
|
||||
elif requirement[1] == "OUTPUT":
|
||||
for _ in range(requirement[0]):
|
||||
self.add_gate(random.randint(0, self.window.width - 300), random.randint(200, self.window.height - 100), "OUTPUT")
|
||||
else:
|
||||
self.task_label = self.anchor.add(arcade.gui.UILabel(text="Task: Have fun! Do whatever you want!", font_size=20), anchor_x="center", anchor_y="top")
|
||||
gate_names.extend(["INPUT 0", "INPUT 1", "OUTPUT"])
|
||||
|
||||
for gate in gate_names:
|
||||
button = self.tools_box.add(arcade.gui.UIFlatButton(width=self.window.width * 0.1, height=self.window.height * 0.075, text=f"Create {gate} gate", style=dropdown_style))
|
||||
button.on_click = lambda event, gate=gate: self.add_gate(random.randint(0, self.window.width - 300), random.randint(200, self.window.height - 100), gate)
|
||||
|
||||
for requirement in LEVELS[level_num]:
|
||||
if requirement[1] == "INPUT":
|
||||
for _ in range(requirement[0]):
|
||||
self.add_gate(random.randint(0, self.window.width - 300), random.randint(200, self.window.height - 100), "INPUT", requirement[2])
|
||||
elif requirement[1] == "OUTPUT":
|
||||
for _ in range(requirement[0]):
|
||||
self.add_gate(random.randint(0, self.window.width - 300), random.randint(200, self.window.height - 100), "OUTPUT")
|
||||
|
||||
evaluate_button = self.tools_box.add(arcade.gui.UIFlatButton(width=self.window.width * 0.1, height=self.window.height * 0.075, text="Evaluate", style=dropdown_style))
|
||||
evaluate_button.on_click = lambda event: self.run_logic()
|
||||
|
||||
screenshot_button = self.tools_box.add(arcade.gui.UIFlatButton(width=self.window.width * 0.1, height=self.window.height * 0.075, text="Screenshot", style=dropdown_style))
|
||||
screenshot_button.on_click = lambda event: self.screenshot()
|
||||
|
||||
@@ -120,7 +126,7 @@ class Game(arcade.gui.UIView):
|
||||
else:
|
||||
hide_button.text = "Show"
|
||||
|
||||
def run_logic(self):
|
||||
def evaluate(self):
|
||||
for gate in self.gates:
|
||||
if not gate.output:
|
||||
gate.calculate_value()
|
||||
@@ -150,12 +156,23 @@ class Game(arcade.gui.UIView):
|
||||
self.selected_output = None
|
||||
self.selected_input = None
|
||||
|
||||
self.evaluate()
|
||||
|
||||
def add_gate(self, x, y, gate_type, value=None):
|
||||
if gate_type == "INPUT 0":
|
||||
gate_type = "INPUT"
|
||||
value = 0
|
||||
elif gate_type == "INPUT 1":
|
||||
gate_type = "INPUT"
|
||||
value = 1
|
||||
|
||||
self.gates.append(self.add_widget(LogicalGate(len(self.gates), x, y, gate_type, value)))
|
||||
|
||||
self.gates[-1].input_add_button.on_click = lambda e, gate_id=len(self.gates) - 1: self.select_input(gate_id)
|
||||
self.gates[-1].output_add_button.on_click = lambda e, gate_id=len(self.gates) - 1: self.select_output(gate_id)
|
||||
|
||||
self.evaluate()
|
||||
|
||||
def on_event(self, event):
|
||||
arcade.gui.UIManager.on_event(self.ui, event)
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ class LevelSelector(arcade.gui.UIView):
|
||||
self.pypresence_client.update(state="In Menus", details="Level Selector")
|
||||
|
||||
self.anchor = self.add_widget(arcade.gui.UIAnchorLayout(size_hint=(1, 1)))
|
||||
self.grid = self.anchor.add(arcade.gui.UIGridLayout(width=self.window.width / 2, height=self.window.height / 2, vertical_spacing=10, horizontal_spacing=10, column_count=5, row_count=ceil(len(LEVELS) / 5)), anchor_x="center", anchor_y="top", align_y=-self.window.height / 8)
|
||||
self.grid = self.anchor.add(arcade.gui.UIGridLayout(width=self.window.width / 2, height=self.window.height / 2, vertical_spacing=10, horizontal_spacing=10, column_count=5, row_count=ceil((len(LEVELS) + 1) / 5)), anchor_x="center", anchor_y="top", align_y=-self.window.height / 8)
|
||||
|
||||
def on_show_view(self):
|
||||
super().on_show_view()
|
||||
@@ -26,8 +26,23 @@ class LevelSelector(arcade.gui.UIView):
|
||||
|
||||
for n in range(len(LEVELS)):
|
||||
row, col = n // 5, n % 5
|
||||
level_button = self.grid.add(arcade.gui.UITextureButton(width=self.window.width / 8, height=self.window.height / 8, text=f"Level {n + 1}", texture=button_texture, texture_hovered=button_hovered_texture, style=button_style), row=row, column=col)
|
||||
|
||||
if n < 5:
|
||||
difficulty = "Easy"
|
||||
elif n < 15:
|
||||
difficulty = "Intermediate"
|
||||
elif n < 20:
|
||||
difficulty = "Hard"
|
||||
else:
|
||||
difficulty = "Extra Hard"
|
||||
|
||||
level_button = self.grid.add(arcade.gui.UITextureButton(width=self.window.width / 8, height=self.window.height / 8, text=f"{difficulty} Level {n + 1}", texture=button_texture, texture_hovered=button_hovered_texture, style=button_style), row=row, column=col)
|
||||
level_button.on_click = lambda event, n=n: self.play(n)
|
||||
|
||||
row, col = (n + 1) // 5, (n + 1) % 5
|
||||
|
||||
diy_button = self.grid.add(arcade.gui.UITextureButton(width=self.window.width / 8, height=self.window.height / 8, text=f"DIY", texture=button_texture, texture_hovered=button_hovered_texture, style=button_style), row=row, column=col)
|
||||
diy_button.on_click = lambda event: self.play(-1)
|
||||
|
||||
self.grid._trigger_size_hint_update()
|
||||
|
||||
|
||||
@@ -17,6 +17,8 @@ LOGICAL_GATES = {
|
||||
}
|
||||
|
||||
LEVELS = [
|
||||
# EASY
|
||||
|
||||
[
|
||||
[2, "INPUT", 1],
|
||||
[1, "AND"],
|
||||
@@ -25,14 +27,201 @@ LEVELS = [
|
||||
[
|
||||
[1, "INPUT", 1],
|
||||
[1, "INPUT", 0],
|
||||
[1, "AND"],
|
||||
[1, "OR"],
|
||||
[1, "OUTPUT", 0]
|
||||
],
|
||||
[
|
||||
[2, "INPUT", 1],
|
||||
[1, "NAND"],
|
||||
[1, "OUTPUT", 0]
|
||||
],
|
||||
[
|
||||
[1, "INPUT", 0],
|
||||
[1, "INPUT", 1],
|
||||
[1, "INPUT", 0],
|
||||
[1, "XOR"],
|
||||
[1, "OUTPUT", 1]
|
||||
],
|
||||
[
|
||||
[2, "INPUT", 1],
|
||||
[1, "INPUT", 0],
|
||||
[1, "AND"],
|
||||
[1, "OR"],
|
||||
[1, "OUTPUT", 1]
|
||||
],
|
||||
|
||||
# INTERMEDIATE
|
||||
|
||||
[
|
||||
[2, "INPUT", 0],
|
||||
[1, "INPUT", 1],
|
||||
[1, "NOR"],
|
||||
[1, "AND"],
|
||||
[1, "OUTPUT", 1]
|
||||
],
|
||||
[
|
||||
[2, "INPUT", 1],
|
||||
[1, "INPUT", 0],
|
||||
[1, "XNOR"],
|
||||
[1, "OR"],
|
||||
[1, "OUTPUT", 1]
|
||||
],
|
||||
[
|
||||
[2, "INPUT", 1],
|
||||
[2, "INPUT", 0],
|
||||
[1, "OR"],
|
||||
[1, "AND"],
|
||||
[1, "XOR"],
|
||||
[1, "OUTPUT", 0]
|
||||
],
|
||||
[
|
||||
[3, "INPUT", 1],
|
||||
[1, "INPUT", 0],
|
||||
[2, "NAND"],
|
||||
[1, "OR"],
|
||||
[1, "OUTPUT", 1]
|
||||
],
|
||||
[
|
||||
[4, "INPUT", 0],
|
||||
[2, "NOR"],
|
||||
[1, "AND"],
|
||||
[1, "OUTPUT", 1]
|
||||
],
|
||||
[
|
||||
[2, "INPUT", 1],
|
||||
[2, "INPUT", 0],
|
||||
[2, "XOR"],
|
||||
[1, "XNOR"],
|
||||
[1, "OUTPUT", 1]
|
||||
],
|
||||
[
|
||||
[3, "INPUT", 1],
|
||||
[1, "INPUT", 0],
|
||||
[1, "AND"],
|
||||
[1, "NAND"],
|
||||
[1, "OR"],
|
||||
[1, "OUTPUT", 1]
|
||||
],
|
||||
[
|
||||
[2, "INPUT", 0],
|
||||
[2, "INPUT", 1],
|
||||
[1, "NOR"],
|
||||
[1, "AND"],
|
||||
[1, "XOR"],
|
||||
[1, "OUTPUT", 1]
|
||||
],
|
||||
[
|
||||
[4, "INPUT", 0],
|
||||
[1, "INPUT", 1],
|
||||
[2, "OR"],
|
||||
[1, "NAND"],
|
||||
[1, "OUTPUT", 0]
|
||||
],
|
||||
[
|
||||
[2, "INPUT", 1],
|
||||
[2, "INPUT", 0],
|
||||
[1, "XNOR"],
|
||||
[1, "NOR"],
|
||||
[1, "AND"],
|
||||
[1, "OUTPUT", 0]
|
||||
],
|
||||
|
||||
# HARD
|
||||
|
||||
[
|
||||
[3, "INPUT", 1],
|
||||
[2, "INPUT", 0],
|
||||
[1, "AND"],
|
||||
[1, "OR"],
|
||||
[1, "NAND"],
|
||||
[1, "XOR"],
|
||||
[1, "OUTPUT", 1]
|
||||
],
|
||||
[
|
||||
[2, "INPUT", 1],
|
||||
[2, "INPUT", 0],
|
||||
[3, "NAND"],
|
||||
[1, "OR"],
|
||||
[1, "OUTPUT", 0]
|
||||
],
|
||||
[
|
||||
[4, "INPUT", 0],
|
||||
[2, "INPUT", 1],
|
||||
[2, "NOR"],
|
||||
[1, "XOR"],
|
||||
[1, "XNOR"],
|
||||
[1, "OUTPUT", 1]
|
||||
],
|
||||
[
|
||||
[3, "INPUT", 1],
|
||||
[3, "INPUT", 0],
|
||||
[2, "AND"],
|
||||
[1, "OR"],
|
||||
[1, "NAND"],
|
||||
[1, "XOR"],
|
||||
[1, "OUTPUT", 0]
|
||||
],
|
||||
[
|
||||
[4, "INPUT", 1],
|
||||
[2, "INPUT", 0],
|
||||
[2, "XOR"],
|
||||
[1, "NAND"],
|
||||
[1, "NOR"],
|
||||
[1, "XNOR"],
|
||||
[1, "OUTPUT", 1]
|
||||
],
|
||||
|
||||
# Extra Hard
|
||||
|
||||
[
|
||||
[2, "INPUT", 1],
|
||||
[2, "INPUT", 0],
|
||||
[1, "AND"],
|
||||
[1, "OR"],
|
||||
[1, "NAND"],
|
||||
[1, "XOR"],
|
||||
[1, "OUTPUT", 0],
|
||||
[1, "OUTPUT", 1]
|
||||
],
|
||||
[
|
||||
[3, "INPUT", 1],
|
||||
[3, "INPUT", 0],
|
||||
[2, "XOR"],
|
||||
[1, "AND"],
|
||||
[1, "NAND"],
|
||||
[1, "OR"],
|
||||
[1, "OUTPUT", 1],
|
||||
[1, "OUTPUT", 0]
|
||||
],
|
||||
[
|
||||
[4, "INPUT", 1],
|
||||
[2, "INPUT", 0],
|
||||
[2, "NOR"],
|
||||
[2, "XNOR"],
|
||||
[1, "AND"],
|
||||
[1, "OR"],
|
||||
[2, "OUTPUT", 1],
|
||||
],
|
||||
[
|
||||
[3, "INPUT", 1],
|
||||
[3, "INPUT", 0],
|
||||
[2, "NAND"],
|
||||
[2, "XOR"],
|
||||
[1, "NOR"],
|
||||
[1, "AND"],
|
||||
[1, "OUTPUT", 0],
|
||||
[1, "OUTPUT", 1]
|
||||
],
|
||||
[
|
||||
[4, "INPUT", 1],
|
||||
[4, "INPUT", 0],
|
||||
[2, "AND"],
|
||||
[2, "OR"],
|
||||
[1, "XOR"],
|
||||
[1, "NAND"],
|
||||
[1, "NOR"],
|
||||
[1, "XNOR"],
|
||||
[1, "OUTPUT", 1],
|
||||
[1, "OUTPUT", 0]
|
||||
]
|
||||
]
|
||||
|
||||
|
||||
@@ -7,8 +7,8 @@ import pyglet.display
|
||||
def generate_task_text(level):
|
||||
text = "Task: You need to use "
|
||||
|
||||
text += f"{', '.join([f'{requirement[0]} {requirement[1]} gate(s)' for requirement in level if not requirement[1] in ['INPUT', 'OUTPUT']])}"
|
||||
text += f" to result in {', '.join([f'{requirement[0]} OUTPUT gate(s) with value {requirement[2]}' for requirement in level if requirement[1] == 'OUTPUT'])}"
|
||||
text += ', '.join([f'{requirement[0]} {requirement[1]} gate(s)' for requirement in level if not requirement[1] in ['INPUT', 'OUTPUT']])
|
||||
text += f"\nResult: {', '.join([f'{requirement[0]} OUTPUT gate(s) with value {requirement[2]}' for requirement in level if requirement[1] == 'OUTPUT'])}"
|
||||
|
||||
return text
|
||||
|
||||
|
||||
Reference in New Issue
Block a user