Pygame:根据构造函数参数绘制椭圆或矩形

发布于 2024-12-11 06:35:19 字数 2523 浏览 0 评论 0原文

我不知道这是否是正确的网站,但是你们之前非常有帮助,我想就我在使用 Python 和 Pygame 时遇到的问题寻求你们的建议。

我正在制作一个简单的游戏,最近才开始学习Python(到目前为止很喜欢它),目前我有一个正在使用的精灵构造函数。这个构造函数将管理我的对象,但我希望它根据传递给它的参数绘制椭圆形或矩形。

#My code
class Block(pygame.sprite.Sprite):
    #Variables!
    speed = 2
    indestructible = True
    #Constructor
    def __init__(self, color, width, height, name, shapeType):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface([width,height])
        self.image.fill(color)
        #Choose what to draw
        if shapeType == "Ellipse":
            pygame.draw.ellipse(self.image,color,[0,0,width,height])
        elif shapeType == "Rect":
            pygame.draw.rect(self.image,color,[0,0,width,height])
        elif shapeType == "":
            print("Shape type for ",name," not defined.")
            pygame.draw.rect(self.image,color,[0,0,width,height])
        #Init the Rect class for sprites
        self.rect = self.image.get_rect()

我用于绘制正方形的编码如下:

#Add 'white star' to the list
for i in range(random.randrange(100,200)):
    whiteStar = Block(white, 1, 1, "White Star", "Rect")
    whiteStar.rect.x = random.randrange(size[0])
    whiteStar.rect.y = random.randrange(size[1])
    whiteStar.speed = 2
    block_list.add(whiteStar)
    all_sprites_list.add(whiteStar)

这非常有效。它为我绘制了一个完美的白色小方块。但是这个不起作用:

#Create Planet
planet = Block(green, 15,15, "Planet", "Ellipse")
planet.rect.x = random.randrange(size[0])
planet.rect.y = 30
planet.speed = 1
block_list.add(planet)
all_sprites_list.add(planet)

“行星”正确生成,但它是一个正方形。为什么会发生这种情况?我该如何解决它?我应该使用位图来纠正这个问题吗?或者我的编码错误?

只是为了澄清,我知道一个事实 self.rect = self.image.get_rect() 确实可以绘制椭圆,因为下面的编码有效。

#Not the code I'm using, but this works and proves self.rect = self.image.get_rect() is not the cause
# Call the parent class (Sprite) constructor
    pygame.sprite.Sprite.__init__(self) 

    # Create an image of the block, and fill it with a color.
    # This could also be an image loaded from the disk.
    self.image = pygame.Surface([width, height])
    self.image.fill(white)
    self.image.set_colorkey(white)
    pygame.draw.ellipse(self.image,color,[0,0,width,height])

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

感谢您的帮助。 :-)

I don't know if this is the correct website, but you guys have been so helpful before, I wanted to get your advice on a problem I'm having with Python and Pygame.

I am making a simple game, and only recently begun learning Python (loving it so far) and at the moment, I having a sprite constructor which I am using. This constructor will manage my objects, but I want it to draw either an ellipse or a rectangle based on an argument passed to it.

#My code
class Block(pygame.sprite.Sprite):
    #Variables!
    speed = 2
    indestructible = True
    #Constructor
    def __init__(self, color, width, height, name, shapeType):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface([width,height])
        self.image.fill(color)
        #Choose what to draw
        if shapeType == "Ellipse":
            pygame.draw.ellipse(self.image,color,[0,0,width,height])
        elif shapeType == "Rect":
            pygame.draw.rect(self.image,color,[0,0,width,height])
        elif shapeType == "":
            print("Shape type for ",name," not defined.")
            pygame.draw.rect(self.image,color,[0,0,width,height])
        #Init the Rect class for sprites
        self.rect = self.image.get_rect()

The coding I am using for drawing a square is below:

#Add 'white star' to the list
for i in range(random.randrange(100,200)):
    whiteStar = Block(white, 1, 1, "White Star", "Rect")
    whiteStar.rect.x = random.randrange(size[0])
    whiteStar.rect.y = random.randrange(size[1])
    whiteStar.speed = 2
    block_list.add(whiteStar)
    all_sprites_list.add(whiteStar)

This works wonderfully. It draws a perfect little white square for me. But this doesn't work:

#Create Planet
planet = Block(green, 15,15, "Planet", "Ellipse")
planet.rect.x = random.randrange(size[0])
planet.rect.y = 30
planet.speed = 1
block_list.add(planet)
all_sprites_list.add(planet)

The 'planet' spawns correctly, but it does so as a square. Why is this happening? And how can I fix it? Should I use a bitmap to correct this? Or is my coding wrong?

Just to clarify, I know for a fact that self.rect = self.image.get_rect() does work to draw an ellipse, because the coding below works.

#Not the code I'm using, but this works and proves self.rect = self.image.get_rect() is not the cause
# Call the parent class (Sprite) constructor
    pygame.sprite.Sprite.__init__(self) 

    # Create an image of the block, and fill it with a color.
    # This could also be an image loaded from the disk.
    self.image = pygame.Surface([width, height])
    self.image.fill(white)
    self.image.set_colorkey(white)
    pygame.draw.ellipse(self.image,color,[0,0,width,height])

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

Thankyou for your help. :-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温柔嚣张 2024-12-18 06:35:19

在 Block 构造函数中,您调用 self.image.fill(color)。这将用该颜色填充精灵的整个图像,因此您会得到一个矩形。

您的示例代码在执行填充后调用 self.image.set_colorkey(white) ,以便在绘制时,背景填充是透明的。这可能是最快的解决方案。

In the Block constructor, you call self.image.fill(color). That will fill the sprite's entire image with that color, so you get a rectangle.

The example code you have calls self.image.set_colorkey(white) after doing the fill, so that when it gets drawn, the background fill is transparent. That's probably the fastest solution.

z祗昰~ 2024-12-18 06:35:19

您使用给定的颜色填充表面,然后用相同的颜色绘制形状。当然,这样它是不可见的,您只会得到纯色的矩形表面。

You are filling the surface with the given color, and then drawing your shape in the same color. Of course it won't be visible that way, and you just get the solid-coloured surface, which is rectangular.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文