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