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?