【问题标题】:How to set alpha transparency property using pygame.draw.line?如何使用 pygame.draw.line 设置 alpha 透明度属性?
【发布时间】:2021-07-25 21:42:02
【问题描述】:

我正在尝试使用pygame.draw.line(tela, (255, 255, 255), self.a, self.b, width=1) 设置绘制线的透明度,其中:

  • tela是屏幕
  • (255, 255, 255) 是颜色
  • self.aself.b分别是位置和方向
  • width 是它的权重

有没有直接简单的方法来做到这一点?是否有一个概念性的论据阻止我们轻易做到这一点?一切都必须有一个复杂的答案吗?

【问题讨论】:

  • 如果您询问如何绘制抗锯齿线,请参阅Pygame draw anti-aliased thick line
  • 感谢您与我们联系。我浏览了上述文件的每一行,但仍然找不到答案。你能在答案上更具体吗?也许举个例子?再次感谢马蒂诺。

标签: python pygame


【解决方案1】:

以下是@Yannis Assael 的answer 对我向您推荐的他自己的问题Pygame draw anti-aliased thick line 的改编。我已经转换成一个名为aaline() 的函数,并简化了代码以使其更高效和可读。

该函数已被定义为具有类似于名为pygame.draw.aaline() 的现有函数的调用序列,用于绘制单像素宽的线条,这使得从另一个切换到另一个更容易一些。

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

出于测试和演示目的,最后有一个驱动程序在某些循环中调用它。下面是显示内容的屏幕截图。随着线条的端点之一越来越远离矩形的中心对角线,线条变得越来越透明。

【讨论】:

  • 太棒了。非常感谢您的宝贵时间!
猜你喜欢
  • 2011-07-02
  • 2013-04-18
  • 1970-01-01
  • 2010-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-09
  • 2011-02-19
相关资源
最近更新 更多