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 ['SPECIAL THANKS', '/r/pygame',
50 'Leif Theden',
51 'Stacey Hunniford']]
52
53 credit_sprites = []
54
55 for credit in credits:
56 subcredit_list = []
57 for i, subcredit in enumerate(credit):
58 text_sprite = pg.sprite.Sprite()
59 text_sprite.text_image = self.font.render(subcredit, True, c.WHITE)
60 text_sprite.rect = text_sprite.text_image.get_rect(centerx = 400,
61 y=100+(i*40))
62 text_sprite.image = pg.Surface(text_sprite.rect.size).convert()
63 text_sprite.image.set_colorkey(c.BLACK)
64 text_sprite.image.set_alpha(self.alpha)
65 subcredit_list.append(text_sprite)
66 credit_sprites.append(subcredit_list)
67
68 return credit_sprites
69
70 def make_state_dict(self):
71 """
72 Make the dictionary of state methods used to update credit.
73 """
74 state_dict = {c.TRANSITION_IN: self.transition_in,
75 c.TRANSITION_OUT: self.transition_out,
76 c.NORMAL: self.normal_update}
77
78 return state_dict
79
80 def transition_in(self):
81 for credit in self.current_credit:
82 credit.image = pg.Surface(credit.rect.size).convert()
83 credit.image.set_colorkey(c.BLACK)
84 credit.image.set_alpha(self.alpha)
85 credit.image.blit(credit.text_image, (0, 0))
86
87 self.alpha += 5
88 if self.alpha >= 255:
89 self.alpha = 255
90 self.state = c.NORMAL
91 self.timer = self.current_time
92
93 def transition_out(self):
94 for credit in self.current_credit:
95 credit.image = pg.Surface(credit.rect.size).convert()
96 credit.image.set_colorkey(c.BLACK)
97 credit.image.set_alpha(self.alpha)
98 credit.image.blit(credit.text_image, (0, 0))
99
100 self.alpha -= 5
101 if self.alpha <= 0:
102 self.alpha = 0
103 if self.index < len(self.credit_sprites) - 1:
104 self.index += 1
105 else:
106 self.level.done = True
107 self.level.next = c.MAIN_MENU
108 self.current_credit = self.credit_sprites[self.index]
109 self.state = c.TRANSITION_IN
110
111 def normal_update(self):
112 if (self.current_time - self.timer) > 4500:
113 self.state = c.TRANSITION_OUT
114
115 def update(self, current_time):
116 self.current_time = current_time
117 update_method = self.state_dict[self.state]
118 update_method()
119
120 def draw(self, surface):
121 """
122 Draw the current credit to main surface.
123 """
124 for credit_sprite in self.current_credit:
125 surface.blit(credit_sprite.image, credit_sprite.rect)
126
127
128class Credits(tools._State):
129 """
130 End Credits Scene.
131 """
132 def __init__(self):
133 super(Credits, self).__init__()
134 self.name = c.CREDITS
135 self.music_title = None
136 self.previous_music = None
137 self.music = None
138 self.volume = None
139 self.credit = None
140
141 def startup(self, current_time, game_data):
142 """
143 Initialize data at scene start.
144 """
145 self.game_data = game_data
146 self.music = setup.MUSIC['overworld']
147 self.volume = 0.4
148 self.current_time = current_time
149 self.background = pg.Surface(setup.SCREEN_RECT.size)
150 self.background.fill(c.BLACK_BLUE)
151 self.credit = CreditEntry(self)
152
153 def update(self, surface, keys, current_time):
154 """
155 Update scene.
156 """
157 self.credit.update(current_time)
158 self.draw_scene(surface)
159
160 def draw_scene(self, surface):
161 """
162 Draw all graphics to the window surface.
163 """
164 surface.blit(self.background, (0, 0))
165 self.credit.draw(surface)
166
167
168