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

More cleaning

parent 6c2ce309
......@@ -482,7 +482,6 @@ class GuiCredits:
(_("Menu"), 'm', Stage.MENU)
)
## Reference point for menu
menu_center = (WIDTH//2, 700)
......@@ -786,27 +785,42 @@ class GuiSetupShips:
def __init__(self, rnd):
self.rnd = rnd
self.reset()
bg = pygame.Surface((WIDTH,HEIGHT))
bg.fill(BLUEGRAY)
self.show_help = False
self.tmp_ship = None
self.hlp_boat_num = None
self.tmp_ship = Ship(size=2, x=4, y=4, direction=Direction.SOUTH)
self.debug = False
self.just_toggled=False
pygame.draw.circle(bg, LIGHTBLUEGRAY, (int(WIDTH*0.05), int(HEIGHT*0.15)), int(WIDTH*0.13))
self.portrait_player = None
self.portrait_player_rect = None
# Precompute background
self.bg = pygame.Surface((WIDTH,HEIGHT))
self.bg.fill(BLUEGRAY)
self.characters = [
## Render player portrait
characters = [
("Ethel Bonny", "ethel-bonny"),
("Bill Wright", "bill-wright"),
("Greaves Black Beard", "greaves"),
]
pygame.draw.circle(self.bg, LIGHTBLUEGRAY, (int(WIDTH*0.05), int(HEIGHT*0.15)), int(WIDTH*0.13))
portrait_size = 240
self.font_player = pygame.font.Font(FONT, 22)
name,slug = characters[rnd.player.character]
portrait_player = pygame.image.load("gfx/portrait_"+slug+".png")
portrait_player = pygame.transform.smoothscale(portrait_player, (portrait_size, portrait_size))
portrait_player_rect = portrait_player.get_rect()
portrait_player_rect.center = (int(WIDTH*0.07), int(HEIGHT*0.18))
self.bg.blit(portrait_player, portrait_player_rect)
self._prepare_grid(bg)
font_player = pygame.font.Font(FONT, 22)
label_player = font_player.render(name, 1, WHITE)
self.bg.blit(label_player, (250, 10))
self.bg = bg
# Render grid
self._prepare_grid(self.bg)
# Precompute help pane
help_pane = pygame.Surface((WIDTH,HEIGHT)).convert_alpha()
help_pane.fill((0, 0, 0, 0))
self._prepare_help_pane(help_pane)
......@@ -817,22 +831,16 @@ class GuiSetupShips:
self.hlp_button_topright = (WIDTH-10, 10)
self.menu_items = []
self.menu_items.append((
hlp_button_active,
hlp_button_inactive,
'h',
None
))
self.menu_rects = []
# Precompute help menu
rect = hlp_button_active.get_rect()
rect.topright = self.hlp_button_topright
self.menu_rects.append(rect)
self.help_sprite = (rect, hlp_button_inactive, hlp_button_active)
self.help_entry = (None, 'h', None)
# Preload ships
self._prepare_ship_descr()
self.notif_rect = pygame.Rect((0, 450), (bg.get_width(), 50))
def _prepare_help_pane(self, s):
hlp_move = pygame.image.load("gfx/help_move-boat.png").convert_alpha()
......@@ -1004,14 +1012,6 @@ class GuiSetupShips:
if self.tmp_ship is not None and not tmp_drawn:
self._draw_ship(s, self.tmp_ship, board_topleft)
def reset(self):
self.show_help = False
self.tmp_ship = None
self.hlp_boat_num = None
self.tmp_ship = Ship(size=2, x=4, y=4, direction=Direction.SOUTH)
self.debug = False
self.just_toggled=False
def update(self, rnd, params):
if rnd.state.options.get('direction', None) is None:
rnd.state.options['direction'] = Direction.EAST
......@@ -1041,24 +1041,11 @@ class GuiSetupShips:
self.ships_descr[self.tmp_ship.size]['turntable'].update()
rnd.state.options['needs_redraw'] = True
if self.portrait_player is None:
name,slug = self.characters[rnd.player.character]
portrait_size = 240
self.portrait_player = pygame.image.load("gfx/portrait_"+slug+".png")
self.portrait_player = pygame.transform.smoothscale(self.portrait_player, (portrait_size, portrait_size))
self.portrait_player_rect = self.portrait_player.get_rect()
self.portrait_player_rect.center = (int(WIDTH*0.07), int(HEIGHT*0.18))
def draw(self, s, rnd, board):
update_rects = []
s.blit(self.bg, (0, 0))
s.blit(self.portrait_player, self.portrait_player_rect)
label_player = self.font_player.render(rnd.characters[board.character], 1, WHITE)
s.blit(label_player, (250, 10))
self._draw_board_objects(s, self.ocean_player_rect.topleft, board)
bigocean = self.ocean_player_rect.copy()
......@@ -1067,12 +1054,12 @@ class GuiSetupShips:
update_rects.append(bigocean)
if not self.show_help:
s.blit(self.menu_items[0][0], self.menu_rects[0])
s.blit(self.help_sprite[1], self.help_sprite[0])
update_rects+=self._draw_ship_descr(s)
else:
s.blit(self.help_pane, (0, 0))
s.blit(self.menu_items[0][1], self.menu_rects[0])
update_rects.append(self.menu_rects[0])
s.blit(self.help_sprite[2], self.help_sprite[0])
update_rects.append(self.help_sprite[0])
if self.just_toggled:
self.just_toggled=False
......@@ -1082,12 +1069,12 @@ class GuiSetupShips:
def input(self, event, rnd):
# help button
if event.type == pygame.MOUSEBUTTONDOWN and self.menu_rects[0].collidepoint(event.pos):
if event.type == pygame.MOUSEBUTTONDOWN and self.help_sprite[0].collidepoint(event.pos):
self.show_help = not self.show_help
self.just_toggled = True
rnd.state.options['needs_redraw'] = True
# help key
elif event.type == pygame.KEYDOWN and event.unicode == self.menu_items[0][2]:
elif event.type == pygame.KEYDOWN and event.unicode == self.help_entry[1]:
self.show_help = not self.show_help
self.just_toggled = True
rnd.state.options['needs_redraw'] = True
......@@ -1229,7 +1216,17 @@ class GuiBattleShips:
return (y,x)
def __init__(self, rnd):
self.reset()
self.rnd = rnd
self.event_anims = []
self.prev_player_hits = rnd.player.hits
self.prev_player_miss = rnd.player.miss
self.prev_cpu_hits = rnd.cpu.hits
self.prev_cpu_miss = rnd.cpu.miss
self.debug = False
self.draw_enemy_ships = False
self.aiming = 4,4
bg = pygame.Surface((WIDTH, HEIGHT))
bg.fill(BLUEGRAY)
......@@ -1290,8 +1287,6 @@ class GuiBattleShips:
self.labels_stats["sunk"] = self.font_stats.render(_("Sunk"), 1, WHITE)
self.notif_rect = pygame.Rect((0, 450), (bg.get_width(), 50))
def _prepare_ship_descr(self):
self.ships_descr = [None, None]
for name, size, descr in [
......@@ -1499,16 +1494,6 @@ class GuiBattleShips:
return update_rects
def reset(self):
self.event_anims = []
self.prev_player_hits = []
self.prev_player_miss = []
self.prev_cpu_hits = []
self.prev_cpu_miss = []
self.debug = False
self.draw_enemy_ships = False
self.aiming = 4,4
def update(self, rnd, params):
if rnd.state.options.get('x', None) is None:
rnd.state.options['x'] = self.aiming[0]
......@@ -1516,8 +1501,6 @@ class GuiBattleShips:
rnd.state.options['y'] = self.aiming[1]
self.aiming = (rnd.state.options['x'], rnd.state.options['y'])
self.draw_enemy_ships = False
#self.draw_enemy_ships = params['ai'] == AI.CHEAT
for (old, new, topleft, channel) in [
(self.prev_player_hits, rnd.player.hits, self.ocean_player_rect.topleft, 0),
(self.prev_cpu_hits, rnd.cpu.hits, self.ocean_cpu_rect.topleft, 1),
......@@ -1663,7 +1646,10 @@ class GuiBattleShips:
class GuiGameOver:
def __init__(self, rnd):
self.bg = None
self.rnd = rnd
if self.rnd.state.options.get('victory', None) is None:
self.rnd.state.options['victory'] = False
self.characters = [
("Ethel Bonny", "ethel-bonny"),
......@@ -1672,108 +1658,56 @@ class GuiGameOver:
#("Damien Monbars", "bill-wright"),
]
menu_font = pygame.font.Font(FONT, 20)
self.menu_items = []
self.menu_items.append((
menu_font.render(_("Continue"), 1, ORANGE),
menu_font.render(_("Continue"), 1, WHITE),
'm',
Stage.CREDITS,
))
self.menu_bottomright = (WIDTH - 30, HEIGHT - 30)
# Precompute background
if rnd.state.options.get('victory', False):
slug = self.characters[self.rnd.player.character][1]
else:
slug = self.characters[self.rnd.cpu.character][1]
self.bg = pygame.image.load("gfx/gameover_"+slug+".jpg").convert()
self.menu_rects = []
x,y = self.menu_bottomright
for (n,(active, inactive, key, stage)) in enumerate(self.menu_items):
rect = active.get_rect()
rect.bottomright = (x,y)
self.menu_rects.append(rect)
y += rect.height + 10
x += 20
## Print outcome
font_gameover = pygame.font.Font(FONT, 20)
self.labels = {}
self.labels["win"] = font_gameover.render(_("You have won."), 1, WHITE)
self.labels["lose"] = font_gameover.render(_("You have lost."), 1, WHITE)
self.font_score = pygame.font.Font(FONT, 12)
self.pos_score = (WIDTH//2, 598)
self.coins = pygame.image.load("gfx/winner_coins_light.png").convert_alpha()
self.reset()
def reset(self):
self.menu_index = 0
self.bg = None
self.old = False
def draw(self, s, rnd):
if rnd.state.options.get('victory', None) is None:
rnd.state.options['victory'] = False
if self.bg is None:
if rnd.state.options.get('victory', False):
slug = self.characters[rnd.player.character][1]
else:
slug = self.characters[rnd.cpu.character][1]
self.bg = pygame.image.load("gfx/gameover_"+slug+".jpg").convert()
outcome_center = (WIDTH//2, HEIGHT//2 + 145)
if self.rnd.state.options.get('victory', False):
outcome_label = font_gameover.render(_("You have won."), 1, WHITE)
else:
outcome_label = font_gameover.render(_("You have lost."), 1, WHITE)
outcome_rect = outcome_label.get_rect()
outcome_rect.center = outcome_center
self.bg.blit(outcome_label, outcome_rect)
update_rects = []
s.blit(self.bg, (0, 0))
update_rects.append(s.get_rect())
## Print score
pos_score = (WIDTH//2, 598)
font_score = pygame.font.Font(FONT, 12)
label_player = self.font_score.render(rnd.characters[rnd.player.character], 1, WHITE)
(x, y) = self.pos_score
x -= 150
y -= 20
### Print players labels
label_player = font_score.render(self.rnd.characters[self.rnd.player.character], 1, WHITE)
p_rect = label_player.get_rect()
p_rect.center = (x,y)
p_rect.center = pos_score[0]-150, pos_score[1]-20
label_enemy = self.font_score.render(rnd.characters[rnd.cpu.character], 1, WHITE)
(x, y) = self.pos_score
x += 150
y -= 20
label_enemy = font_score.render(self.rnd.characters[self.rnd.cpu.character], 1, WHITE)
c_rect = label_enemy.get_rect()
c_rect.center = (x,y)
s.blit(label_player, p_rect)
s.blit(label_enemy, c_rect)
c_rect.center = pos_score[0]+150, pos_score[1]-20
for (n, (active, inactive, key, stage)) in enumerate(self.menu_items):
if self.menu_index == n:
item = active
else:
item = inactive
s.blit(item, self.menu_rects[n])
update_rects.append(self.menu_rects[n])
self.bg.blit(label_player, p_rect)
self.bg.blit(label_enemy, c_rect)
outcome_center = (s.get_width()//2, s.get_height()//2 + 145)
### Print winner's coins
coins = pygame.image.load("gfx/winner_coins_light.png").convert_alpha()
coins_r = coins.get_rect()
if self.rnd.state.options.get('victory', False):
coins_r.midbottom = (pos_score[0] - 150, pos_score[1] - 15)
else:
coins_r.midbottom = (pos_score[0] + 150, pos_score[1] - 15)
self.bg.blit(coins, coins_r)
if rnd.state.options.get('victory', False):
self.labels["win"]
outcome_rect = self.labels["win"].get_rect()
outcome_rect.center = outcome_center
s.blit(self.labels["win"], outcome_rect)
update_rects.append(outcome_rect)
coins_r = self.coins.get_rect()
coins_r.midbottom = (self.pos_score[0] - 150, self.pos_score[1] - 15)
s.blit(self.coins, coins_r)
update_rects.append(coins_r)
elif not rnd.state.options.get('victory', True):
self.labels["lose"]
outcome_rect = self.labels["lose"].get_rect()
outcome_rect.center = outcome_center
s.blit(self.labels["lose"], outcome_rect)
update_rects.append(outcome_rect)
coins_r = self.coins.get_rect()
coins_r.midbottom = (self.pos_score[0] + 150, self.pos_score[1] - 15)
s.blit(self.coins, coins_r)
update_rects.append(coins_r)
for (board, midtop) in [(rnd.cpu, (self.pos_score[0] - 150, self.pos_score[1] + 5)), (rnd.player, (self.pos_score[0] + 150, self.pos_score[1] + 5))]:
### Print stats
for (board, midtop) in [
(self.rnd.cpu, (pos_score[0] - 150, pos_score[1] + 5)),
(self.rnd.player, (pos_score[0] + 150, pos_score[1] + 5))
]:
(x, y) = midtop
for score in [
str(len(board.hits) + len(board.miss)) + " " + _("Shots"),
......@@ -1781,29 +1715,66 @@ class GuiGameOver:
str(len(board.miss)) + " " + _("Miss"),
str(len(board.sunk)) + " " + _("Sunk"),
]:
label = self.font_score.render(score, 1, WHITE)
label = font_score.render(score, 1, WHITE)
rect = label.get_rect()
rect.midtop = (x, y+5)
(x, y) = rect.midbottom
s.blit(label, rect)
update_rects.append(rect)
self.bg.blit(label, rect)
return update_rects
# Precompute menu
self.menu_entries = []
self.menu_entries.append(
(_("Continue"), 'c', Stage.CREDITS)
)
## Reference point for menu
menu_bottomright = (WIDTH - 30, HEIGHT - 30)
## 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)
inactive = menu_font.render(text, 1, WHITE)
rect = active.get_rect()
rect.bottomright = menu_bottomright
self.menu_sprites.append((rect, inactive, active))
# Initially select first entry
self.menu_index = 0
def update(self, rnd, params):
if self.old:
self.reset()
pass
def draw(self, s, rnd):
update_rects = []
# Draw background
s.blit(self.bg, (0, 0))
# 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, rect)
update_rects.append(rect)
return update_rects
def input(self, event, rnd):
if event.type == pygame.MOUSEBUTTONDOWN:
if self.menu_rects[0].collidepoint(event.pos):
rnd.state.options['skip'] = True
self.reset()
self.old = True
elif event.type == pygame.KEYDOWN:
if event.key in [pygame.K_RETURN, pygame.K_ESCAPE]:
rnd.state.options['skip'] = True
self.old = True
class PyGameUI:
def __init__(self):
......@@ -1838,20 +1809,25 @@ class PyGameUI:
self.gui = None
# Precompute notifications
self.font_notifs = pygame.font.Font(FONT, 18)
self.labels_notifs = {}
self.labels_notifs["CPU hit you."] = self.font_notifs.render(_("CPU hit you."), 1, WHITE)
self.labels_notifs["CPU missed you."] = self.font_notifs.render(_("CPU missed you."), 1, WHITE)
self.labels_notifs["CPU sunk you."] = self.font_notifs.render(_("CPU sunk you."), 1, WHITE)
self.labels_notifs["You hit CPU."] = self.font_notifs.render(_("You hit CPU."), 1, WHITE)
self.labels_notifs["You missed CPU."] = self.font_notifs.render(_("You missed CPU."), 1, WHITE)
self.labels_notifs["You sunk CPU."] = self.font_notifs.render(_("You sunk CPU."), 1, WHITE)
self.labels_notifs["Already shot there."] = self.font_notifs.render(_("Already shot there."), 1, WHITE)
for (msg,ctx) in [
("CPU hit you.",_("CPU hit you.")),
("CPU missed you.",_("CPU missed you.")),
("CPU sunk you.",_("CPU sunk you.")),
("You hit CPU.",_("You hit CPU.")),
("You missed CPU.",_("You missed CPU.")),
("You sunk CPU.",_("You sunk CPU.")),
("Already shot there.",_("Already shot there.")),
("CPU starts.",_("CPU starts.")),
("You start.",_("You start.")),
]:
self.labels_notifs[msg] = self.font_notifs.render(ctx, 1, WHITE)
self.labels_notifs["CPU starts."] = self.font_notifs.render(_("CPU starts."), 1, WHITE)
self.labels_notifs["You start."] = self.font_notifs.render(_("You start."), 1, WHITE)
maxw,maxh = max([l.get_width() for (k,l) in self.labels_notifs.items()]), max([l.get_height() for (k,l) in self.labels_notifs.items()])
self.labels_rect = pygame.Rect((0,0), (maxw, maxh))
self.labels_rect.center = (WIDTH//2, int(HEIGHT*0.85))
pygame.mixer.music.load("music/scottbuckley_titan.ogg")
pygame.mixer.music.set_volume(0.25)
......@@ -1865,12 +1841,9 @@ class PyGameUI:
def draw_notification(self, s, message):
notif_msg = self.labels_notifs[message]
notif_rect = notif_msg.get_rect()
notif_rect.center = (WIDTH//2, int(HEIGHT*0.8))
notif_rect.center = self.labels_rect.center
s.blit(notif_msg, notif_rect)
redraw_rect = notif_rect
redraw_rect.left = 0
redraw_rect.right = s.get_width()
return [redraw_rect]
return [self.labels_rect]
def update(self, rnd):
bgsound_param = rnd.params.get('music', None)
......
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