وعليكم السلام ورحمة الله وبركاته،
إذا كنت تقصد الشيفرات التي تجعل الكرة تعكس اتجاهاها عندما تصل إلى إحدى حواف الشاشة فإليك الشيفرات
import pygame
from pygame.locals import *
# تهيئة محرك Pygame
pygame.init()
# إعداد النافذة
screen_width, screen_height = 800, 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("لعبة الكرة")
# متغيرات الكرة
ball_radius = 20
ball_x, ball_y = screen_width // 2, screen_height // 2
ball_speed_x, ball_speed_y = 5, 5
# دورة اللعبة
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
exit()
# تحديث موقع الكرة
ball_x += ball_speed_x
ball_y += ball_speed_y
# ارتداد الكرة عند الحواف
# عندما يكون الإحداثي السيني صفرًا أو أكبر من عرض الشاشة
if ball_x < 0 or ball_x > screen_width - ball_radius:
# نضرب سرعة الكرة في -1 حتى نعكس اتجاه الكرة
ball_speed_x *= -1
# نفس الأسلوب مع الإحداثي الصادي
if ball_y < 0 or ball_y > screen_height - ball_radius:
ball_speed_y *= -1
# رسم الكرة
screen.fill((0, 0, 0)) # مسح الشاشة
pygame.draw.circle(screen, (255, 255, 255), (ball_x, ball_y), ball_radius)
pygame.display.update() # تحديث الشاشة