import pygame
import time
# تهيئة اللعبة
pygame.init()
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Air ball")
# الألوان
background_color = (0, 128, 0)
winner_background_color = (0, 255, 0) # لون الشاشة عند إعلان الفائز
player_color = (255, 255, 255)
ball_color = (255, 255, 0)
goal_color = (255, 0, 0)
# المرمى
goal_width = 10
goal_height = 0
goal_x = screen_width - goal_width - 10
goal_y = screen_height // 2 - goal_height // 2
# اللاعبين
player_width = 20
player_height = 100
player_speed = 4.7
player1_x = 50
player1_y = screen_height // 2 - player_height // 2
player2_x = screen_width - player_width - 50
player2_y = screen_height // 2 - player_height // 2
# الكرة
ball_radius = 10
ball_x = screen_width // 2
ball_y = screen_height // 2
ball_speed_x = 4
ball_speed_y = 4
# النتائج
score1 = 0
score2 = 0
font = pygame.font.Font(None, 36)
clock = pygame.time.Clock()
game_started = False
while not game_started:
screen.fill(background_color)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.KEYDOWN:
game_started = True
start_text = font.render("Press any key to start", True, (255, 255, 255))
screen.blit(start_text, (screen_width // 2 - start_text.get_width() // 2, screen_height // 2 - start_text.get_height() // 2))
pygame.display.flip()
clock.tick(60)
max_score = 10
running = True
while running:
screen.fill(background_color)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
if keys[pygame.K_w] and player1_y > 0:
player1_y -= player_speed
if keys[pygame.K_s] and player1_y < screen_height - player_height:
player1_y += player_speed
if keys[pygame.K_UP] and player2_y > 0:
player2_y -= player_speed
if keys[pygame.K_DOWN] and player2_y < screen_height - player_height:
player2_y += player_speed
ball_x += ball_speed_x
ball_y += ball_speed_y
if ball_y > screen_height - ball_radius or ball_y < ball_radius:
ball_speed_y *= -1
if ball_x > screen_width - ball_radius - goal_width and goal_y < ball_y < goal_y + goal_height:
ball_speed_x *= -1
if ball_x > screen_width - ball_radius:
ball_x = screen_width // 2
ball_y = screen_height // 2
ball_speed_x *= -1
score1 += 1
if ball_x < ball_radius:
ball_x = screen_width // 2
ball_y = screen_height // 2
ball_speed_x *= -1
score2 += 1
player1_rect = pygame.Rect(player1_x, player1_y, player_width, player_height)
player2_rect = pygame.Rect(player2_x, player2_y, player_width, player_height)
ball_rect = pygame.Rect(ball_x - ball_radius, ball_y - ball_radius, ball_radius * 2, ball_radius * 2)
goal_rect = pygame.Rect(goal_x, goal_y, goal_width, goal_height)
if player1_rect.colliderect(ball_rect) or player2_rect.colliderect(ball_rect):
ball_speed_x *= -1
pygame.draw.rect(screen, player_color, player1_rect)
pygame.draw.rect(screen, player_color, player2_rect)
pygame.draw.circle(screen, ball_color, (ball_x, ball_y), ball_radius)
pygame.draw.rect(screen, goal_color, goal_rect)
score_text = font.render(f"Player A: {score1} Player B: {score2}", True, (255, 255, 255))
screen.blit(score_text, (screen_width // 2 - score_text.get_width() // 2, 20))
if score1 == max_score or score2 == max_score:
# تحديث لون الشاشة عند إعلان الفائز
screen.fill(winner_background_color)
winner_text = font.render(f"Player {'A' if score1 == max_score else 'B'} Wins!", True, (255, 255, 255))
# تكبير حجم النص عند إعلان الفائز
winner_text = pygame.transform.scale(winner_text, (winner_text.get_width() * 2, winner_text.get_height() * 2))
screen.blit(winner_text, (screen_width // 2 - winner_text.get_width() // 2, screen_height // 2 - winner_text.get_height() // 2))
pygame.display.flip()
time.sleep(5)
score1 = 0
score2 = 0
ball_x = screen_width // 2
ball_y = screen_height // 2
ball_speed_x = 4
ball_speed_y = 4
# إعادة تهيئة اللعبة عند البداية
game_started = False
while not game_started:
screen.fill(background_color)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.KEYDOWN:
game_started = True
start_text = font.render("Press any key to start", True, (255, 255, 255))
screen.blit(start_text, (screen_width // 2 - start_text.get_width() // 2, screen_height // 2 - start_text.get_height() // 2))
pygame.display.flip()
clock.tick(60)
pygame.display.flip()
clock.tick(90)
pygame.quit()
لعبة ping pong صنعت هذه اللعبة باستخدام برنامج pygame لغه python استغرقت هذه اللعبة 4 ايام فقط
من مميزاتها تعمل بدون انترنت تحتاج الى شخصين للعب و هذا قد يجعل الناس يكونون صداقات عندما يلعبونها مع ناس اخرين
لعبه مسليه فيها تحدي لأنه يجب على اللاعب الوصول الى 10 نقاط اولا .