PyGame,如何blit列表中的级别

时间:2014-03-16 13:52:51

标签: python-2.7 pygame pygame-surface

我想在我的游戏中加入一个级别,即在一个字符串列表中。 我知道我应该遍历清单,但仍然无法弄清楚。

level = [
   "BB                                BB"
   "BB             BB     BB    BB    BB"
   "BB        BB                      BB"
   "BB                                BB"
   "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
]

1 个答案:

答案 0 :(得分:0)

import pygame
pygame.init()

level = [
   "BB                                BB",
   "BB             BB     BB    BB    BB",
   "BB        BB                      BB",
   "BB                                BB",
   "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
]

x = y = 0
BLACK = (0,0,0) #RGB
WHITE = (255,255,255) #RGB
BLOCKSIZE = 16 #width and height of the block

screen = pygame.display.set_mode((len(level[0])*BLOCKSIZE,len(level)*BLOCKSIZE),0,32)
screen.fill(WHITE)

for row in level: #level is your array that you have shown above in your question
    for cell in row:
        if cell == "B":
            screen.fill(BLACK,(x,y,BLOCKSIZE,BLOCKSIZE))
        x += BLOCKSIZE
    y += BLOCKSIZE
    x = 0

pygame.display.update()
while True:
    #loop through your code

基本上,你遍历列表中的每个字符,如果那里有一个块,你在相应的位置绘制一个黑色方块。

此外,您的关卡列表应包含所有行后的逗号(因为它是一个列表)

相关问题