Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
822 views
in Technique[技术] by (71.8m points)

python - How to make a circular sprite appear - pygame

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...