[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Sprite Collision



On 4/30/07, Casey Duncan <casey@xxxxxxxxxxx> wrote:
passible_at = [[True] * 100] * 100 # Create a 2 dimensional list with
an element for each tile

This does not do what you think it would:
passible_at = [[True] * 3] * 4
passible_at
[[True, True, True], [True, True, True], [True, True, True], [True, True, True]]
passible_at[0][0] = False
passible_at
[[False, True, True], [False, True, True], [False, True, True],
[False, True, True]]


One way to get what you're talking about is as follows:
rows = 4
cols = 3
passible_at = []
for _ in xrange(rows):
...     passible_at.append( [True] * cols )
...
passible_at
[[True, True, True], [True, True, True], [True, True, True], [True, True, True]]
passible_at[0][0] = False
passible_at
[[False, True, True], [True, True, True], [True, True, True], [True,
True, True]]


Someone more skilled than I can explain *why* the first one does what it does. I just know one way to get around it (and I'm sure there's a more elegant solution).

Ian