如何使圆形精灵出现-pygame [英] How to make a circular sprite appear - pygame

查看:66
本文介绍了如何使圆形精灵出现-pygame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要我的精灵出现在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

在代码末尾,我有 pygame.display.update().我的Sprite类(正确导入):

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()

可能是一个愚蠢的人为错误.我尝试将 self.rect = self.image.get_rect()替换为 self.rect = self.image.get_circle(),因为我的精灵是圆形的,但这返回了:/p>

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'

请给我一些帮助吗?

推荐答案

get_circle()不存在.请参见 pygame.Surface . get_rect() 返回具有Surface的宽度和高度的矩形.圆形只是表面上的大量像素,没有圆形"对象. pygame.draw.circle()在Surface上绘制一些像素,这些像素排列成圆形.

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.

您必须将圆心定位到Surface对象 self.image .Surface的大小为(width,height ),因此中心为(width//2,height//2):

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()


请注意,由于圆的半径为5,因此创建尺寸为60x80的曲面没有任何意义.另外,我建议将 x y 坐标以及 radius 坐标传递给 player :


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)


对于类和类的实例,请不要使用相同的名称,因为变量名会覆盖类的名称.尽管类名通常应使用CapWords约定,href ="https://www.python.org/dev/peps/pep-0008/#function-and-variable-names" rel ="nofollow noreferrer">变量名称应该是小写.
因此,类的名称为 Player ,变量(实例)的名称为 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.

这篇关于如何使圆形精灵出现-pygame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