import pygame
import random
pygame.init()
screen_width = 1050
screen_height = 700
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Car Game")
# الألوان
background_color = (135, 206, 250) # لون أزرق فاتح
car_color = (255, 0, 0) # لون أحمر
clock = pygame.time.Clock()
car_img = pygame.image.load("C:/Users/DELL/Desktop/pygame/data/car.png.png")
car_width = 60
car_height = 110
car_img = pygame.transform.scale(car_img, (car_width, car_height))
car_x = screen_width // 2 - car_width // 2
car_y = screen_height - car_height - 10
car_speed = 6
obstacle_img = pygame.image.load("C:/Users/DELL/Desktop/pygame/data/chicken.png.png")
obstacle_width = 58
obstacle_height = 58
obstacle_img = pygame.transform.scale(obstacle_img, (obstacle_width, obstacle_height))
obstacle_speed = 3
score = 0
high_score = 0
font = pygame.font.Font(None, 36)
obstacles = []
def create_obstacles():
if len(obstacles) < 2:
for _ in range(7):
x = random.randint(0, screen_width - obstacle_width)
y = random.randint(-screen_height, -obstacle_height)
obstacles.append(pygame.Rect(x, y, obstacle_width, obstacle_height))
def draw_obstacles():
for obstacle in obstacles:
screen.blit(obstacle_img, obstacle)
def move_obstacles():
for obstacle in obstacles:
obstacle.y += obstacle_speed
def check_collision():
for obstacle in obstacles:
if car_rect.colliderect(obstacle):
return True
return False
running = True
game_started = False
while running:
screen.fill(background_color)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if not game_started and event.key == pygame.K_SPACE:
game_started = True
obstacles.clear()
obstacle_speed = 5
score = 0
elif not game_started and event.key == pygame.K_r:
game_started = True
obstacles.clear()
obstacle_speed = 5
score = 0
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and car_x > 0:
car_x -= car_speed
if keys[pygame.K_RIGHT] and car_x < screen_width - car_width:
car_x += car_speed
car_rect = pygame.Rect(car_x, car_y, car_width, car_height)
if game_started:
create_obstacles()
move_obstacles()
draw_obstacles()
if check_collision():
game_started = False
if score > high_score:
high_score = score
obstacles.clear()
for obstacle in obstacles[:]:
if obstacle.y > screen_height:
obstacles.remove(obstacle)
score += 1
obstacle_speed += 0.1
score_text = font.render(f"Score: {score} High Score: {high_score}", True, (0, 0, 0))
screen.blit(score_text, (10, 10))
screen.blit(car_img, (car_x, car_y))
if not game_started:
start_text = font.render("Press SPACE to Start ", True, (0, 0, 0))
screen.blit(start_text, (screen_width // 2 - start_text.get_width() // 2, screen_height // 2))
pygame.display.flip()
clock.tick(100)
pygame.quit()
لعبة سيارة تتخطى العقبات (الحجارة) استغرقت هذه اللعبة مني 13 يوم لكنها لعبه ممتعه و مساحتها صغيره جدا جدا و تشتغل بدون انترنت كما انها مسليه عند الملل.