all repos — Legends-RPG @ c5d70de66c1b6acc4456bef4e66bb278f0375c37

A fantasy mini-RPG built with Python and Pygame.

data/components/attackitems.py (view raw)

  1"""
  2Attack equipment for battles.
  3"""
  4import copy
  5import pygame as pg
  6from .. import tools, setup
  7from .. import constants as c
  8
  9
 10class Sword(object):
 11    """
 12    Sword that appears during regular attacks.
 13    """
 14    def __init__(self, player):
 15        self.player = player
 16        self.sprite_sheet = setup.GFX['shopsigns']
 17        self.image_list = self.make_image_list()
 18        self.index = 0
 19        self.timer = 0.0
 20
 21    def make_image_list(self):
 22        """
 23        Make the list of two images for animation.
 24        """
 25        image_list = [tools.get_image(48, 0, 16, 16, self.sprite_sheet),
 26                      tools.get_image(0, 0, 22, 16, setup.GFX['sword2'])]
 27        return image_list
 28
 29    @property
 30    def image(self):
 31        new_image = self.image_list[self.index]
 32        return pg.transform.scale2x(new_image)
 33
 34    @property
 35    def rect(self):
 36        new_rect = copy.copy(self.player.rect)
 37        new_rect.bottom += 17
 38        new_rect.right -= 16
 39        return new_rect
 40
 41    def update(self, current_time):
 42        if (current_time - self.timer) > 60:
 43            self.timer = current_time
 44            if self.index == 0:
 45                self.index += 1
 46            else:
 47                self.index -= 1
 48
 49    def draw(self, surface):
 50        """
 51        Draw sprite to surface.
 52        """
 53        if self.player.state == 'attack':
 54            surface.blit(self.image, self.rect)
 55
 56
 57class HealthPoints(pg.sprite.Sprite):
 58    """
 59    A sprite that shows how much damage an attack inflicted.
 60    """
 61    def __init__(self, points, topleft_pos, damage=True):
 62        super(HealthPoints, self).__init__()
 63        self.damage = damage
 64        self.font = pg.font.Font(setup.FONTS[c.MAIN_FONT], 27)
 65        self.text_image = self.make_surface(points)
 66        self.rect = self.text_image.get_rect(x=topleft_pos[0]+20,
 67                                             bottom=topleft_pos[1]+10)
 68        self.image = pg.Surface(self.rect.size).convert()
 69        self.image.set_colorkey(c.BLACK)
 70        self.alpha = 255
 71        self.image.set_alpha(self.alpha)
 72        self.image.blit(self.text_image, (0, 0))
 73        self.start_posy = self.rect.y
 74        self.y_vel = -1
 75        self.fade_out = False
 76
 77    def make_surface(self, points):
 78        """
 79        Make the surface for the sprite.
 80        """
 81        if self.damage:
 82            if points > 0:
 83                text = "-{}".format(str(points))
 84                surface = self.font.render(text, True, c.RED)
 85                return surface
 86            else:
 87                return self.font.render('Miss', True, c.WHITE).convert_alpha()
 88        else:
 89            text = "+{}".format(str(points))
 90            surface = self.font.render(text, True, c.GREEN)
 91            return surface
 92
 93    def update(self):
 94        """
 95        Update sprite position or delete if necessary.
 96        """
 97        self.fade_animation()
 98        self.rect.y += self.y_vel
 99        if self.rect.y < (self.start_posy - 29):
100            self.fade_out = True
101
102    def fade_animation(self):
103        """
104        Fade score in and out.
105        """
106        if self.fade_out:
107            self.image = pg.Surface(self.rect.size).convert()
108            self.image.set_colorkey(c.BLACK)
109            self.image.set_alpha(self.alpha)
110            self.image.blit(self.text_image, (0, 0))
111            self.alpha -= 15
112            if self.alpha <= 0:
113                self.kill()
114
115
116
117
118
119
120
121
122