简体   繁体   中英

How to pull a list out of a multi-dimensional list in python?

I know how to pull an element out of a simple list using indexes, but I don't know how to pull a list that's inside another list.

Here's where I need this: I'm working on a snake game in pygame. I largely have the game running, but I'm having an issue getting the game to draw the snake's tail.

Here's how my code works: I've been using pygame.draw for the graphics. The 'snake' starts off as a box with an x and y coordinate. There's also another pair of x and y coordinates for the food (indicated with the variables posx and posy, to differentiate them from the x and y variables used for the snake's head). I have the game saving all previous coordinates for the snake to a list, which its doing fine (I checked by having the game print out what it was saving). My code for this part is this:

path.insert(0,(x,y))

'path' is the name of the list I'm using to track the snake's path.

I can move around and eat food with no problem (beyond the fact that my game is a bit unresponsive), the problem is if I try to implement code to draw the snake's tale.

The snake's length (how many 'food' its eaten) is saved in a variable simply called 'length'. I wrote this code to draw the snake's tale:

if length>0:
        for i in range(length-1):
            pygame.draw.rect(mainWindow, (255,255,255),(path[i], snakeSize,snakeSize))

Forgive the wrap-around on the last line.

I thought that what this would pull the individual x,y list from the 'path' list. But if I eat a piece of food, nothing happens beyond the food appearing in a new, random location. If I eat a second one, then the game freezes, and I get an error message that says:

TypeError: Rect argument is invalid

I guess it doesn't like how I'm trying to pull the x and y coordinates out of 'path'. I don't know how I could pull individual elements from a list from within another list.

Also, I don't understand why nothing happens the first time I eat food. All I know is if I change the formula from length-1 to length, then I just freeze immediately upon eating food. It doesn't seem to like length being equal to 1, and I have no clue why.

The problem you're facing with length-1 is that when you do for i in range(length-1) then when the game starts I'm guessing that length is equal to 1 , so length-1 equals to 0 and the loop doesn't execute at all so no error is raised.

So, because we will fix that error soon, first of all change the loop to for i in range(length) .


As to your error, according to your information, path is something like:

[(1, 2), (4, 1), (6, 4)]

So now lets assume that i=1 . By doing path[i] you get (4, 1) . So that means you are passing to rect 's last argument :

((4, 1), snakeSize, snakeSize)

But it expects a 4-tuple of ints. So you have 2 ways to go about this:

  1. Simply pass it element-by-element that it expects. Something like:

     (path[i][0], path[i][1], snakeSize, snakeSize) 

    And this is for sure a 4-tuple.

  2. Another way, more "pythonic" one might say, is to use the * operator to unpack the tuple. It will look like this:

     (*path[i], snakeSize, snakeSize) 

    To understand what this does lets look at a simple example:

     >>> a = (4, 6) >>> print( (a, 1, 2) ) ((4, 6), 1, 2) >>> print( (*a, 1, 2) ) (4, 6, 1, 2) 

    And again, this is indeed a 4-tuple.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM