Untitled

 avatar
unknown
plain_text
2 years ago
3.1 kB
5
Indexable
import pygame
import random

# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)

# Set the width and height of the screen
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600

# Define the Car class
class Car(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()

        # Load the car image and resize it
        self.image = pygame.image.load("car.png").convert_alpha()
        self.image = pygame.transform.scale(self.image, (80, 160))

        # Set the rect and initial position
        self.rect = self.image.get_rect()
        self.rect.x = SCREEN_WIDTH // 2 - self.rect.width // 2
        self.rect.y = SCREEN_HEIGHT - self.rect.height - 10

        # Set the speed
        self.speed_x = 0

    def update(self):
        # Move the car left or right
        self.rect.x += self.speed_x

        # Keep the car on the screen
        if self.rect.x < 0:
            self.rect.x = 0
        elif self.rect.x > SCREEN_WIDTH - self.rect.width:
            self.rect.x = SCREEN_WIDTH - self.rect.width

# Define the Obstacle class
class Obstacle(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()

        # Load the obstacle image and resize it
        self.image = pygame.image.load("obstacle.png").convert_alpha()
        self.image = pygame.transform.scale(self.image, (80, 160))

        # Set the rect and initial position
        self.rect = self.image.get_rect()
        self.rect.x = random.randint(0, SCREEN_WIDTH - self.rect.width)
        self.rect.y = -self.rect.height

        # Set the speed
        self.speed_y = random.randint(5, 10)

    def update(self):
        # Move the obstacle down
        self.rect.y += self.speed_y

        # Reset the obstacle position if it goes off the screen
        if self.rect.y > SCREEN_HEIGHT:
            self.rect.x = random.randint(0, SCREEN_WIDTH - self.rect.width)
            self.rect.y = -self.rect.height
            self.speed_y = random.randint(5, 10)

# Initialize Pygame
pygame.init()

# Set the screen size
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

# Set the caption
pygame.display.set_caption("Car Game")

# Load the font
font = pygame.font.SysFont(None, 48)

# Create the car sprite
car = Car()

# Create the obstacle sprite group
obstacles = pygame.sprite.Group()

# Set the initial score
score = 0

# Set the game over flag
game_over = False

# Set the clock
clock = pygame.time.Clock()

# Main game loop
while not game_over:
    # Handle events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_over = True
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                car.speed_x = -5
            elif event.key == pygame.K_RIGHT:
                car.speed_x = 5
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT and car.speed_x < 0:
                car.speed_x = 0
            elif event.key == pygame.K_RIGHT and car.speed_x > 0:
                car.speed_x = 0

    # Update the sprites
    car.update()
    obstacles.update()

Editor is loading...