Как сделать так чтобы обе платформы отталкивали шар правильно
Как сделать так чтобы шар отталкивался правильно без кривизны?
import random
import pygame
pygame.init()
wight = 800
height = 500
fps = 60
title = 'Pong Duo'
platform_wight_1 = 100
platform_height_1 = 15
platform_wight_2 = 100
platform_height_2 = 15
platform_speed = 10
# 1 - 760, 2 - 25
platform_rect_1 = pygame.rect.Rect(740, 215, platform_height_1, platform_wight_1)
platform_rect_2 = pygame.rect.Rect(45, 215, platform_height_1, platform_wight_1)
white = (255, 255, 255)
black = (0, 0, 0)
circle_radius = 15
circle_speed = 10
circle_first_collide = True
circle_x_speed = circle_speed
circle_y_speed = 0
circle_rect = pygame.rect.Rect(wight / 2 - circle_radius,
height / 2 - circle_radius,
circle_radius * 2,
circle_radius * 2)
win = pygame.display.set_mode([wight, height])
pygame.display.set_caption(title)
clock = pygame.time.Clock()
run = True
game_over = False
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
run = False
if event.key == pygame.K_TAB:
game_over = False
platform_rect_1.y = 215
platform_rect_2.y = 215
circle_rect.center = [wight / 2, height / 2]
score = 0
circle_x_speed = circle_speed
circle_y_speed = 0
circle_first_collide = False
if not game_over:
keys = pygame.key.get_pressed()
if keys[pygame.K_UP] and platform_rect_1.y >= 0:
platform_rect_1.y -= platform_speed
if keys[pygame.K_DOWN] and platform_rect_1.y <= 400:
platform_rect_1.y += platform_speed
if keys[pygame.K_w] and platform_rect_2.y >= 0:
platform_rect_2.y -= platform_speed
if keys[pygame.K_s] and platform_rect_2.y <= 400:
platform_rect_2.y += platform_speed
if platform_rect_1.colliderect(circle_rect) or platform_rect_2.colliderect(circle_rect):
if not circle_first_collide:
if random.randint(0, 1) == 0:
circle_x_speed = -circle_speed
else:
circle_x_speed = circle_speed
circle_first_collide = False
circle_y_speed = -circle_speed
pygame.draw.rect(win, white, platform_rect_2)
pygame.draw.rect(win, white, platform_rect_1)
circle_rect.x += circle_x_speed
circle_rect.y += circle_y_speed
if circle_rect.x >= 750 or circle_rect.x <= 0:
game_over = True
if circle_rect.bottom >= height:
circle_y_speed -= circle_speed
elif circle_rect.top <= 0:
circle_y_speed = circle_speed
elif circle_rect.left <= 0:
circle_x_speed = circle_speed
elif circle_rect.right >= wight:
circle_x_speed = -circle_speed
pygame.draw.circle(win, white, circle_rect.center, circle_radius)
clock.tick(fps)
pygame.display.flip()
win.fill(black)
pygame.quit()