简体   繁体   English

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

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

I have sone code to represent collision tests within python, using pygame Surfaces to represent what they look like:我有一个代码来表示 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()

However when runnning this code, I get a blank pygame window.但是,在运行此代码时,我得到一个空白的 pygame window。

When I fill display , it works.当我填充display时,它可以工作。 But blitting the drawn shape onto the display does not.但是将绘制的形状blitting到显示器上不会。

Any problems with this code?这段代码有什么问题吗?

The alpha channel of the color is 0, so the pygame.Surface is completely transparent.颜色的 alpha 通道为 0,因此pygame.Surface是完全透明的。 [ blit (https://www.pygame.org/docs/ref/surface.html#pygame.Surface.blit) blends the source Surface with the target Surface . [ blit (https://www.pygame.org/docs/ref/surface.html#pygame.Surface.blit) 将源Surface与目标Surface混合。 Change the alpha channel:更改 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