all repos — Legends-RPG @ 8493ba3ac66e56efea1ab86e9a28500b1ac0e1c7

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.image = self.make_surface(damage)
65        self.rect = self.image.get_rect(x=topleft_pos[0]+20,
66                                        bottom=topleft_pos[1]+10)
67        self.start_posy = self.rect.y
68        self.y_vel = -1
69        self.dead = False
70
71    def make_surface(self, damage):
72        """
73        Make the surface for the sprite.
74        """
75        return self.font.render(str(damage), True, c.RED)
76
77    def update(self):
78        """
79        Update sprite position or delete if necessary.
80        """
81        self.rect.y += self.y_vel
82        if self.rect.y < (self.start_posy - 50):
83            self.kill()
84
85
86
87
88
89
90