0

i want to blit a level to my game, that is inside a list of strings. i know i should loop through the list, but still couldn't figure it out.

level = [
   "BB                                BB"
   "BB             BB     BB    BB    BB"
   "BB        BB                      BB"
   "BB                                BB"
   "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
]
Nobody
  • 605
  • 1
  • 7
  • 9

1 Answers1

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

Basically, you loop through each character of the list and if there is a block there, you draw a black square in its corresponding location.

Also your level list should contain commas after all lines (since it is a list)

sshashank124
  • 31,495
  • 9
  • 67
  • 76