all repos — Legends-RPG @ 5bfef920882a049cc06fa2648364428c4e0f1507

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.game_data = tools.create_game_data_dict()
  9        self.music = setup.MUSIC['kings_theme']
 10        self.volume = 0.4
 11        self.next = c.INSTRUCTIONS
 12        self.tmx_map = setup.TMX['title']
 13        self.name = c.MAIN_MENU
 14        self.startup(0, 0)
 15    
 16    def startup(self, *args):
 17        self.renderer = tilerender.Renderer(self.tmx_map)
 18        self.map_image = self.renderer.make_2x_map()
 19        self.map_rect = self.map_image.get_rect()
 20        self.viewport = self.make_viewport(self.map_image)
 21        self.level_surface = pg.Surface(self.map_rect.size)
 22        self.title_box = setup.GFX['title_box']
 23        self.title_rect = self.title_box.get_rect()
 24        self.title_rect.midbottom = self.viewport.midbottom
 25        self.title_rect.y -= 30
 26        self.state_dict = self.make_state_dict()
 27        self.state = c.TRANSITION_IN
 28        self.alpha = 255
 29        self.transition_surface = pg.Surface(setup.SCREEN_RECT.size)
 30        self.transition_surface.fill(c.BLACK_BLUE)
 31        self.transition_surface.set_alpha(self.alpha)
 32
 33    def make_viewport(self, map_image):
 34        """
 35        Create the viewport to view the level through.
 36        """
 37        map_rect = map_image.get_rect()
 38        return setup.SCREEN.get_rect(bottomright=map_rect.bottomright)
 39
 40    def make_state_dict(self):
 41        """
 42        Make the dictionary of state methods for the level.
 43        """
 44        state_dict = {c.TRANSITION_IN: self.transition_in,
 45                      c.TRANSITION_OUT: self.transition_out,
 46                      c.NORMAL: self.normal_update}
 47
 48        return state_dict
 49        
 50    def update(self, surface, *args):
 51        """
 52        Update scene.
 53        """
 54        update_level = self.state_dict[self.state]
 55        update_level()
 56        self.draw_level(surface)
 57
 58    def draw_level(self, surface):
 59        """
 60        Blit tmx map and title box onto screen.
 61        """
 62        self.level_surface.blit(self.map_image, self.viewport, self.viewport)
 63        self.level_surface.blit(self.title_box, self.title_rect)
 64        surface.blit(self.level_surface, (0,0), self.viewport)
 65        surface.blit(self.transition_surface, (0,0))
 66        
 67    def get_event(self, event):
 68        if event.type == pg.KEYDOWN:
 69            self.state = c.TRANSITION_OUT
 70
 71    def transition_in(self):
 72        """
 73        Transition into scene with a fade.
 74        """
 75        self.transition_surface.set_alpha(self.alpha)
 76        self.alpha -= c.TRANSITION_SPEED
 77        if self.alpha <= 0:
 78            self.alpha = 0
 79            self.state = c.NORMAL
 80        
 81
 82    def transition_out(self):
 83        """
 84        Transition out of scene with a fade.
 85        """
 86        self.transition_surface.set_alpha(self.alpha)
 87        self.alpha += c.TRANSITION_SPEED
 88        if self.alpha >= 255:
 89            self.game_data['last state'] = self.name
 90            self.done = True
 91
 92    def normal_update(self):
 93        pass
 94
 95
 96class Instructions(tools._State):
 97    """
 98    Instructions page.
 99    """
100    def __init__(self):
101        super(Instructions, self).__init__()
102        self.next = c.OVERWORLD
103        self.tmx_map = setup.TMX['title']
104        self.music = None
105
106    def startup(self, *args):
107        self.renderer = tilerender.Renderer(self.tmx_map)
108        self.map_image = self.renderer.make_2x_map()
109        self.map_rect = self.map_image.get_rect()
110        self.viewport = self.make_viewport(self.map_image)
111        self.level_surface = pg.Surface(self.map_rect.size)
112        self.title_box = setup.GFX['instructions_box']
113        self.title_rect = self.title_box.get_rect()
114        self.title_rect.midbottom = self.viewport.midbottom
115        self.title_rect.y -= 30
116        self.game_data = tools.create_game_data_dict()
117        self.state_dict = self.make_state_dict()
118        self.name = c.MAIN_MENU
119        self.state = c.TRANSITION_IN
120        self.alpha = 255
121        self.transition_surface = pg.Surface(setup.SCREEN_RECT.size)
122        self.transition_surface.fill(c.BLACK_BLUE)
123        self.transition_surface.set_alpha(self.alpha)
124
125    def make_viewport(self, map_image):
126        """
127        Create the viewport to view the level through.
128        """
129        map_rect = map_image.get_rect()
130        return setup.SCREEN.get_rect(bottomright=map_rect.bottomright)
131
132    def make_state_dict(self):
133        """
134        Make the dictionary of state methods for the level.
135        """
136        state_dict = {c.TRANSITION_IN: self.transition_in,
137                      c.TRANSITION_OUT: self.transition_out,
138                      c.NORMAL: self.normal_update}
139
140        return state_dict
141        
142    def update(self, surface, *args):
143        """
144        Update scene.
145        """
146        update_level = self.state_dict[self.state]
147        update_level()
148        self.draw_level(surface)
149
150    def draw_level(self, surface):
151        """
152        Blit tmx map and title box onto screen.
153        """
154        self.level_surface.blit(self.map_image, self.viewport, self.viewport)
155        self.level_surface.blit(self.title_box, self.title_rect)
156        surface.blit(self.level_surface, (0,0), self.viewport)
157        surface.blit(self.transition_surface, (0,0))
158        
159    def get_event(self, event):
160        if event.type == pg.KEYDOWN:
161            self.state = c.TRANSITION_OUT
162
163    def transition_in(self):
164        """
165        Transition into scene with a fade.
166        """
167        self.transition_surface.set_alpha(self.alpha)
168        self.alpha -= c.TRANSITION_SPEED
169        if self.alpha <= 0:
170            self.alpha = 0
171            self.state = c.NORMAL
172        
173
174    def transition_out(self):
175        """
176        Transition out of scene with a fade.
177        """
178        self.transition_surface.set_alpha(self.alpha)
179        self.alpha += c.TRANSITION_SPEED
180        if self.alpha >= 255:
181            self.game_data['last state'] = self.name
182            self.done = True
183
184    def normal_update(self):
185        pass
186
187