aaa
import pygame
import sys
# تهيئة pygame
pygame.init()
# أبعاد النافذة
WIDTH, HEIGHT = 800, 400
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("لعبة كرة قدم")
# الألوان
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
# الإعدادات الأساسية
clock = pygame.time.Clock()
FPS = 60
# حجم ومكان اللاعب
player_width, player_height = 50, 50
player_x, player_y = WIDTH // 2 - player_width // 2, HEIGHT - player_height - 20
player_speed = 5
# حجم ومكان الكرة
ball_radius = 15
ball_x, ball_y = WIDTH // 2, HEIGHT - player_height - 30
ball_speed_x, ball_speed_y = 0, 0
# حجم ومكان المرمى
goal_post_width, goal_post_height = 100, 10
goal_post_x = WIDTH // 2 - goal_post_width // 2
goal_post_y = 0
# متغير للتحقق من تسجيل الهدف
scored = False
# دالة لرسم العناصر على الشاشة
def draw_objects():
screen.fill(WHITE) # خلفية بيضاء
pygame.draw.rect(screen, BLUE, (player_x, player_y, player_width, player_height)) # اللاعب
pygame.draw.circle(screen, RED, (ball_x, ball_y), ball_radius) # الكرة
pygame.draw.rect(screen, BLACK, (goal_post_x, goal_post_y, goal_post_width, goal_post_height)) # المرمى
# دالة لتحريك اللاعب
def move_player(keys):
global player_x
if keys[pygame.K_LEFT] and player_x > 0:
player_x -= player_speed
if keys[pygame.K_RIGHT] and player_x < WIDTH - player_width:
player_x += player_speed
# دالة لتحريك الكرة
def move_ball():
global ball_x, ball_y, ball_speed_x, ball_speed_y, scored
# تحريك الكرة
ball_x += ball_speed_x
ball_y += ball_speed_y
# التحقق من اصطدام الكرة مع الحواف
if ball_x - ball_radius <= 0 or ball_x + ball_radius >= WIDTH:
ball_speed_x = -ball_speed_x
if ball_y - ball_radius <= 0 or ball_y + ball_radius >= HEIGHT:
ball_speed_y = -ball_speed_y
# التحقق من تسجيل الهدف
if (goal_post_y <= ball_y <= goal_post_y + goal_post_height and
goal_post_x <= ball_x <= goal_post_x + goal_post_width):
scored = True
# دالة لإعادة تشغيل اللعبة بعد تسجيل الهدف
def reset_game():
global ball_x, ball_y, ball_speed_x, ball_speed_y, scored
ball_x, ball_y = WIDTH // 2, HEIGHT - player_height - 30
ball_speed_x, ball_speed_y = 0, 0
scored = False
# الحلقة الرئيسية للعبة
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE and not scored: # تسريع الكرة عند الضغط على المسافة
ball_speed_x = 5
ball_speed_y = -7
keys = pygame.key.get_pressed()
move_player(keys)
move_ball()
# إعادة تشغيل اللعبة إذا تم تسجيل الهدف
if scored:
font = pygame.font.Font(None, 36)
text = font.render("هدف! اضغط R لإعادة اللعبة", True, BLACK)
screen.blit(text, (WIDTH // 2 - 150, HEIGHT // 2))
pygame.display.flip()
clock.tick(FPS)
keys = pygame.key.get_pressed()
if keys[pygame.K_r]:
reset_game()
else:
draw_objects()
pygame.display.flip()
clock.tick(FPS)
pygame.quit()
sys.exit()