all repos — Legends-RPG @ a03054accb71fa89eadb61df1441c401d572085e

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