Reputation: 93
I am trying to add a circle at my players center position. However i can only see edge of the circle. Heres my code.
class Cavit(pg.sprite.Sprite):
def __init__(self):
pg.sprite.Sprite.__init__(self)
self.image = pg.Surface((50,50))
self.rad = 100
self.image.fill((255,0,0))
self.rect = self.image.get_rect()
self.rect.center = 100,100
pg.draw.circle(self.image,(0,0,0),self.rect.center,self.rad,5)
self.pos = vec(100,100)
self.vel = vec(0,0)
self.acc = vec(0,0)
self.friction = 0.1
self.playeracc = 0.8
And ı am sharing the picture so you can see the weirdness What i expect is. i want a circle with 100 radius that surrounds the rectangle .
Upvotes: 1
Views: 53
Reputation: 211278
The radius of the circle is half the size of the image. You need to find the center of the image rectangle before you set the center of the rectangle in the world:
class Cavit(pg.sprite.Sprite):
def __init__(self):
pg.sprite.Sprite.__init__(self)
self.image = pg.Surface((50,50))
self.image.fill((255,0,0))
self.rect = self.image.get_rect()
self.rad = min(self.rect.width, self.rect.height) // 2
pg.draw.circle(self.image, (0,0,0), self.rect.center, self.rad, 5)
self.rect.center = 100, 100
# [...]
Notice that you are drawing the circle on the image, not on the screen. Therefore, the center of the circle is the center of the image, but not the center of the image on the screen.
If the circle needs to be larger than the rectangle, the image needs to be the size of the circle:
class Cavit(pg.sprite.Sprite):
def __init__(self):
pg.sprite.Sprite.__init__(self)
self.rad = 100
self.image = pg.Surface((self.rad*2, self.rad*2), pygame.SRCALPHA)
self.rect = self.image.get_rect()
inner_rect = pygame.Rect(0, 0, 50, 50)
inner_rect.center = self.rect.center
pygame.draw.rect(self.image, (255, 0, 0), inner_rect)
pg.draw.circle(self.image, (0,0,0), self.rect.center, self.rad, 5)
self.rect.center = 100, 100
# [...]
Upvotes: 1