all repos — Legends-RPG @ 2ded5c74f1c07582e76cc562f5d3027c49637745

A fantasy mini-RPG built with Python and Pygame.

Added instructions screen, set volume as an attribute of each level
Justin Armstrong justinmeister@gmail.com
Sat, 31 May 2014 10:14:20 -0700
commit

2ded5c74f1c07582e76cc562f5d3027c49637745

parent

a452513181f1eef80107430ce479b678dc2fa21c

M data/constants.pydata/constants.py

@@ -21,6 +21,7 @@ DUNGEON2 = 'dungeon2'

DUNGEON3 = 'dungeon3' DUNGEON4 = 'dungeon4' DUNGEON5 = 'dungeon5' +INSTRUCTIONS = 'instructions' ##Colors
M data/main.pydata/main.py

@@ -21,6 +21,7 @@ DUNGEON2 = 'dungeon2'

DUNGEON3 = 'dungeon3' DUNGEON4 = 'dungeon4' DUNGEON5 = 'dungeon5' +INSTRUCTIONS = 'instructions' def main():

@@ -42,7 +43,8 @@ DUNGEON: levels.LevelState(DUNGEON, True),

DUNGEON2: levels.LevelState(DUNGEON2, True), DUNGEON3: levels.LevelState(DUNGEON3, True), DUNGEON4: levels.LevelState(DUNGEON4, True), - DUNGEON5: levels.LevelState(DUNGEON5, True) + DUNGEON5: levels.LevelState(DUNGEON5, True), + INSTRUCTIONS: main_menu.Instructions() } run_it.setup_states(state_dict, c.MAIN_MENU)
M data/states/battle.pydata/states/battle.py

@@ -18,6 +18,7 @@ def __init__(self):

super(Battle, self).__init__() self.name = 'battle' self.music = setup.MUSIC['high_action'] + self.volume = 0.4 def startup(self, current_time, game_data): """Initialize state attributes"""
M data/states/levels.pydata/states/levels.py

@@ -26,28 +26,29 @@ super(LevelState, self).__init__()

self.name = name self.tmx_map = setup.TMX[name] self.allow_battles = battles - self.music = self.set_music() + self.music, self.volume = self.set_music() def set_music(self): """ Set music based on name. """ - music_dict = {c.TOWN: 'town_theme', - c.OVERWORLD: 'overworld', - c.CASTLE: 'kings_theme', - c.DUNGEON:'dungeon_theme', - c.DUNGEON2: 'dungeon_theme', - c.DUNGEON3: 'dungeon_theme', - c.DUNGEON4: 'dungeon_theme', - c.DUNGEON5: 'dungeon_theme', - c.HOUSE: 'pleasant_creek', - c.BROTHER_HOUSE: 'pleasant_creek'} + music_dict = {c.TOWN: ('town_theme', .4), + c.OVERWORLD: ('overworld', .4), + c.CASTLE: ('kings_theme', .4), + c.DUNGEON: ('dungeon_theme', .4), + c.DUNGEON2: ('dungeon_theme', .4), + c.DUNGEON3: ('dungeon_theme', .4), + c.DUNGEON4: ('dungeon_theme', .4), + c.DUNGEON5: ('dungeon_theme', .4), + c.HOUSE: ('pleasant_creek', .1), + c.BROTHER_HOUSE: ('pleasant_creek', .1)} if self.name in music_dict: - music = music_dict[self.name] - return setup.MUSIC[music] + music = music_dict[self.name][0] + volume = music_dict[self.name][1] + return setup.MUSIC[music], volume else: - return None + return None, None def startup(self, current_time, game_data): """
M data/states/main_menu.pydata/states/main_menu.py

@@ -9,7 +9,7 @@ self.music = setup.MUSIC['kings_theme']

pg.mixer.music.load(self.music) pg.mixer.music.set_volume(.4) pg.mixer.music.play(-1) - self.next = c.OVERWORLD + self.next = c.INSTRUCTIONS self.tmx_map = setup.TMX['title'] self.renderer = tilerender.Renderer(self.tmx_map) self.map_image = self.renderer.make_2x_map()

@@ -90,3 +90,97 @@ self.done = True

def normal_update(self): pass + + +class Instructions(tools._State): + """ + Instructions page. + """ + def __init__(self): + super(Instructions, self).__init__() + self.next = c.OVERWORLD + self.tmx_map = setup.TMX['title'] + self.music = None + + def startup(self, *args): + self.renderer = tilerender.Renderer(self.tmx_map) + self.map_image = self.renderer.make_2x_map() + self.map_rect = self.map_image.get_rect() + self.viewport = self.make_viewport(self.map_image) + self.level_surface = pg.Surface(self.map_rect.size) + self.title_box = setup.GFX['instructions_box'] + self.title_rect = self.title_box.get_rect() + self.title_rect.midbottom = self.viewport.midbottom + self.title_rect.y -= 30 + self.game_data = tools.create_game_data_dict() + self.state_dict = self.make_state_dict() + self.name = c.MAIN_MENU + self.state = c.TRANSITION_IN + self.alpha = 255 + self.transition_surface = pg.Surface(setup.SCREEN_RECT.size) + self.transition_surface.fill(c.BLACK_BLUE) + self.transition_surface.set_alpha(self.alpha) + + def make_viewport(self, map_image): + """ + Create the viewport to view the level through. + """ + map_rect = map_image.get_rect() + return setup.SCREEN.get_rect(bottomright=map_rect.bottomright) + + def make_state_dict(self): + """ + Make the dictionary of state methods for the level. + """ + state_dict = {c.TRANSITION_IN: self.transition_in, + c.TRANSITION_OUT: self.transition_out, + c.NORMAL: self.normal_update} + + return state_dict + + def update(self, surface, *args): + """ + Update scene. + """ + update_level = self.state_dict[self.state] + update_level() + self.draw_level(surface) + + def draw_level(self, surface): + """ + Blit tmx map and title box onto screen. + """ + self.level_surface.blit(self.map_image, self.viewport, self.viewport) + self.level_surface.blit(self.title_box, self.title_rect) + surface.blit(self.level_surface, (0,0), self.viewport) + surface.blit(self.transition_surface, (0,0)) + + def get_event(self, event): + if event.type == pg.KEYDOWN: + self.state = c.TRANSITION_OUT + + def transition_in(self): + """ + Transition into scene with a fade. + """ + self.transition_surface.set_alpha(self.alpha) + self.alpha -= c.TRANSITION_SPEED + if self.alpha <= 0: + self.alpha = 0 + self.state = c.NORMAL + + + def transition_out(self): + """ + Transition out of scene with a fade. + """ + self.transition_surface.set_alpha(self.alpha) + self.alpha += c.TRANSITION_SPEED + if self.alpha >= 255: + self.game_data['last state'] = self.name + self.done = True + + def normal_update(self): + pass + +
M data/states/shop.pydata/states/shop.py

@@ -19,6 +19,7 @@ super(Shop, self).__init__()

self.key = None self.sell_items = None self.music = setup.MUSIC['shop_theme'] + self.volume = 0.4 def startup(self, current_time, game_data): """Startup state"""
M data/tools.pydata/tools.py

@@ -50,10 +50,8 @@ Set music for the new state.

""" if self.state.music: pg.mixer.music.load(self.state.music) - pg.mixer.music.set_volume(.4) + pg.mixer.music.set_volume(self.state.volume) pg.mixer.music.play(-1) - else: - pg.mixer.music.stop() def event_loop(self): self.events = pg.event.get()