简体   繁体   中英

How would I go about making a camera like movement in pygame?

My game is a platform game. I want the player to move when it is X pixels away from the center, moving left or right.

I understand pygame doesn't have anything that would make a camera move.

When the player has reached the point where it is X pixels away from the center, stop the player movement and have the terrain move in the opposite direction to display the illusion of a movable terrain, acting like camera motion.

A very basic way of getting the camera centered on the player would be to just offset everything you draw so that the player is always in the center of the camera. In my own game, I use a function to translate coordinates:

def to_pygame_coords(coords):
    # move the coordinates so that 0, 0 is the player's position
    # then move the origin to the center of the window
    return coords - player.position.center + window.position.center

To expand on this so that it's not absolutely positioned on the player, you can instead center the window upon a box. Then you update the center of the box such that if the player leaves the box, the box will move along with him (thus moving the camera).

Pseudo code (not tested for negative coordinates):

BOX_WIDTH = 320
BOX_HEIGHT = 240
box_origin = player.position.center
def update_box(player_coords):
    if player_coords.x - box_origin.x > BOX_WIDTH:
        box_origin.x = player_coords.x - BOX_WIDTH
    elif box_origin.x - player_coords.x > BOX_WIDTH:
        box_origin.x = player_coords.x + BOX_WIDTH
    if player_coords.y - box_origin.y > BOX_HEIGHT:
        box_origin.y = player_coords.y - BOX_HEIGHT
    elif box_origin.y - player_coords.y > BOX_HEIGHT:
        box_origin.y = player_coords.y + BOX_HEIGHT

def to_pygame_coords(coords):
    # move the coordinates so that 0, 0 is the box's position
    # then move the origin to the center of the window
    return coords - box_origin + window.position.center

You could just make something called xscroll that is added to everything that is supposed to scroll across the screen. Then, when you reach a certain distance from the center, instead of adding your players movespeed to his position, you add or subtract the movespeed from xscroll. This makes everything move very smoothly back at the same speed your character would move. I use this in all of my games and I have never had a problem with it.

Visualization of:

Parallax scrolling: http://blog.shinylittlething.com/wp-content/uploads/2009/08/parallax.png (Usually has multiple layers, that scroll at different speeds, to show distance. )

2d tilemap scrolling: http://mikecann.co.uk/wp-content/uploads/2011/11/tm.png

Drawing coordinates on paper / these images help visualize the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM