olls / graphics

A console based graphics engine for simple Unicode games and animations.
GNU General Public License v3.0
11 stars 4 forks source link

Inconsistency of self.position datatype #25

Closed geraintwhite closed 10 years ago

geraintwhite commented 10 years ago

In sprite.py self.position is stored as a list. You pass a tuple when creating a Sprite, which is then converted to a list. Sprite.move() will only work for a list, so if you want to change the position directly you must use a list.

# The following will work fine, but is inconsistent

sprite = Sprite(position=(20,20)
sprite.position = [10,10]
sprite.move(0)

# This part is consistent but will error

sprite2 = Sprite(position=(20,20)
sprite2.position = (15,15)
sprite2.move(0)

# TypeError: 'tuple' object does not support item assignment

It would make sense to either document this fact or to rewrite Sprite.move() so it works with tuples.

In clock.py you are using tuples which will not work if you were to call Sprite.move() afterwards.

olls commented 10 years ago

Wouldn't both be supported if you just used lists for it all? Or is that what you did?

geraintwhite commented 10 years ago

As tuples aren't immutable, I changed Sprite.move() so it creates new tuples:

p = self.position

if direction == 0:
    p = (p[0], p[1]+1)
elif direction == 1:
    p = (p[0]-1, p[1])
elif direction == 2:
    p = (p[0], p[1]-1)
elif direction == 3:
    p = (p[0]+1, p[1])

self.position

So now it will work with both, yes.

I suppose I could have used list() to convert the tuples.