Wednesday, August 28, 2013

How do I use values from a tuple whic is in a list of tuples as arguments for a function?

How do I use values from a tuple whic is in a list of tuples as arguments
for a function?

I'm trying to construct a simple tilemap thingy, as part of a larger
project, and one of the solutions I came out with was to use a function
like so:
def tile((value, X, Y)):
if value == 1:
pygame.draw.rect(screen, (0, 0, 0) (X, Y, 64, 64), 0)
The idea being that I set the level out as a list of tuples that contain a
value which denotes the tile to be drawn there, and then the coordinates
of that tile.
This, of course, doesn't work since you can't use a tuple as an argument.
My solution to this was to instead get the values out of the tuple in the
list:
def tile(value, X, Y):
if value == 1:
pygame.draw.rect(screen, (0, 0, 0) (X, Y, 64, 64), 0)
level = [(1, 0, 0), (rest of list)]
tile(level[0[0]], level[0[1]], level[0[2]])
Which didn't work, and gave me
Traceback (most recent call last):
tile(level[0[0]], level[0[1]], level[0[2]])
TypeError: 'int' object has no attribute '__getitem__'
Does anyone have a workaround for this? Some way I can feed a tuple into a
function as a series of arguments? Or am I just using a completely
idiosyncratic method that won't ever work ever? I'm guessing this is just
due to my blinding incompetence, although I would like to be told
otherwise.
Python 2.7, pygame 1.9

No comments:

Post a Comment