Changeset 36
- Timestamp:
- 08/29/08 14:38:04 (4 months ago)
- Files:
-
- project/trunk/example/particle/explosion.py (modified) (4 diffs)
- project/trunk/example/particle/fountain.py (modified) (3 diffs)
- project/trunk/example/particle/mouse_gravity.py (modified) (4 diffs)
- project/trunk/src/pyxoo/displayobject.py (modified) (1 diff)
- project/trunk/src/pyxoo/engine.py (modified) (4 diffs)
- project/trunk/src/pyxoo/event.py (modified) (3 diffs)
- project/trunk/src/pyxoo/particle/initializer.py (modified) (2 diffs)
- project/trunk/src/pyxoo/particle/system.py (modified) (2 diffs)
- project/trunk/src/pyxoo/utils/monitor.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
project/trunk/example/particle/explosion.py
r35 r36 5 5 from pyxoo.particle import DisplayObjectParticle 6 6 7 RESOLUTION = 1680, 10508 COLOR = 255,150,07 RESOLUTION = 800, 600 8 MAX_FPS = 120 9 9 10 class App(pyxoo.engine.Application):11 def init(self):10 class ExplosionSample(pyxoo.engine.Application): 11 def onInitApp(self, event): 12 12 if sys.platform == "win32": 13 13 os.environ["SDL_VIDEODRIVER"] = "windib" 14 14 pygame.init() 15 self.screen = pygame.display.set_mode(RESOLUTION, pygame.HWSURFACE|pygame.DOUBLEBUF|pygame.FULLSCREEN, 32)15 self.screen = pygame.display.set_mode(RESOLUTION, pygame.HWSURFACE | pygame.DOUBLEBUF, 32) 16 16 self.bg = pygame.Surface(self.screen.get_size()).convert() 17 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)20 self. input_dispatcher.add_event_listener(pyxoo.event.onMouseButtonDownEVENT, self.onClick)18 self.add_event_listener(pyxoo.event.onQuitEVENT, self.onQuit) 19 self.add_event_listener(pyxoo.event.onKeyboardKeyDownEVENT, self.onQuit) 20 self.add_event_listener(pyxoo.event.onMouseButtonDownEVENT, self.onClick) 21 21 22 22 self.monitor = pyxoo.utils.monitor.Monitor() … … 32 32 action = pyxoo.particle.action.StackAction() 33 33 action.append(pyxoo.particle.action.LifeAction()) 34 action.append(pyxoo.particle.action.ForceAction(pyxoo.structure.Point(0, 60)))34 action.append(pyxoo.particle.action.ForceAction(pyxoo.structure.Point(0, 60))) 35 35 action.append(pyxoo.particle.action.MoveAction()) 36 36 action.append(pyxoo.particle.action.OutOfBoxDeadAction(self.screen.get_rect())) … … 39 39 self.system = pyxoo.particle.system.SimpleParticleSystem(initializer, action, renderer) 40 40 41 def update(self):41 def onTick(self, event): 42 42 self.all_sprites.update() 43 43 44 def draw(self): 45 self.screen.blit(self.bg,(0,0)) 44 def onDraw(self, event): 45 pygame.display.flip() 46 self.screen.fill(pygame.Color("black")) 46 47 self.all_sprites.draw(self.screen) 47 48 48 49 def onClick(self, event): 49 50 x, y = event.get_value().pos 50 emission = pyxoo.particle.emission.ByRateLimitedEmission(DisplayObjectParticle, pyxoo.structure.Point( x, y), 500, 200, 100)51 emission = pyxoo.particle.emission.ByRateLimitedEmission(DisplayObjectParticle, pyxoo.structure.Point(x, y), 500, 200, 100) 51 52 self.system.emit(emission) 52 53 … … 56 57 57 58 if __name__ == '__main__': 58 App().run() 59 app = ExplosionSample(MAX_FPS) 60 app.run() project/trunk/example/particle/fountain.py
r35 r36 3 3 import pygame, pyxoo, math, random, os, sys 4 4 5 RESOLUTION = 1680, 10506 COLOR = 0,255,2557 5 8 class App(pyxoo.engine.Application): 9 def init(self): 6 RESOLUTION = 800, 600 7 MAX_FPS = 120 8 9 class FountainSample(pyxoo.engine.Application): 10 def onInitApp(self, event): 10 11 if sys.platform == "win32": 11 12 os.environ["SDL_VIDEODRIVER"] = "windib" 12 13 pygame.init() 13 self.screen = pygame.display.set_mode(RESOLUTION,pygame.HWSURFACE|pygame.DOUBLEBUF|pygame.FULLSCREEN, 32) 14 self.bg = pygame.Surface(self.screen.get_size()).convert() 15 self.input_dispatcher.add_event_listener(pyxoo.event.onQuitEVENT, self.onQuit) 16 self.input_dispatcher.add_event_listener(pyxoo.event.onKeyboardKeyDownEVENT, self.onQuit) 17 14 self.screen = pygame.display.set_mode(RESOLUTION, pygame.HWSURFACE | pygame.DOUBLEBUF, 32) 15 pygame.mouse.set_visible(False) 16 17 self.add_event_listener(pyxoo.event.onQuitEVENT, self.onQuit) 18 self.add_event_listener(pyxoo.event.onKeyboardKeyDownEVENT, self.onKeyboardKeyDown) 19 18 20 self.monitor = pyxoo.utils.monitor.Monitor() 19 self.monitor.add_field(pyxoo.utils.monitor.FPSField(self.clock, 100, (0, 0, 255)))20 self.monitor.add_field(pyxoo.utils.monitor.ParticleField(3000, (255, 0,0)))21 self.monitor.add_field(pyxoo.utils.monitor.FPSField(self.clock, 300, (0, 0, 255))) 22 self.monitor.add_field(pyxoo.utils.monitor.ParticleField(3000, (255, 0, 0))) 21 23 self.monitor.start() 22 24 self.all_sprites = pygame.sprite.RenderPlain((self.monitor)) 23 25 24 26 initializer = pyxoo.particle.initializer.StackInitializer() 25 initializer.append(pyxoo.particle.initializer.ImageClassInitializer(pyxoo.displayobject. Star, 5))27 initializer.append(pyxoo.particle.initializer.ImageClassInitializer(pyxoo.displayobject.Dot, 5, pygame.Color('white'), 1)) 26 28 initializer.append(pyxoo.particle.initializer.LifeInitializer(3000, 1000)) 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)) 29 initializer.append(pyxoo.particle.initializer.ExplosionInitializer(400, pyxoo.structure.Range(math.pi - .1, math.pi + .1), 50)) 29 30 30 31 action = pyxoo.particle.action.StackAction() 31 32 action.append(pyxoo.particle.action.LifeAction()) 32 action.append(pyxoo.particle.action.ForceAction(pyxoo.structure.Point(0,90))) 33 #action.append(pyxoo.particle.action.ForceAction(pyxoo.structure.Point(50,0))) 33 action.append(pyxoo.particle.action.ForceAction(pyxoo.structure.Point(50, 90))) 34 34 action.append(pyxoo.particle.action.FrictionAction(.995)) 35 35 action.append(pyxoo.particle.action.MoveAction()) … … 37 37 renderer = pyxoo.particle.renderer.BitmapRenderer(self.screen) 38 38 39 emission = pyxoo.particle.emission.ByRateEmission(pyxoo.particle.DisplayObjectParticle, pyxoo.structure.Point( 300, 5), 200)39 self.emission = pyxoo.particle.emission.ByRateEmission(pyxoo.particle.DisplayObjectParticle, pyxoo.structure.Point(300, 5), 200) 40 40 self.system = pyxoo.particle.system.SimpleParticleSystem(initializer, action, renderer) 41 self.system.emit( emission)41 self.system.emit(self.emission) 42 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) 50 51 def update(self): 43 def onTick(self, event): 52 44 self.all_sprites.update() 45 x, y = pygame.mouse.get_pos() 46 self.emission.set_position(pyxoo.structure.Point(x, y)) 53 47 54 def draw(self): 55 self.screen.blit(self.bg,(0,0)) 48 def onDraw(self, event): 49 pygame.display.flip() 50 self.screen.fill(pygame.Color('black')) 56 51 self.all_sprites.draw(self.screen) 57 52 … … 59 54 self.is_running = False 60 55 56 def onKeyboardKeyDown(self, event): 57 key = event.get_value().key 58 if key == pygame.K_ESCAPE: 59 event.type = pyxoo.event.onQuitEVENT 60 self.broadcast(event) 61 elif key == pygame.K_UP: 62 self.system.set_local_speed(self.system.get_local_speed() + 1) 63 elif key == pygame.K_DOWN: 64 self.system.set_local_speed(self.system.get_local_speed() - 1) 65 66 61 67 62 68 if __name__ == '__main__': 63 App().run() 69 app = FountainSample(MAX_FPS) 70 app.run() project/trunk/example/particle/mouse_gravity.py
r35 r36 6 6 from pyxoo.structure import Point 7 7 8 RESOLUTION = 1680, 10509 COLOR = 0,255,255 8 RESOLUTION = 800, 600 9 MAX_FPS = 120 10 10 11 class App(pyxoo.engine.Application):12 def init(self):11 class MouseGravitySample(pyxoo.engine.Application): 12 def onInitApp(self, event): 13 13 if sys.platform == "win32": 14 14 os.environ["SDL_VIDEODRIVER"] = "windib" 15 15 pygame.init() 16 self.screen = pygame.display.set_mode(RESOLUTION,pygame.HWSURFACE|pygame.DOUBLEBUF |pygame.FULLSCREEN, 32)16 self.screen = pygame.display.set_mode(RESOLUTION,pygame.HWSURFACE|pygame.DOUBLEBUF, 32) 17 17 self.bg = pygame.Surface(self.screen.get_size()).convert() 18 self. input_dispatcher.add_event_listener(pyxoo.event.onQuitEVENT, self.onQuit)19 self. input_dispatcher.add_event_listener(pyxoo.event.onKeyboardKeyDownEVENT, self.onQuit)20 18 self.add_event_listener(pyxoo.event.onQuitEVENT, self.onQuit) 19 self.add_event_listener(pyxoo.event.onKeyboardKeyDownEVENT, self.onQuit) 20 21 21 self.monitor = pyxoo.utils.monitor.Monitor() 22 22 self.monitor.add_field(pyxoo.utils.monitor.FPSField(self.clock, 300, (0, 0, 255))) … … 28 28 initializer.append(pyxoo.particle.initializer.ImageClassInitializer(pyxoo.displayobject.Star, 5)) 29 29 initializer.append(pyxoo.particle.initializer.LifeInitializer(3000, 1000)) 30 #initializer.append(pyxoo.particle.initializer.ExplosionInitializer(3, pyxoo.structure.Range(math.pi-.1, math.pi+.1), 50))31 30 initializer.append(pyxoo.particle.initializer.BoxInitializer(self.screen.get_rect())) 32 33 31 34 32 action = pyxoo.particle.action.StackAction() 35 action.append(pyxoo.particle.action.MouseGravity(1 20, 100))33 action.append(pyxoo.particle.action.MouseGravity(150, 70)) 36 34 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)) 35 action.append(pyxoo.particle.action.SpeedLimit(100)) 39 36 action.append(pyxoo.particle.action.MoveAction()) 40 37 … … 43 40 44 41 self.system = pyxoo.particle.system.SimpleParticleSystem(initializer, action, renderer) 42 self.system.set_local_speed(3) 45 43 self.system.emit(emission) 46 44 47 def update(self):45 def onTick(self, event): 48 46 self.all_sprites.update() 49 47 50 def draw(self): 51 self.screen.blit(self.bg,(0,0)) 48 def onDraw(self, event): 49 pygame.display.flip() 50 self.screen.fill(pygame.Color("black")) 52 51 self.all_sprites.draw(self.screen) 53 52 … … 57 56 58 57 if __name__ == '__main__': 59 App().run() 58 app = MouseGravitySample(MAX_FPS) 59 app.run() project/trunk/src/pyxoo/displayobject.py
r35 r36 28 28 29 29 class Dot(SurfaceWithColorKey): 30 def __init__(self, radius, color=_WHITE ):30 def __init__(self, radius, color=_WHITE, width=0): 31 31 SurfaceWithColorKey.__init__(self, (radius * 2, radius * 2), _get_color_key(color)) 32 pygame.draw.circle(self, color, (radius, radius), radius )32 pygame.draw.circle(self, color, (radius, radius), radius, width) 33 33 34 34 35 35 class Star(SurfaceWithColorKey): 36 def __init__(self, radius, color=_WHITE, width= 1):36 def __init__(self, radius, color=_WHITE, width=0): 37 37 SurfaceWithColorKey.__init__(self, (radius * 2, radius * 2), _get_color_key(color)) 38 38 rot_step = math.pi / 5.0 39 39 inner_radius = radius * math.cos(rot_step * 2) 40 half_pi = math.pi * 0.5 41 40 half_pi = math.pi * 0.5 42 41 points = list() 43 42 points.append((radius, 0)) project/trunk/src/pyxoo/engine.py
r35 r36 1 1 import pyxoo, pyxoo.event, pygame 2 3 from pyxoo.event import ApplicationBroadcaster, Event, InputEventConverter, SYSTEM_CHANNEL, onInitAppEVENT, onQuitAppEVENT, onDrawEVENT, onTickEVENT 2 4 3 5 __version__ = pyxoo.__version__ 4 6 __licence__ = pyxoo.__licence__ 5 __url__ = pyxoo.__url__6 __author__ = "Alexis Couronne"7 __url__ = pyxoo.__url__ 8 __author__ = "Alexis Couronne" 7 9 8 10 9 11 class Application: 10 def __init__(self, max_fps=0, flip_method=pygame.display.flip): 11 self.input_dispatcher = pyxoo.event.InputDispatcher() 12 def __init__(self, max_fps=0): 13 self._eventbroadcaster = ApplicationBroadcaster().get_channel_dispatcher(SYSTEM_CHANNEL) 14 self._event_converter = InputEventConverter() 12 15 self.is_running = False 16 13 17 self.clock = pygame.time.Clock() 14 18 self.max_fps = max_fps 15 self._flip_method = flip_method 16 self.__onInitEVENT = pyxoo.event.Event(pyxoo.event.onInitAppEVENT) 17 self.__onUpdateEVENT = pyxoo.event.Event(pyxoo.event.onUpdateEVENT) 18 self.__onDrawEVENT = pyxoo.event.Event(pyxoo.event.onDrawEVENT) 19 self.__onQuitEVENT = pyxoo.event.Event(pyxoo.event.onQuitAppEVENT) 20 self.__onTickEVENT = pyxoo.engine.RTEvent(pyxoo.event.onTickEVENT, self) 19 self._global_speed = 1 20 self._max_step_size = 0 21 21 22 def init(self): 22 self._onInitAppEVT = Event(onInitAppEVENT) 23 self._onTickEVT = RTEvent(onTickEVENT, self) 24 self._onDrawEVT = Event(onDrawEVENT) 25 self._onQuitAppEVT = Event(onQuitAppEVENT) 26 27 self.add_event_listener(onInitAppEVENT, self) 28 self.add_event_listener(onTickEVENT, self) 29 self.add_event_listener(onDrawEVENT, self) 30 self.add_event_listener(onQuitAppEVENT, self) 31 32 def onInitApp(self, event): 23 33 pass 24 34 25 def quit(self):35 def onQuitApp(self, event): 26 36 pass 27 37 28 def update(self):38 def onTick(self, event): 29 39 pass 30 40 31 def draw(self):41 def onDraw(self, event): 32 42 pass 33 43 34 44 def run(self): 35 45 self.is_running = True 36 self.init() 37 self._fire_onInit() 46 self._fire_onInitApp() 38 47 while self.is_running: 39 48 self.clock.tick(self.max_fps) 40 self.input_dispatcher.dispatch() 41 self.update() 42 self._fire_onUpdate() 49 self._fire_input_events() 43 50 self._fire_onTick() 44 self.draw()45 51 self._fire_onDraw() 46 self._flip_method() 47 self.quit() 48 self._fire_onQuit() 52 self._fire_onQuitApp() 53 54 def get_global_speed(self): 55 return self._global_speed 49 56 50 def _fire_onInit(self): 51 pyxoo.event.ApplicationBroadcaster().broadcast(self.__onInitEVENT) 57 def set_global_speed(self, speed): 58 if speed <= 0: 59 raise ValueError("speed value must positive") 60 self._global_speed = speed 61 62 def get_max_step_size(self): 63 return self._max_step_size 52 64 53 def _fire_onUpdate(self): 54 pyxoo.event.ApplicationBroadcaster().broadcast(self.__onUpdateEVENT) 65 def set_max_step_size(self, size): 66 if size >= 0: 67 self._max_step_size = size 68 else: 69 self._max_step_size = 0 70 71 def add_listener(self, listener): 72 return self._eventbroadcaster.add_listener(listener) 73 74 def remove_listener(self, listener): 75 return self._eventbroadcaster.remove_listener(listener) 76 77 def add_event_listener(self, type, listener, *args): 78 return self._eventbroadcaster.add_event_listener(type, listener, *args) 79 80 def remove_event_listener(self, type, listener): 81 return self._eventbroadcaster.remove_event_listener(type, listener) 82 83 def broadcast(self, event): 84 self._eventbroadcaster.broadcast(event) 55 85 86 def _fire_input_events(self): 87 for event in pygame.event.get(): 88 e = self._event_converter.convert(event) 89 self._eventbroadcaster.broadcast(e) 90 91 def _fire_onInitApp(self): 92 self._eventbroadcaster.broadcast(self._onInitAppEVT) 93 56 94 def _fire_onTick(self): 57 self.__onTickEVENT.set_step(self.clock.get_time()) 58 pyxoo.event.ApplicationBroadcaster().broadcast(self.__onTickEVENT) 95 step = self.clock.get_time() * self._global_speed 96 if self._max_step_size: 97 step = self._restrict(step) 98 self._onTickEVT.set_step(step) 99 self._eventbroadcaster.broadcast(self._onTickEVT) 59 100 60 101 def _fire_onDraw(self): 61 pyxoo.event.ApplicationBroadcaster().broadcast(self.__onDrawEVENT)102 self._eventbroadcaster.broadcast(self._onDrawEVT) 62 103 63 def _fire_onQuit(self): 64 pyxoo.event.ApplicationBroadcaster().broadcast(self.__onQuitEVENT) 104 def _fire_onQuitApp(self): 105 self._eventbroadcaster.broadcast(self._onQuitAppEVT) 106 107 def _restrict(self, step): 108 tmp = self._max_step_size * self._global_speed 109 if step > tmp: 110 return tmp 111 else: 112 return step 65 113 66 114 67 115 class RTObject: 68 116 def __init__(self): 69 self._broadcaster = pyxoo.event.ApplicationBroadcaster().get_channel_dispatcher(pyxoo.event.SYSTEM_CHANNEL)117 self._broadcaster = ApplicationBroadcaster().get_channel_dispatcher(pyxoo.event.SYSTEM_CHANNEL) 70 118 self._local_speed = 1 71 119 self._is_playing = False … … 87 135 88 136 def set_local_speed(self, rate): 89 if rate < 1:90 raise ArithmeticError137 if rate <= 0: 138 raise ValueError("rate parameter must be positive") 91 139 self._local_speed = rate 92 140 … … 107 155 108 156 109 class RTEvent( pyxoo.event.Event):157 class RTEvent(Event): 110 158 def __init__(self, type, target=None): 111 pyxoo.event.Event.__init__(self, type, None, target)159 Event.__init__(self, type, None, target) 112 160 self._step = 0 113 161 … … 116 164 117 165 def get_step(self): 118 return self._step166 return float(self._step) 119 167 120 168 def get_step_in_second(self): project/trunk/src/pyxoo/event.py
r34 r36 728 728 """__str__():String 729 729 Returns the string representation of this instance.""" 730 return pyxoo.utils.PyxooStringifier.stringify(self) 730 return pyxoo.utils.PyxooStringifier.stringify(self) + "(type:'%s', value:'%s', target:'%s')"%(self.type, self._value, self.target) 731 731 732 732 … … 834 834 835 835 836 class Input Dispatcher:836 class InputEventConverter: 837 837 def __init__(self): 838 self.__eb = EventBroadcaster(self) 839 self.__event_types = dict() 840 838 self.__event_types = dict() 841 839 self.__event_types[pygame.MOUSEMOTION] = onMouseMoveEVENT 842 840 self.__event_types[pygame.MOUSEBUTTONDOWN] = onMouseButtonDownEVENT … … 853 851 self.__event_types[pygame.JOYHATMOTION] = onJoystickHatMotionEVENT 854 852 self.__event_types[pygame.JOYBUTTONUP] = onJoystickButtonUpEVENT 855 self.__event_types[pygame.JOYBUTTONDOWN] = onJoystickButtonDownEVENT 856 857 def dispatch(self): 858 for event in pygame.event.get(): 859 self.__eb.broadcast(Event(self.__event_types[event.type], event)) 860 861 def add_listener(self, listener): 862 return self.__eb.add_listener(listener) 863 864 def remove_listener(self, listener): 865 return self.__eb.remove_listener(listener) 866 867 def add_event_listener(self, event_type, listener, *args): 868 return self.__eb.add_event_listener(event_type, listener, *args) 869 870 def remove_event_listener(self, event_type, listener): 871 return self.__eb.remove_event_listener(event_type, listener) 872 873 def __str__(self): 874 """__str__():String 875 Returns the string representation of this instance.""" 876 return pyxoo.utils.PyxooStringifier.stringify(self) 877 853 self.__event_types[pygame.JOYBUTTONDOWN] = onJoystickButtonDownEVENT 854 855 def convert(self, event): 856 return Event(self.__event_types[event.type], event) 857 project/trunk/src/pyxoo/particle/initializer.py
r35 r36 44 44 self._b = b 45 45 self._rand = rand 46 self._segment = b - a #TODO: voir si c bon46 self._segment = b - a 47 47 48 48 def initialize(self, particle): … … 89 89 90 90 class ImageClassInitializer(InitializeStrategy): 91 def __init__(self, image, *args ):91 def __init__(self, image, *args, **kwargs): 92 92 self._image = image 93 93 self._args = args 94 self._kwargs = kwargs 94 95 95 96 def initialize(self, particle): 96 particle.image = self._image(*self._args ).convert()97 particle.image = self._image(*self._args, **self._kwargs).convert() project/trunk/src/pyxoo/particle/system.py
r35 r36 55 55 56 56 def _process_emissions(self, event): 57 t = event.get_step()57 t = self.get_local_step(event) 58 58 for emission in self._emissions: 59 59 emission.prepare_emission(t) … … 70 70 71 71 def _process_particles(self, event): 72 time = event.get_step()72 time = self.get_local_step(event) 73 73 self._prepare_action(time) 74 74 for particle in self._particles: project/trunk/src/pyxoo/utils/monitor.py
r34 r36 56 56 57 57 class FPSField(Field): 58 def __init__(self, clock, max_value , color):58 def __init__(self, clock, max_value=300, color=pygame.Color("green")): 59 59 Field.__init__(self, "FPS", 0, max_value, color) 60 60 self.clock = clock
