【问题标题】:Neon effect in pygame?pygame中的霓虹灯效果?
【发布时间】:2021-06-11 08:44:53
【问题描述】:

我正在使用 pygame 项目,我需要我的游戏立方体具有霓虹灯效果。 pygame 是否具有制作霓虹灯效果的功能或类似这样的想法:

pygame.draw.rect(win, (255, 0, 255[neon]), ...)

【问题讨论】:

  • “霓虹灯效应”到底是什么意思?你的意思是某种发光或绽放?如果是这样,答案是否定的。 Pygame 没有“发光”功能。

标签: pygame effects


【解决方案1】:

Pygame 没有“glow”、“bloom”或“neon”功能。您必须使用cv2 和/或numpy 来创建这样的效果。

编写一个对pygame.Surface 应用效果的函数:

def create_neon(surf):
    surf_alpha = surf.convert_alpha()
    rgb = pygame.surfarray.array3d(surf_alpha)
    alpha = pygame.surfarray.array_alpha(surf_alpha).reshape((*rgb.shape[:2], 1))
    image = numpy.concatenate((rgb, alpha), 2)
    cv2.GaussianBlur(image, ksize=(9, 9), sigmaX=10, sigmaY=10, dst=image)
    cv2.blur(image, ksize=(5, 5), dst=image)
    bloom_surf = pygame.image.frombuffer(image.flatten(), image.shape[1::-1], 'RGBA')
    return bloom_surf

小例子:

import pygame
import numpy
import cv2

pygame.init()
window = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()

def create_neon(surf):
    surf_alpha = surf.convert_alpha()
    rgb = pygame.surfarray.array3d(surf_alpha)
    alpha = pygame.surfarray.array_alpha(surf_alpha).reshape((*rgb.shape[:2], 1))
    image = numpy.concatenate((rgb, alpha), 2)
    cv2.GaussianBlur(image, ksize=(9, 9), sigmaX=10, sigmaY=10, dst=image)
    cv2.blur(image, ksize=(5, 5), dst=image)
    bloom_surf = pygame.image.frombuffer(image.flatten(), image.shape[1::-1], 'RGBA')
    return bloom_surf

image = pygame.Surface((100, 100), pygame.SRCALPHA)
pygame.draw.rect(image, (255, 128, 128), (10, 10, 80, 80))
neon_image = create_neon(image)

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False          

    window.fill((127, 127, 127))
    window.blit(neon_image, neon_image.get_rect(center = window.get_rect().center), special_flags = pygame.BLEND_PREMULTIPLIED)
    pygame.display.flip()
    clock.tick(60)

pygame.quit()
exit()

【讨论】:

  • 非常感谢,这对我有很大帮助(现在才说,因为在我不知道如何添加评论之前)
猜你喜欢
  • 1970-01-01
  • 2016-07-27
  • 1970-01-01
  • 1970-01-01
  • 2021-01-06
  • 2021-12-31
  • 2015-02-14
相关资源
最近更新 更多