如何在pygame中使圆圈半透明? [英] How to make a circle semi-transparent in pygame?

查看:98
本文介绍了如何在pygame中使圆圈半透明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 pygame 有点陌生,并试图弄清楚如何使圆半透明.然而,诀窍是圆圈的背景也必须是透明的.这是我正在谈论的代码:

大小 = 10表面 = pygame.Surface(size, size), pygame.SRCALPHA, 32)pygame.draw.circle(表面,pygame.Color("黑色"),(整数(大小/2),整数(大小/2)),整数(大小/2),2)

我尝试使用 surface.set_alpha(127) 但这没有用.我假设是因为 surface 已经是透明的.

感谢任何帮助.

解决方案

一些事情.首先,您的表面定义应该会崩溃,因为它缺少括号.应该是:

surface = pygame.Surface((size, size), pygame.SRCALPHA, 32)

我假设在你的代码后面的某个地方,你有一些效果:

mainWindow.blit(surface, (x, y))pygame.display.update() #或翻转

这是你真正的问题:

<预><代码>>>>导入pygame>>>打印 pygame.Color("black")(0, 0, 0, 255)

请注意末尾的 255.这意味着 pygame.Color("black") 返回完全不透明的颜色.而 (0, 0, 0, 0) 将是完全透明的.如果要设置透明度,直接定义颜色.这将使您的绘图功能看起来像:

pygame.draw.circle(表面,(0, 0, 0, 透明度),(整数(大小/2),整数(大小/2)),整数(大小/2),2)

I'm somewhat new to pygame and trying to figure out how to make a circle semi-transparent. The trick however is that the background for the circle also has to be transparent. Here is the code I'm talking about:

size = 10
surface = pygame.Surface(size, size), pygame.SRCALPHA, 32)
pygame.draw.circle(
    surface, 
    pygame.Color("black"),
    (int(size/2), int(size/2)),
    int(size/2), 2)

I tried using surface.set_alpha(127) but that didn't work. I'm assuming because the surface is already transparent.

Any help is appreciated.

解决方案

A couple things. First, your surface definition should crash, as it missing a parenthesis. It should be:

surface = pygame.Surface((size, size), pygame.SRCALPHA, 32)

I assume that somewhere later in your code, you have something to the effect of:

mainWindow.blit(surface, (x, y))
pygame.display.update() #or flip

Here is your real problem:

>>> import pygame
>>> print pygame.Color("black")
(0, 0, 0, 255)

Notice that 255 at the end. That means that pygame.Color("black") returns a fully opaque color. Whereas (0, 0, 0, 0) would be fully transparent. If you want to set the transparency, define the color directly. That would make your draw function look like:

pygame.draw.circle(
    surface, 
    (0, 0, 0, transparency), 
    (int(size/2), int(size/2)),
    int(size/2), 2)

这篇关于如何在pygame中使圆圈半透明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