Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.0 kB
0
Indexable
Never
import pygame

# Initialize Pygame
pygame.init()

# Set up the game window
win = pygame.display.set_mode((500, 500))
pygame.display.set_caption("Car Game")

# Load the car image
car_img = pygame.image.load('car.png')
car_width = 50
car_height = 100

# Set up the car starting position
car_x = 225
car_y = 400

# Set up the car speed
car_speed = 5

# Main game loop
run = True
while run:
    # Handle events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    
    # Get the current state of the arrow keys
    keys = pygame.key.get_pressed()
    
    # Move the car left or right based on arrow keys
    if keys[pygame.K_LEFT] and car_x > 0:
        car_x -= car_speed
    elif keys[pygame.K_RIGHT] and car_x + car_width < 500:
        car_x += car_speed
    
    # Clear the screen
    win.fill((255, 255, 255))
    
    # Draw the car on the screen
    win.blit(car_img, (car_x, car_y))
    
    # Update the screen
    pygame.display.update()

# Quit Pygame
pygame.quit()