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

查看:46
本文介绍了如何使圆形精灵出现 - 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().我的精灵类(正确导入):

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() 因为我的精灵是圆形的,但这返回:

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 的曲面是没有意义的.此外,我建议将 xy 坐标和 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)

<小时>

不要对类和类的实例使用相同的名称,因为变量名覆盖了类名.虽然 Class Names 通常应该使用 CapWords 约定,变量名称 应为小写.
所以类的名字是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天全站免登陆