def testEngineCallsWorldWhenSelecting(self): """testEngineCallsWorldWhenSelecting: enging should call method on world when selecting it""" self.assertEqual(0, self.w.activations) self.assertEqual(0, self.w.deactivations) # engine = serge.engine.Engine() # # Adding should do nothing engine.addWorld(self.w) engine.addWorld(self.w2) self.assertEqual(0, self.w.activations) # # Setting current should activate engine.setCurrentWorld(self.w) self.assertEqual(1, self.w.activations) self.assertEqual(0, self.w.deactivations) # # Setting again should do nothing engine.setCurrentWorld(self.w) self.assertEqual(1, self.w.activations) self.assertEqual(0, self.w.deactivations) # # Another world engine.setCurrentWorld(self.w2) self.assertEqual(1, self.w.activations) self.assertEqual(1, self.w.deactivations) self.assertEqual(1, self.w2.activations) # # And back engine.setCurrentWorld(self.w) self.assertEqual(2, self.w.activations) self.assertEqual(1, self.w.deactivations) self.assertEqual(1, self.w2.deactivations)
def testEngineCanHandleMouseEvents(self): """testEngineCanHandleMouseEvents: engine should be able to handle mouse events""" self.a1.setSpatial(50, 100, 51, 30) self.a2.setSpatial(100, 50, 20, 51) self.w.addZone(self.z1) self.w.addZone(self.z2) engine = self.engine mouse = serge.input.Mouse(engine) # Fake a click mouse.current_mouse_state.setState(serge.input.M_LEFT, True) mouse.getScreenPos = lambda: (x, y) mouse.getStaticScreenPos = lambda: (x, y) # self.a1.layer = "one" self.a2.layer = "two" # x, y = 55, 100 # engine.addWorld(self.w) engine.setCurrentWorld(self.w) engine._mouse = mouse engine.processEvents() # self.assertEqual(serge.events.E_LEFT_MOUSE_DOWN, self.a1.action[0]) self.assertEqual(mouse, self.a1.action[1]) self.assertEqual(None, self.a2.action)
def testEngineCanDelinkEvents(self): """testEngineCanDelinkEvents: should be able to delink events from callbacks""" self.a1.setSpatial(50, 100, 51, 30) self.w.addZone(self.z1) engine = self.engine mouse = serge.input.Mouse(engine) # Fake a click mouse.current_mouse_state.setState(serge.input.M_LEFT, True) mouse.getScreenPos = lambda: (x, y) # self.a1.layer = "one" x, y = 55, 100 # global test test = 0 def doit(obj, x): global test test += x # # Link events self.a1.linkEvent(serge.events.E_LEFT_MOUSE_DOWN, doit, 10) self.a1.unlinkEvent(serge.events.E_LEFT_MOUSE_DOWN) # engine.addWorld(self.w) engine.setCurrentWorld(self.w) engine._mouse = mouse engine.processEvents() # self.assertEqual(0, test) self.assertEqual(serge.events.E_LEFT_MOUSE_DOWN, self.a1.action[0]) self.assertEqual(mouse, self.a1.action[1])
def testEngineCanDelinkEvents(self): """testEngineCanDelinkEvents: should be able to delink events from callbacks""" self.a1.setSpatial(50, 100, 51, 30) self.w.addZone(self.z1) engine = self.engine mouse = serge.input.Mouse(engine) # Fake a click mouse.current_mouse_state.setState(serge.input.M_LEFT, True) mouse.getScreenPos = lambda : (x, y) # self.a1.layer = 'one' x, y = 55, 100 # global test test = 0 def doit(obj, x): global test test += x # # Link events self.a1.linkEvent(serge.events.E_LEFT_MOUSE_DOWN, doit, 10) self.a1.unlinkEvent(serge.events.E_LEFT_MOUSE_DOWN) # engine.addWorld(self.w) engine.setCurrentWorld(self.w) engine._mouse = mouse engine.processEvents() # self.assertEqual(0, test) self.assertEqual(serge.events.E_LEFT_MOUSE_DOWN, self.a1.action[0]) self.assertEqual(mouse, self.a1.action[1])
def testEngineCanAutoLinkEvents(self): """testEngineCanAutoLinkEvents: should be able to automatically link events to a callback""" self.a1.setSpatial(50, 100, 51, 30) self.w.addZone(self.z1) engine = self.engine mouse = serge.input.Mouse(engine) # Fake a click mouse.current_mouse_state.setState(serge.input.M_LEFT, True) mouse.getScreenPos = lambda : (x, y) # self.a1.layer = 'one' x, y = 55, 100 # global test test = 0 def doit(obj, x): global test test += x # # Link events self.a1.linkEvent(serge.events.E_LEFT_MOUSE_DOWN, doit, 10) # engine.addWorld(self.w) engine.setCurrentWorld(self.w) engine._mouse = mouse engine.processEvents() # self.assertEqual(10, test)
def testEngineCanHandleMouseEvents(self): """testEngineCanHandleMouseEvents: engine should be able to handle mouse events""" self.a1.setSpatial(50, 100, 51, 30) self.a2.setSpatial(100, 50, 20, 51) self.w.addZone(self.z1) self.w.addZone(self.z2) engine = self.engine mouse = serge.input.Mouse(engine) # Fake a click mouse.current_mouse_state.setState(serge.input.M_LEFT, True) mouse.getScreenPos = lambda : (x, y) mouse.getStaticScreenPos = lambda : (x, y) # self.a1.layer = 'one' self.a2.layer = 'two' # x, y = 55, 100 # engine.addWorld(self.w) engine.setCurrentWorld(self.w) engine._mouse = mouse engine.processEvents() # self.assertEqual(serge.events.E_LEFT_MOUSE_DOWN, self.a1.action[0]) self.assertEqual(mouse, self.a1.action[1]) self.assertEqual(None, self.a2.action)
def createWorldsForEngine(engine, worlds): """Add a number of worlds to the engine The words parameter is a list of names of the worlds to create. Each world is created with a single active zone which is quite large. """ for name in worlds: world = serge.world.World(name) zone = serge.zone.Zone() zone.active = True zone.setSpatial(-2000, -2000, 4000, 4000) world.addZone(zone) engine.addWorld(world)
def createWorldsForEngine(engine, worlds, world_class=serge.world.World): """Add a number of worlds to the engine The words parameter is a list of names of the worlds to create. Each world is created with a single active zone which is quite large. """ for name in worlds: world = world_class(name) zone = serge.zone.Zone() zone.active = True zone.setSpatial(-2000, -2000, 4000, 4000) world.addZone(zone) engine.addWorld(world)
def testCanConsumeMouseEvent(self): """testCanConsumeMouseEvent: should be able to consume a mouse event to prevent others seeing it""" self.a1.setSpatial(50, 100, 51, 30) self.a2.setSpatial(50, 100, 51, 30) self.w.addZone(self.z1) self.w.addZone(self.z2) engine = self.engine mouse = serge.input.Mouse(engine) # Fake a click mouse.current_mouse_state.setState(serge.input.M_LEFT, True) mouse.getScreenPos = lambda: (x, y) # self.a1.layer = "one" self.a2.layer = "one" x, y = 55, 100 # global test, consume test = 0 consume = False def doit(obj, x): global test test += x if consume: return serge.events.E_LEFT_MOUSE_DOWN else: return None # # Link events self.a1.linkEvent(serge.events.E_LEFT_MOUSE_DOWN, doit, 10) self.a2.linkEvent(serge.events.E_LEFT_MOUSE_DOWN, doit, 1) # # First non consuming engine.addWorld(self.w) engine.setCurrentWorld(self.w) engine._mouse = mouse engine.processEvents() # # Should fire twice self.assertEqual(11, test) # # Now with consuming - only one of the events should work consume = True test = 0 engine.processEvents() self.assertTrue(test in (1, 10))
def testCanConsumeMouseEvent(self): """testCanConsumeMouseEvent: should be able to consume a mouse event to prevent others seeing it""" self.a1.setSpatial(50, 100, 51, 30) self.a2.setSpatial(50, 100, 51, 30) self.w.addZone(self.z1) self.w.addZone(self.z2) engine = self.engine mouse = serge.input.Mouse(engine) # Fake a click mouse.current_mouse_state.setState(serge.input.M_LEFT, True) mouse.getScreenPos = lambda : (x, y) # self.a1.layer = 'one' self.a2.layer = 'one' x, y = 55, 100 # global test, consume test = 0 consume = False def doit(obj, x): global test test += x if consume: return serge.events.E_LEFT_MOUSE_DOWN else: return None # # Link events self.a1.linkEvent(serge.events.E_LEFT_MOUSE_DOWN, doit, 10) self.a2.linkEvent(serge.events.E_LEFT_MOUSE_DOWN, doit, 1) # # First non consuming engine.addWorld(self.w) engine.setCurrentWorld(self.w) engine._mouse = mouse engine.processEvents() # # Should fire twice self.assertEqual(11, test) # # Now with consuming - only one of the events should work consume = True test = 0 engine.processEvents() self.assertTrue(test in (1, 10))
def testStaticZonedActorsAreHandledFirst(self): """testStaticZonedActorsAreHandledFirst: should be able to specify an event handler as important by putting on static""" # Here is the key to ordering the event listeners self.engine.getRenderer().getLayer('two').setStatic(True) # self.a1.setSpatial(0, 0, 1000, 1000) self.a2.setSpatial(0, 0, 1000, 1000) self.w.addZone(self.z1) self.w.addZone(self.z2) engine = self.engine mouse = serge.input.Mouse(engine) # Fake a click mouse.current_mouse_state.setState(serge.input.M_LEFT, True) mouse.getScreenPos = lambda : (x, y) # self.a1.layer = 'one' self.a2.layer = 'two' x, y = 55, 100 # global test, consume test = 0 consume = True def doit(obj, x): global test test += x if consume: return serge.events.E_LEFT_MOUSE_DOWN else: return None # # Link events - normally a2 would fire first being the last # registered but since a2 is on a static layer now it will be the first self.a1.linkEvent(serge.events.E_LEFT_MOUSE_DOWN, doit, 10) self.a2.linkEvent(serge.events.E_LEFT_MOUSE_DOWN, doit, 1) # # First non consuming engine.addWorld(self.w) engine.setCurrentWorld(self.w) engine._mouse = mouse engine.processEvents() self.assertEqual(1, test)
def main(options, args): """Run the game""" # # Debug options if options.any: game.Game.check_spelling = False if options.suggest: keypad.KeyPad.can_suggest = True gamestart.Start.possible_holes = map(int, options.holes.split(',')) ### Sounds ### serge.sound.Register.setPath('sound') r = serge.sound.Register.registerItem r('start', 'start.wav') r('begin', 'begin.wav') r('letter', 'letter.wav') r('word', 'word.wav') r('hole', 'hole.wav') r('good-hole', 'good_hole.wav') r('poor-hole', 'poor_hole.wav') r('unletter', 'unletter.wav') r('error', 'error.wav') r('bad-letter', 'badletter.wav') r('end-game', 'end_game.wav') r('sand', 'sand.wav') r('water', 'water.wav') ### Graphics ### serge.visual.Register.setPath('graphics') r = serge.visual.Register.registerItem rf = serge.visual.Register.registerFromFiles r('start-bg', 'start_bg.png') r('course', 'course1.png') r('hole2', 'hole1.png') r('hole1', 'hole1.png') r('logo', 'logo.png') rf('game1', 'game1_%d.png', 2) rf('game2', 'game2_%d.png', 2) rf('game3', 'game3_%d.png', 2) rf('button', 'button%d.png', 2) r('button_back', 'button_back.png') r('big_button_back', 'big_button.png') rf('letter', 'letter_%d.png', 2) r('help-page', 'helppage.png') r('escape-page', 'helppage.png') r('scores-page', 'scorespage.png') r('end-game-page', 'endgamepage.png') r('grass', 'grass.png') r('sand', 'sand.png') r('water', 'water.png') r('hole', 'hole.png') r('past', 'past.png') r('icon', 'wordgolf.ico') # thegame = game.Game(game.holes[0], 1, 3) engine = serge.engine.Engine(title='Word Golf', icon='icon') renderer = engine.getRenderer() course = serge.render.VirtualLayer('course', 0) renderer.addLayer(course) keys = serge.render.VirtualLayer('keys', 1) renderer.addLayer(keys) ui = serge.render.VirtualLayer('ui', 2) renderer.addLayer(ui) result = serge.render.VirtualLayer('results', 3) renderer.addLayer(result) camera = renderer.getCamera() camera.setSpatial(0, 0, 640, 480) ### The main world for the game ### game_world = serge.world.World('game') main = serge.zone.Zone() main.active = True game_world.addZone(main) pad = keypad.KeyPad(game_world, thegame) game_world.addActor(pad) ### The world for the end of game display ### end_world = serge.world.World('end') main = serge.zone.Zone() main.active = True end_world.addZone(main) game_over = gameover.Results(thegame, end_world) end_world.addActor(game_over) ### The world for the start of game display ### start_world = serge.world.World('start') main = serge.zone.Zone() main.active = True start_world.addZone(main) start = gamestart.Start(thegame, start_world) start_world.addActor(start) ### The world for help ### help_world = serge.world.World('help') z = serge.zone.Zone() z.active = True help_world.addZone(z) help = helppage.HelpPage(thegame, help_world) help_world.addActor(help) ### The world for escape ### escape_world = serge.world.World('escape') z = serge.zone.Zone() z.active = True escape_world.addZone(z) escape = escapepage.EscapePage(thegame, escape_world) escape_world.addActor(escape) ### The world for high scores ### scores_world = serge.world.World('scores') z = serge.zone.Zone() z.active = True scores_world.addZone(z) scores = scorespage.ScoresPage(thegame, scores_world) scores_world.addActor(escape) scores_world.activateWorld = scores.activateWorld # Tell the keypad about the high scores table pad.score = scores pad.gamestart = start scores.gamestart = start scores.pad = pad scores.updateTable() game_over.pad = pad game_over.results.visual.pad = pad engine.addWorld(start_world) engine.addWorld(end_world) engine.addWorld(game_world) engine.addWorld(help_world) engine.addWorld(escape_world) engine.addWorld(scores_world) engine.setCurrentWorld(start_world) thegame.tryHole() #serge.builder.builder.main(engine) engine.run(options.framerate)
def main(options, args, observation): """Start the engine and the game""" # # Set the levels to use - this allows us to switch to AI testing # levels rather than the main levels if options.test: import tests theme.setProperty('start-level', 1) common.levels = tests # # Check networkx install if not serge.blocks.utils.checkNetworkXVersion(1.8): return # # Create the high scores if options.high_score: createHighScores(options) # # Create the engine engine = startEngine(options) engine.linkEvent(serge.events.E_BEFORE_STOP, stoppingNow) engine.addWorld(common.TWEENER) # registerSounds() registerMusic() registerGraphics() registerEvents() # # Record a movie if options.movie: serge.blocks.utils.RecordDesktop(options.movie) # # Change theme settings if options.theme: theme.updateFromString(options.theme) # # Muting mute = serge.blocks.actors.MuteButton('mute-button', 'ui', alpha=G('mute-button-alpha')) serge.blocks.utils.addMuteButtonToWorlds( mute, center_position=G('mute-button-position')) # if options.muted: mute.toggleSound() if options.music_off: serge.sound.Music.toggle() # # Initialise the main logic registerAchievements(options) mainscreen.main(options, observation) startscreen.main(options) helpscreen.main(options) creditsscreen.main(options) levelscreen.main(options) actionreplayscreen.main(options) randomlevelscreen.main(options) # if options.debug: serge.builder.builder.main(engine, options.framerate) else: engine.run(options.framerate)
self.zoom *= factor engine = serge.engine.Engine() renderer = engine.getRenderer() layer = serge.render.Layer('base', 0) renderer.addLayer(layer) camera = renderer.getCamera() camera.setSpatial(0, 0, 600, 600) world = serge.world.World('surface') main = serge.zone.Zone() main.setSpatial(-6000, -6000, 12000, 12000) main.active = True world.addZone(main) engine.addWorld(world) engine.setCurrentWorld(world) gc = [(0, i, 0, i) for i in range(255)] + \ [(i, 255, 0, 255) for i in range(255)] + \ [(255, 255, 0, 255-i) for i in range(255)] for i in range(20): grass = Plant(gc, random.randint(10, 50), 0.01, 0.001/10.0, 10, 100) grass.setSpatial(random.randint(200, 400), random.randint(100, 300), random.randint(1, 5), 20) grass.setLayerName('base') world.addActor(grass) #engine.run(60) serge.builder.builder.main(engine)