示例#1
0
文件: engine.py 项目: ryanlid87/RPG
class Engine:
    def __init__(self, ioSystem):
        self.ioSystem = ioSystem
        self.loadLevel("1")

    def loadLevel(self, level):
        self.level = Level(level)
        self.player = Player(self.level.spawnLocation, self.ioSystem)

    def battle(self, attacker, defender):
        """Perform a battle"""
        while attacker.isAlive():
            action = attacker.getBattleAction(defender)
            if action == Action.Battle.Attack:
                damage = attacker.attack - defender.defence
                if damage <= 0:  # Defence > attack
                    self.ioSystem.attackMissed(attacker, defender)
                else:
                    self.ioSystem.attackHit(attacker, defender, damage)
                    defender.hp -= damage
            elif action == Action.Battle.Run:
                if chance(0.50):
                    self.ioSystem.runSuccess(attacker, defender)
                    return
                else:
                    self.ioSystem.runFailed(attacker, defender)
            else:
                raise Exception("Invalid battle action: %s" % action)

            attacker, defender = defender, attacker

    def move(self, player, direction):
        """Move the player in [direction]."""
        position = player.position

        # Get a copy of the players position and move it to the destination
        nextPosition = position.copy()
        nextPosition.move(direction)

        nextTile = self.level.at(nextPosition)
        if nextTile.canEnter(player):
            player.position = nextPosition
            nextTile.onEnter(player, self)
            self.ioSystem.enteredLocation(nextPosition)

    def replaceTile(self, location, tile):
        self.level.setTile(location, tile)

    def teleport(self, player, position):
        self.player.position = position
        self.ioSystem.teleported(position)

    def run(self):
        while True:
            action = self.ioSystem.getWorldAction(self.player)
            self.move(self.player, action)
示例#2
0
    def test_mobs(self):
        level = Level(25, 25)
        level.up_stairs_pos = Pos(1, 1)
        world = World([level])

        mob = level.mobs[2, 2] = Mob(Pos(2, 2), 0, mobinfo['orc'])
        mob.move_to(Pos(3, 3))
        self.assertNotIn((2, 2), level.mobs)
        self.assertEquals(mob, level.mobs.get((3, 3)))

        self.assertEquals(level.mobs.get(world.levels[0].up_stairs_pos),
                          world.player)
示例#3
0
    def draw( self, surface, interpol, time_sec ):
        surface.fill( (50,50,50) )

        #resman.get("game.hud_left_surf").draw( surface, Vec2D(0,0) )


        frame = Frame( surface, time_sec, interpol )
        frame.X_OFFSET = MonorailEditor.X_OFFSET
        frame.Y_OFFSET = MonorailEditor.Y_OFFSET
        frame.draw_z( [self.level] )

        if self.current_tile in [MonorailEditor.FLAT, MonorailEditor.ENTERANCE]:
            frame.draw( self.edit_tile1 )

        elif self.current_tile in [MonorailEditor.NORTH_SLOPE ,MonorailEditor.EAST_SLOPE, MonorailEditor.SOUTH_SLOPE, MonorailEditor.WEST_SLOPE]:
            frame.draw( self.edit_tile1 )
            frame.draw( self.edit_tile2 )

        elif self.current_tile == MonorailEditor.ERASE:
            pass

        # draw filename
        font = pygame.font.Font( None, 24 )
        render_text = font.render( Level.get_filename( self.level_nr ), 0, (255,255,255) )
        surface.blit( render_text, (100,10) )
