all repos — Legends-RPG @ 0802f998231bfac166df0e8174a45a00e71776da

A fantasy mini-RPG built with Python and Pygame.

data/states/credits.py (view raw)

  1import copy
  2import pygame as pg
  3from .. import tools, setup
  4from .. import constants as c
  5
  6
  7class CreditEntry(object):
  8    """
  9    The text for each credit for the game.
 10    """
 11    def __init__(self, level):
 12        self.alpha = 0
 13        self.font = pg.font.Font(setup.FONTS[c.MAIN_FONT], 22)
 14        self.credit_sprites = self.make_credits()
 15        self.index = 0
 16        self.current_credit = self.credit_sprites[self.index]
 17        self.state_dict = self.make_state_dict()
 18        self.state = c.TRANSITION_IN
 19        self.timer = 0.0
 20        self.level = level
 21
 22    def make_credits(self):
 23        """
 24        Make a list of lists for all the credit surfaces.
 25        """
 26        credits = [['THE STOLEN CROWN', 'A Fantasy RPG'],
 27                   ['PROGRAMMING AND GAME DESIGN', 'Justin Armstrong'],
 28                   ['ART', 'JPhilipp', 
 29                           'Reemax', 
 30                           'Lanea Zimmerman', 
 31                           'Redshrike', 
 32                           'StumpyStrust', 
 33                           'Benjamin Larsson', 
 34                           'russpuppy', 
 35                           'hc',
 36                           'Iron Star Media'],
 37                   ['MUSIC', 'Telaron: The King\'s Theme', 
 38                             'Mekathratos: Forest Dance (Town Theme)', 
 39                             'bart: Adventure Begins (Overworld Theme)', 
 40                             '8th Mode Music: High Action (Battle Theme)', 
 41                             'Arron Krogh: Coastal Town (Shop Theme)', 
 42                             'Arron Krogh: My Enemy (Dungeon Theme)', 
 43                             'Matthew Pablo: Enchanted Festival (Victory Theme)', 
 44                             'Matthew Pablo: Pleasant Creek (Brother Theme)'],
 45                   ['SOUND EFFECTS', 'Kenney',
 46                                     'Nic3_one',
 47                                     'Ekokubza123',
 48                                     'kuzyaburst',
 49                                     'audione'],
 50                   ['SPECIAL THANKS', '/r/pygame', 
 51                                      'Leif Theden', 
 52                                      'Stacey Hunniford']]
 53        
 54        credit_sprites = []
 55
 56        for credit in credits:
 57            subcredit_list = []
 58            for i, subcredit in enumerate(credit):
 59                text_sprite = pg.sprite.Sprite()
 60                text_sprite.text_image = self.font.render(subcredit, True, c.WHITE)
 61                text_sprite.rect = text_sprite.text_image.get_rect(centerx = 400,
 62                                                                   y=100+(i*40))
 63                text_sprite.image = pg.Surface(text_sprite.rect.size).convert()
 64                text_sprite.image.set_colorkey(c.BLACK)
 65                text_sprite.image.set_alpha(self.alpha)
 66                subcredit_list.append(text_sprite)
 67            credit_sprites.append(subcredit_list)
 68        
 69        return credit_sprites
 70
 71    def make_state_dict(self):
 72        """
 73        Make the dictionary of state methods used to update credit.
 74        """
 75        state_dict = {c.TRANSITION_IN: self.transition_in, 
 76                      c.TRANSITION_OUT: self.transition_out,
 77                      c.NORMAL: self.normal_update}
 78
 79        return state_dict
 80
 81    def transition_in(self):
 82        for credit in self.current_credit:
 83            credit.image = pg.Surface(credit.rect.size).convert()
 84            credit.image.set_colorkey(c.BLACK)
 85            credit.image.set_alpha(self.alpha)
 86            credit.image.blit(credit.text_image, (0, 0))
 87
 88        self.alpha += 5
 89        if self.alpha >= 255:
 90            self.alpha = 255
 91            self.state = c.NORMAL
 92            self.timer = self.current_time
 93
 94    def transition_out(self):
 95        for credit in self.current_credit:
 96            credit.image = pg.Surface(credit.rect.size).convert()
 97            credit.image.set_colorkey(c.BLACK)
 98            credit.image.set_alpha(self.alpha)
 99            credit.image.blit(credit.text_image, (0, 0))
100           
101        self.alpha -= 5
102        if self.alpha <= 0:
103            self.alpha = 0
104            if self.index < len(self.credit_sprites) - 1:
105                self.index += 1
106            else:
107                self.level.done = True
108                self.level.next = c.MAIN_MENU
109            self.current_credit = self.credit_sprites[self.index]
110            self.state = c.TRANSITION_IN
111
112    def normal_update(self):
113        if (self.current_time - self.timer) > 4500:
114            self.state = c.TRANSITION_OUT
115
116    def update(self, current_time):
117        self.current_time = current_time
118        update_method = self.state_dict[self.state]
119        update_method()
120
121    def draw(self, surface):
122        """
123        Draw the current credit to main surface.
124        """
125        for credit_sprite in self.current_credit:
126            surface.blit(credit_sprite.image, credit_sprite.rect)
127
128
129class Credits(tools._State):
130    """
131    End Credits Scene.
132    """
133    def __init__(self):
134        super(Credits, self).__init__()
135        self.name = c.CREDITS
136        self.music_title = None
137        self.previous_music = None
138        self.music = None
139        self.volume = None
140        self.credit = None
141    
142    def startup(self, current_time, game_data):
143        """
144        Initialize data at scene start. 
145        """
146        self.game_data = game_data
147        self.music = setup.MUSIC['overworld']
148        self.volume = 0.4
149        self.current_time = current_time
150        self.background = pg.Surface(setup.SCREEN_RECT.size)
151        self.background.fill(c.BLACK_BLUE)
152        self.credit = CreditEntry(self)
153
154    def update(self, surface, keys, current_time):
155        """
156        Update scene.
157        """
158        self.credit.update(current_time)
159        self.draw_scene(surface)
160
161    def draw_scene(self, surface):
162        """
163        Draw all graphics to the window surface.
164        """
165        surface.blit(self.background, (0, 0))
166        self.credit.draw(surface)
167        
168    
169