Untitled
unknown
plain_text
a year ago
1.6 kB
3
Indexable
import pygame
import random
import sys
# Initialize pygame
pygame.init()
# Screen dimensions
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
# Initialize screen
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Car Racing Game")
# Clock for controlling frame rate
clock = pygame.time.Clock()
# Load assets
car_width = 50
car_height = 100
car_image = pygame.Surface((car_width, car_height))
car_image.fill(GREEN)
obstacle_width = 50
obstacle_height = 100
# Fonts
font = pygame.font.SysFont(None, 50)
def display_message(text):
"""Displays a message in the center of the screen."""
render_text = font.render(text, True, RED)
text_rect = render_text.get_rect(center=(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2))
screen.blit(render_text, text_rect)
pygame.display.update()
pygame.time.delay(2000)
def game_loop():
"""Main game loop."""
# Car properties
car_x = SCREEN_WIDTH // 2
car_y = SCREEN_HEIGHT - car_height - 10
car_speed = 10
# Obstacle properties
obstacle_x = random.randint(0, SCREEN_WIDTH - obstacle_width)
obstacle_y = -obstacle_height
obstacle_speed = 7
score = 0
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Get keypresses
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and car_xEditor is loading...
Leave a Comment