Pygame sprites not rendering

Description:
I’m making a simple physics simulator in python, using pygame for rendering and such. However I can’t seem to be able to render more than 1 ball at a time. I’ve tried google for a few hours, and also using things like self.rect and trying to draw outside the Ball class (in the main loop). But nothing seems to work.

Problem:
Only one ball gets rendered at a time, but there are 5 balls calculating physics. The physics seem fine when printing positions of every ball, and they aren’t overlapping due to the random start positions and velocities.

Repl link:
https://replit.com/@ByteDice/Ball-Pit?v=1#main.py
main.py and ball.py are the important files
(maybe game.py too but I don’t think so)

Possible places for problems:

- main.py - line 46-51

  # render all balls (problem most likely here)
  for ball_class in game.Game.all_balls:
    game.Game.display.blit(
      ball_class.surface,
      (ball_class.Bounds.x, ball_class.Bounds.y)
    )

- ball.py - line 32-38

  # draw ball
  image = pygame.draw.circle(
    surface,
    color,
    (Bounds.r, Bounds.r),
    Bounds.r
  )

SOLVED

Solution:
wrap all variables of the ball class inside a super().__init__() like this:

class Ball(pygame.sprite.Sprite):
  def __init__(self): # __init__
    super().__init__() # then super().__init__()
    self.Bounds = Bounds()
    self.Vel = Vel()
    
    # the balls fill color
    self.color = tuple(random.randint(100, 255) for _ in range(3))

    # make surface
    self.surface = pygame.Surface((self.Bounds.d, self.Bounds.d))
    self.surface.set_colorkey((0, 0, 0)) # transparency

Why does this work:
I do not know the full aspect, but pygame seems to require super classes for the pygame.sprite.Sprite class

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.