Commit 13ad0fd2 authored by captnfab's avatar captnfab 🦃
Browse files

Begin code cleaning

- rnd in Gui (Closes #34)
- document GuiMenu (WIP #38)
- prepare for sprites (WIP #37)
- cleaner menu logic
parent b7f0abd7
......@@ -36,116 +36,106 @@ def cut_text(width, text):
class GuiMenu:
def __init__(self):
def __init__(self, rnd):
self.rnd = rnd
# Precompute background
splashscreen = pygame.image.load("gfx/splashscreen.jpg").convert()
title_font = pygame.font.Font(FONT, 20)
title = title_font.render(_("Pirate Battleship"), 1, WHITE)
splashscreen.blit(title, (30, 100))
self.bg = splashscreen
# Precompute menu
self.menu_entries = []
## First meny entry is either sart or resume game
if self.rnd.state.options.get('old_stage', None) is not None:
self.menu_entries.append((
_("Continue the game"), 'r', Stage.NEWGAME,
))
elif rnd.state.options.get('old_stage', None) is None:
self.menu_entries.append((
_("Start the game"), 'n', Stage.NEWGAME
))
self.menu_entries += [
( _("Manage options"), 'o', Stage.OPTIONS),
( _("See credits"), 'c', Stage.CREDITS),
( _("Quit"), 'q', Stage.QUIT),
]
## Reference point for menu
menu_topleft = (int(WIDTH*0.7), int(HEIGHT*0.7))
## Font face/size for menu entries
self.menu_font = pygame.font.Font(FONT, 20)
self.menu_items = []
self.menu_items.append((
self.menu_font.render(_("Start the game"), 1, ORANGE),
self.menu_font.render(_("Start the game"), 1, WHITE),
'n',
Stage.NEWGAME,
))
self.menu_items.append((
self.menu_font.render(_("Manage options"), 1, ORANGE),
self.menu_font.render(_("Manage options"), 1, WHITE),
'o',
Stage.OPTIONS,
))
self.menu_items.append((
self.menu_font.render(_("See credits"), 1, ORANGE),
self.menu_font.render(_("See credits"), 1, WHITE),
'c',
Stage.CREDITS,
))
self.menu_items.append((
self.menu_font.render(_("Quit"), 1, ORANGE),
self.menu_font.render(_("Quit"), 1, WHITE),
'q',
Stage.QUIT,
))
self.menu_topleft = (int(WIDTH*0.7), int(HEIGHT*0.7))
self.menu_rects = []
x,y = self.menu_topleft
for (n,(active, inactive, key, stage)) in enumerate(self.menu_items):
## Generate menu's sprites
self.menu_sprites = []
for n,(text, key, stage) in enumerate(self.menu_entries):
active = self.menu_font.render(text, 1, ORANGE)
inactive = self.menu_font.render(text, 1, WHITE)
rect = active.get_rect()
rect.topleft = (x,y)
self.menu_rects.append(rect)
y += rect.height + 10
x += 20
rect.topleft = menu_topleft
rect.left += n*20
rect.top += n*(10+rect.height)
self.reset()
self.menu_sprites.append((rect, inactive, active))
def reset(self):
# Initially select first entry
self.menu_index = 0
def update(self, rnd, params):
pass
def draw(self, s):
update_rects = []
# Draw background
s.blit(self.bg, (0, 0))
for (n, (active, inactive, key, stage)) in enumerate(self.menu_items):
# Draw menu
for (n, (rect, inactive, active)) in enumerate(self.menu_sprites):
if self.menu_index == n:
item = active
else:
item = inactive
s.blit(item, self.menu_rects[n])
update_rects.append(self.menu_rects[n])
return update_rects
s.blit(item, rect)
update_rects.append(rect)
def update(self, rnd, params):
if self.menu_items[0][2]=='n' and rnd.state.options.get('old_stage', None) is not None:
self.menu_items[0] = (
self.menu_font.render(_("Continue the game"), 1, ORANGE),
self.menu_font.render(_("Continue the game"), 1, WHITE),
'c',
Stage.NEWGAME,
)
elif self.menu_items[0][2]=='c' and rnd.state.options.get('old_stage', None) is None:
self.menu_items[0] = (
self.menu_font.render(_("Start the game"), 1, ORANGE),
self.menu_font.render(_("Start the game"), 1, WHITE),
'n',
Stage.NEWGAME,
)
return update_rects
def input(self, event, rnd):
if event.type == pygame.MOUSEBUTTONDOWN:
for (n, (active, inactive, key, stage)) in enumerate(self.menu_items):
if self.menu_rects[n].collidepoint(event.pos):
for n, ((rect,inactive,active), (text,key,stage)) in enumerate(zip(self.menu_sprites, self.menu_entries)):
if rect.collidepoint(event.pos):
rnd.state.options['choice'] = stage
rnd.state.options['needs_redraw'] = True
elif event.type == pygame.KEYDOWN:
for (active, inactive, k, stage) in self.menu_items:
if event.unicode == k:
rnd.state.options['choice'] = stage
for n, (text,key,stage) in enumerate(self.menu_entries):
if event.unicode == key:
rnd.state.options['choice'] = key
if event.key == pygame.K_DOWN:
self.menu_index += 1
if self.menu_index == len(self.menu_items):
self.menu_index = 0
self.menu_index %= len(self.menu_entries)
rnd.state.options['needs_redraw'] = True
elif event.key == pygame.K_UP:
self.menu_index -= 1
if self.menu_index == -1:
self.menu_index += len(self.menu_items)
self.menu_index += len(self.menu_entries)-1
self.menu_index %= len(self.menu_entries)
rnd.state.options['needs_redraw'] = True
elif event.key == pygame.K_RETURN:
rnd.state.options['choice'] = self.menu_items[self.menu_index][3]
rnd.state.options['choice'] = self.menu_entries[self.menu_index][2]
rnd.state.options['needs_redraw'] = True
elif event.type == pygame.MOUSEMOTION:
for i, r in enumerate(self.menu_rects):
if r.collidepoint(event.pos):
self.menu_index = i
for n, ((rect,inactive,active), (text,key,stage)) in enumerate(zip(self.menu_sprites, self.menu_entries)):
if rect.collidepoint(event.pos):
self.menu_index = n
rnd.state.options['needs_redraw'] = True
class GuiOptions:
def __init__(self):
def __init__(self, rnd):
options = pygame.image.load("gfx/options.jpg").convert()
self.bg = options
......@@ -358,7 +348,7 @@ class GuiOptions:
return
class GuiCredits:
def __init__(self):
def __init__(self, rnd):
credits = pygame.image.load("gfx/credits.jpg").convert()
self.quotes = [
......@@ -523,7 +513,7 @@ class GuiCredits:
rnd.state.options['needs_redraw'] = True
class GuiSelection:
def __init__(self):
def __init__(self, rnd):
bg = pygame.Surface((WIDTH, HEIGHT))
bg.fill(DARKGRAY)
pygame.draw.line(
......@@ -755,7 +745,8 @@ class GuiSetupShips:
return (y,x)
def __init__(self):
def __init__(self, rnd):
self.rnd = rnd
self.reset()
bg = pygame.Surface((WIDTH,HEIGHT))
......@@ -1198,7 +1189,7 @@ class GuiBattleShips:
return (y,x)
def __init__(self):
def __init__(self, rnd):
self.reset()
bg = pygame.Surface((WIDTH, HEIGHT))
......@@ -1632,7 +1623,7 @@ class GuiBattleShips:
rnd.state.switch(Stage.MENU, save=True)
class GuiGameOver:
def __init__(self):
def __init__(self, rnd):
self.bg = None
self.characters = [
......@@ -1870,19 +1861,19 @@ class PyGameUI:
if self.last_state != rnd.state.stage:
new_gui = None
if rnd.state.stage == Stage.MENU and self.last_state != Stage.MENU:
new_gui = GuiMenu()
new_gui = GuiMenu(rnd)
elif rnd.state.stage == Stage.CREDITS and self.last_state != Stage.CREDITS:
new_gui = GuiCredits()
new_gui = GuiCredits(rnd)
elif rnd.state.stage == Stage.OPTIONS and self.last_state != Stage.OPTIONS:
new_gui = GuiOptions()
new_gui = GuiOptions(rnd)
elif rnd.state.stage == Stage.CHARACTERS and self.last_state != Stage.CHARACTERS:
new_gui = GuiSelection()
new_gui = GuiSelection(rnd)
elif rnd.state.stage in [Stage.PLAYER_SHIPS,Stage.CPU_SHIPS] and self.last_state not in [Stage.PLAYER_SHIPS,Stage.CPU_SHIPS]:
new_gui = GuiSetupShips()
new_gui = GuiSetupShips(rnd)
elif rnd.state.stage in [Stage.PLAYER_TURN,Stage.CPU_TURN,Stage.NOTIFICATION] and self.last_state not in [Stage.PLAYER_TURN,Stage.CPU_TURN,Stage.NOTIFICATION]:
new_gui = GuiBattleShips()
new_gui = GuiBattleShips(rnd)
elif rnd.state.stage == Stage.GAMEOVER and self.last_state != Stage.GAMEOVER:
new_gui = GuiGameOver()
new_gui = GuiGameOver(rnd)
if new_gui is not None:
del self.gui
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment