-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGIFImage.py
73 lines (55 loc) · 2.53 KB
/
GIFImage.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import os
import re
import pygame
from PIL import Image
#HOW TO USE THIS CLASS!!
#Put the animation frames in a folder, and give that folderpath to the gifImage object
#You can set the size, placement, and how many frames you want each picture to be!
#The game is currently set to 60 fps.
class GIFImage(object):
def __init__(self, folderPath, x=0, y=0, frameCycleLen = 1): #Initialize
self.folderPath = folderPath
self.imgNames = []
self.images = []
self.x = x
self.y = y
self.center = (self.x, self.y)
self.frameNum = 0
self.frameCycleLen = frameCycleLen
self.frameCycleCount = 1
self.getFrames()
def setCoords(self, x, y): #Set coordinates
self.x = x
self.y = y
def frameCycleLen(self, cycleLen): #Sets how many frames per photo
self.frameCycleLen = cycleLen
def getFrames(self): #Generates the frames and gets them ready to be iterated over
self.imgNames = os.listdir(self.folderPath)
self.imgNames = self.naturalSort(self.imgNames)
for i in range(len(self.imgNames)):
self.images.append(pygame.image.load(self.folderPath + "/" + self.imgNames[i]))
self.width = self.images[0].get_width
self.height = self.images[0].get_height
def resize(self, width, height): #Resizes each frame
for i in range(len(self.images)):
self.images[i] = pygame.transform.scale(self.images[i], (width, height))
self.width = width
self.height = height
def animate(self, screen): #Iterates over the images (call this in the loop where you want the image to be animated!)
screen.blit(self.images[self.frameNum], (self.x, self.y))
if self.frameCycleCount >= self.frameCycleLen:
self.frameNum = self.frameNum + 1
self.frameCycleCount = 1
if self.frameNum >= len(self.images):
self.frameNum = 0
self.frameCycleCount = self.frameCycleCount + 1
def naturalSort(self, l): #Sorts image names in natural order
convert = lambda text: int(text) if text.isdigit() else text.lower()
alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ]
return sorted(l, key = alphanum_key)
def get_width(self): #Gets width of image
return self.width
def get_height(self): #Gets height of image
return self.height
def get_cycleEnd(self): #Tells you when its about to change the image
return (self.frameCycleCount >= self.frameCycleLen)