gpt4 book ai didi

python - 如何将绘制的圆圈与pygame混合

转载 作者:太空狗 更新时间:2023-10-30 02:06:47 28 4
gpt4 key购买 nike

我正在尝试创建一个像这里这样的应用程序:

http://www.eigenfaces.com/

基本上是用 pygame 绘制的很多重叠圆圈。我不知道如何混合圆圈使它们半透明。那就是让重叠的颜色透出来。到目前为止我的代码是这样的:

import sys, random, time
import pygame
from pygame.locals import *
from pygame import draw

rand = random.randint

pygame.init( )

W = 320
H = 320
size = (W, H)

screen = pygame.display.set_mode(size)

run = True
while 1:

for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE :
run = not run
else:
sys.exit()
if run:
xc = rand(1, W)
yc = rand(1, H)
rc = rand(1, 25)

red = rand(1, 255)
grn = rand(1, 255)
blu = rand(1, 255)

draw.circle(screen, (red, grn, blu, 200), (xc, yc), rc, 0)

pygame.display.flip()

最佳答案

我通过绘制到一个不是显示器的表面并组合设置颜色键和设置 alpha 函数来让它工作。

import pygame
from pygame.locals import *

TRANSPARENT = (255,0,255)
pygame.init()
screen = pygame.display.set_mode((500,500))

surf1 = pygame.Surface((200,200))
surf1.fill(TRANSPARENT)
surf1.set_colorkey(TRANSPARENT)
pygame.draw.circle(surf1, (0,0,200,100),(100,100), 100)

surf2 = pygame.Surface((200,200))
surf2.fill(TRANSPARENT)
surf2.set_colorkey(TRANSPARENT)
pygame.draw.circle(surf2, (200,0,0,100),(100,100), 100)

surf1.set_alpha(100)
surf2.set_alpha(100)

while True:
screen.fill((255,255,255))

for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()

screen.blit(surf1, (100,100,100,100))
screen.blit(surf2, (200,200,100,100))
pygame.display.flip()

附言您还可以将混合标志放入 blit() 参数中: Pygame.org - Surface.blit

关于python - 如何将绘制的圆圈与pygame混合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/353297/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com