def __init__(self, name, game): self.next = self # this is the name of the level, which is '1', '2', etc. self.name = name self.game = game # get background info for this scene (not currently used) # get level data for this scene self.level = Level(name, self.game) self.player = self.level.player self.lsize = self.level.get_size() # print "level size:", lsize self.camera = Camera(self.player.rect, self.lsize[0], self.lsize[1]) # my name is the name displayed at the top of the screen self.myname = name + ':' + LEVEL_NAMES[name] self.namefont = pygame.font.Font('fonts/ShareTechMono-Regular.ttf' , 20) self.nametxt = self.namefont.render(self.myname, True, WHITE) # play music if not already going self.game.jukebox.play_music_if('game') # get special stuff, e.g. tutorial (this should be a list, and # empty if there is no special stuff). self.special_stuff = special.get_special_stuff(self, self.game)
def __init__(self, state, filename): self.state = state # Create Object Manager self.object_mgr = objectmgr.ObjectManager(self) # Load TMX file tmx = tmxlib.Map.open(assets.path(filename)) # Initialize variables using TMX map properties # Properties used in maps are: # name - The name of the map # script - File name of custom script that should be used with map # music - The file name of the music that should play during the level # camera_x - Camera's starting x coordinate # camera_y - Camera's starting y coordinate # camera_state - State camera should start in # camera_params - Parameters to initialize camera's state in dict format self.width, self.height = tmx.pixel_size self.properties = tmx.properties self.name = tmx.properties.get("name", "") self.script = tmx.properties.get("script") self.music = tmx.properties.get("music", "music.ogg") # Camera initialization properties self.camera = Camera(self, "camera", int(tmx.properties.get("camera_x", 0)), int(tmx.properties.get("camera_y", 0))) # TODO: Camera state (normal, follow, move to, etc.) # TODO: Camera state parameters # Generate tile map using TMX tile map data self.tilemap = tilemap.TileMap(tmx) # Pass TMX object data to Object Manager to generate objects self.object_mgr.createFromTMX(tmx)
class Scene: def __init__(self, state, filename): self.state = state # Create Object Manager self.object_mgr = objectmgr.ObjectManager(self) # Load TMX file tmx = tmxlib.Map.open(assets.path(filename)) # Initialize variables using TMX map properties # Properties used in maps are: # name - The name of the map # script - File name of custom script that should be used with map # music - The file name of the music that should play during the level # camera_x - Camera's starting x coordinate # camera_y - Camera's starting y coordinate # camera_state - State camera should start in # camera_params - Parameters to initialize camera's state in dict format self.width, self.height = tmx.pixel_size self.properties = tmx.properties self.name = tmx.properties.get("name", "") self.script = tmx.properties.get("script") self.music = tmx.properties.get("music", "music.ogg") # Camera initialization properties self.camera = Camera(self, "camera", int(tmx.properties.get("camera_x", 0)), int(tmx.properties.get("camera_y", 0))) # TODO: Camera state (normal, follow, move to, etc.) # TODO: Camera state parameters # Generate tile map using TMX tile map data self.tilemap = tilemap.TileMap(tmx) # Pass TMX object data to Object Manager to generate objects self.object_mgr.createFromTMX(tmx) # TODO: Load script that may have been in TMX's map properties # TODO: Initialize script def destroy(self): self.object_mgr.clear() def setPlayer(self, player): """Set the player object used with this scene.""" self.state.setPlayer(player) self.player = player self.camera.follow(player) def update(self, td): self.object_mgr.update(td) self.camera.update(td) def draw(self, surface): cx = -self.camera.x cy = -self.camera.y # Draw tile map back layer self.tilemap.draw(surface, cx, cy, 0, 1) # Draw object manager self.object_mgr.draw(surface, cx, cy) # Draw tile map front layer self.tilemap.draw(surface, cx, cy, 1, 2) def debug_draw(self, surface): self.object_mgr.debug_draw(surface, int(-self.camera.x), int(-self.camera.y))
class PlayScene(Scene): def __init__(self, name, game): self.next = self # this is the name of the level, which is '1', '2', etc. self.name = name self.game = game # get background info for this scene (not currently used) # get level data for this scene self.level = Level(name, self.game) self.player = self.level.player self.lsize = self.level.get_size() # print "level size:", lsize self.camera = Camera(self.player.rect, self.lsize[0], self.lsize[1]) # my name is the name displayed at the top of the screen self.myname = name + ':' + LEVEL_NAMES[name] self.namefont = pygame.font.Font('fonts/ShareTechMono-Regular.ttf' , 20) self.nametxt = self.namefont.render(self.myname, True, WHITE) # play music if not already going self.game.jukebox.play_music_if('game') # get special stuff, e.g. tutorial (this should be a list, and # empty if there is no special stuff). self.special_stuff = special.get_special_stuff(self, self.game) def process_input(self, events, pressed, dt): # move the player self.player.process_input(events, pressed, dt) for ev in events: if (ev.action == SWITCH and ev.down): self.game.jukebox.play_sfx('switch') self.level.switch() # process special stuff (e.g. tutorial) for spec in self.special_stuff: spec.process_input(events, pressed, dt) def update(self, dt): self.player.update() # check for player going off the level ybot = self.player.rect.y + self.player.rect.h xleft = self.player.rect.x if ((xleft < 0) or (xleft > self.lsize[0]) or (ybot < 0) or (ybot > self.lsize[1])): self.player.isdead = True if self.player.isdead: self.game.jukebox.play_sfx('dead') self.next = DeadScene(self, self.game) if self.player.collidedoor: self.player.complete_image = self.player.image self.game.jukebox.play_sfx('complete') self.next = LevelCompleteScene(self, self.game) # move camera self.camera.update() # process special stuff (e.g. tutorial) for spec in self.special_stuff: spec.update(dt) def render(self, screen): # background is white at the moment screen.blit(self.game.bg, (0, 0)) # screen.fill(BLACK) # sprites in the foreground (current level) for s in self.level.current_sprites: if s.rect.colliderect(self.camera.rect): screen.blit(s.image, RelRect(s, self.camera)) # sprites in the background (other level) for s in self.level.other_sprites: if s.rect.colliderect(self.camera.rect): screen.blit(s.image_other, RelRect(s, self.camera)) # permanent sprites (e.g. spikes, doors) for s in self.level.permanent_sprites: if s.rect.colliderect(self.camera.rect): screen.blit(s.image, RelRect(s, self.camera)) # player if self.player.image: screen.blit(self.player.image, RelRect(self.player, self.camera)) # name of the level screen.blit(self.nametxt, NAME_POS) # render special stuff (e.g. tutorial) for spec in self.special_stuff: spec.render(screen)