G

car game

public
Guest Jun 01, 2025 Never 24
Clone
This code snippet is a simple 2D racing game implemented using the Pygame library in Python. Let's break down the key components and functionality of the code: 1. The code begins by importing the necessary modules: `pygame` for game development and `random` for generating random numbers. 2. Pygame is initialized with `pygame.init()`, and a screen of dimensions 500x600 pixels is created for the game. 3. An image of a car is loaded and scaled to a size of 50x80 pixels. The initial position of the car (`car_x` and `car_y`) is set at the bottom center of the screen. 4. Obstacles are set up as a list of lists, where each sublist contains the x and y coordinates of an obstacle. The obstacles are initially placed above the screen with random x-coordinates. 5. Colors are defined, white color `(255, 255, 255)` is used for filling the screen. 6. The game loop (`while running:`) starts where the game logic is executed. It includes event handling for quitting the game, controlling car movement using the arrow keys, moving the obstacles downward, repositioning obstacles when they go past the screen boundaries, and drawing both the car and obstacles on the screen. 7. The game loop continuously updates the display with the current game state and maintains a constant frame rate of 30 frames per second using `clock.tick(30)`. 8. The game runs until the player quits (`running = False`) by closing the game window, at which point `pygame.quit()` is called to safely exit the Pygame application. Overall, this code snippet sets up a basic 2D racing game environment where the player can control a car to avoid obstacles moving downwards on the screen. It demonstrates fundamental concepts of game development, such as creating game assets, handling user input, managing game objects, and updating the game state within a game loop.
1
import pygame
2
import random
3
4
# Initialize Pygame
5
pygame.init()
6
7
# Screen setup
8
WIDTH, HEIGHT = 500, 600
9
screen = pygame.display.set_mode((WIDTH, HEIGHT))
10
pygame.display.set_caption("Simple 2D Racing Game")
11
12
# Load assets
13
car = pygame.image.load("car.png") # Add a car image
14
car = pygame.transform.scale(car, (50, 80))
15
car_x, car_y = WIDTH//2 - 25, HEIGHT - 100
16
17
# Obstacle setup
18
obstacle_w, obstacle_h = 50, 80
19
obstacles = [[random.randint(0, WIDTH-obstacle_w), -random.randint(100, 400)] for _ in range(5)]
20
21
# Colors
22
WHITE = (255, 255, 255)
23
24
# Game variables
25
clock = pygame.time.Clock()
26
speed = 5
27
running = True
28
29
while running:
30
screen.fill(WHITE)
31
32
# Event handling
33
for event in pygame.event.get():
34
if event.type == pygame.QUIT:
35
running = False
36
37
# Car movement
38
keys = pygame.key.get_pressed()
39
if keys[pygame.K_LEFT] and car_x > 0:
40
car_x -= speed
41
if keys[pygame.K_RIGHT] and car_x < WIDTH - 50:
42
car_x += speed
43
44
# Move obstacles
45
for obs in obstacles:
46
obs[1] += speed
47
if obs[1] > HEIGHT:
48
obs[1] = -random.randint(100, 400)
49
obs[0] = random.randint(0, WIDTH - obstacle_w)
50
51
pygame.draw.rect(screen, (200, 0, 0), (obs[0], obs[1], obstacle_w, obstacle_h))
52
53
# Draw car
54
screen.blit(car, (car_x, car_y))
55
56
# Update screen
57
pygame.display.update()
58
clock.tick(30)
59
60
pygame.quit()