示例#4
0
	def __init__(self, data, level_title, level_file, player_health, win_func, lose_func, return_to_picker_func, restart_me_func):	
		super(LevelScreen, self).__init__()	
		
		# Store props
		self.screen_size = data.get_screen_size()
		self.level_title = level_title
		self._on_win_func = win_func
		self._on_lose_func = lose_func
		self._screen_manager = data.get_screen_manager()
		self._return_to_picker_func = return_to_picker_func
		self._restart_me_func = restart_me_func
				
		# Create a level object
		self.my_level = Level(level_file, player_health)
		self.my_level.init()
		
		# Create a camera object
		surface = data.get_surface()
		level_size = self.my_level.get_size()
		self._camera_factory = lambda rect: Camera(surface.get_rect(), rect, level_size[0], level_size[1], _settings.SLACK_X, _settings.SLACK_Y)
		self.camera = self._camera_factory(self.my_level.player.rect)
		
		# Create dependencies and overlay renderers
		self.sprite_renderer = SpriteRenderer(surface)
		self.shape_renderer = ShapeRenderer(surface)
		self.health_bar = HealthBar(self.shape_renderer, self.screen_size, self.my_level.player)
		self.level_rings = LevelRings(self.sprite_renderer)
		
		# Initialize key handling
		self.up = self.down = self.left = self.right = False
		
		# Create an array for all of the fired entities from attackers
		self.entities = []
示例#5
0
    def draw(self, surface, interpol, time_sec):
        surface.fill((50, 50, 50))

        #resman.get("game.hud_left_surf").draw( surface, Vec2D(0,0) )

        frame = Frame(surface, time_sec, interpol)
        frame.X_OFFSET = MonorailEditor.X_OFFSET
        frame.Y_OFFSET = MonorailEditor.Y_OFFSET
        frame.draw_z([self.level])

        if self.current_tile in [
                MonorailEditor.FLAT, MonorailEditor.ENTERANCE
        ]:
            frame.draw(self.edit_tile1)

        elif self.current_tile in [
                MonorailEditor.NORTH_SLOPE, MonorailEditor.EAST_SLOPE,
                MonorailEditor.SOUTH_SLOPE, MonorailEditor.WEST_SLOPE
        ]:
            frame.draw(self.edit_tile1)
            frame.draw(self.edit_tile2)

        elif self.current_tile == MonorailEditor.ERASE:
            pass

        # draw filename
        font = pygame.font.Font(None, 24)
        render_text = font.render(Level.get_filename(self.level_nr), 0,
                                  (255, 255, 255))
        surface.blit(render_text, (100, 10))
示例#6
0
    def __init__(self, data, level_title, level_file, player_health, win_func,
                 lose_func, return_to_picker_func, restart_me_func):
        super(LevelScreen, self).__init__()

        # Store props
        self.screen_size = data.get_screen_size()
        self.level_title = level_title
        self._on_win_func = win_func
        self._on_lose_func = lose_func
        self._screen_manager = data.get_screen_manager()
        self._return_to_picker_func = return_to_picker_func
        self._restart_me_func = restart_me_func

        # Create a level object
        self.my_level = Level(level_file, player_health)
        self.my_level.init()

        # Create a camera object
        surface = data.get_surface()
        level_size = self.my_level.get_size()
        self._camera_factory = lambda rect: Camera(surface.get_rect(
        ), rect, level_size[0], level_size[1], _settings.SLACK_X, _settings.
                                                   SLACK_Y)
        self.camera = self._camera_factory(self.my_level.player.rect)

        # Create dependencies and overlay renderers
        self.sprite_renderer = SpriteRenderer(surface)
        self.shape_renderer = ShapeRenderer(surface)
        self.health_bar = HealthBar(self.shape_renderer, self.screen_size,
                                    self.my_level.player)
        self.level_rings = LevelRings(self.sprite_renderer)

        # Initialize key handling
        self.up = self.down = self.left = self.right = False

        # Create an array for all of the fired entities from attackers
        self.entities = []
示例#7
0
    def test_out_of_bounds(self):
        level = Level(100, 100)

        self.assertNotIn((100, 100), level)
        self.assertNotIn((-1, -1), level)
        self.assertIn((50, 50), level)

        self.assertEquals(level[(-1, -1)].name, 'stone wall')
        self.assertTrue(level[(-1, -1)].blocked)
        self.assertTrue(level[(-1, -1)].opaque)
        self.assertFalse(level[(-1, -1)].explored)

        self.assertEquals(level[(101, 101)].name, 'stone wall')
        self.assertTrue(level[(101, 101)].blocked)
        self.assertTrue(level[(101, 101)].opaque)
        self.assertFalse(level[(101, 101)].explored)
