I need to create a background of square sprites generated with a matrix of letters and have a ball that collides perfectly with these sprites regardless of its angle and speed. As you can see in my code the collision and bounce bugs are numerous, I cannot find the solution, can you help me, thank you
import pygame
from math import *
import sys
pygame.init()
clock = pygame.time.Clock()
display = pygame.display.set_mode((900, 900))
white = (255, 255, 255)
black = (30, 30, 30)
arial_font = pygame.font.SysFont("arial Black", 36)
TILE_SIZE = 100
level = [
"EEEEEEEE",
"E E",
"E E E E",
"E E",
"E X E",
"E E E E",
"E E",
"EEEEEEEE", ]
walls_group_sprites = pygame.sprite.Group()
spawn_ball = []
def create_level():
x = y = TILE_SIZE
for row in level:
for column in row:
if column == "E":
wall = Sprite("wall.png", x, y)
walls_group_sprites.add(wall)
if column == "X":
spawn_ball.append(x)
spawn_ball.append(y)
x += TILE_SIZE
y += TILE_SIZE
x = TILE_SIZE
class Sprite(pygame.sprite.Sprite):
def __init__(self, image_path, pos_x, pos_y):
super().__init__()
self.image = pygame.image.load(image_path)
self.rect = self.image.get_rect()
self.rect.center = [pos_x, pos_y]
class Ball:
def __init__(self, x, y, speed, angle):
self.x = x
self.y = y
self.color = white
self.angle = angle
self.speed = speed
self.radius = 25
def move(self):
# move Ball with math import cosinus/sinus
self.x += self.speed * cos(radians(self.angle))
self.y += self.speed * sin(radians(self.angle))
# Draw ball with pygame
self.circle = pygame.draw.circle(display, self.color, (self.x, self.y), self.radius)
def collide(self):
# My wall Sprite is a image (100X100 pixels) with rect, there are 100 sprites in the pygame group sprite
for wall_index in walls_group_sprites:
if self.y + self.radius > wall_index.rect.top and self.y - self.radius < wall_index.rect.bottom and self.x + self.radius > wall_index.rect.left and self.x - self.radius < wall_index.rect.right:
# if the ball.y is in the height of the sprite then the rebound is vertical
if wall_index.rect.top < self.y < wall_index.rect.bottom:
self.angle = 180 - int(self.angle)
# if the ball.x is in the width of the sprite then the rebound is horizontal
elif wall_index.rect.left < self.x < wall_index.rect.right:
self.angle = 360 - int(self.angle)
else:
#if the ball is in one of the two lower left or upper right corners then the rebound and sideways
if (self.y > wall_index.rect.bottom and self.x < wall_index.rect.left) or (self.y < wall_index.rect.top and self.x > wall_index.rect.right):
self.angle = 90 - int(self.angle)
#if the ball is in one of the two corners at the bottom right or the top left then the rebound and on the other side
elif (self.y > wall_index.rect.bottom and self.x > wall_index.rect.right) or (self.y < wall_index.rect.top and self.x < wall_index.rect.left):
self.angle = 270 - int(self.angle)
create_level()
ball = Ball(spawn_ball[0], spawn_ball[1], 7, 40)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN: ball.angle += 10
display.fill(black)
walls_group_sprites.draw(display)
ball.move()
ball.collide()
text = arial_font.render(f"Click to change Angle {abs(ball.angle)}", True, white)
display.blit(text, [200, 0])
clock.tick(60)
pygame.display.update()