data/states/death.py (view raw)
1import copy, pickle, sys
2import pygame as pg
3from .. import setup, tools
4from ..components import person
5from .. import constants as c
6
7#Python 2/3 compatibility.
8if sys.version_info[0] == 2:
9 import cPickle
10 pickle = cPickle
11
12
13class DeathScene(tools._State):
14 """
15 Scene when the player has died.
16 """
17 def __init__(self):
18 super(DeathScene, self).__init__()
19 self.next = c.TOWN
20 self.music = None
21
22 def startup(self, current_time, game_data):
23 self.game_data = game_data
24 self.background = pg.Surface(setup.SCREEN_RECT.size)
25 self.background.fill(c.BLACK_BLUE)
26 self.player = person.Player('down', self.game_data, 1, 1, 'resting', 1)
27 self.player.image = pg.transform.scale2x(self.player.image)
28 self.player.rect = self.player.image.get_rect()
29 self.player.rect.center = setup.SCREEN_RECT.center
30 self.state_dict = self.make_state_dict()
31 self.state = c.TRANSITION_IN
32 self.alpha = 255
33 self.name = c.DEATH_SCENE
34 self.transition_surface = copy.copy(self.background)
35 self.transition_surface.fill(c.BLACK_BLUE)
36 self.transition_surface.set_alpha(self.alpha)
37
38 def make_state_dict(self):
39 """
40 Make the dicitonary of state methods for the scene.
41 """
42 state_dict = {c.TRANSITION_IN: self.transition_in,
43 c.TRANSITION_OUT: self.transition_out,
44 c.NORMAL: self.normal_update}
45
46 return state_dict
47
48 def update(self, surface, *args):
49 """
50 Update scene.
51 """
52 update_level = self.state_dict[self.state]
53 update_level()
54 self.draw_level(surface)
55
56 def transition_in(self):
57 """
58 Transition into scene with a fade.
59 """
60 self.transition_surface.set_alpha(self.alpha)
61 self.alpha -= c.TRANSITION_SPEED
62 if self.alpha <= 0:
63 self.alpha = 0
64 self.state = c.NORMAL
65
66 def transition_out(self):
67 """
68 Transition out of scene with a fade.
69 """
70 self.transition_surface.set_alpha(self.alpha)
71 self.alpha += c.TRANSITION_SPEED
72 if self.alpha >= 255:
73 self.game_data = pickle.load(open("save.p", "rb"))
74 self.game_data['last state'] = self.name
75 self.done = True
76
77 def normal_update(self):
78 pass
79
80 def draw_level(self, surface):
81 """
82 Draw background, player, and message box.
83 """
84 surface.blit(self.background, (0, 0))
85 surface.blit(self.player.image, self.player.rect)
86
87
88
89
90
91