示例#8
0
 def save_all( self ):
     self.level.save( Level.get_filename( self.level_nr ) )
示例#9
0
 def load_level( self ):
     self.level = Level()
     if os.path.exists( Level.get_filename( self.level_nr ) ):
         self.level.load( Level.get_filename( self.level_nr ) )
示例#10
0
class MonorailEditor:
    FLAT, NORTH_SLOPE, EAST_SLOPE, SOUTH_SLOPE, WEST_SLOPE, ENTERANCE,\
    ERASE, MAX = range( 8 )

    X_OFFSET, Y_OFFSET = 20, 300

    def __init__( self, level_nr ):
        self.level_nr = level_nr
        self.load_level()
        self.current_tile = MonorailEditor.FLAT
        self.update_edit_tiles()

    def load_level( self ):
        self.level = Level()
        if os.path.exists( Level.get_filename( self.level_nr ) ):
            self.level.load( Level.get_filename( self.level_nr ) )

    def save_all( self ):
        self.level.save( Level.get_filename( self.level_nr ) )

    def do_tick( self, indev ):
        self.update_tiles()
        self.update_edit_tiles()

        if pygame.mouse.get_pressed()[0]: # Left mouse button
            if self.current_tile in [MonorailEditor.FLAT, MonorailEditor.ENTERANCE]:
                self.level.set_tile( self.edit_tile1 )
            elif self.current_tile in [MonorailEditor.NORTH_SLOPE, MonorailEditor.EAST_SLOPE, MonorailEditor.SOUTH_SLOPE, MonorailEditor.WEST_SLOPE]:
                self.level.set_tile( self.edit_tile1 )
                self.level.set_tile( self.edit_tile2 )
            elif self.current_tile == MonorailEditor.ERASE:
                self.level.remove_tile( self.edit_tile1.pos.x, self.edit_tile1.pos.y )


        if indev.key.went_down( K_PAGEUP ):
            if self.level_nr > 0:
                self.save_all()
                self.level_nr -= 1
                self.load_level()
        elif indev.key.went_down( K_PAGEDOWN ):
            self.save_all()
            self.level_nr += 1
            self.load_level()

        if indev.key.went_down( K_UP ):
            for tile in self.level.tiles:
                tile.pos.y -= 1
        if indev.key.went_down( K_DOWN ):
            for tile in self.level.tiles:
                tile.pos.y += 1
        if indev.key.went_down( K_LEFT ):
            for tile in self.level.tiles:
                tile.pos.x -= 1
        if indev.key.went_down( K_RIGHT ):
            for tile in self.level.tiles:
                tile.pos.x += 1


    def draw( self, surface, interpol, time_sec ):
        surface.fill( (50,50,50) )

        #resman.get("game.hud_left_surf").draw( surface, Vec2D(0,0) )


        frame = Frame( surface, time_sec, interpol )
        frame.X_OFFSET = MonorailEditor.X_OFFSET
        frame.Y_OFFSET = MonorailEditor.Y_OFFSET
        frame.draw_z( [self.level] )

        if self.current_tile in [MonorailEditor.FLAT, MonorailEditor.ENTERANCE]:
            frame.draw( self.edit_tile1 )

        elif self.current_tile in [MonorailEditor.NORTH_SLOPE ,MonorailEditor.EAST_SLOPE, MonorailEditor.SOUTH_SLOPE, MonorailEditor.WEST_SLOPE]:
            frame.draw( self.edit_tile1 )
            frame.draw( self.edit_tile2 )

        elif self.current_tile == MonorailEditor.ERASE:
            pass

        # draw filename
        font = pygame.font.Font( None, 24 )
        render_text = font.render( Level.get_filename( self.level_nr ), 0, (255,255,255) )
        surface.blit( render_text, (100,10) )

    def draw_mouse( self, surface, interpol, time_sec ):
        x, y = pygame.mouse.get_pos()
        resman.get("gui_surf").draw( surface, Vec2D(x, y), (0,0,32,32) )


    def mouse_down( self, button ):
        if button == 4: # Wheel up
            self.current_tile = (self.current_tile+MonorailEditor.MAX-1) % MonorailEditor.MAX
        if button == 5: # Wheel down
            self.current_tile = (self.current_tile+1) % MonorailEditor.MAX

        self.update_edit_tiles()

    def update_edit_tiles( self ):
        mouse_x, mouse_y = pygame.mouse.get_pos()

        pos = Vec3D((-mouse_y + (mouse_x+32)/2 - MonorailEditor.X_OFFSET/2 + MonorailEditor.Y_OFFSET) / 32,
    	            (mouse_y + (mouse_x-32)/2 - MonorailEditor.X_OFFSET/2 - MonorailEditor.Y_OFFSET) / 32,
                     0)

        if self.current_tile == MonorailEditor.FLAT:
            self.edit_tile1 = Tile( pos, Tile.Type.FLAT )
        elif self.current_tile == MonorailEditor.NORTH_SLOPE:
            self.edit_tile1 = Tile( pos, Tile.Type.NORTH_SLOPE_TOP )
            pos += Vec3D(-1,0,0)
            self.edit_tile2 = Tile( pos, Tile.Type.NORTH_SLOPE_BOT )
        elif self.current_tile == MonorailEditor.EAST_SLOPE:
            self.edit_tile1 = Tile( pos, Tile.Type.EAST_SLOPE_TOP )
            pos += Vec3D(0,1,0)
            self.edit_tile2 = Tile( pos, Tile.Type.EAST_SLOPE_BOT )
        elif self.current_tile == MonorailEditor.SOUTH_SLOPE:
            self.edit_tile1 = Tile( pos, Tile.Type.SOUTH_SLOPE_TOP )
            pos += Vec3D(-1,2,0)
            self.edit_tile2 = Tile( pos, Tile.Type.SOUTH_SLOPE_BOT )
        elif self.current_tile == MonorailEditor.WEST_SLOPE:
            self.edit_tile1 = Tile( pos, Tile.Type.WEST_SLOPE_TOP )
            pos += Vec3D(-2,1,0)
            self.edit_tile2 = Tile( pos, Tile.Type.WEST_SLOPE_BOT )
        elif self.current_tile == MonorailEditor.ENTERANCE:
            self.edit_tile1 = Enterance( pos )
        #elif self.current_tile == MonorailEditor.RAILGATE:
        #    self.edit_tile1 = RailGate( pos )
        elif self.current_tile == MonorailEditor.ERASE:
            self.edit_tile1 = Tile( pos, Tile.Type.FLAT )
            pass

    def update_tiles( self ):
        for tile in self.level.tiles:
            tile.pickup = None
            tile.set_selected( tile.is_switch() )
