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:
csd4ni3l
2025-10-14 11:17:15 +02:00
parent f4054fafa8
commit 2915d36763
4 changed files with 243 additions and 22 deletions

View File

@@ -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)