Actual code below
According to behavior i assume the engine suffering difficulties preloading so many sprites. Have you any ways/ideas to make it faster or maybe preload it separately in chunks, so i could sneak it in game without much notice.
Thanks!
Code: Select all
init python:
import math
def inrange(input,min,max): #ensures input numbers won't fall out of range
if input<min:
return min
elif input> max:
return max
else:
return input
class MosaicStats(): #bunch of support global vars
def __init__(self,child,od,td,pixel):
self.sx = config.screen_width
self.sy = config.screen_height
self.od = od #radius of small circle of opaque squares
self.td = td #big circle of semi-transparent squares
self.child = child #blurred ver.of image pixellated
self.pixel = pixel #pixellation square size
self.children = [] #list of Sprite objects
self.predict = [] #list of predictable objects
self.mx = 0 #coordinates of mouse pointer
self.my = 0
m = MosaicStats('images/cg-blur.png',2,4,32)
def predict_spawn(ix=None): #takes an argument for verbose mode
if ix==None:
for ix in range(0,m.sx,m.pixel):
for iy in range(0,m.sy,m.pixel):
ix_=inrange(ix,0,m.sx-1) #get last pixel if beyond dimentions
iy_=inrange(iy,0,m.sy-1)
t = im.Crop(m.child,(ix_,iy_,1,1))
t = im.Scale(t,m.pixel,m.pixel)
m.predict.append(t)
t = im.Alpha(t,.5)
m.predict.append(t)
else:
for iy in range(0,m.sy,m.pixel):
ix_=inrange(ix,0,m.sx-1) #get last pixel if beyond dimentions
iy_=inrange(iy,0,m.sy-1)
t = im.Crop(m.child,(ix_,iy_,1,1))
t = im.Scale(t,m.pixel,m.pixel)
#scaled 1pixel crop = pixellation square :)
m.predict.append(t)
t = im.Alpha(t,.5)
m.predict.append(t)
#predict_spawn()
def mosaic_spawn():
i = 0
for ix in range(0,m.sx,m.pixel):
for iy in range(0,m.sy,m.pixel):
ix_=inrange(ix,0,m.sx-1) #get last pixel if beyond dimentions
iy_=inrange(iy,0,m.sy-1)
m.children.append(mosaic.create(Null()))
o = m.children[-1] #throw addition entities to just created Sprite
o.t = i #opaque child of each squares
i+=1
o.t5 = i
i+=1
o.x = ix #coordinates of each
o.y = iy
o.a = 0 #alpha value of each
def mosaic_update(st):
return .04
def mosaic_event(ev, x, y, st):
global m
if m.children == []:
mosaic_spawn()
if True: #delay to ensure mosaic_spawn() runs properly
m.mx, m.my = (x,y)
for i in m.children:
r = math.hypot(i.x-m.mx,i.y-m.my)
if r>m.td*m.pixel: #hide all pixels if too far
if i.a == 0:
continue
else:
i.a = 0
i.set_child(Null())
else:
i.a = 1 if r<m.od*m.pixel else .5
i.set_child(m.predict[i.t] if r<m.od*m.pixel else m.predict[i.t5] )
#opaque pixel if near, semi-transparent if a bit farther
def mosaic_predict():
return m.predict
mosaic = SpriteManager(update=mosaic_update,event=mosaic_event,predict=mosaic_predict)
label main_menu:
return
label start:
scene cg
$ ix = 0
while ix<m.sx:
$predict_spawn(ix)
'LOADING [ix]{nw}'
$ix+=m.pixel
show expression mosaic as mosaic
'TEST POINT 1'
'TEST POINT 2'
'RESTART POINT'
return