gpt4 book ai didi

python - 如何在pygame中绘制透明线

转载 作者:太空宇宙 更新时间:2023-11-04 06:11:10 24 4
gpt4 key购买 nike

我做了一个小游戏,我需要一个带线条的背景图案。由于性能更好,我想用 Python 绘制图案而不是拍摄图像。

问题是我找不到绘制透明线条的方法。有面的解决方案,但线没有。

这是模式代码:

import pygame
from math import pi

pygame.init()

size = [600, 600]
screen = pygame.display.set_mode(size)

while True:

for i in range(0, 600, 20):
pygame.draw.aaline(screen, (0, 255, 0), [i, 0],[i, 600], True)
pygame.draw.aaline(screen, (0, 255, 0), [0, i],[600, i], True)

pygame.display.flip()

pygame.quit()

有没有人有解决办法?

最佳答案

我花了一点点时间来敲打自己的脑袋,但我最终明白了。 Pygame.draw 不会处理透明度,因此您必须制作单独的表面:

import pygame from math import pi

pygame.init()

size = [600, 600] screen = pygame.display.set_mode(size)
while True:
screen.fill((0, 0, 0))
for i in range(0, 600, 20):
vertical_line = pygame.Surface((1, 600), pygame.SRCALPHA)
vertical_line.fill((0, 255, 0, 100)) # You can change the 100 depending on what transparency it is.
screen.blit(vertical_line, (i - 1, 0))
horizontal_line = pygame.Surface((600, 1), pygame.SRCALPHA)
horizontal_line.fill((0, 255, 0, 100)) # You can change the 100 depending on what transparency it is.
screen.blit(horizontal_line, (0, i - 1))

pygame.display.flip()

pygame.quit()

我希望这就是您要找的。

关于python - 如何在pygame中绘制透明线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18701453/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com