4

I am trying to create a pygame environment with various shapes of sprites but my code seems not working. Here is what I have:

class Object(pygame.sprite.Sprite):

    def __init__(self, position, color, size, type):

        # create square sprite
        pygame.sprite.Sprite.__init__(self)
        if type == 'agent':
            self.image = pygame.Surface((size, size))
            self.image.fill(color)
            self.rect = self.image.get_rect()
        else:
            red = (200,0,0)
            self.image = pygame.display.set_mode((size, size))
            self.image.fill(color)
            self.rect = pygame.draw.circle(self.image, color,(), 20)


        # initial conditions
        self.start_x = position[0]
        self.start_y = position[1]
        self.state = np.asarray([self.start_x, self.start_y])
        self.rect.x = int((self.start_x * 500) + 100 - self.rect.size[0] / 2)
        self.rect.y = int((self.start_y * 500) + 100 - self.rect.size[1] / 2)

Does anyone notice any issues with the Object class?

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
jigz
  • 57
  • 4

1 Answers1

4

You have to create a pygame.Surface, instead of creating a new window (pygame.display.set_mode).
The pixel format of the Surface must include a per-pixel alpha (SRCALPHA). The center point of the circle must be the center of the Surface. The radius must be half the size of the Surface:

self.image = pygame.Surface((size, size), pygame.SRCALPHA)
radius = size // 2
pygame.draw.circle(self.image, color, (radius, radius), radius)

Class Object:

class Object(pygame.sprite.Sprite):

    def __init__(self, position, color, size, type):

        # create square sprite
        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.Surface((size, size), pygame.SRCALPHA)
        self.rect = self.image.get_rect()
        
        if type == 'agent':
            self.image.fill(color)
        else:
            radius = size // 2
            pygame.draw.circle(self.image, color, (radius, radius), radius)

        # initial conditions
        self.start_x = position[0]
        self.start_y = position[1]
        self.state = np.asarray([self.start_x, self.start_y])
        self.rect.x = int((self.start_x * 500) + 100 - self.rect.size[0] / 2)
        self.rect.y = int((self.start_y * 500) + 100 - self.rect.size[1] / 2)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174