Skip to content

Commit c863d30

Browse files
committed
GH-2: Improving user interface for selecting player type.
1 parent 800c3be commit c863d30

File tree

8 files changed

+52
-16
lines changed

8 files changed

+52
-16
lines changed
-5.52 KB
Binary file not shown.
-5.29 KB
Binary file not shown.
-5.52 KB
Binary file not shown.

assets/images/menu/add-human-slot.png

-5.29 KB
Binary file not shown.

assets/images/menu/computer-icon.png

141 Bytes
Loading

assets/images/menu/human-icon.png

195 Bytes
Loading

project/menus/newgame.py

+47-16
Original file line numberDiff line numberDiff line change
@@ -195,13 +195,9 @@ def __init__(self, max_amount, origin):
195195

196196
self.colour_manager = ColourPicker()
197197

198-
# pygame_gui.Button not designed to be moved once defined, inherited to SlotButton to allow for this.
199-
self.add_human_button = SlotButton(paths.uiMenuPath + "add-human-slot.png",
200-
paths.uiMenuPath + "add-human-slot-hover.png",
201-
self.origin[0], self.origin[1])
202-
self.add_computer_button = SlotButton(paths.uiMenuPath + "add-computer-slot.png",
203-
paths.uiMenuPath + "add-computer-slot-hover.png",
204-
self.origin[0] + 250, self.origin[1])
198+
# pygame_gui.Button not designed to be moved once defined, inherited to NewPlayerSlot to allow for this.
199+
self.add_human_button = NewPlayerSlot([self.origin[0], self.origin[1]], "human")
200+
self.add_computer_button = NewPlayerSlot([self.origin[0] + 260, self.origin[1]], "computer")
205201

206202
self.slot_size = [200, 30]
207203
self.slot_padding = 40
@@ -248,8 +244,7 @@ def refresh_slots(self):
248244
old_slots = self.players.copy()
249245
self.players = []
250246
for player_slot in old_slots: # allows slots to auto move up if an above slot is deleted.
251-
self.players.append(PlayerSlot(self, self.get_slot_bottom(),
252-
player_slot.colour, player_slot.name_entry.text.text))
247+
self.players.append(PlayerSlot(self, self.get_slot_bottom(), player_slot.colour, player_slot.control, player_slot.name_entry.text.text))
253248

254249
def handle_click(self):
255250
added = False
@@ -283,7 +278,7 @@ def draw(self, display):
283278
self.add_human_button.change_position(new_position)
284279
self.add_human_button.draw(display)
285280

286-
computer_slot_position = [new_position[0] + 250, new_position[1]]
281+
computer_slot_position = [new_position[0] + 260, new_position[1]]
287282
self.add_computer_button.change_position(computer_slot_position)
288283
self.add_computer_button.draw(display)
289284

@@ -309,6 +304,9 @@ def __init__(self, player_manager, origin, colour, control, name=""):
309304
self.delete_self = pygame_gui.Button(paths.uiPath + "cross.png", paths.uiPath + "cross-hover.png",
310305
self.origin[0]+430, self.origin[1]+8)
311306

307+
self.computer_player_indicator = pygame_gui.Image(paths.uiMenuPath + "computer-icon.png", self.origin[0] + 300, self.origin[1] + 10)
308+
self.human_player_indicator = pygame_gui.Image(paths.uiMenuPath + "human-icon.png", self.origin[0] + 300, self.origin[1] + 10)
309+
312310
def get_dict(self):
313311
return {
314312
"name": self.name_entry.get_text(),
@@ -341,7 +339,9 @@ def draw(self, display):
341339
pygame.draw.ellipse(display, constants.COLOURS[self.colour], [self.origin[0]+360, self.origin[1]+10, 28, 28])
342340

343341
if self.control == "computer":
344-
pygame.draw.ellipse(display, constants.COLOURS["white"], [self.origin[0] + 300, self.origin[1] + 10, 28, 28])
342+
self.computer_player_indicator.draw(display)
343+
else:
344+
self.human_player_indicator.draw(display)
345345

346346
self.delete_self.draw(display)
347347

@@ -422,9 +422,40 @@ def draw(self, display):
422422
self.forward_button.draw(display)
423423

424424

425-
class SlotButton(pygame_gui.Button):
426-
""" Specific to PlayerManager, button must change position depending on number of slots """
425+
class NewPlayerSlot:
426+
""" A individual game slot, seen on the screen. managed by FileSelector """
427+
def __init__(self, position, player_control):
428+
self.position = position
429+
self.player_control = player_control
430+
431+
# GUI Setup
432+
self.back_panel = pygame_gui.Panel([self.position[0], self.position[1], 240, 50],
433+
100,
434+
constants.COLOURS["panel"])
435+
self.back_panel_hover = pygame_gui.Panel([self.position[0], self.position[1], 240, 50],
436+
50,
437+
constants.COLOURS["panel"])
438+
439+
self.text = pygame_gui.Text(
440+
"Add " + player_control + " player",
441+
constants.FONTS["sizes"]["medium"], constants.FONTS["colour"], constants.FONTS["main"],
442+
self.position[0] + 50, self.position[1] + 15)
443+
444+
def check_clicked(self):
445+
return self.mouse_over()
446+
447+
def mouse_over(self):
448+
return self.back_panel.rect.collidepoint(pygame.mouse.get_pos())
449+
427450
def change_position(self, position):
428-
self.rect.topleft = position
429-
self.rest_image.rect.topleft = position
430-
self.hover_image.rect.topleft = position
451+
self.back_panel.rect.topleft = position
452+
self.back_panel_hover.rect.topleft = position
453+
self.text.change_position(position[0] + 50, position[1] + 15)
454+
455+
def draw(self, display):
456+
if self.mouse_over():
457+
self.back_panel_hover.draw(display)
458+
else:
459+
self.back_panel.draw(display)
460+
461+
self.text.draw(display)

pygame_gui/text.py

+5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ def _config_text(self):
2222
self.graphic_text = self.graphic_font.render(self.text, True, self.colour)
2323
self.rect = self.graphic_text.get_rect().move(self.x, self.y)
2424

25+
def change_position(self, x, y):
26+
self.x = x
27+
self.y = y
28+
self._config_text()
29+
2530
def get_rect(self):
2631
return self.rect
2732

0 commit comments

Comments
 (0)