示例#11
0
class LevelScreen(Screen):
    """Handles rendering of the actual side scrolling game
	"""

    # Constructor
    # NOTE: This c'tor is not a legal Screen.ScreenManger factory
    def __init__(self, data, level_title, level_file, player_health, win_func,
                 lose_func, return_to_picker_func, restart_me_func):
        super(LevelScreen, self).__init__()

        # Store props
        self.screen_size = data.get_screen_size()
        self.level_title = level_title
        self._on_win_func = win_func
        self._on_lose_func = lose_func
        self._screen_manager = data.get_screen_manager()
        self._return_to_picker_func = return_to_picker_func
        self._restart_me_func = restart_me_func

        # Create a level object
        self.my_level = Level(level_file, player_health)
        self.my_level.init()

        # Create a camera object
        surface = data.get_surface()
        level_size = self.my_level.get_size()
        self._camera_factory = lambda rect: Camera(surface.get_rect(
        ), rect, level_size[0], level_size[1], _settings.SLACK_X, _settings.
                                                   SLACK_Y)
        self.camera = self._camera_factory(self.my_level.player.rect)

        # Create dependencies and overlay renderers
        self.sprite_renderer = SpriteRenderer(surface)
        self.shape_renderer = ShapeRenderer(surface)
        self.health_bar = HealthBar(self.shape_renderer, self.screen_size,
                                    self.my_level.player)
        self.level_rings = LevelRings(self.sprite_renderer)

        # Initialize key handling
        self.up = self.down = self.left = self.right = False

        # Create an array for all of the fired entities from attackers
        self.entities = []

    # Handles pygame key change events
    def _handle_key_change(self, key, value):
        if key == settingsmanager.get_user_setting(Keys.SETTING_JUMP_KEY):
            self.up = value
        elif key == settingsmanager.get_user_setting(Keys.SETTING_CROUCH_KEY):
            self.down = value
        elif key == settingsmanager.get_user_setting(Keys.SETTING_LEFT_KEY):
            self.left = value
        elif key == settingsmanager.get_user_setting(Keys.SETTING_RIGHT_KEY):
            self.right = value

    # Handles key up events, and clears the stored value for any of the keys we were listening for
    def handle_key_up(self, key):
        self._handle_key_change(key, False)

        # if this is the escape key, quit the game
        if key == K_ESCAPE:
            self._pause_game()

    # Handles key down events, and stores a flag for any of the keys we are listening for
    def handle_key_down(self, key):
        self._handle_key_change(key, True)

    def _explore_map(self):
        self._screen_manager.set(lambda *args: MapViewerScreen(
            *args, level=self.my_level, camera_factory=self._camera_factory))

    def _pause_game(self):
        self._screen_manager.set(lambda *args: PauseMenuScreen(
            *args,
            level_name=self.level_title,
            return_to_picker_screen_func=self._return_to_picker_func,
            restart_func=self._restart_me_func,
            view_map_func=self._explore_map))

    def _add_entity(self, bullet):
        self.my_level.all_sprite.add(bullet)
        self.entities.append(bullet)

    def _on_entity_die(self, ent):
        self.my_level.all_sprite.remove(ent)
        self.entities.remove(ent)

    # Renders a side scoller display
    def render(self, refresh_time):
        # Check if the player has one. If they have,
        # call on_win_func() and return; we're done.
        if self.my_level.player.has_won(self.my_level.win_blocks):
            self._on_win_func(self.my_level.total_stars, self.my_level.stars,
                              self.my_level.player.health)
            return

        # Set the backgroud to sky blue
        self.shape_renderer.render_rect(
            (0, 0, self.screen_size[0], self.screen_size[1]),
            color=colors.SKY_BLUE)

        pygame.mouse.set_visible(False)  # Hide the mouse inside the game

        # Update the player object based of the currently pressed keys
        self.my_level.player.update(self.up, self.down, self.left, self.right,
                                    self.my_level.obstacles)

        # Refresh the camera
        self.camera.update()  # Update the camera position

        # Find the visible sprites
        visible_sprites, invisible_sprites = self.camera.get_all_that_can_see(
            self.my_level.all_sprite)
        visible_obstacles = [
            o for o in self.my_level.obstacles if o in visible_sprites
        ]

        # Handle updating a spawner
        for s in self.my_level.attackers:
            s.add_time(refresh_time)
            if s in visible_sprites:
                s.try_attack(self.my_level.player, visible_obstacles,
                             self._add_entity)

        # Handle updating a current enitity
        for s in self.entities:
            s.add_time(refresh_time)
            if s in visible_sprites:
                s.update(self.entities, visible_obstacles,
                         self.my_level.player, lambda: self._on_entity_die(s),
                         lambda: self._on_lose_func())

        # Handle rendering the visible sprites
        for s in visible_sprites:
            # Render the sprite to the screen
            self.sprite_renderer.render(
                s, convert_rect=self.camera.convert_rect_for_render)

        # Update other items
        self.my_level.update_health_packs()
        self.my_level.update_bonus_stars()

        # Render the health bar - if there is any reason to show it
        if len(self.my_level.attackers) != 0:
            self.health_bar.render()

        # If there are any rings (named stars TODO rename) render the ring count at the top
        if self.my_level.total_stars != 0:
            self.level_rings.render(
                self.screen_size[0] - self.screen_size[0] / 64,
                self.screen_size[1] / 64, self.my_level.total_stars,
                self.my_level.stars)
