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.font = pg.font.Font(setup.FONTS[c.MAIN_FONT], 22)
13 self.credit_sprites = self.make_credits()
14 self.index = 0
15 self.current_credit = self.credit_sprites[self.index]
16 self.state_dict = self.make_state_dict()
17 self.state = c.TRANSITION_IN
18 self.alpha = 0
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', 'John Smith'],
29 ['MUSIC', 'John Smith'],
30 ['SPECIAL THANKS', '/r/pygame']]
31
32 credit_sprites = []
33
34 for credit in credits:
35 subcredit_list = []
36 for i, subcredit in enumerate(credit):
37 text_sprite = pg.sprite.Sprite()
38 text_sprite.text_image = self.font.render(subcredit, True, c.WHITE)
39 text_sprite.rect = text_sprite.text_image.get_rect(centerx = 400,
40 y=250+(i*50))
41 text_sprite.image = pg.Surface(text_sprite.rect.size)
42 subcredit_list.append(text_sprite)
43 credit_sprites.append(subcredit_list)
44
45 return credit_sprites
46
47 def make_state_dict(self):
48 """
49 Make the dictionary of state methods used to update credit.
50 """
51 state_dict = {c.TRANSITION_IN: self.transition_in,
52 c.TRANSITION_OUT: self.transition_out,
53 c.NORMAL: self.normal_update}
54
55 return state_dict
56
57 def transition_in(self):
58 for credit in self.current_credit:
59 credit.image = pg.Surface(credit.rect.size).convert()
60 credit.image.set_colorkey(c.BLACK)
61 credit.image.set_alpha(self.alpha)
62 credit.image.blit(credit.text_image, (0, 0))
63
64 self.alpha += 5
65 if self.alpha >= 255:
66 self.alpha = 255
67 self.state = c.NORMAL
68 self.timer = self.current_time
69
70 def transition_out(self):
71 for credit in self.current_credit:
72 credit.image = pg.Surface(credit.rect.size).convert()
73 credit.image.set_colorkey(c.BLACK)
74 credit.image.set_alpha(self.alpha)
75 credit.image.blit(credit.text_image, (0, 0))
76
77 self.alpha -= 5
78 if self.alpha <= 0:
79 self.alpha = 0
80 if self.index < len(self.credit_sprites) - 1:
81 self.index += 1
82 else:
83 self.level.done = True
84 self.level.next = c.MAIN_MENU
85 self.current_credit = self.credit_sprites[self.index]
86 self.state = c.TRANSITION_IN
87
88 def normal_update(self):
89 if (self.current_time - self.timer) > 2500:
90 self.state = c.TRANSITION_OUT
91
92 def update(self, current_time):
93 self.current_time = current_time
94 update_method = self.state_dict[self.state]
95 update_method()
96
97 def draw(self, surface):
98 """
99 Draw the current credit to main surface.
100 """
101 for credit_sprite in self.current_credit:
102 surface.blit(credit_sprite.image, credit_sprite.rect)
103
104
105class Credits(tools._State):
106 """
107 End Credits Scene.
108 """
109 def __init__(self):
110 super(Credits, self).__init__()
111 self.name = c.CREDITS
112 self.music_title = None
113 self.previous_music = None
114 self.music = None
115 self.volume = None
116 self.credit = None
117
118 def startup(self, current_time, game_data):
119 """
120 Initialize data at scene start.
121 """
122 self.game_data = game_data
123 self.music = setup.MUSIC['overworld']
124 self.volume = 0.4
125 self.current_time = current_time
126 self.background = pg.Surface(setup.SCREEN_RECT.size)
127 self.background.fill(c.BLACK_BLUE)
128 self.credit = CreditEntry(self)
129
130 def update(self, surface, keys, current_time):
131 """
132 Update scene.
133 """
134 self.credit.update(current_time)
135 self.draw_scene(surface)
136
137 def draw_scene(self, surface):
138 """
139 Draw all graphics to the window surface.
140 """
141 surface.blit(self.background, (0, 0))
142 self.credit.draw(surface)
143
144
145