PyGame:更改精灵表面的起始位置,使其不会从 (0,0) 开始

问题描述 投票:0回答:1

有没有办法改变精灵表面的起始位置?

例如,下面的代码适用于球精灵。椭圆在 (50,50) 处绘制,但添加绘图的精灵表面从 (0,0) 开始,这意味着仅显示了球的一部分。

我需要球精灵表面从左上角开始。这可以做到吗?如果是的话,怎么办?

我的代码:

class Ball(pygame.sprite.Sprite):
    """
    This class represents the ball.
    """
    def __init__(self, width, height):
        super().__init__()

        self.image = pygame.Surface([width, height])
        self.image.fill(WHITE)
        self.image.set_colorkey(WHITE)

        pygame.draw.ellipse(self.image, (255,0,0), [50,50,width,height], 10)

        self.rect = self.image.get_rect()
python pygame
1个回答
0
投票

树懒的评论引起了共鸣。 我只需要重写精灵图像的直角坐标即可使表面在不同的位置生成。因此,椭圆坐标返回到 (0,0),以便在精灵表面上正确绘制,并且精灵矩形坐标被覆盖为 (50,50):

pygame.draw.ellipse(self.image, (255,0,0), [0,0,width,height], 10)

# Fetch the rectangle object that has the dimensions of the image.
# Update the position of this object by setting the values of rect.x and rect.y

self.rect = self.image.get_rect()
self.rect.y = 50
self.rect.x = 50
© www.soinside.com 2019 - 2024. All rights reserved.