Skip to content

Commit 1555056

Browse files
committed
Functional buttons and text input!
1 parent 209005e commit 1555056

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

src/interface/char_create.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
import pygame
88

9-
from ..constants import BKG_COLOR, CIRCLE_XY, FRG_COLOR, HALF_RES, INTERNAL_RES, GameStateID
10-
from .renderer import Button, TextRender
9+
from ..constants import BKG_COLOR, CIRCLE_XY, FRG_COLOR, HALF_RES, INTERNAL_RES, Difficulty, GameStateID
10+
from .renderer import Button, TextInput, TextRender
1111
from .state import State
1212

1313

@@ -19,6 +19,9 @@ def __init__(self, game) -> None:
1919
self.font: pygame.font.Font = game.font_sm
2020
super().__init__(game)
2121

22+
self._cmdr_name = "Jameson"
23+
self._cmdr_input = TextInput(55, 20, 80, self.font)
24+
self.sprite_group = pygame.sprite.Group(self._cmdr_input)
2225
self._curr_difficulty = 2
2326
self._curr_points = 16
2427
self._curr_pilot = 1
@@ -68,6 +71,25 @@ def handle_events(self, event: pygame.event) -> None:
6871
self.game.current_state = GameStateID.SPLASH
6972
if event.key == pygame.K_RETURN:
7073
self.game.current_state = GameStateID.SYSTEM_INFO
74+
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
75+
for index, button in enumerate(self._buttons):
76+
if button.is_clicked(event.pos) and index < 2:
77+
if button.text == "+":
78+
self._curr_difficulty += 1
79+
elif button.text == "-":
80+
self._curr_difficulty -= 1
81+
else:
82+
raise ValueError(f"Invalid button text! Expected '+' or '-' but got {button.text}")
83+
elif button.is_clicked(event.pos) and index >= 2:
84+
if button.text == "+":
85+
self._curr_points -= 1
86+
self._curr_values[index // 2] += 1
87+
elif button.text == "-":
88+
self._curr_points += 1
89+
self._curr_values[index // 2] -= 1
90+
else:
91+
raise ValueError(f"Invalid button text! Expected '+' or '-' but got {button.text}")
92+
self.sprite_group.update(event)
7193

7294
def update(self, actions) -> None:
7395
pass
@@ -114,6 +136,9 @@ def render(self, canvas: pygame.Surface) -> pygame.Surface:
114136
button.draw(canvas)
115137

116138
# Draw the current values
139+
self.sprite_group.draw(canvas)
140+
difficulty_text = TextRender(Difficulty.name(self._curr_difficulty), (HALF_RES, 40), self.font)
141+
difficulty_text.draw(canvas)
117142
for idx, value in enumerate(self._curr_values):
118143
value_text = TextRender(str(value), (HALF_RES, 65 + (idx * 14)), self.font)
119144
value_text.draw(canvas)

0 commit comments

Comments
 (0)