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.font = pg.font.Font(setup.FONTS[c.MAIN_FONT], 22)
25 self.background = pg.Surface(setup.SCREEN_RECT.size)
26 self.background.fill(c.BLACK_BLUE)
27 self.player = person.Player('down', self.game_data, 1, 1, 'resting', 1)
28 self.player.image = pg.transform.scale2x(self.player.image)
29 self.player.rect = self.player.image.get_rect()
30 self.player.rect.center = setup.SCREEN_RECT.center
31 self.message_box = self.make_message_box()
32 self.state_dict = self.make_state_dict()
33 self.state = c.TRANSITION_IN
34 self.alpha = 255
35 self.name = c.DEATH_SCENE
36 self.transition_surface = copy.copy(self.background)
37 self.transition_surface.fill(c.BLACK_BLUE)
38 self.transition_surface.set_alpha(self.alpha)
39
40 def make_message_box(self):
41 """
42 Make the text box informing of death.
43 """
44 box_image = setup.GFX['dialoguebox']
45 box_rect = box_image.get_rect()
46 text = 'You have died. Restart from last save point?'
47 text_render = self.font.render(text, True, c.NEAR_BLACK)
48 text_rect = text_render.get_rect(centerx=box_rect.centerx,
49 y=30)
50
51 temp_surf = pg.Surface(box_rect.size)
52 temp_surf.set_colorkey(c.BLACK)
53 temp_surf.blit(box_image, box_rect)
54 temp_surf.blit(text_render, text_rect)
55
56 box_sprite = pg.sprite.Sprite()
57 box_sprite.image = temp_surf
58 box_sprite.rect = temp_surf.get_rect(bottom=608)
59
60 return box_sprite
61
62 def make_state_dict(self):
63 """
64 Make the dicitonary of state methods for the scene.
65 """
66 state_dict = {c.TRANSITION_IN: self.transition_in,
67 c.TRANSITION_OUT: self.transition_out,
68 c.NORMAL: self.normal_update}
69
70 return state_dict
71
72 def update(self, surface, *args):
73 """
74 Update scene.
75 """
76 update_level = self.state_dict[self.state]
77 update_level()
78 self.draw_level(surface)
79
80 def transition_in(self):
81 """
82 Transition into scene with a fade.
83 """
84 self.transition_surface.set_alpha(self.alpha)
85 self.alpha -= c.TRANSITION_SPEED
86 if self.alpha <= 0:
87 self.alpha = 0
88 self.state = c.NORMAL
89
90 def transition_out(self):
91 """
92 Transition out of scene with a fade.
93 """
94 self.transition_surface.set_alpha(self.alpha)
95 self.alpha += c.TRANSITION_SPEED
96 if self.alpha >= 255:
97 self.game_data = pickle.load(open("save.p", "rb"))
98 self.game_data['last state'] = self.name
99 self.done = True
100
101 def normal_update(self):
102 pass
103
104 def draw_level(self, surface):
105 """
106 Draw background, player, and message box.
107 """
108 surface.blit(self.background, (0, 0))
109 surface.blit(self.player.image, self.player.rect)
110 surface.blit(self.message_box.image, self.message_box.rect)
111
112
113
114
115
116