all repos — Legends-RPG @ 7606c0c7c7335bda8b7801f1f9498f3f965723fd

A fantasy mini-RPG built with Python and Pygame.

Began working on a credits scene.
Justin Armstrong justinmeister@gmail.com
Tue, 17 Jun 2014 10:15:51 -0700
commit

7606c0c7c7335bda8b7801f1f9498f3f965723fd

parent

2927a342d54f5d95eda1568b0d7d7b31caa4a63b

2 files changed, 132 insertions(+), 2 deletions(-)

jump to
M data/constants.pydata/constants.py

@@ -1,5 +1,4 @@

-#Constants used throughout the game - +#Constants used throughout the game SCREEN_SIZE = (800, 608) ##GAME STATES

@@ -24,6 +23,7 @@ DUNGEON5 = 'dungeon5'

INSTRUCTIONS = 'instructions' DEATH_SCENE = 'death scene' LOADGAME = 'load game' +CREDITS = 'credits' ##Colors
A data/states/credits.py

@@ -0,0 +1,130 @@

+import pygame as pg +from .. import tools, setup +from .. import constants as c + + +class CreditEntry(object): + """ + The text for each credit for the game. + """ + def __init__(self): + self.font = pg.font.Font(setup.FONTS[c.MAIN_FONT], 22) + self.credit_sprites = self.make_credits() + self.index = 0 + self.current_credit = self.credit_sprites[self.index] + self.state_dict = self.make_state_dict() + self.state = c.TRANSITION_IN + self.alpha = 0 + self.timer = 0.0 + + def make_credits(self): + """ + Make a list of lists for all the credit surfaces. + """ + credits = [['PROGRAMMING AND GAME DESIGN', 'Justin Armstrong'], + ['ART', 'John Smith'], + ['MUSIC', 'John Smith'], + ['SPECIAL THANKS', '/r/pygame']] + + credit_sprites = [] + + for credit in credits: + subcredit_list = [] + for i, subcredit in enumerate(credit): + text_sprite = pg.sprite.Sprite() + text_sprite.image = self.font.render(subcredit, True, c.WHITE) + text_sprite.rect = text_sprite.image.get_rect(centerx = 400, + y=300+(i*50)) + subcredit_list.append(text_sprite) + credit_sprites.append(subcredit_list) + + return credit_sprites + + def make_state_dict(self): + """ + Make the dictionary of state methods used to update credit. + """ + state_dict = {c.TRANSITION_IN: self.transition_in, + c.TRANSITION_OUT: self.transition_out, + c.NORMAL: self.normal_update} + + return state_dict + + def transition_in(self): + for credit in self.current_credit: + credit.image.set_alpha(self.alpha) + + self.alpha += c.TRANSITION_SPEED + if self.alpha >= 255: + self.alpha = 255 + self.state = c.NORMAL + self.timer = self.current_time + + def transition_out(self): + for credit in self.current_credit: + credit.image.set_alpha(self.alpha) + self.alpha -= c.TRANSITION_SPEED + if self.alpha <= 0: + self.alpha = 0 + self.index += 1 + self.current_credit = self.credit_sprites[self.index] + self.state = c.TRANSITION_IN + + def normal_update(self): + if (self.current_time - self.timer) > 5000: + self.state = c.TRANSITION_OUT + + def update(self, surface, current_time, *args): + self.current_time = current_time + update_method = self.state_dict[self.state] + update() + + def draw(self, surface): + """ + Draw the current credit to main surface. + """ + for credit_sprite in self.current_credit: + surface.blit(credit_sprite.image, credit_sprite.rect) + + +class Credits(tools._state): + """ + End Credits Scene. + """ + def __init__(self): + super(Credits, self).__init__() + self.name = c.CREDITS + self.music_title = None + self.previous_music = None + self.music = None + self.volume = None + self.credit = None + + def startup(self, current_time, game_data): + """ + Initialize data at scene start. + """ + self.game_data = game_data + self.music = setup.MUSIC['overworld'] + self.volume = 0.4 + self.current_time = current_time + self.background = pg.Surface(setup.SCREEN_RECT.size) + self.background.fill(c.BLACKBLUE) + self.credit = CreditEntry() + + def update(self, surface, current_time, *args): + """ + Update scene. + """ + self.credit.update(current_time) + self.draw_scene(surface) + + def draw_scene(self, surface): + """ + Draw all graphics to the window surface. + """ + surface.blit(self.background, (0, 0)) + self.credit.draw(surface) + + +