首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >添加Lighting Pygame

添加Lighting Pygame
EN

Stack Overflow用户
提问于 2013-07-20 18:09:34
回答 1查看 1.3K关注 0票数 2

我已经在我的游戏中创建了一个日夜循环,在屏幕上画了一个矩形,并让它的alpha不断变化。然而,我显然想给游戏增加一些亮点。有没有一种方法可以使用pygame将矩形的特定部分的alpha设置为0?或者有没有其他的方式来处理整个照明的事情?

这就是我的日光周期是如何工作的(它相当糟糕,黑夜更长,但这只是为了测试):

代码语言:javascript
复制
#Setting up lighting
game_alpha = 4 #Keeping it simple for now
game_time = 15300
time_increment = -1
alpha_increment = 1

#Main Game Loop:
if float(game_time)%game_alpha == 0:
       game_alpha += alpha_increment
       print "Game Alpha: ",game_alpha
       print "Game Time: ", game_time
if game_time < 0:
       time_increment = 1
       alpha_increment = -1
elif game_time >= 15300:
       time_increment = -1
       alpha_increment = 1

    game_shadow = pygame.Surface((640, 640))
    game_shadow.fill(pygame.Color(0, 0, 0))
    game_shadow.set_alpha(game_alpha)
    game_shadow.convert_alpha()
    screen.blit(game_shadow, (0, 0))
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-06 06:29:02

虽然可能有一种方法可以将不同的alpha通道分配给不同的像素,但这将是困难的,如果您按像素进行分配,则会显着减慢您的程序(如果您确实决心这样做,我能找到的最接近的方法是pygame.Surface.set_at)。看起来你最好还是把屏幕分成更小的几个面。您甚至可以通过使它们重叠来实现简单的渐变。这样你可以设置不同的亮度区域,以获得这两种效果。下面是一个用来实现你想要的东西的平铺网格的基本例子:

代码语言:javascript
复制
tiles = []
column = []
for row in range(10):
    for column in range(10):           #These dimensions mean that the screen is broken up into a grid of ten by ten smaller tiles for lighting.
        tile = pygame.Surface((64, 64))
        tile.fill(pygame.Color(0, 0, 0))
        tile.set_alpha(game_alpha)
        tile.convert_alpha()
        column.append(tile)
    tiles.append(column)               #this now gives you a matrix of surfaces to set alphas to

def draw():                            #this will draw the matrix on the screen to make a grid of tiles
    x = 0
    y = 0
    for column in tiles:
        for tile in column:
            screen.blit(tile,(x,y))
            x += 64
        y += 64

def set_all(alpha):
    for column in tiles:
        for tile in column:
            tile.set_alpha(alpha)

def set_tile(x,y,alpha):        #the x and y args refer to the location on the matrix, not on the screen. So the tile one to the right and one down from the topleft corner, with the topleft coordinates of (64,64), would be sent as 1, 1
    Xindex = 0
    Yindex = 0
    for column in tiles:
        for tile in column:
            if Xindex == x and Yindex == y:
                tile.set_alpha(alpha)            #when we find the correct tile in the coordinates, we set its alpha and end the function
                return
            x += 1
        y += 1

这应该会给你想要的东西。我还包含了一些访问tiles集的函数。Set_all会将整个屏幕的alpha更改一定的量,set_tile只会更改一个磁贴的alpha,而draw将绘制所有磁贴。您可以通过重叠tiles来获得更精确的照明和渐变,并通过创建一个继承pygame.Surface的tiles类来管理像tiles的位置这样的事情,从而进一步改进此模型。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17761080

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档