Python Pygame 游戏照明 [英] Python Pygame Game Lighting

查看:34
本文介绍了Python Pygame 游戏照明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在制作一个 2D 横向卷轴游戏,游戏中的一个物品将是一个火炬.我们有一个球员,他的手臂可以旋转,我们可以调整手臂的角度.我们正在研究具有三角形光束形状,遵循手臂的角度.我们有一些想法,比如在整个屏幕上放置一个 alpha 图像,并根据手臂角度从每个像素中单独删除 alpha,但我们认为这太密集了.任何想法将不胜感激.谢谢.

We are making a 2D side scrolling game and an item in the game will be a torch. We have a player who's arm can rotate and we can take the arms angle. We are looking at having a triangular beam shape, following the angle of the arm. We have had a few ideas like have an alpha image over the whole screen and individually removing alpha from each pixel based on the arm angle, but we think this will be too intensive. Any ideas would be greatly appreciated. Thanks.

推荐答案

单独改变像素真的很慢;它仅在您使用时才有效,例如numpy 操作图像数据,从那时起大部分工作将在优化的、编译的 C 代码中完成,而不是在 python 运行时中完成.

Changing pixels individually is really slow; it only works if you would use e.g. numpy to manipulate the image data, since then most work will be done in optimized, compiled C code and not in the python runtime.

一个简单的方法是使用另一个 Surface 来使用不同的渲染模式来执行此操作,例如 BLEND_RGBA_SUB.

An easy way is to just use another Surface to do this manipulation using a different render mode, like BLEND_RGBA_SUB.

这是一个最小示例:

import pygame
pygame.init()
screen=pygame.display.set_mode((640, 480))
light=pygame.image.load('circle.png')
while True:
    for e in pygame.event.get():
        if e.type == pygame.QUIT: break
    else:
        screen.fill(pygame.color.Color('Red'))
        for x in xrange(0, 640, 20):
            pygame.draw.line(screen, pygame.color.Color('Green'), (x, 0), (x, 480), 3)
        filter = pygame.surface.Surface((640, 480))
        filter.fill(pygame.color.Color('Grey'))
        filter.blit(light, map(lambda x: x-50, pygame.mouse.get_pos()))
        screen.blit(filter, (0, 0), special_flags=pygame.BLEND_RGBA_SUB)
        pygame.display.flip()
        continue
    break

circle.png:

截图:

这篇关于Python Pygame 游戏照明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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