def activate(self): self.blank = True self.background = (203, 204, 177) self.foreground = (0, 0, 0) self.controller = WorldStateController(self, self.area) self.msgFont = pygame.font.Font((res.fontPath("volter.ttf")), 9) self.border = gui.GraphicBox("dialog2-h.png", hollow=True) self.borderFilled = gui.GraphicBox("dialog2.png") self.player_vector = Vec2d(0, 0) # load the pygame and data assests for the area self.area.load() # get the root and the hero from it root = self.area.getRoot() self.hero = root.getChildByGUID(1) self.hero.move_speed = 1 # add the hero to this map if it isn't already there if not self.area.hasChild(self.hero): self.area.add(self.hero) # attach a camera sw, sh = sd.get_size() mw = sw * .75 mh = sh * .75 self.camera = AreaCamera(self.area, (4, 4, mw, mh), tmxdata=self.area.tmxdata) # define the borders self.mapBorder = pygame.Rect((0, 0, mw + 6, mh + 6)) self.msgBorder = pygame.Rect((0, mh, sw, sh - mh)) self.hudBorder = pygame.Rect((mw, 0, sw - mw, mh + 6)) # load sounds from area for filename in self.area.soundFiles: SoundMan.loadSound(filename)
def activate(self): self.blank = True self.background = (203, 204, 177) self.foreground = (0, 0, 0) self.controller = WorldStateController(self, self.area) self.msgFont = pygame.font.Font((res.fontPath("volter.ttf")), 9) self.border = gui.GraphicBox("dialog2-h.png", hollow=True) self.borderFilled = gui.GraphicBox("dialog2.png") self.player_vector = Vec2d(0,0) # load the pygame and data assests for the area self.area.load() # get the root and the hero from it root = self.area.getRoot() self.hero = root.getChildByGUID(1) self.hero.move_speed = 1 # add the hero to this map if it isn't already there if not self.area.hasChild(self.hero): self.area.add(self.hero) # attach a camera sw, sh = sd.get_size() mw = sw * .75 mh = sh * .75 self.camera = AreaCamera(self.area,(4,4, mw, mh), tmxdata=self.area.tmxdata) # define the borders self.mapBorder = pygame.Rect((0,0,mw+6,mh+6)) self.msgBorder = pygame.Rect((0,mh,sw,sh-mh)) self.hudBorder = pygame.Rect((mw,0,sw-mw,mh+6)) # load sounds from area for filename in self.area.soundFiles: SoundMan.loadSound(filename)
class WorldState(GameState): """ This state is where the player will move the hero around the map interacting with npcs, other players, objects, etc. Expects to load a specially formatted TMX map created with Tiled. Layers: Control Tiles Upper Partial Tiles Lower Partial Tiles Lower Full Tiles The control layer is where objects and boundries are placed. It will not be rendered. Your map must not have any spaces that are open. Each space must have a tile in it. Blank spaces will not be rendered properly and will leave annoying trails on the map. The control layer must be created with the utility included with lib2d. It contains metadata that lib2d can use to layout and position objects correctly. i would really like the game to be sandboxable...set traps, make contraptions, etc controls: picking up objects will affect what your buttons do equipted items always have a dedicated button should have hot-swap button and drop button """ def __init__(self, area, startPosition=None): GameState.__init__(self) self.area = area self.heroOnExit = False # use this flag when warping self.background = (203, 204, 177) self.foreground = (0, 0, 0) self.blank = True def activate(self): self.walkSound = None self.walkSoundDelay = 400 self.walkSoundPlaying = 0 self.sounds = {} self.msgFont = pygame.font.Font((res.fontPath("volter.ttf")), 9) self.border = gui.GraphicBox("dialog2-h.png", hollow=True) self.borderFilled = gui.GraphicBox("dialog2.png") self.player_vector = Vec2d(0,0) # get the root and the hero from it root = self.area.getRoot() self.hero = root.getChildByGUID(1) self.hero.move_speed = 1 # add the hero to this map if it isn't ready there if not self.area.hasChild(self.hero): self.area.add(self.hero) # load the tmx data here. it will be shared with the camera. self.tmxdata = tmxloader.load_pygame( self.area.mappath, force_colorkey=(128,128,0)) # attach a camera sw, sh = sd.get_size() mw = sw * .75 mh = sh * .75 self.camera = AreaCamera(self.area,((0,0), (mw, mh)), tmxdata=self.tmxdata) self.mapBorder = pygame.Rect((0,0,mw+6,mh+6)) self.msgBorder = pygame.Rect((0,mh,sw,sh-mh)) self.hudBorder = pygame.Rect((mw,0,sw-mw,mh+6)) # play music if any has been set in tiled try: res.playMusic(self.tmxdata.music) except AttributeError: res.fadeoutMusic() # quadtree for handling collisions with exit tiles rects = [] for guid, param in self.area.exits.items(): try: x, y, l = param[0] except: continue rects.append(ExitTile((x,y, self.tmxdata.tilewidth, self.tmxdata.tileheight), guid)) self.exitQT = QuadTree(rects) # load tile sounds for i, layer in enumerate(self.tmxdata.tilelayers): props = self.tmxdata.getTilePropertiesByLayer(i) for gid, tileProp in props: for key, value in tileProp.items(): if key[4:].lower() == "sound": self.sounds[value] = res.loadSound(value) # determine if the hero is on an exit warp. # if so, then we need to ignore collisions with it until the player # moves off of the exit. #if self.exitQT.hit(self.area.getpygame.Rect(self.hero)): # self.heroOnExit = True def deactivate(self): pass def drawSidebar(self, surface, rect): # draw the static portions of the sidebar sx, sy, sw, sh = rect self.border.draw(surface, self.hudBorder) titleFont = pygame.font.Font(res.fontPath("northwoodhigh.ttf"), 20) i = titleFont.render("MH", 1, (128,128,128)) surface.blit(i, (sw/2+sx-i.get_size()[0]/2+1, sy+3)) i = titleFont.render("MH", 1, self.foreground) surface.blit(i, (sw/2+sx-i.get_size()[0]/2, sy+2)) headFont = pygame.font.Font(res.fontPath("red_mamba.ttf"), 6) i = headFont.render("Left Hand", 1, self.foreground, self.background) surface.blit(i, (sx+ 10, sy+30)) def draw(self, surface): sx, sy = surface.get_size() if self.blank: self.blank = False surface.fill(self.background) self.drawSidebar(surface, self.hudBorder) # the main map self.camera.center(self.area.getPosition(self.hero)) self.camera.draw(surface, origin=(4, 4)) # borders self.borderFilled.draw(surface, self.msgBorder) self.border.draw(surface, self.mapBorder) log = "\n".join(self.area.messages[-5:]) rect = self.msgBorder.inflate(-16,-12) gui.drawText(surface, log, (0,0,0), rect, self.msgFont) return # debug stuff...may/may not work for obj, pos in self.area.getPositions(): y,x,w,h = self.area.topygame.Rect(self.area.getBBox(obj)) surface.fill((128,0,64), (self.camera.toScreen((x,y)), (w,h))) for gid, param in self.area.exits.items(): x, y, l = param[0] size = (16, 16) pygame.draw.rect(surface,(128,128,255), (self.camera.toScreen((x,y)),size)) for rect in self.area.geopygame.Rect: y, x, sx, sy = rect pygame.draw.rect(surface, (255,0,128, 20), (self.camera.toScreen((x, y)), (sx, sy))) def update(self, time): self.area.update(time) self.camera.update(time) if self.walkSoundPlaying > 0: self.walkSoundPlaying += time if self.walkSoundPlaying >= self.walkSoundDelay: self.walkSoundPlaying = 0 x, y = self.player_vector if x==y==0: if self.hero.avatar.isPlaying("walk"): self.hero.avatar.play("stand") else: self.hero.avatar.play("walk") if self.area.movePosition(self.hero, (x, y, 0), True): # get the type of ground being moved on heroPos = self.area.getPosition(self.hero) pos = self.worldToTile(heroPos) prop = self.tmxdata.getTileProperties(pos) if prop == None: self.walkSound = res.dummySound else: t = prop.get('walkSound', None) if t: self.walkSound = self.sounds[t] self.walkSound.set_volume(.50) else: self.walkSound = res.dummySound if self.walkSoundPlaying == 0: self.walkSoundPlaying += time self.walkSound.play() """ # test for collisions with exits exits = self.exitQT.hit(self.area.getpygame.Rect(self.hero)) if not exits and self.heroOnExit: self.heroOnExit = False if exits and not self.heroOnExit: # warp the player exit = exits.pop() position, guid = self.area.exits[exit.value] if not guid == None: area = self.area.getRoot().getChildByGUID(guid) position, otherExit = area.exits[exit.value] x, y, l = position l = 4 ox, oy, ol = self.area.getOldPosition(self.hero) if x-ox > 0: dx = self.tmxdata.tilewidth / 2 elif x-ox < 0: dx = -self.tmxdata.tilewidth / 2 else: dx = 0 if y-oy > 0: dy = self.tmxdata.tileheight / 2 elif y-oy < 0: dy = -self.tmxdata.tileheight / 2 else: dy = 0 face = self.area.getOrientation(self.hero) area.add(self.hero) print x+dx, y+dy area.setPosition(self.hero, (x, y+dy, l)) area.setOrientation(self.hero, face) sd.push(WorldState(area)) sd.done() """ def tileToWorld(self, (x, y, l)): xx = int(y) * self.tmxdata.tilewidth yy = int(x) * self.tmxdata.tileheight return xx, yy, l
def activate(self): self.walkSound = None self.walkSoundDelay = 400 self.walkSoundPlaying = 0 self.sounds = {} self.msgFont = pygame.font.Font((res.fontPath("volter.ttf")), 9) self.border = gui.GraphicBox("dialog2-h.png", hollow=True) self.borderFilled = gui.GraphicBox("dialog2.png") self.player_vector = Vec2d(0,0) # get the root and the hero from it root = self.area.getRoot() self.hero = root.getChildByGUID(1) self.hero.move_speed = 1 # add the hero to this map if it isn't ready there if not self.area.hasChild(self.hero): self.area.add(self.hero) # load the tmx data here. it will be shared with the camera. self.tmxdata = tmxloader.load_pygame( self.area.mappath, force_colorkey=(128,128,0)) # attach a camera sw, sh = sd.get_size() mw = sw * .75 mh = sh * .75 self.camera = AreaCamera(self.area,((0,0), (mw, mh)), tmxdata=self.tmxdata) self.mapBorder = pygame.Rect((0,0,mw+6,mh+6)) self.msgBorder = pygame.Rect((0,mh,sw,sh-mh)) self.hudBorder = pygame.Rect((mw,0,sw-mw,mh+6)) # play music if any has been set in tiled try: res.playMusic(self.tmxdata.music) except AttributeError: res.fadeoutMusic() # quadtree for handling collisions with exit tiles rects = [] for guid, param in self.area.exits.items(): try: x, y, l = param[0] except: continue rects.append(ExitTile((x,y, self.tmxdata.tilewidth, self.tmxdata.tileheight), guid)) self.exitQT = QuadTree(rects) # load tile sounds for i, layer in enumerate(self.tmxdata.tilelayers): props = self.tmxdata.getTilePropertiesByLayer(i) for gid, tileProp in props: for key, value in tileProp.items(): if key[4:].lower() == "sound": self.sounds[value] = res.loadSound(value)
class WorldState(GameState): """ This state is where the player will move the hero around the map interacting with npcs, other players, objects, etc. i would really like the game to be sandboxable...set traps, make contraptions, etc controls: picking up objects will affect what your buttons do equipted items always have a dedicated button should have hot-swap button and drop button This is a VIEW in mvc. """ def __init__(self, area, startPosition=None): GameState.__init__(self) self.area = area def activate(self): self.blank = True self.background = (203, 204, 177) self.foreground = (0, 0, 0) self.controller = WorldStateController(self, self.area) self.msgFont = pygame.font.Font((res.fontPath("volter.ttf")), 9) self.border = gui.GraphicBox("dialog2-h.png", hollow=True) self.borderFilled = gui.GraphicBox("dialog2.png") self.player_vector = Vec2d(0,0) # load the pygame and data assests for the area self.area.load() # get the root and the hero from it root = self.area.getRoot() self.hero = root.getChildByGUID(1) self.hero.move_speed = 1 # add the hero to this map if it isn't already there if not self.area.hasChild(self.hero): self.area.add(self.hero) # attach a camera sw, sh = sd.get_size() mw = sw * .75 mh = sh * .75 self.camera = AreaCamera(self.area,(4,4, mw, mh), tmxdata=self.area.tmxdata) # define the borders self.mapBorder = pygame.Rect((0,0,mw+6,mh+6)) self.msgBorder = pygame.Rect((0,mh,sw,sh-mh)) self.hudBorder = pygame.Rect((mw,0,sw-mw,mh+6)) # load sounds from area for filename in self.area.soundFiles: SoundMan.loadSound(filename) def deactivate(self): res.fadeoutMusic(1000) # unload the children for child in self.area.getChildren(): child.unload() self.area.music_pos = float(pygame.mixer.music.get_pos()) / 1000 SoundMan.unload() def reactivate(self): # play music if any has been set in tiled try: pygame.mixer.music.stop() res.playMusic(self.area.tmxdata.music, start=self.area.music_pos) except AttributeError: res.fadeoutMusic() self.music_playing = False else: self.music_playing = True def _drawSidebar(self, surface, rect): # draw the static portions of the sidebar sx, sy, sw, sh = rect self.border.draw(surface, self.hudBorder) titleFont = pygame.font.Font(res.fontPath("northwoodhigh.ttf"), 20) i = titleFont.render("MH", 1, (128,128,128)) surface.blit(i, (sw/2+sx-i.get_size()[0]/2+1, sy+3)) i = titleFont.render("MH", 1, self.foreground) surface.blit(i, (sw/2+sx-i.get_size()[0]/2, sy+2)) headFont = pygame.font.Font(res.fontPath("red_mamba.ttf"), 6) i = headFont.render("Left Hand", 1, self.foreground, self.background) surface.blit(i, (sx+ 10, sy+30)) def draw(self, surface): sx, sy = surface.get_size() if self.blank: self.blank = False surface.fill(self.background) self._drawSidebar(surface, self.hudBorder) self.camera.center(self.area.getPosition(self.hero)) # the main map self.camera.draw(surface) # borders self.borderFilled.draw(surface, self.msgBorder) self.border.draw(surface, self.mapBorder) log = "\n".join(self.area.messages[-5:]) rect = self.msgBorder.inflate(-16,-12) gui.drawText(surface, log, (0,0,0), rect, self.msgFont) return originalClip = surface.get_clip() surface.set_clip(self.camera.rect) # debug stuff...may/may not work for obj, pos in self.area.getPositions(): x,y,w,h = self.area.toRect(self.area.getBBox(obj)) ox, oy = self.camera.rect.topleft surface.fill((128,0,64), (self.camera.toScreen((x+ox,y+oy)), (w,h))) for gid, param in self.area.exits.items(): x, y, l = param[0] size = (16, 16) ox, oy = self.camera.rect.topleft pygame.draw.rect(surface,(128,128,255), (self.camera.toScreen((x+ox,y+oy)),size)) for rect in self.area.geoRect: x, y, sx, sy = rect ox, oy = self.camera.rect.topleft pygame.draw.rect(surface, (255,0,128, 20), (self.camera.toScreen((x+ox, y+oy)), (sy, sx))) surface.set_clip(originalClip) def update(self, time): self.area.update(time) self.camera.update(time) x, y = self.player_vector if x==y==0: if self.hero.avatar.isPlaying("walk"): self.hero.avatar.play("stand") else: self.area.movePosition(self.hero, (x, y, 0), True, caller=self) def handle_commandlist(self, cmdlist): self.controller.process(cmdlist)
class WorldState(GameState): """ This state is where the player will move the hero around the map interacting with npcs, other players, objects, etc. i would really like the game to be sandboxable...set traps, make contraptions, etc controls: picking up objects will affect what your buttons do equipted items always have a dedicated button should have hot-swap button and drop button This is a VIEW in mvc. """ def __init__(self, area, startPosition=None): GameState.__init__(self) self.area = area def activate(self): self.blank = True self.background = (203, 204, 177) self.foreground = (0, 0, 0) self.controller = WorldStateController(self, self.area) self.msgFont = pygame.font.Font((res.fontPath("volter.ttf")), 9) self.border = gui.GraphicBox("dialog2-h.png", hollow=True) self.borderFilled = gui.GraphicBox("dialog2.png") self.player_vector = Vec2d(0, 0) # load the pygame and data assests for the area self.area.load() # get the root and the hero from it root = self.area.getRoot() self.hero = root.getChildByGUID(1) self.hero.move_speed = 1 # add the hero to this map if it isn't already there if not self.area.hasChild(self.hero): self.area.add(self.hero) # attach a camera sw, sh = sd.get_size() mw = sw * .75 mh = sh * .75 self.camera = AreaCamera(self.area, (4, 4, mw, mh), tmxdata=self.area.tmxdata) # define the borders self.mapBorder = pygame.Rect((0, 0, mw + 6, mh + 6)) self.msgBorder = pygame.Rect((0, mh, sw, sh - mh)) self.hudBorder = pygame.Rect((mw, 0, sw - mw, mh + 6)) # load sounds from area for filename in self.area.soundFiles: SoundMan.loadSound(filename) def deactivate(self): res.fadeoutMusic(1000) # unload the children for child in self.area.getChildren(): child.unload() self.area.music_pos = float(pygame.mixer.music.get_pos()) / 1000 SoundMan.unload() def reactivate(self): # play music if any has been set in tiled try: pygame.mixer.music.stop() res.playMusic(self.area.tmxdata.music, start=self.area.music_pos) except AttributeError: res.fadeoutMusic() self.music_playing = False else: self.music_playing = True def _drawSidebar(self, surface, rect): # draw the static portions of the sidebar sx, sy, sw, sh = rect self.border.draw(surface, self.hudBorder) titleFont = pygame.font.Font(res.fontPath("northwoodhigh.ttf"), 20) i = titleFont.render("MH", 1, (128, 128, 128)) surface.blit(i, (sw / 2 + sx - i.get_size()[0] / 2 + 1, sy + 3)) i = titleFont.render("MH", 1, self.foreground) surface.blit(i, (sw / 2 + sx - i.get_size()[0] / 2, sy + 2)) headFont = pygame.font.Font(res.fontPath("red_mamba.ttf"), 6) i = headFont.render("Left Hand", 1, self.foreground, self.background) surface.blit(i, (sx + 10, sy + 30)) def draw(self, surface): sx, sy = surface.get_size() if self.blank: self.blank = False surface.fill(self.background) self._drawSidebar(surface, self.hudBorder) self.camera.center(self.area.getPosition(self.hero)) # the main map self.camera.draw(surface) # borders self.borderFilled.draw(surface, self.msgBorder) self.border.draw(surface, self.mapBorder) log = "\n".join(self.area.messages[-5:]) rect = self.msgBorder.inflate(-16, -12) gui.drawText(surface, log, (0, 0, 0), rect, self.msgFont) return originalClip = surface.get_clip() surface.set_clip(self.camera.rect) # debug stuff...may/may not work for obj, pos in self.area.getPositions(): x, y, w, h = self.area.toRect(self.area.getBBox(obj)) ox, oy = self.camera.rect.topleft surface.fill((128, 0, 64), (self.camera.toScreen( (x + ox, y + oy)), (w, h))) for gid, param in self.area.exits.items(): x, y, l = param[0] size = (16, 16) ox, oy = self.camera.rect.topleft pygame.draw.rect(surface, (128, 128, 255), (self.camera.toScreen( (x + ox, y + oy)), size)) for rect in self.area.geoRect: x, y, sx, sy = rect ox, oy = self.camera.rect.topleft pygame.draw.rect(surface, (255, 0, 128, 20), (self.camera.toScreen( (x + ox, y + oy)), (sy, sx))) surface.set_clip(originalClip) def update(self, time): self.area.update(time) self.camera.update(time) x, y = self.player_vector if x == y == 0: if self.hero.avatar.isPlaying("walk"): self.hero.avatar.play("stand") else: self.area.movePosition(self.hero, (x, y, 0), True, caller=self) def handle_commandlist(self, cmdlist): self.controller.process(cmdlist)