import pygame import sys class Car(pygame.sprite.Sprite): def __init__(self): super().__init__() self.original_image = pygame.image.load('Audi.png') self.image = self.original_image self.rect = self.image.get_rect(center=(640, 360)) self.angle = 0 self.rotation_speed = 1.8 self.direction = 0 self.forward = pygame.math.Vector2(0, -1) self.velocity = pygame.math.Vector2(0, 0) self.acceleration = 0.5 self.drag = 0.02 self.max_speed = 10 self.brake = 0.8 self.active = False def set_rotation(self): if self.direction == 1: self.angle -= self.rotation_speed if self.direction == -1: self.angle += self.rotation_speed self.image = pygame.transform.rotozoom(self.original_image, self.angle, 0.25) self.rect = self.image.get_rect(center=self.rect.center) def get_rotation(self): if self.direction == 1: self.forward.rotate_ip(self.rotation_speed) if self.direction == -1: self.forward.rotate_ip(-self.rotation_speed) def accelerate(self): if self.active: self.velocity += self.forward * self.acceleration if self.velocity.magnitude() > self.max_speed: self.velocity.scale_to_length(self.max_speed) def apply_drag(self): self.velocity *= (1 - self.drag) def update(self): self.set_rotation() self.get_rotation() self.accelerate() self.apply_drag() self.rect.center += self.velocity # Implement drifting if self.direction != 0 and not self.active: self.velocity.rotate_ip(-self.direction * self.rotation_speed * 0.5) # Reduce velocity while drifting pygame.init() screen = pygame.display.set_mode((1280, 720)) clock = pygame.time.Clock() bg_track = pygame.image.load('Track.png') car = pygame.sprite.GroupSingle(Car()) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_RIGHT: car.sprite.direction += 1 if event.key == pygame.K_LEFT: car.sprite.direction -= 1 if event.key == pygame.K_SPACE: car.sprite.active = True if event.type == pygame.KEYUP: if event.key == pygame.K_RIGHT: car.sprite.direction -= 1 if event.key == pygame.K_LEFT: car.sprite.direction += 1 if event.key == pygame.K_SPACE: car.sprite.active = False screen.blit(bg_track, (0, 0)) car.draw(screen) car.update() pygame.display.update() clock.tick(120)