Баг в игре в шарики, возникающий на высокой скорости

Про баг:

  1. при высокой скорости движения шариков они не отскакивают друг от друга, а пролетают сквозь, как поправить?
  2. как правильно прописать, изменение цвета (на рандомный) после отскакивания мячика от границы поля?
import pygame
import math
import random

S_WIDHT = 600
S_HEIGHT = 600

class Ball:

    def __init__(self, x, y, r, dx, dy, color):
        self.x = x
        self.y = y
        self.r = r
        self.dx = dx # швидкість по осі х
        self.dy = dy # швидкість по осі у
        self.color = color

    def move(self):
        if self.x - self.r <= 0 or self.x + self.r >= S_WIDHT:
            self.dx = -self.dx

        if self.y - self.r<= 0 or self.y + self.r >= S_HEIGHT:
            self.dy = -self.dy

        self.x += self.dx
        self.y += self.dy

    def draw(self, sc):
        pygame.draw.circle (sc, self.color, (self.x, self.y), self.r)

    def check_collision(self, other_ball):
        l = math.sqrt((self.x - other_ball.x)**2 + (self.y - other_ball.y)**2) # перевірка зіткнення м'ячів через  корінь квадратний
        return l < self.r + other_ball.r  # якщо довжина (l) < суми радіусів == м'ячі перетнулися

    def punch(self, other_ball):
        self.dx, other_ball.dx = other_ball.dx, self.dx
        self.dy, other_ball.dy = other_ball.dy, self.dy



class Balls:
    def __init__(self, n):
        self.n = n
        self.balls = []

        for i in range(n):
            r = random.randint(13, 66)
            new_ball = Ball(
                x = random.randint(r, S_WIDHT-r),
                y = random.randint(r, S_WIDHT-r),
                r = r,
                dx=random.randint(1, 9)/10,
                dy = random.randint(1, 9)/10,
                color=(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
            )
            self.balls.append(new_ball)

    def draw(self, sc):
        for ball in self.balls:
            ball.draw(sc)
    def move(self):
        self.update_by_collision()

        for ball in self.balls:
            ball.move()

    def update_by_collision(self): # перевірка чи зіткнулися м'ячики
        for i in range(self.n):
            for j in range(i+1, self.n):
                ball1 = self.balls[i]
                ball2 = self.balls[j]
                if ball1.check_collision(ball2):
                    ball1.punch(ball2)
                    while ball1.check_collision(ball2):
                        ball1.move()
                        ball2.move()

def main():
    screen = pygame.display.set_mode((S_WIDHT, S_HEIGHT))
    pygame.display.set_caption("My first game")
    bg_color = (220, 220, 220)  # rgb
    balls = Balls(9) # кількість м'ячів

    is_ran = True

    while is_ran:
        screen.fill(bg_color)

        balls.draw(screen)
        balls.move()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                is_ran = False

        pygame.display.update()


if __name__ == "__main__":
    main()

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