Untitled

 avatar
unknown
plain_text
a year ago
2.7 kB
3
Indexable
import pygame
import random

# Initialize Pygame
pygame.init()

# Set screen dimensions
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

# Set title and icon
pygame.display.set_caption("Speedster")
icon = pygame.image.load("car_icon.png")
pygame.display.set_icon(icon)

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

# Define car properties
CAR_WIDTH = 50
CAR_HEIGHT = 50
CAR_SPEED = 5
car_x = SCREEN_WIDTH / 2
car_y = SCREEN_HEIGHT / 2
car_lane = 1  # 1, 2, or 3

# Define obstacle properties
OBSTACLE_WIDTH = 20
OBSTACLE_HEIGHT = 20
obstacles = []

# Define power-up properties
POWER_UP_WIDTH = 20
POWER_UP_HEIGHT = 20
power_ups = []

# Define fuel properties
FUEL_CAPACITY = 100
fuel_level = FUEL_CAPACITY

# Game loop
while True:
    # Handle events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                car_lane -= 1
            elif event.key == pygame.K_DOWN:
                car_lane += 1

    # Move car
    car_x += CAR_SPEED

    # Check for collisions with obstacles
    for obstacle in obstacles:
        if car_x + CAR_WIDTH > obstacle[0] and car_x < obstacle[0] + OBSTACLE_WIDTH:
            if car_lane == obstacle[1]:
                print("Game Over!")
                pygame.quit()
                sys.exit()

    # Check for power-ups
    for power_up in power_ups:
        if car_x + CAR_WIDTH > power_up[0] and car_x < power_up[0] + POWER_UP_WIDTH:
            if car_lane == power_up[1]:
                # Apply power-up effect
                print("Power-up collected!")
                fuel_level += 10

    # Update fuel level
    fuel_level -= 1
    if fuel_level <= 0:
        print("Out of fuel!")
        pygame.quit()
        sys.exit()

    # Draw everything
    screen.fill(BLACK)
    pygame.draw.rect(screen, WHITE, (car_x, car_y, CAR_WIDTH, CAR_HEIGHT))
    for obstacle in obstacles:
        pygame.draw.rect(screen, RED, (obstacle[0], obstacle[1], OBSTACLE_WIDTH, OBSTACLE_HEIGHT))
    for power_up in power_ups:
        pygame.draw.rect(screen, WHITE, (power_up[0], power_up[1], POWER_UP_WIDTH, POWER_UP_HEIGHT))
    pygame.display.flip()

    # Add new obstacles and power-ups
    if random.randint(0, 100) < 10:
        obstacles.append((random.randint(0, SCREEN_WIDTH - OBSTACLE_WIDTH), random.randint(1, 3)))
    if random.randint(0, 100) < 5:
        power_ups.append((random.randint(0, SCREEN_WIDTH - POWER_UP_WIDTH), random.randint(1, 3)))

    # Cap framerate
    pygame.time.Clock().tick(60)
Editor is loading...
Leave a Comment