示例#12
0
class LevelScreen(Screen):
	"""Handles rendering of the actual side scrolling game
	"""
	
	# Constructor
	# NOTE: This c'tor is not a legal Screen.ScreenManger factory
	def __init__(self, data, level_title, level_file, player_health, win_func, lose_func, return_to_picker_func, restart_me_func):	
		super(LevelScreen, self).__init__()	
		
		# Store props
		self.screen_size = data.get_screen_size()
		self.level_title = level_title
		self._on_win_func = win_func
		self._on_lose_func = lose_func
		self._screen_manager = data.get_screen_manager()
		self._return_to_picker_func = return_to_picker_func
		self._restart_me_func = restart_me_func
				
		# Create a level object
		self.my_level = Level(level_file, player_health)
		self.my_level.init()
		
		# Create a camera object
		surface = data.get_surface()
		level_size = self.my_level.get_size()
		self._camera_factory = lambda rect: Camera(surface.get_rect(), rect, level_size[0], level_size[1], _settings.SLACK_X, _settings.SLACK_Y)
		self.camera = self._camera_factory(self.my_level.player.rect)
		
		# Create dependencies and overlay renderers
		self.sprite_renderer = SpriteRenderer(surface)
		self.shape_renderer = ShapeRenderer(surface)
		self.health_bar = HealthBar(self.shape_renderer, self.screen_size, self.my_level.player)
		self.level_rings = LevelRings(self.sprite_renderer)
		
		# Initialize key handling
		self.up = self.down = self.left = self.right = False
		
		# Create an array for all of the fired entities from attackers
		self.entities = []
	
	# Handles pygame key change events
	def _handle_key_change(self, key, value):
		if key == settingsmanager.get_user_setting(Keys.SETTING_JUMP_KEY):
			self.up = value
		elif key == settingsmanager.get_user_setting(Keys.SETTING_CROUCH_KEY):
			self.down = value
		elif key == settingsmanager.get_user_setting(Keys.SETTING_LEFT_KEY):
			self.left = value
		elif key == settingsmanager.get_user_setting(Keys.SETTING_RIGHT_KEY):
			self.right = value
	
	# Handles key up events, and clears the stored value for any of the keys we were listening for
	def handle_key_up(self, key):
		self._handle_key_change(key, False)
		
		# if this is the escape key, quit the game
		if key == K_ESCAPE:
			self._pause_game()
	
	# Handles key down events, and stores a flag for any of the keys we are listening for
	def handle_key_down(self, key):
		self._handle_key_change(key, True)
	
	def _explore_map(self):
		self._screen_manager.set(lambda *args: MapViewerScreen(*args, level=self.my_level, camera_factory=self._camera_factory))
	
	def _pause_game(self):
		self._screen_manager.set(lambda *args: PauseMenuScreen(*args, level_name=self.level_title, return_to_picker_screen_func=self._return_to_picker_func, restart_func=self._restart_me_func, view_map_func=self._explore_map))
	
	def _add_entity(self, bullet):
		self.my_level.all_sprite.add(bullet)
		self.entities.append(bullet)
		
	def _on_entity_die(self, ent):
		self.my_level.all_sprite.remove(ent)
		self.entities.remove(ent)
	
	# Renders a side scoller display
	def render(self, refresh_time):
		# Check if the player has one. If they have, 
		# call on_win_func() and return; we're done.
		if self.my_level.player.has_won(self.my_level.win_blocks):		
			self._on_win_func(self.my_level.total_stars, self.my_level.stars, self.my_level.player.health)
			return
		
		# Set the backgroud to sky blue
		self.shape_renderer.render_rect((0, 0, self.screen_size[0], self.screen_size[1]), color=colors.SKY_BLUE)
		
		pygame.mouse.set_visible(False)	# Hide the mouse inside the game
		
		# Update the player object based of the currently pressed keys	
		self.my_level.player.update(self.up, self.down, self.left, self.right, self.my_level.obstacles)
				
		# Refresh the camera 
		self.camera.update() # Update the camera position
		
		# Find the visible sprites
		visible_sprites, invisible_sprites = self.camera.get_all_that_can_see(self.my_level.all_sprite)
		visible_obstacles = [o for o in self.my_level.obstacles if o in visible_sprites]
		
		# Handle updating a spawner
		for s in self.my_level.attackers:
			s.add_time(refresh_time)
			if s in visible_sprites:
				s.try_attack(self.my_level.player, visible_obstacles, self._add_entity)
		
		# Handle updating a current enitity
		for s in self.entities:
			s.add_time(refresh_time)
			if s in visible_sprites:
				s.update(self.entities, visible_obstacles, self.my_level.player, lambda: self._on_entity_die(s), lambda: self._on_lose_func())
		
		# Handle rendering the visible sprites
		for s in visible_sprites:
			# Render the sprite to the screen
			self.sprite_renderer.render(s, convert_rect=self.camera.convert_rect_for_render)
		
		# Update other items
		self.my_level.update_health_packs()
		self.my_level.update_bonus_stars()
		
		# Render the health bar - if there is any reason to show it
		if len(self.my_level.attackers) != 0:
			self.health_bar.render()
		
		# If there are any rings (named stars TODO rename) render the ring count at the top
		if self.my_level.total_stars != 0:
			self.level_rings.render(self.screen_size[0] - self.screen_size[0]/64, self.screen_size[1]/64, self.my_level.total_stars, self.my_level.stars)
