0

I have two 2D arrays, each containing information that will display a layer:

layer0 = [[info], [info]]
layer1 = [[info], [info]]

I would like to contain these two 2D-arrays, within yet another array:

map = [[layer0], [layer1]]

However, my program will not display the tiles correctly. My question is: Is it possible to store 2D arrays, within another 2D array? Thank you.

I have a certain loop-system for iterating through said arrays and displaying tiles corresponding to the array content:

for array in maplayer:
            for tile in array:
                if tile == 0:
                    screen.blit(self.tile_dict[0], (self.tileX, self.tileY))
                    self.tileX = self.tileX+16
                if tile == 1:
                    screen.blit(self.tile_dict[1], (self.tileX, self.tileY))
                    self.tileX = self.tileX+16
            self.tileX = self.cameraX
            self.tileY += 16

I have tried adding another simple for loop, to iterate through the actual map array, but PyGame displays a blank screen:

for maplayer in map:
        for array in maplayer:
            for tile in array:
                if tile == 0:
                    screen.blit(self.tile_dict[0], (self.tileX, self.tileY))
                    self.tileX = self.tileX+16
                if tile == 1:
                    screen.blit(self.tile_dict[1], (self.tileX, self.tileY))
                    self.tileX = self.tileX+16
            self.tileX = self.cameraX
            self.tileY += 16

Here is the full method:

def LoadMap(self, map):
    self.tileX = self.cameraX
    self.tileY = self.cameraY
    for maplayer in map:
        for array in maplayer:
            for tile in array:
                if tile == 0:
                    screen.blit(self.tile_dict[0], (self.tileX, self.tileY))
                    self.tileX = self.tileX+16
                if tile == 1:
                    screen.blit(self.tile_dict[1], (self.tileX, self.tileY))
                    self.tileX = self.tileX+16
            self.tileX = self.cameraX
            self.tileY += 16

Thanks.

2
  • What tiles are you referring to? How are you trying to display these things? What is happening, and how is it different than what you expect? But quick answer, yes it is possible to store 2D arrays inside of 2D arrays. Example: [[ [[1,2][3,4]] ]] Commented Apr 28, 2014 at 22:39
  • Hey FrobberOfBits, I edited the question to be more specific. :) Commented Apr 28, 2014 at 22:55

1 Answer 1

1

I have done the same thing with reading 2D arrays and can't actually see why you need to have the array inside of another.. I used this code to create the array from a file:

gameMap = [list(row.rstrip('\n')) for row in open('Room 1.txt')]

Then this to read it:

for i in range(0, len(gameMap)):

    for x in range(0, len(gameMap[i])):
        xC = x * 30
        y = i * 30

        if gameMap[i][x] == "*":
            screen.blit(wallImage, (xC, y))

        elif gameMap[i][x] == ".":
            screen.blit(floorImage, (xC, y))

        elif gameMap[i][x] == "+":

            if playerDirection == "up" or playerDirection == "":
                screen.blit(playerForwardImage, (xC, y))

            elif playerDirection == "right":
                screen.blit(playerRightImage, (xC, y))

            elif playerDirection == "left":
                screen.blit(playerLeftImage, (xC, y))

            elif playerDirection == "down":
                screen.blit(playerDownImage, (xC, y))

        elif gameMap[i][x] == "#":
            pygame.draw.rect(screen, black, (xC, y, 30, 30))

        elif gameMap[i][x] == "=":
            pygame.draw.rect(screen, red, (xC, y, 30, 30))

Hope that helps, am a bit confused about why you even need to put it inside another array.

Sign up to request clarification or add additional context in comments.

4 Comments

You know, you're right, I actually don't need to have it in 2D, I don't know why I did...
That's cool. Happy to help. You okay with going about creating one and reading it?
Not trynna sound dodgy but If i solved your Q could you tick the tick :)
Oop, sorry, still new here. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.