get_circle()
does not exist. See pygame.Surface
. get_rect()
returns a rectangle with the width and height of the Surface. The circle is just a buch of pixels on the surface, there is no "circle" object. pygame.draw.circle()
paints some pixels on a Surface, which are arranged to a circular shape.
You have to center the circle to Surface object self.image
. The size of the Surface is (width, height
), thus the center is (width // 2, height // 2)
:
self.image = pygame.Surface([width, height])
self.image.fill(WHITE)
self.image.set_colorkey(WHITE)
pygame.draw.circle(self.image, self.color, (width // 2, height // 2), 5)
self.rect = self.image.get_rect()
Note, since the radius of the circle is 5, it makes no sense to create a surface with a size of 60x80. Further more, I recommend to pass the x
and y
coordinate and the radius
to player
:
class Player(pygame.sprite.Sprite):
def __init__(self, color, x, y, radius, speed):
# Call the parent class (Sprite) constructor
super().__init__()
# Pass in the color of the player, and its x and y position, width and height.
# Set the background color and set it to be transparent
self.image = pygame.Surface((radius*2, radius*2))
self.image.fill(WHITE)
self.image.set_colorkey(WHITE)
#Initialise attributes of the car.
self.color = color
self.speed = speed
# Draw the player
pygame.draw.circle(self.image, self.color, (radius, radius), radius)
self.rect = self.image.get_rect(center = (x, y))
all_sprites_list = pygame.sprite.Group()
player = Player(BLUE, 200, 300, 5, 70)
all_sprites.add(player)
Do not use the same name for the class and the instance of the class, because the variable name covers the class name. While Class Names should normally use the CapWords convention, Variable Names should be lowercase.
So the name of the class is Player
and the name of the variable (instance) is player
.