all repos — Legends-RPG @ 680235e09a98538dcc6f7165b3f8186889ed6ba5

A fantasy mini-RPG built with Python and Pygame.

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.INSTRUCTIONS
 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
 93
 94
 95class Instructions(tools._State):
 96    """
 97    Instructions page.
 98    """
 99    def __init__(self):
100        super(Instructions, self).__init__()
101        self.next = c.OVERWORLD
102        self.tmx_map = setup.TMX['title']
103        self.music = None
104
105    def startup(self, *args):
106        self.renderer = tilerender.Renderer(self.tmx_map)
107        self.map_image = self.renderer.make_2x_map()
108        self.map_rect = self.map_image.get_rect()
109        self.viewport = self.make_viewport(self.map_image)
110        self.level_surface = pg.Surface(self.map_rect.size)
111        self.title_box = setup.GFX['instructions_box']
112        self.title_rect = self.title_box.get_rect()
113        self.title_rect.midbottom = self.viewport.midbottom
114        self.title_rect.y -= 30
115        self.game_data = tools.create_game_data_dict()
116        self.state_dict = self.make_state_dict()
117        self.name = c.MAIN_MENU
118        self.state = c.TRANSITION_IN
119        self.alpha = 255
120        self.transition_surface = pg.Surface(setup.SCREEN_RECT.size)
121        self.transition_surface.fill(c.BLACK_BLUE)
122        self.transition_surface.set_alpha(self.alpha)
123
124    def make_viewport(self, map_image):
125        """
126        Create the viewport to view the level through.
127        """
128        map_rect = map_image.get_rect()
129        return setup.SCREEN.get_rect(bottomright=map_rect.bottomright)
130
131    def make_state_dict(self):
132        """
133        Make the dictionary of state methods for the level.
134        """
135        state_dict = {c.TRANSITION_IN: self.transition_in,
136                      c.TRANSITION_OUT: self.transition_out,
137                      c.NORMAL: self.normal_update}
138
139        return state_dict
140        
141    def update(self, surface, *args):
142        """
143        Update scene.
144        """
145        update_level = self.state_dict[self.state]
146        update_level()
147        self.draw_level(surface)
148
149    def draw_level(self, surface):
150        """
151        Blit tmx map and title box onto screen.
152        """
153        self.level_surface.blit(self.map_image, self.viewport, self.viewport)
154        self.level_surface.blit(self.title_box, self.title_rect)
155        surface.blit(self.level_surface, (0,0), self.viewport)
156        surface.blit(self.transition_surface, (0,0))
157        
158    def get_event(self, event):
159        if event.type == pg.KEYDOWN:
160            self.state = c.TRANSITION_OUT
161
162    def transition_in(self):
163        """
164        Transition into scene with a fade.
165        """
166        self.transition_surface.set_alpha(self.alpha)
167        self.alpha -= c.TRANSITION_SPEED
168        if self.alpha <= 0:
169            self.alpha = 0
170            self.state = c.NORMAL
171        
172
173    def transition_out(self):
174        """
175        Transition out of scene with a fade.
176        """
177        self.transition_surface.set_alpha(self.alpha)
178        self.alpha += c.TRANSITION_SPEED
179        if self.alpha >= 255:
180            self.game_data['last state'] = self.name
181            self.done = True
182
183    def normal_update(self):
184        pass
185
186