Changeset 35
- Timestamp:
- 08/24/08 22:51:32 (5 months ago)
- Files:
-
- project/trunk/.project (added)
- project/trunk/example/particle.py (deleted)
- project/trunk/example/particle/explosion.py (modified) (1 diff)
- project/trunk/example/particle/fountain.py (modified) (1 diff)
- project/trunk/example/particle/mouse_gravity.py (modified) (2 diffs)
- project/trunk/src/pyxoo/displayobject.py (modified) (2 diffs)
- project/trunk/src/pyxoo/engine.py (modified) (1 diff)
- project/trunk/src/pyxoo/particle/action.py (modified) (5 diffs)
- project/trunk/src/pyxoo/particle/initializer.py (modified) (4 diffs)
- project/trunk/src/pyxoo/particle/renderer.py (modified) (1 diff)
- project/trunk/src/pyxoo/particle/system.py (modified) (1 diff)
- project/trunk/src/pyxoo/structure.py (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
project/trunk/example/particle/explosion.py
r34 r35 1 1 2 2 3 import pygame, pyxoo, math, os 3 import pygame, pyxoo, math, os, sys 4 4 5 5 from pyxoo.particle import DisplayObjectParticle 6 6 7 RESOLUTION = 640, 4807 RESOLUTION = 1680, 1050 8 8 COLOR = 255,150,0 9 9 10 10 class App(pyxoo.engine.Application): 11 11 def init(self): 12 os.environ["SDL_VIDEODRIVER"] = "quartz"13 12 if sys.platform == "win32": 13 os.environ["SDL_VIDEODRIVER"] = "windib" 14 14 pygame.init() 15 self.screen = pygame.display.set_mode(RESOLUTION,pygame.HWSURFACE )15 self.screen = pygame.display.set_mode(RESOLUTION,pygame.HWSURFACE|pygame.DOUBLEBUF|pygame.FULLSCREEN, 32) 16 16 self.bg = pygame.Surface(self.screen.get_size()).convert() 17 17 project/trunk/example/particle/fountain.py
r34 r35 1 1 2 2 3 import pygame, pyxoo, math, random, os 3 import pygame, pyxoo, math, random, os, sys 4 4 5 RESOLUTION = 640, 4805 RESOLUTION = 1680, 1050 6 6 COLOR = 0,255,255 7 7 8 8 class App(pyxoo.engine.Application): 9 9 def init(self): 10 os.environ["SDL_VIDEODRIVER"] = "quartz" 10 if sys.platform == "win32": 11 os.environ["SDL_VIDEODRIVER"] = "windib" 11 12 pygame.init() 12 self.screen = pygame.display.set_mode(RESOLUTION, pygame.HWSURFACE)13 self.screen = pygame.display.set_mode(RESOLUTION,pygame.HWSURFACE|pygame.DOUBLEBUF|pygame.FULLSCREEN, 32) 13 14 self.bg = pygame.Surface(self.screen.get_size()).convert() 14 15 self.input_dispatcher.add_event_listener(pyxoo.event.onQuitEVENT, self.onQuit) 16 self.input_dispatcher.add_event_listener(pyxoo.event.onKeyboardKeyDownEVENT, self.onQuit) 15 17 16 18 self.monitor = pyxoo.utils.monitor.Monitor() 17 19 self.monitor.add_field(pyxoo.utils.monitor.FPSField(self.clock, 100, (0, 0, 255))) 18 self.monitor.add_field(pyxoo.utils.monitor.ParticleField( 1000, (255,0,0)))20 self.monitor.add_field(pyxoo.utils.monitor.ParticleField(3000, (255,0,0))) 19 21 self.monitor.start() 20 22 self.all_sprites = pygame.sprite.RenderPlain((self.monitor)) 21 23 22 24 initializer = pyxoo.particle.initializer.StackInitializer() 23 initializer.append(pyxoo.particle.initializer.ImageClassInitializer(pyxoo.displayobject. Dot, 2))25 initializer.append(pyxoo.particle.initializer.ImageClassInitializer(pyxoo.displayobject.Star, 5)) 24 26 initializer.append(pyxoo.particle.initializer.LifeInitializer(3000, 1000)) 25 initializer.append(pyxoo.particle.initializer.ExplosionInitializer(200, pyxoo.structure.Range(math.pi-.1, math.pi+.1), 50)) 27 #initializer.append(pyxoo.particle.initializer.ExplosionInitializer(400, pyxoo.structure.Range(math.pi-.1, math.pi+.1), 50)) 28 initializer.append(pyxoo.particle.initializer.ExplosionInitializer(400, pyxoo.structure.Range(math.pi*-.25, math.pi*.25), 50)) 26 29 27 30 action = pyxoo.particle.action.StackAction() 28 31 action.append(pyxoo.particle.action.LifeAction()) 29 32 action.append(pyxoo.particle.action.ForceAction(pyxoo.structure.Point(0,90))) 30 action.append(pyxoo.particle.action.ForceAction(pyxoo.structure.Point(30,0)))33 #action.append(pyxoo.particle.action.ForceAction(pyxoo.structure.Point(50,0))) 31 34 action.append(pyxoo.particle.action.FrictionAction(.995)) 32 35 action.append(pyxoo.particle.action.MoveAction()) 33 36 34 37 renderer = pyxoo.particle.renderer.BitmapRenderer(self.screen) 35 emission = pyxoo.particle.emission.ByRateEmission(pyxoo.particle.DisplayObjectParticle, pyxoo.structure.Point( 300, 350 ), 100)36 38 39 emission = pyxoo.particle.emission.ByRateEmission(pyxoo.particle.DisplayObjectParticle, pyxoo.structure.Point( 300, 5 ), 200) 37 40 self.system = pyxoo.particle.system.SimpleParticleSystem(initializer, action, renderer) 38 41 self.system.emit(emission) 42 43 emission2 = pyxoo.particle.emission.ByRateEmission(pyxoo.particle.DisplayObjectParticle, pyxoo.structure.Point( 700, 5 ), 200) 44 self.system2 = pyxoo.particle.system.SimpleParticleSystem(initializer, action, renderer) 45 self.system2.emit(emission2) 46 47 emission3 = pyxoo.particle.emission.ByRateEmission(pyxoo.particle.DisplayObjectParticle, pyxoo.structure.Point( 1100, 5 ), 200) 48 self.system3 = pyxoo.particle.system.SimpleParticleSystem(initializer, action, renderer) 49 self.system3.emit(emission3) 39 50 40 51 def update(self): project/trunk/example/particle/mouse_gravity.py
r34 r35 1 1 2 2 3 import pygame, pyxoo, math, random, os 3 import pygame, pyxoo, math, random, os, sys 4 4 5 5 from pyxoo.particle import DisplayObjectParticle 6 6 from pyxoo.structure import Point 7 7 8 RESOLUTION = 640, 4808 RESOLUTION = 1680, 1050 9 9 COLOR = 0,255,255 10 10 11 11 class App(pyxoo.engine.Application): 12 12 def init(self): 13 os.environ["SDL_VIDEODRIVER"] = "quartz" 13 if sys.platform == "win32": 14 os.environ["SDL_VIDEODRIVER"] = "windib" 14 15 pygame.init() 15 self.screen = pygame.display.set_mode(RESOLUTION, pygame.HWSURFACE)16 self.screen = pygame.display.set_mode(RESOLUTION,pygame.HWSURFACE|pygame.DOUBLEBUF|pygame.FULLSCREEN, 32) 16 17 self.bg = pygame.Surface(self.screen.get_size()).convert() 17 18 self.input_dispatcher.add_event_listener(pyxoo.event.onQuitEVENT, self.onQuit) 19 self.input_dispatcher.add_event_listener(pyxoo.event.onKeyboardKeyDownEVENT, self.onQuit) 18 20 19 21 self.monitor = pyxoo.utils.monitor.Monitor() 20 self.monitor.add_field(pyxoo.utils.monitor.FPSField(self.clock, 100, (0, 0, 255)))22 self.monitor.add_field(pyxoo.utils.monitor.FPSField(self.clock, 300, (0, 0, 255))) 21 23 self.monitor.add_field(pyxoo.utils.monitor.ParticleField(1000, (255,0,0))) 22 24 self.monitor.start() … … 24 26 25 27 initializer = pyxoo.particle.initializer.StackInitializer() 26 initializer.append(pyxoo.particle.initializer.ImageClassInitializer(pyxoo.displayobject. Dot, 15))28 initializer.append(pyxoo.particle.initializer.ImageClassInitializer(pyxoo.displayobject.Star, 5)) 27 29 initializer.append(pyxoo.particle.initializer.LifeInitializer(3000, 1000)) 28 initializer.append(pyxoo.particle.initializer.ExplosionInitializer(3, pyxoo.structure.Range(math.pi-.1, math.pi+.1), 50)) 30 #initializer.append(pyxoo.particle.initializer.ExplosionInitializer(3, pyxoo.structure.Range(math.pi-.1, math.pi+.1), 50)) 31 initializer.append(pyxoo.particle.initializer.BoxInitializer(self.screen.get_rect())) 32 29 33 30 34 action = pyxoo.particle.action.StackAction() 35 action.append(pyxoo.particle.action.MouseGravity(120, 100)) 36 action.append(pyxoo.particle.action.BoundingBox(Point(0,0), Point(RESOLUTION[0], RESOLUTION[1]))) 37 #action.append(pyxoo.particle.action.MutualGravity(500, 10)) 38 action.append(pyxoo.particle.action.SpeedLimit(150)) 31 39 action.append(pyxoo.particle.action.MoveAction()) 32 action.append(pyxoo.particle.action.MouseGravity(120, 100))33 40 34 41 renderer = pyxoo.particle.renderer.BitmapRenderer(self.screen) 35 emission = pyxoo.particle.emission.FixedCountEmission(DisplayObjectParticle, Point( 300, 350 ), 4)42 emission = pyxoo.particle.emission.FixedCountEmission(DisplayObjectParticle, Point( 300, 350 ), 30) 36 43 37 44 self.system = pyxoo.particle.system.SimpleParticleSystem(initializer, action, renderer) project/trunk/src/pyxoo/displayobject.py
r34 r35 1 1 2 2 3 import pygame 3 import pygame, math, pyxoo 4 from pyxoo.structure import Point 4 5 6 _BLACK = pygame.Color('black') 7 _WHITE = pygame.Color('white') 5 8 6 def get_color_key(color):7 if color == (0, 0, 0):8 return (255, 255, 255)9 def _get_color_key(color): 10 if color == _BLACK: 11 return _WHITE 9 12 else: 10 return (0, 0, 0) 13 return _BLACK 14 15 def _add_radius(radius, point): 16 point.x += radius 17 point.y += radius 18 return point 11 19 12 20 13 21 class SurfaceWithColorKey(pygame.Surface): 14 def __init__(self, size, color_key= (0, 0, 0)):22 def __init__(self, size, color_key=_BLACK): 15 23 pygame.Surface.__init__(self, size) 16 if color_key != (0, 0, 0):24 if color_key != _BLACK: 17 25 self.fill(color_key) 18 26 self.set_colorkey(color_key, pygame.RLEACCEL) … … 20 28 21 29 class Dot(SurfaceWithColorKey): 22 def __init__(self, radius, color= (255, 255, 255)):23 SurfaceWithColorKey.__init__(self, (radius *2, radius*2),get_color_key(color))30 def __init__(self, radius, color=_WHITE): 31 SurfaceWithColorKey.__init__(self, (radius * 2, radius * 2), _get_color_key(color)) 24 32 pygame.draw.circle(self, color, (radius, radius), radius) 33 34 35 class Star(SurfaceWithColorKey): 36 def __init__(self, radius, color=_WHITE, width=1): 37 SurfaceWithColorKey.__init__(self, (radius * 2, radius * 2), _get_color_key(color)) 38 rot_step = math.pi / 5.0 39 inner_radius = radius * math.cos(rot_step * 2) 40 half_pi = math.pi * 0.5 41 42 points = list() 43 points.append((radius, 0)) 44 points.append(_add_radius(radius, Point.polar(inner_radius, rot_step - half_pi)).to_sequence()) 45 points.append(_add_radius(radius, Point.polar(radius, 2 * rot_step - half_pi)).to_sequence()) 46 points.append(_add_radius(radius, Point.polar(inner_radius, 3 * rot_step - half_pi)).to_sequence()) 47 points.append(_add_radius(radius, Point.polar(radius, 4 * rot_step - half_pi)).to_sequence()) 48 points.append(_add_radius(radius, Point.polar(inner_radius, 5 * rot_step - half_pi)).to_sequence()) 49 points.append(_add_radius(radius, Point.polar(radius, 6 * rot_step - half_pi)).to_sequence()) 50 points.append(_add_radius(radius, Point.polar(inner_radius, 7 * rot_step - half_pi)).to_sequence()) 51 points.append(_add_radius(radius, Point.polar(radius, 8 * rot_step - half_pi)).to_sequence()) 52 points.append(_add_radius(radius, Point.polar(inner_radius, 9 * rot_step - half_pi)).to_sequence()) 53 pygame.draw.polygon(self, color, points, width) 54 project/trunk/src/pyxoo/engine.py
r34 r35 1 import pyxoo, pyxoo.event, pygame 1 2 2 3 4 import pyxoo, pyxoo.event 5 6 7 8 9 import pygame 3 __version__ = pyxoo.__version__ 4 __licence__ = pyxoo.__licence__ 5 __url__ = pyxoo.__url__ 6 __author__ = "Alexis Couronne" 10 7 11 8 12 9 class Application: 13 14 15 10 def __init__(self, max_fps=0, flip_method=pygame.display.flip): 16 11 self.input_dispatcher = pyxoo.event.InputDispatcher() project/trunk/src/pyxoo/particle/action.py
r34 r35 5 5 6 6 class ActionStrategy: 7 def set_system(self, system): 8 pass 9 10 def get_system(self): 11 pass 12 7 13 def prepare_action(self, time): 8 14 pass … … 14 20 class AbstractAction(ActionStrategy): 15 21 _time_step = None 22 def set_system(self, system): 23 self._system = system 24 25 def get_system(self): 26 return self._system 27 16 28 def prepare_action(self, time): 17 29 self._time_step = time / 1000.0 … … 25 37 26 38 27 class StackAction(ActionStrategy, list): 39 class StackAction(ActionStrategy, list): 40 def set_system(self, system): 41 self._system = system 42 for action in self: 43 action.set_system(system) 44 45 def get_system(self): 46 return self._system 47 28 48 def prepare_action(self, time): 29 49 for action in self: … … 49 69 50 70 51 class _SineData:52 def __init__(self, position, sine=0, amplitude=1, frequency=1, direction=None):53 self.position = position54 self.sine = sine55 self.amplitude56 self.frequency = frequency57 self.direction = direction58 59 60 class SineMoveAction(AbstractAction):61 def __init__(self, direction, amplitude=0, frequency=1, random_start=0, random_direction=0, random_amplitude=0, random_frequency=0):62 self._data = dict()63 self._direction = direction64 self._amplitude = amplitude65 self._frequency = frequency * math.pi * 266 self._random_start = random_start67 self._random_direction = random_direction68 self._random_amplitude = random_amplitude69 self._random_frequency = random_frequency * math.pi * 270 71 def process(self, particle):72 if particle not in self._data:73 self._data[particle] = _SineData(particle.position.clone(), )74 #TODO: a finir75 particle.last_position.x = particle.position.x76 particle.last_position.y = particle.position.y77 data = self._data[particle]78 pos = data.position79 norm = data.direction.clone()80 #TODO: PointUtils.scale( norm, data.amplitude * Math.sin( data.sine ) );81 pos.x += particle.velocity.x * self._time_step82 pos.y += particle.velocity.y * self._time_step83 particle.position = pos + norm84 data.sine += data.frequency *self._time_step85 71 #class _SineData: 72 # def __init__(self, position, sine=0, amplitude=1, frequency=1, direction=None): 73 # self.position = position 74 # self.sine = sine 75 # self.amplitude 76 # self.frequency = frequency 77 # self.direction = direction 78 # 79 # 80 #class SineMoveAction(AbstractAction): 81 # def __init__(self, direction, amplitude=0, frequency=1, random_start=0, random_direction=0, random_amplitude=0, random_frequency=0): 82 # self._data = dict() 83 # self._direction = direction 84 # self._amplitude = amplitude 85 # self._frequency = frequency * math.pi * 2 86 # self._random_start = random_start 87 # self._random_direction = random_direction 88 # self._random_amplitude = random_amplitude 89 # self._random_frequency = random_frequency * math.pi * 2 90 # 91 # def process(self, particle): 92 # if particle not in self._data: 93 # self._data[particle] = _SineData(particle.position.clone(), ) 94 # #TODO: a finir 95 # particle.last_position.x = particle.position.x 96 # particle.last_position.y = particle.position.y 97 # data = self._data[particle] 98 # pos = data.position 99 # norm = data.direction.clone() 100 # #TODO: PointUtils.scale( norm, data.amplitude * Math.sin( data.sine ) ); 101 # pos.x += particle.velocity.x * self._time_step 102 # pos.y += particle.velocity.y * self._time_step 103 # particle.position = pos + norm 104 # data.sine += data.frequency *self._time_step 105 # 86 106 87 107 class MoveAction(AbstractAction): … … 177 197 PonctualForceAction.process(self, particle) 178 198 179 180 #class MouseGravity(AbstractAction): 181 # def __init__(self, power, epsilon=100): 182 # self._power = power 183 # self.set_epsilon(epsilon) 184 # 185 # def set_epsilon(self, epsilon): 186 # self._epsilon = epsilon * epsilon 187 # 188 # def get_epsilon(self): 189 # return self._epsilon 190 # 191 # def set_power(self, power): 192 # self._power = power * 10000 193 # 194 # def get_power(self): 195 # return self._power 196 # 197 # def process(self, particle): 198 # mouse_x, mouse_y = pygame.mouse.get_pos() 199 # x, y = mouse_x - particle.position.x, mouse_y - particle.position.y 200 # dsq = x * x + y * y 201 # if dsq != 0: 202 # d = math.sqrt(dsq) 203 # if dsq < self._epsilon: 204 # dsq = self._epsilon 205 # factor = (self._power * self._time_step) / (dsq*d) 206 # particle.velocity.x += x * factor 207 # particle.velocity.y += y * factor 208 199 200 class BoundingBox(AbstractAction): 201 def __init__(self, position, size): 202 self._position = position 203 self._size = size 204 205 def process(self, particle): 206 if particle.position.x > self._position.x + self._size.x: 207 particle.velocity.x *= -1 208 particle.position.x = self._position.x + self._size.x 209 elif particle.position.x < self._position.x: 210 particle.velocity.x *= -1 211 particle.position.x = self._position.x 212 213 if particle.position.y > self._position.y + self._size.y: 214 particle.velocity.y *= -1 215 particle.position.y = self._position.y + self._size.y 216 elif particle.position.y < self._position.y: 217 particle.velocity.y *= -1 218 particle.position.y = self._position.y 219 220 221 class MutualGravity(AbstractAction): 222 def __init__(self, force_radius=0, force_strength=0): 223 self.set_force_radius(force_radius) 224 self.set_force_strength(force_strength) 225 226 def set_force_radius(self, force_radius): 227 self._force_radius = force_radius 228 229 def get_force_radius(self): 230 return self._force_radius 231 232 def set_force_strength(self, force_strength): 233 self._force_strength = force_strength 234 235 def get_force_strength(self): 236 return self._force_strength 237 238 def process(self, particle): 239 for other in self._system.get_particles(): 240 if other != self: 241 force = particle.position - other.position 242 dist_length = force.get_length() 243 if dist_length <= self._force_radius: 244 l = self._force_strength * self._time_step 245 try: 246 force.normalize(l) 247 except: 248 pass 249 particle.velocity -= force 250 251 252 class SpeedLimit(AbstractAction): 253 def __init__(self, speed, is_minimum=False): 254 self.set_speed(speed) 255 self.set_is_minimum(is_minimum) 256 257 def is_minimum(self): 258 return self._is_minimum 259 260 def set_is_minimum(self, is_minimum): 261 self._is_minimum = is_minimum 262 263 def get_speed(self): 264 return self._speed 265 266 def set_speed(self, speed): 267 if speed < 0: 268 speed = 0 269 self._speed = speed 270 self._speed_sqrt = speed * speed 271 272 def process(self, particle): 273 speed_sq = particle.velocity.x * particle.velocity.x + particle.velocity.y * particle.velocity.y; 274 if ( self._is_minimum and speed_sq < self._speed_sqrt ) or ( not self._is_minimum and speed_sq > self._speed_sqrt ): 275 scale = self._speed / math.sqrt( speed_sq ) 276 particle.velocity.x *= scale 277 particle.velocity.y *= scale 278 279 project/trunk/src/pyxoo/particle/initializer.py
r34 r35 27 27 28 28 def initialize(self, particle): 29 particle.max_life = self._life + round( random.random() * self._rand - self._rand / 2)29 particle.max_life = self._life + round(random.random() * self._rand - self._rand / 2) 30 30 31 31 … … 35 35 36 36 def initialize(self, particle): 37 particle.last_position.x = particle.position.x = self._area. x+ random.random() * self._area.width38 particle.last_position.y = particle.position.y = self._area. y+ random.random() * self._area.height37 particle.last_position.x = particle.position.x = self._area.left + random.random() * self._area.width 38 particle.last_position.y = particle.position.y = self._area.top + random.random() * self._area.height 39 39 40 40 … … 47 47 48 48 def initialize(self, particle): 49 #TODO: A implementer 50 pass 49 p = self._segment.clone() 50 p.scale(random.random()) 51 particle.last_position.x = particle.position.x = self._a.x + p.x + pyxoo.structure.random_balance(self._rand) 52 particle.last_position.y = particle.position.y = self._a.y + p.y + pyxoo.structure.random_balance(self._rand) 53 51 54 52 55 … … 91 94 92 95 def initialize(self, particle): 93 particle.image = self._image(*self._args) 96 particle.image = self._image(*self._args).convert() project/trunk/src/pyxoo/particle/renderer.py
r34 r35 30 30 self._canvas.lock() 31 31 for particle in particles: 32 self._canvas.set_at(particle.position.to_sequence(), particle.color)32 pygame.draw.rect(self._canvas, particle.color, pygame.Rect(particle.position.to_sequence(), (1,2))) 33 33 self._canvas.unlock() 34 34 project/trunk/src/pyxoo/particle/system.py
r34 r35 111 111 def set_action(self, action): 112 112 self._action = action 113 self._action.set_system(self) 113 114 114 115 def get_action(self): project/trunk/src/pyxoo/structure.py
r34 r35 3 3 __version__ = pyxoo.__version__ 4 4 __licence__ = pyxoo.__licence__ 5 __url__ = pyxoo.__url__6 __author__ = "Alexis Couronne"5 __url__ = pyxoo.__url__ 6 __author__ = "Alexis Couronne" 7 7 8 8 import math, random 9 9 10 10 def distance(p1, p2): 11 return math.sqrt(((p2.x -p1.x)**2) + ((p2.y-p1.y)**2))11 return math.sqrt(((p2.x - p1.x) ** 2) + ((p2.y - p1.y) ** 2)) 12 12 13 13 def random_range(range): 14 14 return range.min + random.random() * (range.max - range.min) 15 15 16 def random_balance(value): 17 return random.random() * value + value / 2 16 18 17 19 class Point: … … 23 25 24 26 def __str__(self): 25 return '<Point (%d, %d)>' %(self.x, self.y)27 return '<Point (%d, %d)>' % (self.x, self.y) 26 28 27 29 def __repr__(self): … … 29 31 30 32 def __add__(self, other): 31 return Point(self.x +other.x, self.y+other.y)33 return Point(self.x + other.x, self.y + other.y) 32 34 33 35 def __sub__(self, other): 34 return Point(self.x -other.x, self.y-other.y)36 return Point(self.x - other.x, self.y - other.y) 35 37 36 38 def get_length(self): … … 47 49 self.x *= value 48 50 self.y *= value 51 52 @staticmethod 53 def polar(length, angle): 54 return Point(length * math.cos(angle), length * math.sin(angle)) 49 55 50 56 def clone(self): … … 73 79 74 80 def __str__(self): 75 return "<Range(%d, %d)>" %(self.min, self.max)81 return "<Range(%d, %d)>" % (self.min, self.max) 76 82 77 83 … … 94 100 self.a = math.cos(rad) 95 101 self.b = math.sin(rad) 96 self.c = - math.sin(rad)102 self.c = - math.sin(rad) 97 103 self.d = math.cos(rad) 98 104
