py_yugi_clone/scripts/utils.py
StochasticMouse 2fc9381d93 added partner
2024-07-13 15:29:03 +02:00

62 lines
2.0 KiB
Python

import pygame
import os
import numpy as np
TILE_DICT = {
1 : ('iso',0,True),
2 : ('iso',1,False),
3 : ('iso',2,False),
4 : ('iso',3,False),
5 : ('iso',4,False),
6 : ('iso',5,False)
}
BASE_IMG_PATH = 'data/images/'
def load_image(path):
img = pygame.image.load(BASE_IMG_PATH + path).convert()
img.set_colorkey((0,0,0))
return img
def load_images(path):
images = []
for img_name in sorted(os.listdir(BASE_IMG_PATH + path)):
images.append(load_image(path + '/' + img_name))
return images
def to_isometric(coord, tilesize):
#(1,0) -> (+tilesize[0]/2,+tilesize[1]/2)
#(0,1) -> (-tilesize[0]/2,+tilesize[1]/2)
#a must be (+tilesize[0]/2,-tilesize[0]/2)
# (+tilesize[1]/2,+tilesize[1]/2)
a = np.array([[1,-1],[1,1]])
a = 0.5 * a * tilesize[:,np.newaxis]
return np.squeeze(np.matmul(a,coord[:,np.newaxis]))
def to_cartesian(coord, tilesize):
#manually calculated the inverse of a in to_isometric
a = np.array([[1,1],[-1,1]])
a = a * (1/tilesize)
return np.squeeze(np.matmul(a,coord[:,np.newaxis]))
def to_isometric_pixel(coord):
a = np.array([[1,-1],[1,1]])
a = 0.5 * a
return np.squeeze(np.matmul(a,coord[:,np.newaxis]))
class Animation:
def __init__(self, images, img_dur=5, loop=True):
self.images = images
self.img_duration = img_dur
self.loop = loop
self.done = False
self.frame = 0 #in the sense of gameframe, not current frame of the animation
def copy(self):
return Animation(self.images, self.img_duration, self.loop)
def update(self):
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
def img(self):
return self.images[int(self.frame / self.img_duration)]