-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyutils.py
43 lines (38 loc) · 1.51 KB
/
myutils.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
37
38
39
40
41
42
43
import os
import pygame
IMG_DIR = ''
def load_image(filename):
return pygame.image.load(os.path.join(IMG_DIR, filename)).convert()
class Spritesheet:
def __init__(self, filename):
self.sheet = load_image(filename)
def imgat(self, rect, colorkey = None):
rect = pygame.Rect(rect)
image = pygame.Surface(rect.size).convert()
image.blit(self.sheet, (0, 0), rect)
if colorkey is not None:
if colorkey is -1:
colorkey = image.get_at((0, 0))
image.set_colorkey(colorkey, pygame.RLEACCEL)
return image
def imgsat(self, rects, colorkey = None):
imgs = []
for rect in rects:
imgs.append(self.imgat(rect, colorkey))
return imgs
class Animacao:
def __init__(self, spritesheet, fps, rects, incremento=1):
self.rects = rects
self.incremento = incremento
self.frameAtual = 0
self.delay = 1000 / fps
self.ultimoUpdate = 0
self.frames = spritesheet.imgsat(self.rects, -1)
self.frame = self.frames[self.frameAtual]
self.rect = pygame.Rect(self.rects[self.frameAtual])
def atualiza(self, tempoAtual):
if tempoAtual - self.ultimoUpdate > self.delay:
self.ultimoUpdate = tempoAtual
self.frameAtual = (self.frameAtual + 1) % len(self.frames)
self.frame = self.frames[self.frameAtual]
self.rect = pygame.Rect(self.rects[self.frameAtual])