问题
I need my sprite to appear in the pygame window. How do I do this? Important code:
#This will be a list that will contain all the sprites we intend to use in our game.
all_sprites_list = pygame.sprite.Group()
#creating the player
player = player(BLUE, 60, 80, 70)
player.rect.x = 200
player.rect.y = 300
At the end of the code I have pygame.display.update()
. My sprite class (correctly imported):
class player(pygame.sprite.Sprite):
def __init__(self, color, width, height, 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([width, height])
self.image.fill(WHITE)
self.image.set_colorkey(WHITE)
#Initialise attributes of the car.
self.width = width
self.height = height
self.color = color
self.speed = speed
# Draw the player
pygame.draw.circle(self.image, self.color, (400, 600), 5)
self.rect = self.image.get_rect()
Could be a stupid human mistake. I tried replacing self.rect = self.image.get_rect()
with self.rect = self.image.get_circle()
as my sprite is circular but this returns:
self.rect = self.image.get_circle()
AttributeError: 'pygame.Surface' object has no attribute 'get_circle'
Can I have some help please?
回答1:
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
.
来源:https://stackoverflow.com/questions/61445178/how-to-make-a-circular-sprite-appear-pygame