colortest: fixed some stuff

This commit is contained in:
madmaurice 2016-02-18 14:38:39 +01:00
parent a5220fb56c
commit 56cb6e3998

View file

@ -9,236 +9,239 @@ color_vel = (0,0,0)
running = True running = True
class Vector(tuple): class Vector(tuple):
def __add__(self,other): def __add__(self,other):
if len(self) != len(other): if len(self) != len(other):
raise Exception("Can't add vectors of different size: %d %d" % (len(self), len(other))) raise Exception("Can't add vectors of different size: %d %d" % (len(self), len(other)))
return Vector([ self[i]+other[i] for i in range(len(self)) ]) return Vector([ self[i]+other[i] for i in range(len(self)) ])
def __sub__(self,other): def __sub__(self,other):
if len(self) != len(other): if len(self) != len(other):
raise Exception("Can't subtract vectors of different size") raise Exception("Can't subtract vectors of different size")
return Vector([ self[i]-other[i] for i in range(len(self)) ]) return Vector([ self[i]-other[i] for i in range(len(self)) ])
def __mul__(self,factor): def __mul__(self,factor):
return Vector([ self[i]*factor for i in range(len(self)) ]) return Vector([ self[i]*factor for i in range(len(self)) ])
def __div__(self,factor): def __div__(self,factor):
return self.__mul__(1.0/factor) return self.__mul__(1.0/factor)
def __rmul__(self,factor): def __truediv__(self,factor):
return self.__mul__(factor) return self.__div__(factor)
def __rdiv__(self,factor): def __rmul__(self,factor):
raise NotImplemented return self.__mul__(factor)
def __iadd__(self,other): def __rdiv__(self,factor):
return self.__add__(other) raise NotImplemented
def __isub__(self,other): def __iadd__(self,other):
return self.__sub__(other) return self.__add__(other)
def __neg__(self): def __isub__(self,other):
return self.__mul__(-1.0) return self.__sub__(other)
def __setitem__(self,k,v): def __neg__(self):
raise Exception("Not available") # immutable return self.__mul__(-1.0)
def distance(self, other): def __setitem__(self,k,v):
if len(self) != len(other): raise Exception("Not available") # immutable
raise Exception("Can't calculate distance between vectors of different size")
return sqrt(sum([ (self[i]-other[i])**2 for i in range(len(self)) ]))
def length(self): def distance(self, other):
return self.distance( Vector( (0,0) ) ) if len(self) != len(other):
raise Exception("Can't calculate distance between vectors of different size")
return sqrt(sum([ (self[i]-other[i])**2 for i in range(len(self)) ]))
def length(self):
return self.distance( Vector( (0,0) ) )
class Tick(object): class Tick(object):
def __init__(self, tickrange): def __init__(self, tickrange):
self.tickrange = tickrange self.tickrange = tickrange
self.m_tick = 0 self.m_tick = 0
def tick(self): def tick(self):
self.m_tick = (self.m_tick+1)%self.tickrange self.m_tick = (self.m_tick+1)%self.tickrange
return self.m_tick == 0 return self.m_tick == 0
class Mode(object): class Mode(object):
def __init__(self, screensize): def __init__(self, screensize):
self.screensize = screensize self.screensize = screensize
def update(self): def update(self):
pass pass
def draw(self,screen): def draw(self,screen):
pass pass
def minmax(vmin,vmax): def minmax(vmin,vmax):
return lambda v: min(vmax,max(vmin,v)) return lambda v: min(vmax,max(vmin,v))
class TransitionMode(Mode): class TransitionMode(Mode):
t = 0.3 t = 0.3
a = 30 a = 30
def __init__(self, screensize): def __init__(self, screensize):
Mode.__init__(self,screensize) Mode.__init__(self,screensize)
self.color = Vector( (127,127,127) ) self.color = Vector( (127,127,127) )
self.color_vel = Vector( 2*self.a*random()+self.a for i in range(3) ) self.color_vel = Vector( 2*self.a*random()+self.a for i in range(3) )
self.utick = Tick(10) self.utick = Tick(10)
def update(self): def update(self):
if self.utick.tick(): if self.utick.tick():
randvec = Vector( (2*self.a*random()-self.a) for i in range(3) ) randvec = Vector( (2*self.a*random()-self.a) for i in range(3) )
self.color_vel = (1-self.t)*self.color_vel+self.t*randvec self.color_vel = (1-self.t)*self.color_vel+self.t*randvec
self.color = Vector( map(minmax(0,255), self.color+self.color_vel) ) self.color = Vector( map(minmax(0,255), self.color+self.color_vel) )
def draw(self,screen): def draw(self,screen):
screen.fill( self.color ) screen.fill( self.color )
class LelMode(Mode): class LelMode(Mode):
color = [(255,0,0),(0,255,0)] color = [(255,0,0),(0,255,0)]
size = 10 size = 10
def __init__(self,screensize): def __init__(self,screensize):
Mode.__init__(self,screensize) Mode.__init__(self,screensize)
self.offset = 0 self.offset = 0
self.tick = 0 self.tick = 0
def update(self): def update(self):
self.offset = (self.offset+1)%2 self.offset = (self.offset+1)%2
def draw(self,screen): def draw(self,screen):
w,h = screen.get_size() w,h = screen.get_size()
for x in range(w): for x in range(w):
color = self.color[ (x//self.size + self.offset)%2] color = self.color[ (x//self.size + self.offset)%2]
pygame.draw.line(screen, color, (x,0), (x,h) ) pygame.draw.line(screen, color, (x,0), (x,h) )
class BallMode(Mode): class BallMode(Mode):
def __init__(self,screensize): def __init__(self,screensize):
Mode.__init__(self,screensize) Mode.__init__(self,screensize)
self.ball = [200,200] self.ball = [200,200]
self.radius = 70 self.radius = 70
self.velocity = [10,10] self.velocity = [10,10]
def update(self): def update(self):
w, h = self.screensize w, h = self.screensize
if self.ball[0] + self.radius >= w: if self.ball[0] + self.radius >= w:
self.velocity[0] = -abs(self.velocity[0]) self.velocity[0] = -abs(self.velocity[0])
elif self.ball[0] - self.radius < 0: elif self.ball[0] - self.radius < 0:
self.velocity[0] = abs(self.velocity[0]) self.velocity[0] = abs(self.velocity[0])
elif self.ball[1] + self.radius >= h: elif self.ball[1] + self.radius >= h:
self.velocity[1] = -abs(self.velocity[1]) self.velocity[1] = -abs(self.velocity[1])
elif self.ball[1] - self.radius < 0: elif self.ball[1] - self.radius < 0:
self.velocity[1] = abs(self.velocity[1]) self.velocity[1] = abs(self.velocity[1])
for i in range(2): for i in range(2):
self.ball[i] += self.velocity[i] self.ball[i] += self.velocity[i]
def draw(self,screen): def draw(self,screen):
screen.fill( (0,0,0) ) screen.fill( (0,0,0) )
pygame.draw.circle(screen, (255,0,0), tuple(self.ball), self.radius) pygame.draw.circle(screen, (255,0,0), tuple(self.ball), self.radius)
class Particle: class Particle:
def __init__(self,color, pos, vel): def __init__(self,color, pos, vel):
self.color = color self.color = color
self.pos = Vector(pos) self.pos = Vector(pos)
self.vel = Vector(vel) self.vel = Vector(vel)
def updateVel(self,gravity,max_v=20): def updateVel(self,gravity,max_v=20):
gravity = Vector(gravity) gravity = Vector(gravity)
#Update velocity #Update velocity
self.vel = 0.95*self.vel+0.01*random()*(gravity-self.pos)+0.1*random()*Vector( randint(-70,70) for i in range(2) ) self.vel = 0.95*self.vel+0.01*random()*(gravity-self.pos)+0.1*random()*Vector( randint(-70,70) for i in range(2) )
#Limit speed #Limit speed
l = self.vel.length() l = self.vel.length()
self.vel *= max_v/l self.vel *= max_v/l
def updatePos(self): def updatePos(self):
self.pos = self.pos + self.vel self.pos = self.pos + self.vel
def draw(self,screen): def draw(self,screen):
w,h = 1280,800 w,h = 1280,800
if self.pos[0] > 0 and self.pos[0] < w and self.pos[1] > 0 and self.pos[1] < h: if self.pos[0] > 0 and self.pos[0] < w and self.pos[1] > 0 and self.pos[1] < h:
pos = tuple(map(int,self.pos)) pos = tuple(map(int,self.pos))
velpos = tuple(map(int,self.pos+self.vel)) velpos = tuple(map(int,self.pos+self.vel))
pygame.draw.circle(screen, self.color, pos, 5) pygame.draw.circle(screen, self.color, pos, 5)
pygame.draw.line(screen, (255,255,255), pos, velpos, 1 ) pygame.draw.line(screen, (255,255,255), pos, velpos, 1 )
@staticmethod @staticmethod
def generate(xmax,ymax): def generate(xmax,ymax):
return Particle( tuple( randint(100,255) for i in range(3) ), ( randint(0,xmax), randint(0,ymax) ), tuple( randint(-50,50) for i in range(2) ) ) return Particle( tuple( randint(100,255) for i in range(3) ), ( randint(0,xmax), randint(0,ymax) ), tuple( randint(-50,50) for i in range(2) ) )
class SwarmMode(Mode): class SwarmMode(Mode):
swarmsize = 60 swarmsize = 60
def __init__(self,screensize): def __init__(self,screensize):
Mode.__init__(self,screensize) Mode.__init__(self,screensize)
self.particles = list( Particle.generate(1280,800) for i in range(self.swarmsize) ) self.particles = list( Particle.generate(1280,800) for i in range(self.swarmsize) )
self.generateGravity() self.generateGravity()
self.gtick = Tick(150) self.gtick = Tick(150)
self.utick = Tick(2) self.utick = Tick(2)
def generateGravity(self): def generateGravity(self):
#self.gravity = tuple( randint(0,m) for m in [1280,800] ) #self.gravity = tuple( randint(0,m) for m in [1280,800] )
self.gravity = Vector( (1280,800) )/2 self.gravity = Vector( (1280,800) )/2
def update(self): def update(self):
if self.gtick.tick(): if self.gtick.tick():
self.generateGravity() self.generateGravity()
v = self.utick.tick() v = self.utick.tick()
#king = self.particles[0] #king = self.particles[0]
#if v: king.updateVel(self.gravity, max_v=3) #if v: king.updateVel(self.gravity, max_v=3)
#king.updatePos() #king.updatePos()
for p in self.particles: for p in self.particles:
if v: p.updateVel(self.gravity) if v: p.updateVel(self.gravity)
p.updatePos() p.updatePos()
def draw(self,screen): def draw(self,screen):
screen.fill( (0,0,0) ) screen.fill( (0,0,0) )
for p in self.particles: for p in self.particles:
p.draw(screen) p.draw(screen)
class Game(object): class Game(object):
def __init__(self, modes=[SwarmMode,TransitionMode,LelMode,BallMode]): def __init__(self, modes=[SwarmMode,TransitionMode,LelMode,BallMode]):
self.modes = modes self.modes = modes
pygame.init() pygame.init()
self.screen = pygame.display.set_mode( (1280,800), pygame.FULLSCREEN ) self.screen = pygame.display.set_mode( (1280,800), pygame.HWSURFACE )
pygame.mouse.set_visible(False) pygame.mouse.set_visible(False)
self.setmode(0) self.setmode(0)
self.running = True self.running = True
def update(self): def update(self):
self.mode.update() self.mode.update()
def setmode(self,i): def setmode(self,i):
self.modeindex = i self.modeindex = i
self.mode = self.modes[i%len(self.modes)](self.screen.get_size()) self.mode = self.modes[i%len(self.modes)](self.screen.get_size())
def nextmode(self): def nextmode(self):
self.setmode( self.modeindex+1 ) self.setmode( self.modeindex+1 )
def draw(self): def draw(self):
self.mode.draw(self.screen) self.mode.draw(self.screen)
pygame.display.flip() pygame.display.flip()
def getevents(self): def getevents(self):
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == QUIT: if event.type == QUIT:
self.running = False self.running = False
elif event.type == KEYDOWN: elif event.type == KEYDOWN:
if event.key == K_ESCAPE: if event.key == K_ESCAPE:
pygame.event.post(pygame.event.Event(QUIT)) pygame.event.post(pygame.event.Event(QUIT))
elif event.key == K_SPACE: elif event.key == K_SPACE:
self.nextmode() self.nextmode()
def run(self): def run(self):
clock = pygame.time.Clock() clock = pygame.time.Clock()
while self.running: while self.running:
clock.tick(30) clock.tick(30)
self.getevents() self.getevents()
self.update() self.update()
self.draw() self.draw()
if __name__ == '__main__': if __name__ == '__main__':
Game().run() Game().run()