Commit c241578d authored by captnfab's avatar captnfab 🦃
Browse files

Continue code cleaning

- document GuiCredits (WIP #38)
- prepare for sprites (WIP #37)
- realign some texts
parent dd51820c
......@@ -155,6 +155,7 @@ class GuiOptions:
## Font face/size for menu entries
menu_font = pygame.font.Font(FONT, 20)
## Generate menu's sprites
self.menu_sprites = []
for n,(text, key, stage) in enumerate(self.menu_entries):
active = menu_font.render(text, 1, ORANGE)
......@@ -212,7 +213,7 @@ class GuiOptions:
]
## Reference point for options
options_topleft = (WIDTH//4, 150)
options_topleft = (WIDTH//4, 100)
## Font face/size for options menu entries
options_font = pygame.font.Font(FONT, 20)
......@@ -378,7 +379,10 @@ class GuiOptions:
class GuiCredits:
def __init__(self, rnd):
credits = pygame.image.load("gfx/credits.jpg").convert()
self.rnd = rnd
# Precompute background
self.bg = pygame.image.load("gfx/credits.jpg").convert()
self.quotes = [
{
......@@ -441,7 +445,7 @@ class GuiCredits:
font_url.set_italic(True)
font_licence = pygame.font.Font(FONT, 16)
topleft = (50, 70)
topleft = (300, 30)
for q in self.quotes:
descr = font_descr.render(q['descr'], 1, WHITE)
descr_r = descr.get_rect()
......@@ -462,83 +466,90 @@ class GuiCredits:
(x, y) = url_r.bottomright
licence_r.bottomleft = (x+15, y)
credits.blit(descr, descr_r)
credits.blit(name, name_r)
credits.blit(url, url_r)
credits.blit(licence, licence_r)
self.bg.blit(descr, descr_r)
self.bg.blit(name, name_r)
self.bg.blit(url, url_r)
self.bg.blit(licence, licence_r)
(x, y) = name_r.bottomleft
topleft = (x, y+25)
self.bg = credits
# Precompute menu
self.menu_entries = []
self.menu_entries.append(
(_("Menu"), 'm', Stage.MENU)
)
## Reference point for menu
menu_center = (WIDTH//2, 700)
## Font face/size for menu entries
menu_font = pygame.font.Font(FONT, 20)
self.menu_items = []
self.menu_items.append((
menu_font.render(_("Menu"), 1, ORANGE),
menu_font.render(_("Menu"), 1, WHITE),
'm',
Stage.MENU,
))
self.menu_center = (512, 700)
self.menu_rects = []
x,y = self.menu_center
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 = menu_font.render(text, 1, ORANGE)
inactive = menu_font.render(text, 1, WHITE)
rect = active.get_rect()
rect.center = (x,y)
self.menu_rects.append(rect)
y += rect.height + 10
x += 20
rect.center = menu_center
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))
update_rects.append(s.get_rect())
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):
pass
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:
for n, (text,key,stage) in enumerate(self.menu_entries):
if event.unicode == key:
rnd.state.options['choice'] = stage
rnd.state.options['needs_redraw'] = True
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 in [pygame.K_RETURN, pygame.K_ESCAPE]:
rnd.state.options['choice'] = self.menu_items[self.menu_index][3]
if self.menu_index is not None:
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 GuiSelection:
......
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