python - Draw a transparent Line in pygame

南笙酒味 提交于 2021-01-03 06:41:49

问题


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.

here is the pattern code:

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!


回答1:


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.



来源:https://stackoverflow.com/questions/18701453/python-draw-a-transparent-line-in-pygame

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!