[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] car game scrolling

On Mon, Sep 5, 2011 at 5:54 PM, Nathan BIAGINI <nathan.open@xxxxxxxxx> wrote:
> Hi again,
> i have a new problem to solve concerning my little car game. Actually i
> thought i did it but i just stopped a bug in my code.
> In fact, i decided to not move the car but to scroll the screen. I only move
> the car when the player reach one of the four edges of the map. I keep the
> 'virtual' position of the car on the circuit and i use them to determinate
> how i need to stop scrolling the screen and start moving the car.

Hi Nathan,

I ran your demo and I have two suggestions:

1. You calculate a new rectangle for your car at the top of the update
method based on self.image from *last* frame. Then you calculate a new
self.image based on the current rotation. When I moved the rectangle
calculation down to be the last thing in update(), the turning was
*much* smoother.

2. When doing scrolling like this, I would normally give the player
object only what you call "virtual_x" and "virtual_y" - the position
of the player relative to the map. These would be the only things
updated in the update() method - I wouldn't track x, y, offset_x or
offset_y at all. I create a separate class that I call "Camera" or
"Viewport" with its own x and y, then at the point that I'm drawing
things I subtract the camera's x and y from the player's x and y to
find the screen coordinates to draw the player.

Here's your demo with those two changes:


Hope that's helpful!