data/states/death.py (view raw)
1import copy, pickle, sys, os
2import pygame as pg
3from .. import setup, tools
4from .. import observer
5from ..components import person
6from .. import constants as c
7
8#Python 2/3 compatibility.
9if sys.version_info[0] == 2:
10 import cPickle
11 pickle = cPickle
12
13
14class Arrow(pg.sprite.Sprite):
15 """
16 Arrow to select restart or saved gamed.
17 """
18 def __init__(self, x, y):
19 super(Arrow, self).__init__()
20 self.image = setup.GFX['smallarrow']
21 self.rect = self.image.get_rect(x=x,
22 y=y)
23 self.index = 0
24 self.pos_list = [y, y+34]
25 self.allow_input = False
26 self.observers = [observer.SoundEffects()]
27
28 def notify(self, event):
29 """
30 Notify all observers of event.
31 """
32 for observer in self.observers:
33 observer.on_notify(event)
34
35 def update(self, keys):
36 """
37 Update arrow position.
38 """
39 if self.allow_input:
40 if keys[pg.K_DOWN] and not keys[pg.K_UP] and self.index == 0:
41 self.index = 1
42 self.allow_input = False
43 self.notify(c.CLICK)
44 elif keys[pg.K_UP] and not keys[pg.K_DOWN] and self.index == 1:
45 self.index = 0
46 self.allow_input = False
47 self.notify(c.CLICK)
48
49 self.rect.y = self.pos_list[self.index]
50
51 if not keys[pg.K_DOWN] and not keys[pg.K_UP]:
52 self.allow_input = True
53
54
55class DeathScene(tools._State):
56 """
57 Scene when the player has died.
58 """
59 def __init__(self):
60 super(DeathScene, self).__init__()
61 self.next = c.TOWN
62 self.music = setup.MUSIC['shop_theme']
63 self.volume = 0.5
64 self.music_title = 'shop_theme'
65
66 def startup(self, current_time, game_data):
67 self.game_data = game_data
68 self.font = pg.font.Font(setup.FONTS[c.MAIN_FONT], 22)
69 self.background = pg.Surface(setup.SCREEN_RECT.size)
70 self.background.fill(c.BLACK_BLUE)
71 self.player = person.Player('down', self.game_data, 1, 1, 'resting', 1)
72 self.player.image = pg.transform.scale2x(self.player.image)
73 self.player.rect = self.player.image.get_rect()
74 self.player.rect.center = setup.SCREEN_RECT.center
75 self.message_box = self.make_message_box()
76 self.arrow = Arrow(300, 532)
77 self.state_dict = self.make_state_dict()
78 self.state = c.TRANSITION_IN
79 self.alpha = 255
80 self.name = c.DEATH_SCENE
81 self.transition_surface = pg.Surface(setup.SCREEN_RECT.size)
82 self.transition_surface.fill(c.BLACK_BLUE)
83 self.transition_surface.set_alpha(self.alpha)
84 if not os.path.isfile("save.p"):
85 game_data = tools.create_game_data_dict()
86 pickle.dump(game_data, open("save.p", "wb"))
87 self.observers = [observer.SoundEffects()]
88
89 def notify(self, event):
90 """
91 Notify all observers of event.
92 """
93 for observer in self.observers:
94 observer.on_notify(event)
95
96 def make_message_box(self):
97 """
98 Make the text box informing of death.
99 """
100 box_image = setup.GFX['dialoguebox']
101 box_rect = box_image.get_rect()
102 text = 'You have died. Restart from last save point?'
103 text_render = self.font.render(text, True, c.NEAR_BLACK)
104 text_rect = text_render.get_rect(centerx=box_rect.centerx,
105 y=30)
106 text2 = 'Yes'
107 text2_render = self.font.render(text2, True, c.NEAR_BLACK)
108 text2_rect = text2_render.get_rect(centerx=box_rect.centerx,
109 y=70)
110
111 text3 = 'No'
112 text3_render = self.font.render(text3, True, c.NEAR_BLACK)
113 text3_rect = text3_render.get_rect(centerx=box_rect.centerx,
114 y=105)
115
116 temp_surf = pg.Surface(box_rect.size)
117 temp_surf.set_colorkey(c.BLACK)
118 temp_surf.blit(box_image, box_rect)
119 temp_surf.blit(text_render, text_rect)
120 temp_surf.blit(text2_render, text2_rect)
121 temp_surf.blit(text3_render, text3_rect)
122
123 box_sprite = pg.sprite.Sprite()
124 box_sprite.image = temp_surf
125 box_sprite.rect = temp_surf.get_rect(bottom=608)
126
127 return box_sprite
128
129 def make_state_dict(self):
130 """
131 Make the dicitonary of state methods for the scene.
132 """
133 state_dict = {c.TRANSITION_IN: self.transition_in,
134 c.TRANSITION_OUT: self.transition_out,
135 c.NORMAL: self.normal_update}
136
137 return state_dict
138
139 def update(self, surface, keys, *args):
140 """
141 Update scene.
142 """
143 update_level = self.state_dict[self.state]
144 update_level(keys)
145 self.draw_level(surface)
146
147 def transition_in(self, *args):
148 """
149 Transition into scene with a fade.
150 """
151 self.transition_surface.set_alpha(self.alpha)
152 self.alpha -= c.TRANSITION_SPEED
153 if self.alpha <= 0:
154 self.alpha = 0
155 self.state = c.NORMAL
156
157 def transition_out(self, *args):
158 """
159 Transition out of scene with a fade.
160 """
161 self.transition_surface.set_alpha(self.alpha)
162 self.alpha += c.TRANSITION_SPEED
163 if self.alpha >= 255:
164 self.done = True
165
166 def normal_update(self, keys):
167 self.arrow.update(keys)
168 self.check_for_input(keys)
169
170 def check_for_input(self, keys):
171 """
172 Check if player wants to restart from last save point
173 or just start from the beginning of the game.
174 """
175 if keys[pg.K_SPACE]:
176 if self.arrow.index == 0:
177 self.next = c.TOWN
178 self.game_data = pickle.load(open("save.p", "rb"))
179 elif self.arrow.index == 1:
180 self.next = c.MAIN_MENU
181 self.state = c.TRANSITION_OUT
182 self.notify(c.CLICK2)
183
184 def draw_level(self, surface):
185 """
186 Draw background, player, and message box.
187 """
188 surface.blit(self.background, (0, 0))
189 surface.blit(self.player.image, self.player.rect)
190 surface.blit(self.message_box.image, self.message_box.rect)
191 surface.blit(self.arrow.image, self.arrow.rect)
192 surface.blit(self.transition_surface, (0, 0))
193
194
195
196
197
198