python - 如何:pygame中灰点颜色的重叠
问题描述
我正在研究一个带有随机移动点的 pygame。这些点在黑色表面上移动。目的是在它们重叠的情况下添加点的 RGB 颜色。下图中有 3 个重叠的点。如何将点的 RGB 相加?图像中的示例是三个重叠的点。重叠的点部分应具有颜色 [3*250/N, 3*250/N., 3*250/N],N = 4 该部分应具有颜色 [187.5,187.5,187.5]。网点不重叠的部分应保持基本网点颜色[250/N、250/N、250/N]。
我不知道如何添加 RGB 颜色。有任何想法吗?
和当前代码:
import math
import random
import pygame
import random
N = 4
class focalspot(object):
def __init__(self,dirnx=0,dirny=0,color=(250/N,250/N,250/N)):
self.pos = 100,100
self.dirnx = 0
self.dirny = 0
self.color = color
def move(self, dirnx, dirny):
self.dirnx = dirnx
self.dirny = dirny
self.pos = (self.pos[0] + self.dirnx, self.pos[1] + self.dirny)
if self.pos[0] >= width:
self.dirnx = -self.dirnx
if self.pos[1] >= width:
self.dirny = -self.dirny
if self.pos[0] <= 0:
self.dirnx = -self.dirnx
if self.pos[1] <= 0:
self.dirny = -self.dirny
def draw(self, surface):
pygame.draw.circle(surface, self.color, self.pos, 80, 0)
dots = [focalspot() for i in range(N)]
def redrawWindow(surface,dots):
global rows, width
surface.fill((0,0,0))
for dot in dots:
focalspot.draw(dot,surface)
pygame.display.update()
def main(flag,dots):
global width, rows
width = 400
win = pygame.display.set_mode((width, width))
counter = 0
clock = pygame.time.Clock()
while flag:
pygame.time.delay(50)
clock.tick(10)
for dot in dots:
dot.dirnx = int(random.uniform(-10, 10))
dot.dirny = int(random.uniform(-10, 10))
focalspot.move(dot,dot.dirnx,dot.dirny)
redrawWindow(win,dots)
pygame.image.save(win,'temp'+str(counter)+'.png')
counter =+ 1
print(counter)
main(True,dots)
解决方案
你应该使用这个Surface
类来画你的圆圈。这允许您使用特殊的 blitting flags,例如BLEND_ADD
,它将执行您想要的操作:
import pygame
class Dot:
def __init__(self, pos=None):
self.image = pygame.Surface((50, 50))
self.image.set_colorkey((2, 4, 6))
self.image.fill((2, 4, 6))
pygame.draw.circle(self.image, (50, 50, 50), (25, 25), 25, 0)
self.rect = self.image.get_rect(center=pos or (0, 0))
def main():
screen = pygame.display.set_mode((400, 400))
dot = Dot()
dots = [dot, *[Dot(pos) for pos in ((50, 50), (100, 200), (120, 230), (200, 300), (300, 150))]]
clock = pygame.time.Clock()
while True:
for e in pygame.event.get():
if e.type == pygame.QUIT:
return
dot.rect.center = pygame.mouse.get_pos()
screen.fill(pygame.Color('black'))
for d in dots:
screen.blit(d.image, d.rect, special_flags=pygame.BLEND_ADD)
pygame.display.flip()
clock.tick(60)
if __name__ == '__main__':
main()
推荐阅读
- c++ - 如何在 C++ 类中实现 CreateThread - LPTHREAD_START_ROUTINE 类型的 ThreadProc 回调函数
- python - 根据列值对数据框进行排序
- javascript - 如何在不破坏 html 代码的情况下在 vuejs 模板中循环
- r - 如何从具有符号参数的函数的结果创建方程
- excel - 循环为每一行发送一封电子邮件
- node.js - 使用 axios 在 nodejs 中执行 post 请求,但我无法在 post 路由中接收数据
- javascript - 如何查找数组内的差异值并记录差异的索引
- python - 无法使用 pool.apply_async 通过多处理聚合结果
- swift - Codable 结构中的 Codable 变量导致错误
- bevy - 如何防止相机在 Bevy 中看穿墙壁?