data/states/main_menu.py (view raw)
1import pygame as pg
2from .. import setup, tools, tilerender
3from .. import constants as c
4
5class Menu(tools._State):
6 def __init__(self):
7 super(Menu, self).__init__()
8 self.music = setup.MUSIC['kings_theme']
9 pg.mixer.music.load(self.music)
10 pg.mixer.music.set_volume(.4)
11 pg.mixer.music.play(-1)
12 self.next = c.OVERWORLD
13 self.tmx_map = setup.TMX['title']
14 self.renderer = tilerender.Renderer(self.tmx_map)
15 self.map_image = self.renderer.make_2x_map()
16 self.map_rect = self.map_image.get_rect()
17 self.viewport = self.make_viewport(self.map_image)
18 self.level_surface = pg.Surface(self.map_rect.size)
19 self.title_box = setup.GFX['title_box']
20 self.title_rect = self.title_box.get_rect()
21 self.title_rect.midbottom = self.viewport.midbottom
22 self.title_rect.y -= 30
23 self.game_data = tools.create_game_data_dict()
24 self.state_dict = self.make_state_dict()
25 self.name = c.MAIN_MENU
26 self.state = c.TRANSITION_IN
27 self.alpha = 255
28 self.transition_surface = pg.Surface(setup.SCREEN_RECT.size)
29 self.transition_surface.fill(c.BLACK_BLUE)
30 self.transition_surface.set_alpha(self.alpha)
31
32 def make_viewport(self, map_image):
33 """
34 Create the viewport to view the level through.
35 """
36 map_rect = map_image.get_rect()
37 return setup.SCREEN.get_rect(bottomright=map_rect.bottomright)
38
39 def make_state_dict(self):
40 """
41 Make the dictionary of state methods for the level.
42 """
43 state_dict = {c.TRANSITION_IN: self.transition_in,
44 c.TRANSITION_OUT: self.transition_out,
45 c.NORMAL: self.normal_update}
46
47 return state_dict
48
49 def update(self, surface, *args):
50 """
51 Update scene.
52 """
53 update_level = self.state_dict[self.state]
54 update_level()
55 self.draw_level(surface)
56
57 def draw_level(self, surface):
58 """
59 Blit tmx map and title box onto screen.
60 """
61 self.level_surface.blit(self.map_image, self.viewport, self.viewport)
62 self.level_surface.blit(self.title_box, self.title_rect)
63 surface.blit(self.level_surface, (0,0), self.viewport)
64 surface.blit(self.transition_surface, (0,0))
65
66 def get_event(self, event):
67 if event.type == pg.KEYDOWN:
68 self.state = c.TRANSITION_OUT
69
70 def transition_in(self):
71 """
72 Transition into scene with a fade.
73 """
74 self.transition_surface.set_alpha(self.alpha)
75 self.alpha -= c.TRANSITION_SPEED
76 if self.alpha <= 0:
77 self.alpha = 0
78 self.state = c.NORMAL
79
80
81 def transition_out(self):
82 """
83 Transition out of scene with a fade.
84 """
85 self.transition_surface.set_alpha(self.alpha)
86 self.alpha += c.TRANSITION_SPEED
87 if self.alpha >= 255:
88 self.game_data['last state'] = self.name
89 self.done = True
90
91 def normal_update(self):
92 pass