Untitled
import pygame import random # Initialize Pygame pygame.init() # Screen dimensions WIDTH, HEIGHT = 800, 600 screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Simple Racing Game") # Colors WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (255, 0, 0) # Load images car_image = pygame.image.load('car.png') car = pygame.transform.scale(car_image, (50, 100)) # Car properties car_x = WIDTH // 2 car_y = HEIGHT - 150 car_speed = 5 # Clock clock = pygame.time.Clock() # Main game loop running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # Key press events keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: car_x -= car_speed if keys[pygame.K_RIGHT]: car_x += car_speed # Update display screen.fill(WHITE) screen.blit(car, (car_x, car_y)) pygame.display.flip() # Frame rate clock.tick(30) pygame.quit()
Leave a Comment