from typing import Any import pygame import sys import random class Car(pygame.sprite.Sprite): def __init__(self, w: int, h: int): super().__init__() self.original_image = pygame.image.load('Top down Car Racing/Audi.png') self.image = self.original_image self.rect = self.image.get_rect(center=(640, 360)) self.angle = 0 self.rotation_speed = 3.6 self.direction = 0 self.forward = pygame.math.Vector2(0, -1) 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: new_center = self.rect.center + self.forward * 5 if 0 <= new_center[0] <= 1280 and 0 <= new_center[1] <= 720: self.rect.center = new_center def update(self): self.set_rotation() self.get_rotation() self.accelerate() class People(pygame.sprite.Sprite): def __init__(self, x: int, y: int, blood_splat_group: pygame.sprite.Group): super().__init__() self.radius = 15 self.image = pygame.Surface((2 * self.radius, 2 * self.radius), pygame.SRCALPHA) # Draw black border pygame.draw.circle(self.image, (0, 0, 0), (self.radius, self.radius), self.radius + 2) # Draw colored circle pygame.draw.circle(self.image, (0, 0, 255), (self.radius, self.radius), self.radius) # Draw smiley face pygame.draw.arc(self.image, (0, 0, 0), pygame.Rect(self.radius // 2, self.radius, self.radius, self.radius // 2), 0, -pygame.math.Vector2(1, 0).angle_to(pygame.math.Vector2(0, 1)), 2) pygame.draw.circle(self.image, (0, 0, 0), (self.radius // 2, self.radius), 2) pygame.draw.circle(self.image, (0, 0, 0), (self.radius * 3 // 2, self.radius), 2) pygame.draw.arc(self.image, (0, 0, 0), pygame.Rect(self.radius // 2, self.radius * 3 // 2, self.radius, self.radius // 4), 0, pygame.math.Vector2(1, 0).angle_to(pygame.math.Vector2(0, -1)), 2) self.rect = self.image.get_rect(center=(x, y)) self.walk_timer = 0 self.stop_duration = random.randint(30, 60) self.move_vector = pygame.math.Vector2(random.uniform(-2, 2), random.uniform(-2, 2)) self.blood_splat_group = blood_splat_group def update(self): if self.walk_timer > 0: self.rect.move_ip(self.move_vector) self.walk_timer -= 1 else: self.stop_duration -= 1 if self.stop_duration <= 0: self.walk_timer = random.randint(10, 30) self.stop_duration = random.randint(30, 60) self.move_vector = pygame.math.Vector2(random.uniform(-2, 2), random.uniform(-2, 2)) # Ensure people stay within screen boundaries self.rect.x = max(0, min(self.rect.x, 1280 - 2 * self.radius)) self.rect.y = max(0, min(self.rect.y, 720 - 2 * self.radius)) def hit(self): # Add blood splat at the person's position blood_splat = BloodSplat(self.rect.center) self.blood_splat_group.add(blood_splat) # Reset the person's position self.rect.center = (random.randint(0, 1280), random.randint(0, 720)) class BloodSplat(pygame.sprite.Sprite): def __init__(self, position): super().__init__() # Triple the size of the blood splat image self.image = pygame.transform.scale(pygame.image.load('Top down Car Racing/bloodsplat.png'), (50, 50)) # Adjust the size as needed self.rect = self.image.get_rect(center=position) self.lifespan = 50 # Adjust the lifespan of the blood splat def update(self): self.lifespan -= 1 if self.lifespan <= 0: self.kill() pygame.init() screen = pygame.display.set_mode((1280, 720)) clock = pygame.time.Clock() bg_track = pygame.image.load('Top down Car Racing/BG2.jpg') car = pygame.sprite.GroupSingle(Car(100, 100)) score = 0 people_group = pygame.sprite.Group() blood_splat_group = pygame.sprite.Group() people_group.add(People(300, 400, blood_splat_group)) people_group.add(People(800, 200, blood_splat_group)) 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)) # Check for collisions with people collisions = pygame.sprite.spritecollide(car.sprite, people_group, dokill=True) for person in collisions: # You hit a person! Increase your score score += 1 # Create a blood splat at the person's position person.hit() # Spawn a new person at a random position new_person = People(random.randint(0, 1280), random.randint(0, 720), blood_splat_group) people_group.add(new_person) # Update the position of all people people_group.update() # Draw the car car.draw(screen) car.update() # Draw people on the screen people_group.draw(screen) # Update and draw blood splats blood_splat_group.update() blood_splat_group.draw(screen) # Display the score font = pygame.font.Font(None, 36) score_text = font.render(f'People Hit: {score}', True, (0, 0, 0)) screen.blit(score_text, (10, 10)) pygame.display.update() clock.tick(120)