0

While making a pool game in pygame, I wanted to animate a rolling ball. Here's an example of what I have and what I want it to look like.

Here is my 'solution':

def update_sprite(self):
    sprite_size = np.repeat([self.radius*2],2)

    new_sprite = pygame.Surface(sprite_size)
    new_sprite.fill((200, 200, 200))
    new_sprite.set_colorkey((200, 200, 200))

    #this is the actual ball part of the circle
    pygame.draw.circle(new_sprite, self.color, sprite_size/2, self.radius)

    if self.sprite_visible:
        #sprite offset is an np array, with two coordinates,
        #which contains the diplacement of the small circle from centre of the circle
        pygame.draw.circle(new_sprite, (255, 255, 255), self.sprite_offset.astype(int)+[self.radius, self.radius], self.sprite_size)
        if self.number!=0:
            new_sprite.blit(self.text, (self.radius - self.text_length / 2)+self.sprite_offset.astype(int))
            # used to remove part of the sprite which is outside the ball
            triag1 = np.array(([0, 0], [self.radius / 2, 0], [0, self.radius / 2]))
            triag2 = np.array(([0, 2*self.radius], [self.radius / 2, 2*self.radius], [0, 3*self.radius / 2]))
            triag3 = np.array(([2*self.radius, 0], [2*self.radius, self.radius / 2], [3*self.radius / 2,0]))
            triag4 = np.array(([2*self.radius, 2*self.radius], [3*self.radius / 2, 2*self.radius], [2*self.radius, 3*self.radius / 2]))
            pygame.draw.polygon(new_sprite,(200,200,200),triag1)
            pygame.draw.polygon(new_sprite, (200, 200, 200), triag2)
            pygame.draw.polygon(new_sprite, (200, 200, 200), triag3)
            pygame.draw.polygon(new_sprite, (200, 200, 200), triag4)


    pygame.draw.circle(new_sprite, (200,200,200), sprite_size/2, self.radius+6,6)
    self.image = new_sprite
    self.rect = self.image.get_rect()
    self.top_left = self.pos - self.radius

The line

pygame.draw.circle(new_sprite, (255, 255, 255), self.sprite_offset.astype(int)+[self.radius, self.radius], self.sprite_size)

Draws the small circle, and the second line after it draws the number, however, if I would leave it like that this would happen. Later I drew a bigger circle over the ball, without the middle part. However, that leaves small lines around the circle (you can see it here). Which is why I needed to draw 4 triangles around the circle to delete them.

Now, please tell me, is there any other way of doing this?

Link to source code

Max
  • 36
  • 7
  • 2
    Ah the joys of trying to do 3d in 2d. – cmd Mar 17 '17 at 22:30
  • 1
    A simple 2D approach would be to use a [sprite sheet](https://en.wikipedia.org/wiki/Texture_atlas). Pre-render the balls in different orientations and use the ones most suitable. Might require a decent amount of sprites, but the balls would be high-quality and cheap to draw. – Meyer Mar 17 '17 at 23:11
  • @Meyer That would work, however, my problem with this that pygame should already have an easy solution to this, and pre-rendering sprites would just move the problem one layer deeper, in my opinion. I now realize this is more of a pygame problem, and not related to animation. So, adding "animation" tag was probably a bad idea. – Max Mar 17 '17 at 23:40

0 Answers0