本文介绍了python-在pygame中画一条透明线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I made a little game in which i need a background pattern with lines. Because of a better performance I would like to draw the pattern in python instead of taking an image.

问题是我找不到一种可以透明绘制线条的方法...有表面的解决方案,但没有线条.

The problem is that I can't find a way to draw the lines with transparency...there are solutions for surfaces, but not for lines.

这是模式代码:

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()

有人可以解决吗?预先感谢!

has any one a solution?thanks in advance!

推荐答案

我花了一些力气扑向头,但最终还是得到了.Pygame.draw不会处理透明度,因此您必须制作单独的表面,这将:

It took me a small amount of bashing my head, but I eventually got it. Pygame.draw will not deal with transparency, so therefore you have to make separate surfaces which will:

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()

我希望这就是您想要的.

I hope that this was what you were looking for.

这篇关于python-在pygame中画一条透明线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 13:52
查看更多