繁体   English   中英

为什么 pygame.Surface.blit 不显示任何内容?

[英]Why is pygame.Surface.blit not displaying anything?

我有一个代码来表示 python 中的碰撞测试,使用 pygame 表面来表示它们的样子:

import abc
import pygame
class Geometry(abc.ABC):
    def __init__(self, centre: tuple[int,int]):
        self.centre=centre
    @abc.abstractmethod
    def Draw(self, colour: tuple[int,int,int,int]) -> pygame.Surface:
        pass
class Line(Geometry):
    def __init__(self, start: tuple[int,int], end: tuple[int,int]):
        self.__start = start
        self.__end = end
        super().__init__(((start[0]+end[0])/2,(start[1]+end[1])/2))
    def Draw(self, colour: tuple[int,int,int,int]) -> pygame.Surface:
        sur = pygame.Surface((abs(end[0]-start[0]) or 3, abs(end[1]-start[1]) or 3), pygame.SRCALPHA)
        sur.fill(colour)
        return sur


pygame.init()
my_shape = Line((300, 300), (500, 300))
pos = tuple(my_shape.centre)
display = pygame.display.set_mode((600, 600), pygame.SRCALPHA)
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()

    display.fill((0, 0, 0))
    display.blit(my_shape.Draw((0, 0, 255, 0)), pos)
    pygame.display.flip()

但是,在运行此代码时,我得到一个空白的 pygame window。

当我填充display时,它可以工作。 但是将绘制的形状blitting到显示器上不会。

这段代码有什么问题吗?

颜色的 alpha 通道为 0,因此pygame.Surface是完全透明的。 [ blit (https://www.pygame.org/docs/ref/surface.html#pygame.Surface.blit) 将源Surface与目标Surface混合。 更改 Alpha 通道:

display.blit(my_shape.Draw((0, 0, 255, 0)), pos)

display.blit(my_shape.Draw((0, 0, 255, 255)), pos)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM