Pygame绘制抗质形状,然后闪闪发光到主窗口

发布于 2025-02-02 23:46:31 字数 1669 浏览 2 评论 0原文

我想绘制抗脉络化的形状。我知道您可以使用Pygame的gfxDraw模块来实现这一目标。但是,它似乎仅在直接在主窗口上绘制时才起作用,这不适合我,因为我打算使用basks进行碰撞检查。 因此,需要不同的表面来创建代表圆圈的掩码。

您如何在Pygame中实现这一目标?

最小工作示例:

import pygame as pg
from pygame import gfxdraw

WIDHT, HEIGHT = 1200, 800
WIN = pg.display.set_mode((WIDHT, HEIGHT))

RADIUS = 80
WHITE = (255, 255, 255)
GREEN = (0, 200, 0)
RED = (200, 0, 0)
TRANS = (1, 1, 1)

class Circle(pg.sprite.Sprite):
    def __init__(self,
                 radius: int,
                 pos: tuple[int, int],
                 color: tuple[int, int, int]):

        super().__init__()
        self.radius = radius
        self.color = color

        self.image = pg.surface.Surface((radius*2, radius*2))
        self.image.fill(TRANS)
        self.image.set_colorkey(TRANS)

        self.rect = self.image.get_rect(center=(pos[0], pos[1]))
        pg.gfxdraw.aacircle(self.image, self.rect.width//2, self.rect.height//2, radius, color)
        pg.gfxdraw.filled_circle(self.image, self.rect.width//2, self.rect.height//2, radius, color)
        self.mask = pg.mask.from_surface(self.image)

    def draw(self):
        WIN.blit(self.image, self.rect)


def main():
    circle_1 = Circle(RADIUS, (500, 500), GREEN)

    running = True
    while running:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                running = False

        WIN.fill(WHITE)

        circle_1.draw()
        pg.gfxdraw.filled_circle(WIN, 700, 500, RADIUS, RED)
        pg.gfxdraw.aacircle(WIN, 700, 500, RADIUS, RED)

        pg.display.update()


if __name__ == "__main__":
    main()

I want to draw antialiased shapes. I know you can use the pygame's gfxdraw module to accomplish this. However, it does seem to work only when drawing directly on the main window which is not suitable for me because I intend to use masks for collision checking.
Therefore a different surface is needed to create a mask that represents the circle.

How can you achieve this in pygame?

Minimal working example:

import pygame as pg
from pygame import gfxdraw

WIDHT, HEIGHT = 1200, 800
WIN = pg.display.set_mode((WIDHT, HEIGHT))

RADIUS = 80
WHITE = (255, 255, 255)
GREEN = (0, 200, 0)
RED = (200, 0, 0)
TRANS = (1, 1, 1)

class Circle(pg.sprite.Sprite):
    def __init__(self,
                 radius: int,
                 pos: tuple[int, int],
                 color: tuple[int, int, int]):

        super().__init__()
        self.radius = radius
        self.color = color

        self.image = pg.surface.Surface((radius*2, radius*2))
        self.image.fill(TRANS)
        self.image.set_colorkey(TRANS)

        self.rect = self.image.get_rect(center=(pos[0], pos[1]))
        pg.gfxdraw.aacircle(self.image, self.rect.width//2, self.rect.height//2, radius, color)
        pg.gfxdraw.filled_circle(self.image, self.rect.width//2, self.rect.height//2, radius, color)
        self.mask = pg.mask.from_surface(self.image)

    def draw(self):
        WIN.blit(self.image, self.rect)


def main():
    circle_1 = Circle(RADIUS, (500, 500), GREEN)

    running = True
    while running:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                running = False

        WIN.fill(WHITE)

        circle_1.draw()
        pg.gfxdraw.filled_circle(WIN, 700, 500, RADIUS, RED)
        pg.gfxdraw.aacircle(WIN, 700, 500, RADIUS, RED)

        pg.display.update()


if __name__ == "__main__":
    main()

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

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

发布评论

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

评论(1

一紙繁鸢 2025-02-09 23:46:31

您需要创建透明 pygame.surface 具有每个像素α格式。使用srcalpha flag:

))

self.image = pg.surface.Surface((radius*2, radius*2), pg.SRCALPHA)

self.image = pg.surface.surface((RADIUS*2,RADIUS* 2 我建议使用opencv/ cv2 (例如/stackoverflow.com/questions/67168804/how-to-to-make-a-circular-countdown-timer-in-imer-in-pygame/67168896#67168896"> y>如何在pygame中做一个圆形的倒数计时器?)

You need to create a transparent pygame.Surface with the per pixel alpha format. Use the SRCALPHA flag:

self.image = pg.surface.Surface((radius*2, radius*2))

self.image = pg.surface.Surface((radius*2, radius*2), pg.SRCALPHA)

However, for the highest quality, I suggest using OpenCV/cv2 (e.g. How to make a circular countdown timer in Pygame?)

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