all repos — Legends-RPG @ 03e23956af527301293a4f59072dddf0e341df8b

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, ether=False):
 62        super(HealthPoints, self).__init__()
 63        self.ether = ether
 64        self.damage = damage
 65        self.font = pg.font.Font(setup.FONTS[c.MAIN_FONT], 27)
 66        self.text_image = self.make_surface(points)
 67        self.rect = self.text_image.get_rect(x=topleft_pos[0]+20,
 68                                             bottom=topleft_pos[1]+10)
 69        self.image = pg.Surface(self.rect.size).convert()
 70        self.image.set_colorkey(c.BLACK)
 71        self.alpha = 255
 72        self.image.set_alpha(self.alpha)
 73        self.image.blit(self.text_image, (0, 0))
 74        self.start_posy = self.rect.y
 75        self.y_vel = -1
 76        self.fade_out = False
 77
 78    def make_surface(self, points):
 79        """
 80        Make the surface for the sprite.
 81        """
 82        if self.damage:
 83            if points > 0:
 84                text = "-{}".format(str(points))
 85                surface = self.font.render(text, True, c.RED)
 86                return surface
 87            else:
 88                return self.font.render('Miss', True, c.WHITE).convert_alpha()
 89        else:
 90            text = "+{}".format(str(points))
 91            if self.ether:
 92                surface = self.font.render(text, True, c.PINK)
 93            else:
 94                surface = self.font.render(text, True, c.GREEN)
 95
 96            return surface
 97
 98    def update(self):
 99        """
100        Update sprite position or delete if necessary.
101        """
102        self.fade_animation()
103        self.rect.y += self.y_vel
104        if self.rect.y < (self.start_posy - 29):
105            self.fade_out = True
106
107    def fade_animation(self):
108        """
109        Fade score in and out.
110        """
111        if self.fade_out:
112            self.image = pg.Surface(self.rect.size).convert()
113            self.image.set_colorkey(c.BLACK)
114            self.image.set_alpha(self.alpha)
115            self.image.blit(self.text_image, (0, 0))
116            self.alpha -= 15
117            if self.alpha <= 0:
118                self.kill()
119
120
121
122
123
124
125
126
127