all repos — Legends-RPG @ a5b828ace1209769c68ffed3ad2178da480599a4

A fantasy mini-RPG built with Python and Pygame.

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 = None
 63
 64    def startup(self, current_time, game_data):
 65        self.game_data = game_data
 66        self.font = pg.font.Font(setup.FONTS[c.MAIN_FONT], 22)
 67        self.background = pg.Surface(setup.SCREEN_RECT.size)
 68        self.background.fill(c.BLACK_BLUE)
 69        self.player = person.Player('down', self.game_data, 1, 1, 'resting', 1)
 70        self.player.image = pg.transform.scale2x(self.player.image)
 71        self.player.rect = self.player.image.get_rect()
 72        self.player.rect.center = setup.SCREEN_RECT.center
 73        self.message_box = self.make_message_box()
 74        self.arrow = Arrow(300, 532)
 75        self.state_dict = self.make_state_dict()
 76        self.state = c.TRANSITION_IN
 77        self.alpha = 255
 78        self.name = c.DEATH_SCENE
 79        self.transition_surface = pg.Surface(setup.SCREEN_RECT.size)
 80        self.transition_surface.fill(c.BLACK_BLUE)
 81        self.transition_surface.set_alpha(self.alpha)
 82        if not os.path.isfile("save.p"):
 83            game_data = tools.create_game_data_dict()
 84            pickle.dump(game_data, open("save.p", "wb"))
 85        self.observers = [observer.SoundEffects()]
 86
 87    def notify(self, event):
 88        """
 89        Notify all observers of event.
 90        """
 91        for observer in self.observers:
 92            observer.on_notify(event)
 93
 94    def make_message_box(self):
 95        """
 96        Make the text box informing of death.
 97        """
 98        box_image = setup.GFX['dialoguebox']
 99        box_rect = box_image.get_rect()
100        text = 'You have died. Restart from last save point?'
101        text_render = self.font.render(text, True, c.NEAR_BLACK) 
102        text_rect = text_render.get_rect(centerx=box_rect.centerx,
103                                         y=30)
104        text2 = 'Yes'
105        text2_render = self.font.render(text2, True, c.NEAR_BLACK)
106        text2_rect = text2_render.get_rect(centerx=box_rect.centerx,
107                                           y=70)
108
109        text3 = 'No'
110        text3_render = self.font.render(text3, True, c.NEAR_BLACK)
111        text3_rect = text3_render.get_rect(centerx=box_rect.centerx,
112                                           y=105)
113
114        temp_surf = pg.Surface(box_rect.size)
115        temp_surf.set_colorkey(c.BLACK)
116        temp_surf.blit(box_image, box_rect)
117        temp_surf.blit(text_render, text_rect)
118        temp_surf.blit(text2_render, text2_rect)
119        temp_surf.blit(text3_render, text3_rect)
120        
121        box_sprite = pg.sprite.Sprite()
122        box_sprite.image = temp_surf
123        box_sprite.rect = temp_surf.get_rect(bottom=608)
124        
125        return box_sprite
126
127    def make_state_dict(self):
128        """
129        Make the dicitonary of state methods for the scene.
130        """
131        state_dict = {c.TRANSITION_IN: self.transition_in,
132                      c.TRANSITION_OUT: self.transition_out,
133                      c.NORMAL: self.normal_update}
134
135        return state_dict
136
137    def update(self, surface, keys, *args):
138        """
139        Update scene.
140        """
141        update_level = self.state_dict[self.state]
142        update_level(keys)
143        self.draw_level(surface)
144
145    def transition_in(self, *args):
146        """
147        Transition into scene with a fade.
148        """
149        self.transition_surface.set_alpha(self.alpha)
150        self.alpha -= c.TRANSITION_SPEED
151        if self.alpha <= 0:
152            self.alpha = 0
153            self.state = c.NORMAL
154
155    def transition_out(self, *args):
156        """
157        Transition out of scene with a fade.
158        """
159        self.transition_surface.set_alpha(self.alpha)
160        self.alpha += c.TRANSITION_SPEED
161        if self.alpha >= 255:
162            self.done = True
163
164    def normal_update(self, keys):
165        self.arrow.update(keys)
166        self.check_for_input(keys)
167
168    def check_for_input(self, keys):
169        """
170        Check if player wants to restart from last save point
171        or just start from the beginning of the game.
172        """
173        if keys[pg.K_SPACE]:
174            if self.arrow.index == 0:
175                self.next = c.TOWN
176                self.game_data = pickle.load(open("save.p", "rb"))
177            elif self.arrow.index == 1:
178                self.next = c.MAIN_MENU
179            self.state = c.TRANSITION_OUT
180            self.notify(c.CLICK2)
181
182    def draw_level(self, surface):
183        """
184        Draw background, player, and message box.
185        """
186        surface.blit(self.background, (0, 0))
187        surface.blit(self.player.image, self.player.rect)
188        surface.blit(self.message_box.image, self.message_box.rect)
189        surface.blit(self.arrow.image, self.arrow.rect)
190        surface.blit(self.transition_surface, (0, 0))
191
192
193
194
195
196