[英]Pygame, set transparency on an image imported using convert_alpha()
[英]How to set alpha transparency property using pygame.draw.line?
我正在嘗試使用pygame.draw.line(tela, (255, 255, 255), self.a, self.b, width=1)
設置繪制線的透明度,其中:
tela
是屏幕(255, 255, 255)
是顏色self.a
和self.b
分別是位置和方向width
是它的重量有沒有直接簡單的方法來做到這一點? 是否有一個概念上的論點阻止我們輕易地做到這一點? 一切都必須有一個復雜的答案嗎?
以下是@Yannis Assael 對他自己的問題Pygame 繪制抗鋸齒粗線的回答的改編,我向您介紹了該問題。 我已經轉換為一個名為aaline()
的函數並簡化了代碼以嘗試使其更高效和可讀。
該函數已被定義為具有類似於現有的名為pygame.draw.aaline()
的調用序列,用於繪制單像素寬的線條,使從 on 切換到另一個更容易一些。
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()
出於測試和演示目的,末尾有一個驅動程序,可以在某些循環中調用它。 下面是顯示內容的屏幕截圖。 隨着它們的端點之一越來越遠離矩形的中心對角線,這些線逐漸變得更加透明。
pygame 中有一個抗鋸齒線函數。 只需使用pygame.draw.aaline
。
例如:
pygame.draw.aaline(tela, (255, 255, 255), self.a, self.b, width=1)
而已!
(這是簡單的答案)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.