all repos — Legends-RPG @ 5bfef920882a049cc06fa2648364428c4e0f1507

A fantasy mini-RPG built with Python and Pygame.

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 Arrow(pg.sprite.Sprite):
 14    """
 15    Arrow to select restart or saved gamed.
 16    """
 17    def __init__(self):
 18        super(Arrow, self).__init__()
 19        self.image = setup.GFX['smallarrow']
 20        self.rect = self.image.get_rect(x=300,
 21                                        y=532)
 22        self.index = 0
 23        self.pos_list = [532, 566]
 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()
 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
 65    def make_message_box(self):
 66        """
 67        Make the text box informing of death.
 68        """
 69        box_image = setup.GFX['dialoguebox']
 70        box_rect = box_image.get_rect()
 71        text = 'You have died. Restart from last save point?'
 72        text_render = self.font.render(text, True, c.NEAR_BLACK) 
 73        text_rect = text_render.get_rect(centerx=box_rect.centerx,
 74                                         y=30)
 75        text2 = 'Yes'
 76        text2_render = self.font.render(text2, True, c.NEAR_BLACK)
 77        text2_rect = text2_render.get_rect(centerx=box_rect.centerx,
 78                                           y=70)
 79
 80        text3 = 'No'
 81        text3_render = self.font.render(text3, True, c.NEAR_BLACK)
 82        text3_rect = text3_render.get_rect(centerx=box_rect.centerx,
 83                                           y=105)
 84
 85        temp_surf = pg.Surface(box_rect.size)
 86        temp_surf.set_colorkey(c.BLACK)
 87        temp_surf.blit(box_image, box_rect)
 88        temp_surf.blit(text_render, text_rect)
 89        temp_surf.blit(text2_render, text2_rect)
 90        temp_surf.blit(text3_render, text3_rect)
 91        
 92        box_sprite = pg.sprite.Sprite()
 93        box_sprite.image = temp_surf
 94        box_sprite.rect = temp_surf.get_rect(bottom=608)
 95        
 96        return box_sprite
 97
 98    def make_state_dict(self):
 99        """
100        Make the dicitonary of state methods for the scene.
101        """
102        state_dict = {c.TRANSITION_IN: self.transition_in,
103                      c.TRANSITION_OUT: self.transition_out,
104                      c.NORMAL: self.normal_update}
105
106        return state_dict
107
108    def update(self, surface, keys, *args):
109        """
110        Update scene.
111        """
112        update_level = self.state_dict[self.state]
113        update_level(keys)
114        self.draw_level(surface)
115
116    def transition_in(self, *args):
117        """
118        Transition into scene with a fade.
119        """
120        self.transition_surface.set_alpha(self.alpha)
121        self.alpha -= c.TRANSITION_SPEED
122        if self.alpha <= 0:
123            self.alpha = 0
124            self.state = c.NORMAL
125
126    def transition_out(self, *args):
127        """
128        Transition out of scene with a fade.
129        """
130        self.transition_surface.set_alpha(self.alpha)
131        self.alpha += c.TRANSITION_SPEED
132        if self.alpha >= 255:
133            self.game_data['last state'] = self.name
134            self.done = True
135
136    def normal_update(self, keys):
137        self.arrow.update(keys)
138        self.check_for_input(keys)
139
140    def check_for_input(self, keys):
141        """
142        Check if player wants to restart from last save point
143        or just start from the beginning of the game.
144        """
145        if keys[pg.K_SPACE]:
146            if self.arrow.index == 0:
147                self.next = c.MAIN_MENU
148            elif self.arrow.index == 1:
149                self.next = c.MAIN_MENU
150            self.state = c.TRANSITION_OUT
151
152    def draw_level(self, surface):
153        """
154        Draw background, player, and message box.
155        """
156        surface.blit(self.background, (0, 0))
157        surface.blit(self.player.image, self.player.rect)
158        surface.blit(self.message_box.image, self.message_box.rect)
159        surface.blit(self.arrow.image, self.arrow.rect)
160        surface.blit(self.transition_surface, (0, 0))
161
162
163
164
165
166