如何在pygame中绘制透明线

问题描述 投票:0回答:4

我做了一个小游戏,我需要一个带有线条的背景图案。由于性能更好,我想用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()

有没有人有解决办法?

python line pygame opacity transparent
4个回答
1
投票

我花了一番心思,但最终还是明白了。 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()

我希望这就是您正在寻找的。


1
投票

只需使用 alpha 颜色,例如:

# Create a temporary surface that supports alpha values
surface = pygame.Surface(screen.get_size(), pygame.SRCALPHA)

color  = (255, 255, 255, 128) # or '#ffffffdd'

# Draw the line on the temporary surface
pygame.draw.line(surf, color,start_pos, end_pos, width)

# Draw the surface on the screen
screen.blit(surface, (0,0))

0
投票

这是我发现更有用的答案: (复制自http://ostack.cn/?qa=761042/;解释在那里。)

from math import atan2, cos, hypot, sin
import pygame
import pygame.gfxdraw
from pygame.locals import *
import sys


def aaline(surface, color, start_pos, end_pos, width=1):
    """ Draws wide transparent anti-aliased lines. """
    # ref https://stackoverflow.com/a/30599392/355230

    x0, y0 = start_pos
    x1, y1 = end_pos
    midpnt_x, midpnt_y = (x0+x1)/2, (y0+y1)/2  # Center of line segment.
    length = hypot(x1-x0, y1-y0)
    angle = atan2(y0-y1, x0-x1)  # Slope of line.
    width2, length2 = width/2, length/2
    sin_ang, cos_ang = sin(angle), cos(angle)

    width2_sin_ang  = width2*sin_ang
    width2_cos_ang  = width2*cos_ang
    length2_sin_ang = length2*sin_ang
    length2_cos_ang = length2*cos_ang

    # Calculate box ends.
    ul = (midpnt_x + length2_cos_ang - width2_sin_ang,
          midpnt_y + width2_cos_ang  + length2_sin_ang)
    ur = (midpnt_x - length2_cos_ang - width2_sin_ang,
          midpnt_y + width2_cos_ang  - length2_sin_ang)
    bl = (midpnt_x + length2_cos_ang + width2_sin_ang,
          midpnt_y - width2_cos_ang  + length2_sin_ang)
    br = (midpnt_x - length2_cos_ang + width2_sin_ang,
          midpnt_y - width2_cos_ang  - length2_sin_ang)

    pygame.gfxdraw.aapolygon(surface, (ul, ur, br, bl), color)
    pygame.gfxdraw.filled_polygon(surface, (ul, ur, br, bl), color)


if __name__ == '__main__':

    # Define some colors.
    BLACK = (0, 0, 0)
    WHITE = (255, 255, 255)
    RED = (255, 0, 0)
    GREEN = (0, 255, 0)
    BLUE = (0, 0, 255)
    AQUA = (0, 255, 255)
    ORANGE = (255, 165, 0)
    YELLOW = (255, 255, 0)

    # Window size.
    WIDTH, HEIGHT = 800, 600

    # Set up pygame.
    pygame.init()

    # Set up window for display.
    window = pygame.display.set_mode((WIDTH, HEIGHT), 0, 32)
    pygame.display.set_caption('Wide Transparent Lines')

    # Set background color of window.
    window.fill(BLACK)

    a = (0, 0)
    b = (WIDTH, HEIGHT)

    LINE_COLOR = ORANGE
    fw = 255 / (WIDTH-1)
    fh = 255 / (HEIGHT-1)
    width = 3

    # Draw an opaque diagonal line then those on either side with
    # ever-increasing transparency.
    color = LINE_COLOR + (255,)  # Add transparency to color.
    aaline(window, color, (0, 0), (WIDTH, HEIGHT), width)

    for x in range(0, WIDTH, 25):
        color = LINE_COLOR + (int(fw*x),)  # Add transparency to color.
        aaline(window, color, (0, 0), (x, HEIGHT), width)

    for y in range(0, HEIGHT, 25):
        color = LINE_COLOR + (int(fh*y),)  # Add transparency to color.
        aaline(window, color, (0, 0), (WIDTH, y), width)

    # Copy window to screen.
    pygame.display.update()

    # Run the game loop.
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

0
投票

只需将宽度设置为0

pygame.draw.line(surface, color, start_pos, end_pos, width=0)
© www.soinside.com 2019 - 2024. All rights reserved.