-
Notifications
You must be signed in to change notification settings - Fork 0
/
sprite.py
executable file
·36 lines (30 loc) · 1.16 KB
/
sprite.py
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
import util
import simplegui
from typing import Tuple, Optional
from geom import Vector
class Sprite(object):
"""
Represents a sprite to be drawn on the canvas.
"""
def __init__(self, image: str, cols: int = 1, rows: int = 1):
"""
Creates a sprite with the specified image and number of cols and rows.
"""
self.image = util.load_image(image)
self.width = self.image.get_width()
self.height = self.image.get_height()
self.cols = cols
self.rows = rows
self.sprite_size = (self.width // cols, self.height // rows)
self.sprite_center = (self.sprite_size[0] / 2, self.sprite_size[1] / 2)
def draw(self, canvas: simplegui.Canvas, pos: Vector, size: Optional[Vector] = None,
index: Tuple[int, int] = (0, 0)):
"""
Draw the sprite on the canvas.
"""
source_center = [self.sprite_size[i] * index[i] + self.sprite_center[i] for i in [0, 1]]
if size is not None:
size = size.into_tuple()
else:
size = self.sprite_size
canvas.draw_image(self.image, source_center, self.sprite_size, pos.into_tuple(), size)