summaryrefslogtreecommitdiff
path: root/sprite.py
blob: 574c1780912fec6e6d09527e7fdfaf933ce97b16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from tkinter import Canvas, NW, PhotoImage

from config import Config


class Sprite:
    """Sprite."""

    @staticmethod
    def remove_destroyed(sprite_list):
        """Remove all destroyed sprites from a list

        :param sprite_list:
        :type sprite_list: list[Sprite]
        """
        return list(filter(lambda s: not s.destroyed, sprite_list))

    def __init__(self, canvas: Canvas, image: PhotoImage, position=(0, 0)):
        """Initialise the sprite class

        :param canvas: The canvas to draw the sprites to
        :type canvas: Canvas
        :param image: The image to be used for the sprite
        :type image: PhotoImage
        :param position: The default position to place the sprite
        """
        # set positions
        self.x, self.y = position

        self.canvas = canvas
        self.canvas_image = canvas.create_image(
            self.x * Config.SCALE, self.y * Config.SCALE,
            anchor=NW, image=image, state="hidden")

        # get pixel width and heigh ignoring scale
        self.w = image.width() // Config.SCALE
        self.h = image.height() // Config.SCALE

        self.destroyed = False
        self.hide()

    def update_position(self):
        """Move the image to the sprites position"""
        self.canvas.coords(self.canvas_image, self.x *
                           Config.SCALE, self.y*Config.SCALE)

    def set_pos(self, pos):
        """Set the player position

        :param pos: Position to move to
        """
        self.x, self.y = pos
        self.update_position()

    def get_pos(self):
        """Return the current position of the sprite"""
        return (self.x, self.y)

    def move(self, x, y):
        """Move the sprite by x and y

        :param x: the number of pixels right to move
        :param y: the number of pixels down to move
        """
        self.x += x
        self.y += y
        self.update_position()

    def collides(self, other):
        """Check if the sprite collides with another sprite

        :param other: The other sprite
        """
        return self.x < other.x + other.w \
            and self.x + self.w > other.x \
            and self.y < other.y + other.h \
            and self.h + self.y > other.y

    def collide_all(self, others):
        """Check if the sprite collides with a list of sprites

        :param others: Array of other sprites to check if collides with
        :returns:      index of the sprite that it collided with first
                       or -1 if not colliding
        """
        for i, other in enumerate(others):
            if self.collides(other):
                return i
        return -1

    def tick(self):
        """Update the sprite"""

    def destroy(self):
        """Remove the image from the canvas"""
        self.canvas.delete(self.canvas_image)
        self.destroyed = True

    def send_to_front(self):
        """Move the sprite to the foreground"""
        self.canvas.tag_raise(self.canvas_image)

    def send_to_back(self):
        """Move the sprite to the background"""
        self.canvas.tag_lower(self.canvas_image)

    def set_image(self, image: PhotoImage):
        """Change the image used by the sprite

        :param image: the image to set the sprite to
        :type image: PhotoImage
        """
        self.canvas.itemconfig(self.canvas_image, image=image)

    def show(self):
        """Set the sprite to be shown"""
        self.canvas.itemconfig(self.canvas_image, state="normal")
        return self

    def hide(self):
        """Set the sprite to be hidden"""
        self.canvas.itemconfig(self.canvas_image, state="hidden")
        return self

    def is_hidden(self):
        """Return True if the sprite is hidden"""
        return self.canvas.itemcget(self.canvas_image, "state") == "hidden"