forked from digiholic/universalSmashSystem
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspriteObject.py
More file actions
213 lines (156 loc) · 6.95 KB
/
spriteObject.py
File metadata and controls
213 lines (156 loc) · 6.95 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import pygame
import os
class Sprite(pygame.sprite.Sprite):
#Create with image
def __init__(self, topleft):
pygame.sprite.Sprite.__init__(self)
#Set whether the object is visible
self.visible = True
#Set whether the object is solid
self.solid = True
self.topleft = topleft
def draw(self,screen,offset,scale):
h = int(round(self.rect.height * scale))
w = int(round(self.rect.width * scale))
newOff = (int(offset[0] * scale), int(offset[1] * scale))
blitSprite = pygame.transform.smoothscale(self.image, (w,h))
screen.blit(blitSprite,pygame.Rect(newOff,(w,h)))
class ImageSprite(Sprite):
def __init__(self,image,topleft, colorKey = [255,255,255],generateAlpha = True, filepath = __file__):
Sprite.__init__(self, topleft)
self.index = 0
self.imagePrefix = os.path.join(os.path.dirname(filepath),"sprites")
self.imageText = image
self.image = pygame.image.load(os.path.join(self.imagePrefix, self.imageText + ".png"))
self.alpha = generateAlpha
if self.alpha:
self.colorKey = colorKey
self.image.set_colorkey(self.colorKey)
self.flip = False
self.angle = 0
self.image.convert_alpha()
self.rect = self.image.get_rect()
self.rect.topleft = self.topleft
def getImageAtIndex(self,index):
self.index = index
self.changeImage(self.imageText + str(self.index))
def flipX(self):
self.flip = not self.flip
def changeImage(self,imageText):
self.imageText = imageText
self.image = pygame.image.load(os.path.join(self.imagePrefix, self.imageText + ".png"))
if self.flip: self.image = pygame.transform.flip(self.image,True,False)
self.rect = self.image.get_rect(center=self.rect.center)
def draw(self,screen,offset,scale):
if self.alpha: self.image.set_colorkey(self.colorKey)
self.image = self.image.convert_alpha()
Sprite.draw(self, screen, offset, scale)
class SheetSprite(ImageSprite):
def __init__(self,image,topleft,offset,colorKey = [255,255,255],generateAlpha = True,filepath = __file__):
Sprite.__init__(self,topleft)
self.index = 0
self.offset = offset
self.imagePrefix = os.path.join(os.path.dirname(filepath),"sprites/")
self.imageText = image
self.sheet = pygame.image.load(os.path.join(self.imagePrefix, self.imageText + ".png"))
self.sheet.set_clip(pygame.Rect(self.index * self.offset, 0, self.offset,self.sheet.get_height()))
self.image = self.sheet.subsurface(self.sheet.get_clip())
self.alpha = generateAlpha
if self.alpha:
self.colorKey = colorKey
self.image.set_colorkey(self.colorKey)
self.flip = False
self.angle = 0
self.image.convert_alpha()
self.rect = self.image.get_rect()
self.rect.topleft = self.topleft
def getImageAtIndex(self,index):
self.index = index
self.changeImage(self.imageText,False)
def changeImage(self,imageText, reset = True):
self.imageText = imageText
if reset: self.index = 0
self.sheet = pygame.image.load(os.path.join(self.imagePrefix, self.imageText + ".png"))
self.sheet.set_clip(pygame.Rect(self.index * self.offset, 0, self.offset,self.sheet.get_height()))
#self.image = self.sheet.subsurface(self.sheet.get_clip())
self.image = self.sheet.subsurface(self.sheet.get_clip())
self.image = self.image.convert_alpha()
#self.color_surface([0, 255, 255])
#self.recolor([0,0,0], [0,0,255])
if self.flip: self.image = pygame.transform.flip(self.image,True,False)
self.rect = self.image.get_rect(center=self.rect.center)
def color_surface(self,color):
arr = pygame.surfarray.pixels3d(self.image)
arr[:,:,0] = color[0]
arr[:,:,1] = color[1]
arr[:,:,2] = color[2]
def recolor(self,fromColor,toColor):
arr = pygame.PixelArray(self.image)
arr.replace(fromColor,toColor)
del arr
class MaskSprite(ImageSprite):
def __init__(self, parentSprite,color,duration,pulse = False,pulseSize = 16):
self.parentSprite = parentSprite
self.duration = duration
self.pulse = pulse
self.pulseSize = pulseSize
self.color = color
if self.pulse: self.alpha = 200
else: self.alpha = 128
self.visible = True
self.image = self.parentSprite.image.copy()
self.color_surface(self.color)
def color_surface(self,color):
arr = pygame.surfarray.pixels3d(self.image)
arr[:,:,0] = color[0]
arr[:,:,1] = color[1]
arr[:,:,2] = color[2]
del arr
arr = pygame.surfarray.pixels_alpha(self.image)
arr[arr>self.alpha] = self.alpha
del arr
def update(self):
if self.duration > 0:
if self.pulse:
self.alpha -= self.pulseSize
if self.alpha > 200:
self.alpha = 200
self.pulseSize = -self.pulseSize
elif self.alpha < 0:
self.alpha = 0
self.pulseSize = -self.pulseSize
self.duration -= 1
self.image = self.parentSprite.image.copy()
self.color_surface(self.color)
self.rect = self.parentSprite.rect
return self
else:
return None
def draw(self, screen, offset, scale):
Sprite.draw(self, screen, offset, scale)
class RectSprite(Sprite):
def __init__(self,topleft,size,color=[0,0,0]):
Sprite.__init__(self,topleft)
self.topleft = topleft
self.size = size
self.color = color
self.image = pygame.Surface(self.size)
self.image.fill(self.color)
self.rect = self.image.get_rect()
self.rect.topleft = self.topleft
self.image.set_alpha(128)
class TextSprite(Sprite):
def __init__(self,topleft,text,font = None,color=[255,255,255],background=None):
Sprite.__init__(self,topleft)
if (font is None):
self.font = pygame.font.SysFont("monospace", 20)
else:
self.font = font
self.text = text
self.color = color
self.background = background
self.image = self.font.render(text,1,color)
self.rect = self.image.get_rect()
self.rect.topleft = self.topleft
def update(self):
self.image = self.font.render(self.text,1,self.color)