示例#13
0
 def save_all(self):
     self.level.save(Level.get_filename(self.level_nr))
示例#14
0
 def load_level(self):
     self.level = Level()
     if os.path.exists(Level.get_filename(self.level_nr)):
         self.level.load(Level.get_filename(self.level_nr))
示例#15
0
class MonorailEditor:
    FLAT, NORTH_SLOPE, EAST_SLOPE, SOUTH_SLOPE, WEST_SLOPE, ENTERANCE,\
    ERASE, MAX = range( 8 )

    X_OFFSET, Y_OFFSET = 20, 300

    def __init__(self, level_nr):
        self.level_nr = level_nr
        self.load_level()
        self.current_tile = MonorailEditor.FLAT
        self.update_edit_tiles()

    def load_level(self):
        self.level = Level()
        if os.path.exists(Level.get_filename(self.level_nr)):
            self.level.load(Level.get_filename(self.level_nr))

    def save_all(self):
        self.level.save(Level.get_filename(self.level_nr))

    def do_tick(self, indev):
        self.update_tiles()
        self.update_edit_tiles()

        if pygame.mouse.get_pressed()[0]:  # Left mouse button
            if self.current_tile in [
                    MonorailEditor.FLAT, MonorailEditor.ENTERANCE
            ]:
                self.level.set_tile(self.edit_tile1)
            elif self.current_tile in [
                    MonorailEditor.NORTH_SLOPE, MonorailEditor.EAST_SLOPE,
                    MonorailEditor.SOUTH_SLOPE, MonorailEditor.WEST_SLOPE
            ]:
                self.level.set_tile(self.edit_tile1)
                self.level.set_tile(self.edit_tile2)
            elif self.current_tile == MonorailEditor.ERASE:
                self.level.remove_tile(self.edit_tile1.pos.x,
                                       self.edit_tile1.pos.y)

        if indev.key.went_down(K_PAGEUP):
            if self.level_nr > 0:
                self.save_all()
                self.level_nr -= 1
                self.load_level()
        elif indev.key.went_down(K_PAGEDOWN):
            self.save_all()
            self.level_nr += 1
            self.load_level()

        if indev.key.went_down(K_UP):
            for tile in self.level.tiles:
                tile.pos.y -= 1
        if indev.key.went_down(K_DOWN):
            for tile in self.level.tiles:
                tile.pos.y += 1
        if indev.key.went_down(K_LEFT):
            for tile in self.level.tiles:
                tile.pos.x -= 1
        if indev.key.went_down(K_RIGHT):
            for tile in self.level.tiles:
                tile.pos.x += 1

    def draw(self, surface, interpol, time_sec):
        surface.fill((50, 50, 50))

        #resman.get("game.hud_left_surf").draw( surface, Vec2D(0,0) )

        frame = Frame(surface, time_sec, interpol)
        frame.X_OFFSET = MonorailEditor.X_OFFSET
        frame.Y_OFFSET = MonorailEditor.Y_OFFSET
        frame.draw_z([self.level])

        if self.current_tile in [
                MonorailEditor.FLAT, MonorailEditor.ENTERANCE
        ]:
            frame.draw(self.edit_tile1)

        elif self.current_tile in [
                MonorailEditor.NORTH_SLOPE, MonorailEditor.EAST_SLOPE,
                MonorailEditor.SOUTH_SLOPE, MonorailEditor.WEST_SLOPE
        ]:
            frame.draw(self.edit_tile1)
            frame.draw(self.edit_tile2)

        elif self.current_tile == MonorailEditor.ERASE:
            pass

        # draw filename
        font = pygame.font.Font(None, 24)
        render_text = font.render(Level.get_filename(self.level_nr), 0,
                                  (255, 255, 255))
        surface.blit(render_text, (100, 10))

    def draw_mouse(self, surface, interpol, time_sec):
        x, y = pygame.mouse.get_pos()
        resman.get("gui_surf").draw(surface, Vec2D(x, y), (0, 0, 32, 32))

    def mouse_down(self, button):
        if button == 4:  # Wheel up
            self.current_tile = (self.current_tile + MonorailEditor.MAX -
                                 1) % MonorailEditor.MAX
        if button == 5:  # Wheel down
            self.current_tile = (self.current_tile + 1) % MonorailEditor.MAX

        self.update_edit_tiles()

    def update_edit_tiles(self):
        mouse_x, mouse_y = pygame.mouse.get_pos()

        pos = Vec3D(
            (-mouse_y + (mouse_x + 32) / 2 - MonorailEditor.X_OFFSET / 2 +
             MonorailEditor.Y_OFFSET) / 32,
            (mouse_y + (mouse_x - 32) / 2 - MonorailEditor.X_OFFSET / 2 -
             MonorailEditor.Y_OFFSET) / 32, 0)

        if self.current_tile == MonorailEditor.FLAT:
            self.edit_tile1 = Tile(pos, Tile.Type.FLAT)
        elif self.current_tile == MonorailEditor.NORTH_SLOPE:
            self.edit_tile1 = Tile(pos, Tile.Type.NORTH_SLOPE_TOP)
            pos += Vec3D(-1, 0, 0)
            self.edit_tile2 = Tile(pos, Tile.Type.NORTH_SLOPE_BOT)
        elif self.current_tile == MonorailEditor.EAST_SLOPE:
            self.edit_tile1 = Tile(pos, Tile.Type.EAST_SLOPE_TOP)
            pos += Vec3D(0, 1, 0)
            self.edit_tile2 = Tile(pos, Tile.Type.EAST_SLOPE_BOT)
        elif self.current_tile == MonorailEditor.SOUTH_SLOPE:
            self.edit_tile1 = Tile(pos, Tile.Type.SOUTH_SLOPE_TOP)
            pos += Vec3D(-1, 2, 0)
            self.edit_tile2 = Tile(pos, Tile.Type.SOUTH_SLOPE_BOT)
        elif self.current_tile == MonorailEditor.WEST_SLOPE:
            self.edit_tile1 = Tile(pos, Tile.Type.WEST_SLOPE_TOP)
            pos += Vec3D(-2, 1, 0)
            self.edit_tile2 = Tile(pos, Tile.Type.WEST_SLOPE_BOT)
        elif self.current_tile == MonorailEditor.ENTERANCE:
            self.edit_tile1 = Enterance(pos)
        #elif self.current_tile == MonorailEditor.RAILGATE:
        #    self.edit_tile1 = RailGate( pos )
        elif self.current_tile == MonorailEditor.ERASE:
            self.edit_tile1 = Tile(pos, Tile.Type.FLAT)
            pass

    def update_tiles(self):
        for tile in self.level.tiles:
            tile.pickup = None
            tile.set_selected(tile.is_switch())
示例#16
0
文件: engine.py 项目: ryanlid87/RPG
 def loadLevel(self, level):
     self.level = Level(level)
     self.player = Player(self.level.spawnLocation, self.ioSystem)