import pygame
import random
import time
pygame.init()
# إعدادات الشاشة
screen_width = 600
screen_height = 400
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("لعبة السيارات")
# الألوان
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
# إعدادات السيارة
car_width = 50
car_height = 100
car_speed = 0
car_x = (screen_width * 0.45)
car_y = (screen_height - car_height)
car_img = pygame.image.load('car.png') # تأكد من وجود صورة للسيارة بهذا الاسم في مجلد العمل
# إعدادات العقبات
obstacle_width = 50
obstacle_height = 100
obstacle_color = red
obstacle_speed = 7
obstacle_startx = random.randrange(0, screen_width - obstacle_width)
obstacle_starty = -600
clock = pygame.time.Clock()
def obstacles(obx, oby, obw, obh, color):
pygame.draw.rect(screen, color, [obx, oby, obw, obh])
def car(x, y):
screen.blit(car_img, (x,y))
game_over = False
# اللعبة الرئيسية
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
# التحكم بالسيارة
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
car_speed = -5
elif event.key == pygame.K_RIGHT:
car_speed = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
car_speed = 0
car_x += car_speed
screen.fill(white)
car(car_x, car_y)
obstacles(obstacle_startx, obstacle_starty, obstacle_width, obstacle_height, obstacle_color)
obstacle_starty += obstacle_speed
if obstacle_starty > screen_height:
obstacle_starty = 0 - obstacle_height
obstacle_startx = random.randrange(0,screen_width)
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()