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

# [pygame] car game scrolling

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.

Here is the code :

def update(self):
"""
Called every frames, update car's state.
"""

self.rect = self.image.get_rect(center = (self.x, self.y))

#x and y components
speedx = math.sin(self.angle * (math.pi/180)) * self.speed
speedy = math.cos(self.angle * (math.pi/180)) * self.speed * -1

#update the virtual positions
self.virtual_x += speedx
self.virtual_y += speedy

#check if the car is not out of the scrolling space on X
if self.virtual_x < MAP_SIZE_X - SCREEN_SIZE_X/2 and self.virtual_x > SCREEN_SIZE_X/2:
self.offset_x += speedx #move the screen

else:
self.x += speedx #move the car

#do the same for Y
if self.virtual_y < MAP_SIZE_Y - SCREEN_SIZE_Y/2 and self.virtual_y > SCREEN_SIZE_Y/2:
self.offset_y += speedy

else:
self.y += speedy

self.image = pygame.transform.rotate(self.original, self.angle * -1)

And i use self.offset_x and self.offset_y with the area parameters of the blit function to blit my circuit :

screen.blit(circuit, (0, 0), area = (self.offset_x, self.offset_y, SCREEN_SIZE_X, SCREEN_SIZE_y))

However, i noticed than i should consider the fact than my car move 'speedx' and 'speedy' pixels each frames. So actually, when i reach the left side of the circuit (for example), the self.offset_x value is never 0 and always increase each time the scrolling stop. I think to that since hours and i still don't find solutions...
I thought to do something like :

#check if the car is not out of the scrolling space on X
if self.virtual_x < MAP_SIZE_X - SCREEN_SIZE_X/2 and self.virtual_x > SCREEN_SIZE_X/2:
self.offset_x += speedx #move the screen

else:
if self.virtual_x < SCREEN_SIZE_X/2:
self.offset_x = 0

if self.virtual_x > MAP_SIZE_X - SCREEN_SIZE_X/2:
self.offset_x = MAP_SIZE_X - SCREEN_SIZE_X

self.x += speedx #move the car

But i'm not sure i'm doing well since after playing sometimes i feel the car virtual position no more match with the screen position.

I'm a bit lost and if anyone know what i'm doing wrong or if someone know a better way to implement scrolling in car game, i would be happy to hear that :)

Thanks for reading me.

PS : here is a code of a little application i use for testing scrolling method : http://pastebin.com/deMv0TpB