76 lines
2.1 KiB
Python
76 lines
2.1 KiB
Python
import pygame as pg
|
|
import os
|
|
|
|
img_path='assets/images'
|
|
WINDOW_SIZE=(800,600)
|
|
|
|
def load_image(path, colorkey=(0,0,0)):
|
|
img = pg.image.load(img_path + path).convert()
|
|
img.set_colorkey(colorkey)
|
|
return img
|
|
|
|
def load_images(dir_path):
|
|
images = []
|
|
for img_name in os.listdir(img_path + dir_path):
|
|
images.append(load_image(dir_path + '/' + img_name))
|
|
return images
|
|
|
|
def load_image_alpha(path):
|
|
img = pg.image.load(img_path + path).convert_alpha()
|
|
return img
|
|
|
|
def load_images_alpha(dir_path):
|
|
images = []
|
|
for img_name in os.listdir(img_path + dir_path):
|
|
images.append(load_image_alpha(dir_path + '/' + img_name))
|
|
return images
|
|
|
|
def tile_to_pixel(waypoint, tile_size, center=False):
|
|
if center:
|
|
return (
|
|
waypoint[0] * tile_size + tile_size // 2,
|
|
waypoint[1] * tile_size + tile_size // 2
|
|
)
|
|
else:
|
|
return (
|
|
waypoint[0] * tile_size,
|
|
waypoint[1] * tile_size
|
|
)
|
|
|
|
def pixel_to_tile(waypoint, tile_size):
|
|
return (
|
|
waypoint[0] / tile_size,
|
|
waypoint[1] / tile_size
|
|
)
|
|
|
|
class Animation:
|
|
def __init__(self, images, img_dur=5, loop=True):
|
|
self.images = images
|
|
self.loop = loop
|
|
self.img_duration = img_dur
|
|
self.done = False
|
|
self.frame = 0
|
|
self.playing = False # not playing until told to
|
|
|
|
def copy(self):
|
|
return Animation(self.images, self.img_duration, self.loop)
|
|
|
|
def update(self):
|
|
if not self.playing:
|
|
return
|
|
|
|
if self.loop:
|
|
self.frame = (self.frame + 1) % (self.img_duration * len(self.images))
|
|
else:
|
|
self.frame = min(self.frame + 1, self.img_duration * len(self.images) - 1)
|
|
if self.frame >= self.img_duration * len(self.images) - 1:
|
|
self.done = True
|
|
self.playing = False
|
|
|
|
def play_animation(self):
|
|
self.playing = True
|
|
self.done = False
|
|
self.frame = 0
|
|
|
|
def img(self):
|
|
return self.images[self.frame // self.img_duration] |