I am doing a top-down racing game, and I want to make the car spin when you press the left and right keys (Ive already done this part), the sprite rotation is stored in a variable as degrees. I would like him to move in accordance with the acceleration in the direction in which he is. I can figure out part of the acceleration myself by simply figuring out which pixel is in that direction. Can someone give me some simple code to help with this?
Here is the content of the corresponding class:
def __init__(self, groups):
super(Car, self).__init__(groups)
self.originalImage = pygame.image.load(os.path.join("Data", "Images", "Car.png"))
self.originalImage.set_colorkey((0,255,0))
self.image = self.originalImage.copy()
self.originalRect = self.originalImage.get_rect()
self.rect = self.originalRect.copy()
self.velocity = 0
self.acceleration = 1
self.topSpeed = 30
self.rotation = 0
self.turnRate = 5
self.moving = 0
self.centerRect = None
def update(self, lastFrame):
if self.rotation >= 360: self.rotation = 0
elif self.rotation < 0: self.rotation += 360
if self.rotation > 0:
self.image = pygame.transform.rotate(self.originalImage.copy(), self.rotation)
self.rect.size = self.image.get_rect().size
self.center()
if self.moving == 1:
self.velocity += self.acceleration
if self.velocity > self.topSpeed: self.velocity = self.topSpeed
source
share