Public Group

# Pygame - 2d SideScroller

This topic is 2262 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I've done just about everything in my platformer except for making the screen follow the player. How would I go about doing this? I've read that I can make all the platforms move around the player, giving the illusion that the player is moving, but I really don't want to do that.

##### Share on other sites
Basically, whenever a move input comes in, you don't move the player. INSTEAD, you move the level in the exact opposite direction to the input.

This is the jist of it. Now, once you have this in place, you can add more tweaks to it. For example, allowing the player character to move around within a threshold, after which the player stops moving and the level start to move in the opposite direction.

##### Share on other sites
These are abstracts things, the implementation depends all what you already have, so without any specific details how do things it's hard to say anything.

*Isn't there some kind of Camera is Pygame? Can't you simply just position it in the same coordinates as the player?
*Or there is a view transformation of some kind? In that case, you have to adjust this transformations to the negated coordinates of the player.
*Or is there something else? Maybe the view/camera can be moved with a delta position? In that case, you have to use the delta position of the player.

One thing is almost certain, you don't have to actually move the level data in the memory, if that's what you thought. Moving the player or the level means altering the rendering function, which should be decoupled from the game logic.
(You can move the level, and with large levels it's almost certain that you have to move level data around, but almost certainly in chunks, and not totally synchronously with the player).

I hope that makes sense. Edited by szecs

##### Share on other sites

Basically, whenever a move input comes in, you don't move the player. INSTEAD, you move the level in the exact opposite direction to the input.

This is the jist of it. Now, once you have this in place, you can add more tweaks to it. For example, allowing the player character to move around within a threshold, after which the player stops moving and the level start to move in the opposite direction.

Thank you. But as I stated I want to avoid this method at all costs.

These are abstracts things, the implementation depends all what you already have, so without any specific details how do things it's hard to say anything.

*Isn't there some kind of Camera is Pygame? Can't you simply just position it in the same coordinates as the player?
*Or there is a view transformation of some kind? In that case, you have to adjust this transformations to the negated coordinates of the player.
*Or is there something else? Maybe the view/camera can be moved with a delta position? In that case, you have to use the delta position of the player.

One thing is almost certain, you don't have to actually move the level data in the memory, if that's what you thought. Moving the player or the level means altering the rendering function, which should be decoupled from the game logic.
(You can move the level, and with large levels it's almost certain that you have to move level data around, but almost certainly in chunks, and not totally synchronously with the player).

I hope that makes sense.

I think I understand what you're saying

*Isn't there some kind of Camera is Pygame? Can't you simply just position it in the same coordinates as the player?[/quote]
This is what I am looking for. Is there a Camera or View in Pygame?

##### Share on other sites
This is actually REALLY easy to do in Pygame. You do it using pygame.rect.Rect()
camera = pygame.rect.Rect(0, 0, w, h)
The 0's just set the initial position of the camera to (0, 0), w and h would be the width and height you want the camera to be, usually you just set this to the width and height of the screen. Then when you move the player, say to the right, you also increase the camera.x value as if it was moving to the right aswell.
 if KeyRight: player.x += player.speed camera.x += player.speed 
Then when you're actually rendering all the other stuff (tiles, items, etc) you render them at their current position, minus the camera's current position.
screen.blit(tile.image, tile.x - camera.x, tile.y - camera.y)
You can mess with it to, so instead of moving the camera when the player moves every frame, you just reset the center of the camera to be at the center of the player's image. That way the camera is always centered around the player. Edited by breinygames

##### Share on other sites
Cool, but would i do camera.x += player.speed or would i use camera.move_ip() (if its the second one, can you please give me an example)

The way you gave me doesn't seem to be moving the camera.

EDIT:
nevermind, the camera wasn't moving because my player's speed values are decimals and appareantly rect coordinates can only be integers. (is there anyway to add a double to a rect's coordinates?

Even when the player's horizontal speed is as small as one, the level zooms away out of site. Edited by xhh

##### Share on other sites

This is actually REALLY easy to do in Pygame. You do it using pygame.rect.Rect()
camera = pygame.rect.Rect(0, 0, w, h)

The 0's just set the initial position of the camera to (0, 0), w and h would be the width and height you want the camera to be, usually you just set this to the width and height of the screen. Then when you move the player, say to the right, you also increase the camera.x value as if it was moving to the right aswell.
 if KeyRight: player.x += player.speed camera.x += player.speed 
Then when you're actually rendering all the other stuff (tiles, items, etc) you render them at their current position, minus the camera's current position.
screen.blit(tile.image, tile.x - camera.x, tile.y - camera.y)
You can mess with it to, so instead of moving the camera when the player moves every frame, you just reset the center of the camera to be at the center of the player's image. That way the camera is always centered around the player.
That doesn't work, and I don't see why it would work either, considering pygame.rect have no extra command called Rect.

Or maybe I'm doing it wrong, could someone whip up a code showing me how its done?

##### Share on other sites
I'm trying to figure out how this guy implemented a camera type object, but his code is so advance that I figure out how he's doing it: http://inventwithpython.com/pygame/chapter8.html

1. 1
Rutin
27
2. 2
3. 3
4. 4
5. 5

• 11
• 11
• 10
• 13
• 20
• ### Forum Statistics

• Total Topics
632948
• Total Posts
3009399
• ### Who's Online (See full list)

There are no registered users currently online

×