Пинг понг : мяч ударяется об верх или низ ракетки и в итоге застревает в ней

 pygame.init()
W = 1000
H = 600
screen = pygame.display.set_mode((W, H))
pygame.display.set_caption("ПИНГ ПОНГ")
sound1 = pygame.mixer.Sound('mp3/pong.mp3')
bg_color = (64, 110, 59)
screen.fill(bg_color)
pygame.display.flip()
clock = pygame.time.Clock()

FPS = 75
bot_speed = 7
x = W // 4
y = H // 2
x2 = W * 3 // 4
y2 = H // 2
rect_width = 10
rect_height = 100
rect3_width = 5
rect3_height = 10000
ball_x = W // 2
ball_y = H // 2
ball_radius = 10
ball_speed_x = 6
ball_speed_y = 6
max_speed_x = 15
max_speed_y = 15
left_count = 0
right_count = 0
speed_increase = 1
f1 = pygame.font.Font('pics/Roboto-Black.ttf', 36)
f2 = pygame.font.Font('pics/Roboto-Black.ttf', 36)
text1 = f1.render(str(left_count),0,'WHITE')
text2 = f2.render(str(right_count), 0, 'WHITE')
quittxt = pygame.font.Font('pics/Roboto-Black.ttf', 30)
Q = quittxt.render('Press Q to quit', 0 ,'White')
time.sleep(1)
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()
    rect1 = pygame.Rect(x, y, rect_width, rect_height)
    rect2 = pygame.Rect(x2, y2, rect_width, rect_height)
    rect3 = pygame.Rect(W // 2,H // 100,rect3_width,rect3_height)
    # обработка столкновений мяча с стенами поля
    if ball_x - ball_radius <= 0: # добавляем текст 2 (количество забитых мячей)
        right_count+=1
        text2 = f2.render(str(right_count), True, 'White')
        ball_x = W // 2
        ball_y = H // 2
        ball_speed_x = 5
        ball_speed_y = 5
        x = W // 4
        y = H // 2
        x2 = W * 3 // 4
        y2 = H // 2
        ball_speed_x = -ball_speed_x
        time.sleep(1)
        #ИИ
    if ball_y < y2 + rect_height / 2:
        y2 -= bot_speed
    elif ball_y > y2 + rect_height / 2:
        y2 += bot_speed
    if ball_y < y + rect_height / 2:
        y -= bot_speed
    elif ball_y > y + rect_height / 2:
        y += bot_speed
    elif ball_x + ball_radius >= W:
        left_count+=1
        text1 = f1.render(str(left_count), True, 'White')
        ball_x = W // 2
        ball_y = H // 2
        ball_speed_x = 5
        ball_speed_y = 5
        x = W // 4
        y = H // 2
        x2 = W * 3 // 4
        y2 = H // 2
        ball_speed_x = -ball_speed_x
        time.sleep(1)
    if ball_y - ball_radius <= 0 or ball_y + ball_radius >= H:
        ball_speed_y = -ball_speed_y
    keys = pygame.key.get_pressed()

    if keys[pygame.K_q]:
        start_menu()
    # обработка столкновений мяча с платформами
    if rect1.colliderect(ball_x - ball_radius, ball_y - ball_radius, ball_radius * 2,
                         ball_radius * 2) or rect2.colliderect(ball_x - ball_radius, ball_y - ball_radius,
                                                               ball_radius * 2, ball_radius * 2):
        ball_speed_x = -ball_speed_x
        ball_speed_x += speed_increase if ball_speed_x < 20 else -speed_increase
        ball_speed_y += speed_increase if ball_speed_y < 20 else -speed_increase
        sound1.play()
    ball_x += ball_speed_x
    ball_y += ball_speed_y

    screen.fill((64, 110, 59))
    pygame.draw.rect(screen, 'WHITE', rect1)
    pygame.draw.rect(screen, 'WHITE', rect2)
    pygame.draw.rect(screen, (124, 130, 124), rect3)
    pygame.draw.circle(screen, 'White', (ball_x, ball_y), ball_radius)
    screen.blit(text1, (0, 0))
    screen.blit(text2, (500, 0))
    screen.blit(Q,(0,50))
    pygame.display.flip()
    clock.tick(FPS)

Как сделать правильную обработку столкновений мяча с платформами?


Ответы (0 шт):