Загрузка данных


import pygame, random, math

pygame.init()
W, H = 800, 500
FLOOR = H - 50
screen = pygame.display.set_mode((W, H))
pygame.display.set_caption("Basket Random Clone")
clock = pygame.time.Clock()
font = pygame.font.SysFont("arial", 36)

GRAVITY = 0.6
BALL_BOUNCE = 0.75

class Player:
    def __init__(self, x, color, left_key, right_key, jump_key, side):
        self.x = x
        self.y = FLOOR - 40
        self.w, self.h = 40, 40
        self.vx = 0
        self.vy = 0
        self.color = color
        self.left_key = left_key
        self.right_key = right_key
        self.jump_key = jump_key
        self.on_ground = True
        self.side = side  # -1 left player, 1 right player
        self.min_x = 0 if side == -1 else W // 2
        self.max_x = W // 2 - self.w if side == -1 else W - self.w

    def update(self, keys):
        self.vx = 0
        if keys[self.left_key]:
            self.vx = -6
        if keys[self.right_key]:
            self.vx = 6
        if keys[self.jump_key] and self.on_ground:
            self.vy = -14
            self.on_ground = False

        self.vy += GRAVITY
        self.x += self.vx
        self.y += self.vy

        self.x = max(self.min_x, min(self.max_x, self.x))

        if self.y >= FLOOR - self.h:
            self.y = FLOOR - self.h
            self.vy = 0
            self.on_ground = True

    def rect(self):
        return pygame.Rect(self.x, self.y, self.w, self.h)

    def draw(self):
        pygame.draw.rect(screen, self.color, self.rect(), border_radius=8)


class Ball:
    def __init__(self):
        self.reset()
        self.r = 15

    def reset(self):
        self.x = W // 2
        self.y = H // 2 - 100
        self.vx = random.choice([-3, 3])
        self.vy = 0

    def update(self):
        self.vy += GRAVITY * 0.6
        self.x += self.vx
        self.y += self.vy

        if self.x - self.r < 0:
            self.x = self.r
            self.vx *= -1
        if self.x + self.r > W:
            self.x = W - self.r
            self.vx *= -1

        if self.y + self.r >= FLOOR:
            self.y = FLOOR - self.r
            self.vy *= -BALL_BOUNCE
            self.vx *= 0.98

    def rect(self):
        return pygame.Rect(self.x - self.r, self.y - self.r, self.r * 2, self.r * 2)

    def draw(self):
        pygame.draw.circle(screen, (255, 140, 0), (int(self.x), int(self.y)), self.r)


def hit_ball(player, ball):
    if player.rect().colliderect(ball.rect()):
        dx = ball.x - (player.x + player.w / 2)
        dy = ball.y - (player.y + player.h / 2)
        dist = math.hypot(dx, dy) or 1
        power = 11
        ball.vx = (dx / dist) * power + player.vx * 0.5
        ball.vy = -abs((dy / dist) * power) - 6


# Кольца: (x, y_top, y_bottom_of_rim)
HOOP_W = 60
HOOP_H = 10
left_hoop = pygame.Rect(20, 140, HOOP_W, HOOP_H)
right_hoop = pygame.Rect(W - 20 - HOOP_W, 140, HOOP_W, HOOP_H)

score_left = 0
score_right = 0

p1 = Player(150, (220, 60, 60), pygame.K_a, pygame.K_d, pygame.K_w, -1)
p2 = Player(W - 190, (60, 120, 220), pygame.K_LEFT, pygame.K_RIGHT, pygame.K_UP, 1)
ball = Ball()

score_cooldown = 0

running = True
while running:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    keys = pygame.key.get_pressed()
    p1.update(keys)
    p2.update(keys)
    ball.update()

    hit_ball(p1, ball)
    hit_ball(p2, ball)

    if score_cooldown > 0:
        score_cooldown -= 1

    # Игрок слева забивает в правое кольцо, справа — в левое
    if score_cooldown == 0:
        if right_hoop.collidepoint(ball.x, ball.y) and ball.vy > 0:
            score_left += 1
            ball.reset()
            score_cooldown = 40
        elif left_hoop.collidepoint(ball.x, ball.y) and ball.vy > 0:
            score_right += 1
            ball.reset()
            score_cooldown = 40

    # Отрисовка
    screen.fill((30, 30, 40))
    pygame.draw.line(screen, (80, 80, 90), (0, FLOOR), (W, FLOOR), 4)
    pygame.draw.line(screen, (80, 80, 90), (W // 2, 0), (W // 2, H), 2)

    pygame.draw.rect(screen, (255, 255, 255), left_hoop, border_radius=4)
    pygame.draw.rect(screen, (255, 255, 255), right_hoop, border_radius=4)
    pygame.draw.line(screen, (200, 200, 200), (20, 140), (20, 300), 3)
    pygame.draw.line(screen, (200, 200, 200), (W - 20, 140), (W - 20, 300), 3)

    p1.draw()
    p2.draw()
    ball.draw()

    score_text = font.render(f"{score_left} : {score_right}", True, (255, 255, 255))
    screen.blit(score_text, (W // 2 - score_text.get_width() // 2, 20))

    pygame.display.flip()

pygame.quit()