data/states/credits.py (view raw)
1import pygame as pg
2from .. import tools, setup
3from .. import constants as c
4
5
6class CreditEntry(object):
7 """
8 The text for each credit for the game.
9 """
10 def __init__(self):
11 self.font = pg.font.Font(setup.FONTS[c.MAIN_FONT], 22)
12 self.credit_sprites = self.make_credits()
13 self.index = 0
14 self.current_credit = self.credit_sprites[self.index]
15 self.state_dict = self.make_state_dict()
16 self.state = c.TRANSITION_IN
17 self.alpha = 0
18 self.timer = 0.0
19
20 def make_credits(self):
21 """
22 Make a list of lists for all the credit surfaces.
23 """
24 credits = [['PROGRAMMING AND GAME DESIGN', 'Justin Armstrong'],
25 ['ART', 'John Smith'],
26 ['MUSIC', 'John Smith'],
27 ['SPECIAL THANKS', '/r/pygame']]
28
29 credit_sprites = []
30
31 for credit in credits:
32 subcredit_list = []
33 for i, subcredit in enumerate(credit):
34 text_sprite = pg.sprite.Sprite()
35 text_sprite.image = self.font.render(subcredit, True, c.WHITE)
36 text_sprite.rect = text_sprite.image.get_rect(centerx = 400,
37 y=300+(i*50))
38 subcredit_list.append(text_sprite)
39 credit_sprites.append(subcredit_list)
40
41 return credit_sprites
42
43 def make_state_dict(self):
44 """
45 Make the dictionary of state methods used to update credit.
46 """
47 state_dict = {c.TRANSITION_IN: self.transition_in,
48 c.TRANSITION_OUT: self.transition_out,
49 c.NORMAL: self.normal_update}
50
51 return state_dict
52
53 def transition_in(self):
54 for credit in self.current_credit:
55 credit.image.set_alpha(self.alpha)
56
57 self.alpha += c.TRANSITION_SPEED
58 if self.alpha >= 255:
59 self.alpha = 255
60 self.state = c.NORMAL
61 self.timer = self.current_time
62
63 def transition_out(self):
64 for credit in self.current_credit:
65 credit.image.set_alpha(self.alpha)
66 self.alpha -= c.TRANSITION_SPEED
67 if self.alpha <= 0:
68 self.alpha = 0
69 self.index += 1
70 self.current_credit = self.credit_sprites[self.index]
71 self.state = c.TRANSITION_IN
72
73 def normal_update(self):
74 if (self.current_time - self.timer) > 5000:
75 self.state = c.TRANSITION_OUT
76
77 def update(self, surface, current_time, *args):
78 self.current_time = current_time
79 update_method = self.state_dict[self.state]
80 update()
81
82 def draw(self, surface):
83 """
84 Draw the current credit to main surface.
85 """
86 for credit_sprite in self.current_credit:
87 surface.blit(credit_sprite.image, credit_sprite.rect)
88
89
90class Credits(tools._state):
91 """
92 End Credits Scene.
93 """
94 def __init__(self):
95 super(Credits, self).__init__()
96 self.name = c.CREDITS
97 self.music_title = None
98 self.previous_music = None
99 self.music = None
100 self.volume = None
101 self.credit = None
102
103 def startup(self, current_time, game_data):
104 """
105 Initialize data at scene start.
106 """
107 self.game_data = game_data
108 self.music = setup.MUSIC['overworld']
109 self.volume = 0.4
110 self.current_time = current_time
111 self.background = pg.Surface(setup.SCREEN_RECT.size)
112 self.background.fill(c.BLACKBLUE)
113 self.credit = CreditEntry()
114
115 def update(self, surface, current_time, *args):
116 """
117 Update scene.
118 """
119 self.credit.update(current_time)
120 self.draw_scene(surface)
121
122 def draw_scene(self, surface):
123 """
124 Draw all graphics to the window surface.
125 """
126 surface.blit(self.background, (0, 0))
127 self.credit.draw(surface)
128
129
130