-
Notifications
You must be signed in to change notification settings - Fork 13
/
utilities.py
59 lines (49 loc) · 1.43 KB
/
utilities.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import copy
from globals import pygame
class Counter:
"""
Useful to count frames.
"""
def __init__(self, frames):
"""
:param frames: Target frames to count to
"""
self.max_frames = frames
self.current_frame = 0
def update(self, amount=1):
"""
Increase counter by 1 or more
:param amount: Amount of frames, standard: 1
:return: True or False, if target is reached or not
"""
self.current_frame += amount
return self.evaluate()
def evaluate(self):
"""
:return: True or False, if target is reached or not
"""
return self.current_frame >= self.max_frames
def reset(self):
"""
Resets the counter
:return: self
"""
self.current_frame = 0
return self
def split_tiled_image(image, tile_size, colorkey=None):
"""Splits a tiled image into single tiles in a return_list."""
image_size = image.get_size()
tmp_surface = pygame.Surface(tile_size)
tmp_surface.set_colorkey(colorkey)
return_list = []
# For tilenr in y axis...
for y in range(int(image_size[1]/tile_size[1])):
# For tilenr in x axis...
for x in range(int(image_size[0]/tile_size[0])):
# ...blit the cutout at (tilenr_x*tilesize_x, tilenr_y*tilesize_y) on tmp_surface
tmp_surface.blit(image, (0, 0), ((x*tile_size[0], y*tile_size[1]), tile_size))
# Append a copy of tmp_surface to the return_list
return_list.append(copy.copy(tmp_surface))
# Fill the tmp_surface black again
tmp_surface.fill((0, 0, 0))
return return_list