def addActorToWorld(world, actor, sprite_name=None, layer_name=None, center_position=None, physics=None, origin=None): """Create a new actor in the world If the center position is not specified then it is placed at the center of the screen. """ # # If not position then put at the center if origin is None and center_position is None: renderer = serge.engine.CurrentEngine().getRenderer() center_position = (renderer.width/2.0, renderer.height/2.0) # # Create the new actor if sprite_name is not None: actor.setSpriteName(sprite_name) if layer_name is not None: actor.setLayerName(layer_name) if physics: actor.setPhysical(physics) if center_position is not None: actor.moveTo(*center_position) else: actor.setOrigin(*origin) world.addActor(actor) return actor
def editActor(self, path, column, new_text): """Edit the actor in the list view""" row = int(path) world = self.engine.getCurrentWorld() actor = world.findActorByName(self.view_actors.model[path][1]) world.removeActor(actor) data = self.view_actors.model[row] if column in (4, 5): data[column] = int(new_text) else: data[column] = new_text tag, name, sprite, layer, x, y = data new_actor = serge.actor.Actor(tag, name) if sprite: new_actor.setSpriteName(sprite) if layer: new_actor.setLayerName(layer) new_actor.moveTo(x, y) world.addActor(new_actor) self.view_actors.model[row] = data
def updateActor(self, interval, world): """Update the actor""" # self.growth += float(interval)/self.rate if self.growth >= len(self.colours): # We have died self.log.info('Removing dead guy at %d, %d. Left is %d' % (self.x, self.y, len(world.getActors())-1)) world.removeActor(self) return # self.visual.growth = self.growth self.visual.size = self.width self.visual.x = self.x self.visual.y = self.y # #if random.random() < self.grow_prob: # self.resizeTo(self.width+1, self.height+1) if random.random() < self.spawn_prob*interval: new_x = self.x + random.randint(-self.spawn_dist, self.spawn_dist) new_y = self.y + random.randint(-self.spawn_dist, self.spawn_dist) new = self.__class__(self.colours, self.rate, self.grow_prob, self.spawn_prob, self.spawn_dist, self.blue) new.setSpatial(new_x, new_y, random.randint(1, 5), 20) new.setLayerName('base') world.addActor(new)
def updateActor(self, interval, world): global posX, posY, testposX, testposY #update everything, this func is constantly updated super(Player, self).updateActor(interval, world) #init this class with the coordinates of the player checktile = CheckTile(posX, posY) #check tile to the right canMoveRight = checktile.checkright(world) #left canMoveLeft = checktile.checkleft(world) #up canMoveUp = checktile.checkup(world) #down canMoveDown = checktile.checkdown(world) """ pseudo code, ignore this canmoveto(x,y) posx+delta,posy posx,posy+delta posx-delta,posy posx,posy-delta check(x,y) check(x+w-1,y) check(x,y+w-1) check(x+w-1,y+w-1) """ #where does the player want to go #the var 'facing' was implemented for different sprites, depending on #where the player was facing at the current moment. if self.keyboard.isDown(pygame.K_LEFT) and canMoveLeft is True: direction0 = -1 direction1 = 0 testposX = posX - self.movespeed facing = "left" elif self.keyboard.isDown(pygame.K_RIGHT) and canMoveRight is True: direction0 = +1 direction1 = 0 testposX = posX + self.movespeed facing = "right" elif self.keyboard.isDown(pygame.K_UP) and canMoveUp is True: direction0 = 0 direction1 = -1 testposY = posY - self.movespeed facing = "up" elif self.keyboard.isDown(pygame.K_DOWN) and canMoveDown is True: direction0 = 0 direction1 = +1 testposY = posY + self.movespeed facing = "down" #drop the bomb when space is pressed elif self.keyboard.isClicked(pygame.K_SPACE): b = bomb.Bomb(posX + 32, posY + 32) world.addActor(b) direction0, direction1 = 0, 0 testposX = posX testposY = posY facing = "forward" #if nothing is pressed, do the following. else: direction0, direction1 = 0, 0 testposX = posX testposY = posY facing = "forward" #fulfill the task, move (if possible) #should be noted, the sprite moves indefinitely. #collision detection prevents the sprite from moving when it shouldn't #NB - coordinates of the sprite are taken from its upper left corner if posX in range(resoX - tilesizeX + 1) and posY in range(resoY - tilesizeY + 1): for i in range(self.movespeed): if posX < 0: for j in range(self.movespeed - 1): self.move(+1, 0) testposX += 1 facing = "left" print facing elif posX > resoX - tilesizeX: for k in range(self.movespeed - 1): self.move(-1, 0) testposX -= 1 facing = "right" print facing elif posY < 0: for l in range(self.movespeed - 1): self.move(0, +1) testposY += 1 facing = "up" print facing elif posY > resoY - tilesizeY: for m in range(self.movespeed - 1): self.move(0, -1) testposY -= 1 facing = "down" print facing self.move(direction0, direction1) posX = testposX posY = testposY #debug stuff global facingoutput global coordoutput if facingoutput is True: print facing if coordoutput is True: print "Pos", posX, posY
self.move(direction0, direction1) posX = testposX posY = testposY #debug stuff global facingoutput global coordoutput if facingoutput is True: print facing if coordoutput is True: print "Pos", posX, posY """ #old code, de-comment to try running the move.py without other modules #start up the game engine engine = serge.blocks.utils.getSimpleSetup(resoX, resoY) world = engine.getWorld("lab") #create the player char = Player() world.addActor(char) char.moveTo(stptX, stptY) posX, posY = stptX-tilesizeX/2, stptY-tilesizeY/2 #start up the game engine.run(60)
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)
def updateActor(self, interval, world): """Update the actor""" new = TestAddingActor("new") world.addActor(new)
def updateActor(self, interval, world): """Update the actor""" new = TestAddingActor('new') world.addActor(new)
def updateActor(self, interval, world): global posX, posY, testposX, testposY #update everything, this func is constantly updated super(Player, self).updateActor(interval, world) #init this class with the coordinates of the player checktile = CheckTile(posX, posY) #check tile to the right canMoveRight = checktile.checkright(world) #left canMoveLeft = checktile.checkleft(world) #up canMoveUp = checktile.checkup(world) #down canMoveDown = checktile.checkdown(world) """ pseudo code, ignore this canmoveto(x,y) posx+delta,posy posx,posy+delta posx-delta,posy posx,posy-delta check(x,y) check(x+w-1,y) check(x,y+w-1) check(x+w-1,y+w-1) """ #where does the player want to go #the var 'facing' was implemented for different sprites, depending on #where the player was facing at the current moment. if self.keyboard.isDown(pygame.K_LEFT) and canMoveLeft is True: direction0 = -1 direction1 = 0 testposX = posX - self.movespeed facing = "left" elif self.keyboard.isDown(pygame.K_RIGHT) and canMoveRight is True: direction0 = +1 direction1 = 0 testposX = posX + self.movespeed facing = "right" elif self.keyboard.isDown(pygame.K_UP) and canMoveUp is True: direction0 = 0 direction1 = -1 testposY = posY - self.movespeed facing = "up" elif self.keyboard.isDown(pygame.K_DOWN) and canMoveDown is True: direction0 = 0 direction1 = +1 testposY = posY + self.movespeed facing = "down" #drop the bomb when space is pressed elif self.keyboard.isClicked(pygame.K_SPACE): b = bomb.Bomb(posX+32, posY+32) world.addActor(b) direction0, direction1 = 0, 0 testposX = posX testposY = posY facing = "forward" #if nothing is pressed, do the following. else: direction0, direction1 = 0, 0 testposX = posX testposY = posY facing = "forward" #fulfill the task, move (if possible) #should be noted, the sprite moves indefinitely. #collision detection prevents the sprite from moving when it shouldn't #NB - coordinates of the sprite are taken from its upper left corner if posX in range(resoX-tilesizeX+1) and posY in range(resoY-tilesizeY+1): for i in range(self.movespeed): if posX < 0: for j in range(self.movespeed-1): self.move(+1, 0) testposX += 1 facing = "left" print facing elif posX > resoX-tilesizeX: for k in range(self.movespeed-1): self.move(-1, 0) testposX -= 1 facing = "right" print facing elif posY < 0: for l in range(self.movespeed-1): self.move(0, +1) testposY += 1 facing = "up" print facing elif posY > resoY-tilesizeY: for m in range(self.movespeed-1): self.move(0, -1) testposY -= 1 facing = "down" print facing self.move(direction0, direction1) posX = testposX posY = testposY #debug stuff global facingoutput global coordoutput if facingoutput is True: print facing if coordoutput is True: print "Pos",posX,posY
self.move(direction0, direction1) posX = testposX posY = testposY #debug stuff global facingoutput global coordoutput if facingoutput is True: print facing if coordoutput is True: print "Pos",posX,posY """ #old code, de-comment to try running the move.py without other modules #start up the game engine engine = serge.blocks.utils.getSimpleSetup(resoX, resoY) world = engine.getWorld("lab") #create the player char = Player() world.addActor(char) char.moveTo(stptX, stptY) posX, posY = stptX-tilesizeX/2, stptY-tilesizeY/2 #start up the game engine.run(60)