我已经在我的游戏中创建了一个日夜循环,在屏幕上画了一个矩形,并让它的alpha不断变化。然而,我显然想给游戏增加一些亮点。有没有一种方法可以使用pygame将矩形的特定部分的alpha设置为0?或者有没有其他的方式来处理整个照明的事情?
这就是我的日光周期是如何工作的(它相当糟糕,黑夜更长,但这只是为了测试):
#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))发布于 2014-01-06 06:29:02
虽然可能有一种方法可以将不同的alpha通道分配给不同的像素,但这将是困难的,如果您按像素进行分配,则会显着减慢您的程序(如果您确实决心这样做,我能找到的最接近的方法是pygame.Surface.set_at)。看起来你最好还是把屏幕分成更小的几个面。您甚至可以通过使它们重叠来实现简单的渐变。这样你可以设置不同的亮度区域,以获得这两种效果。下面是一个用来实现你想要的东西的平铺网格的基本例子:
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的位置这样的事情,从而进一步改进此模型。
https://stackoverflow.com/questions/17761080
复制相似问题