Untitled
unknown
plain_text
2 years ago
1.1 kB
11
Indexable
import pygame
import sys
# Initialize Pygame
pygame.init()
# Set up the screen
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Car Game")
# Colors
WHITE = (255, 255, 255)
RED = (255, 0, 0)
# Car variables
car_width = 50
car_height = 100
car_x = screen_width // 2 - car_width // 2
car_y = screen_height - car_height - 20
car_speed = 5
clock = pygame.time.Clock()
def draw_car(x, y):
pygame.draw.rect(screen, RED, (x, y, car_width, car_height))
def game_loop():
game_over = False
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
car_x -= car_speed
if keys[pygame.K_RIGHT]:
car_x += car_speed
screen.fill(WHITE)
draw_car(car_x, car_y)
pygame.display.update()
clock.tick(60)
game_loop()
Editor is loading...
Leave a Comment