I was creating a snake game in the wonderful module pygame today, as one does and in my sub-optimal implementation I got very interesting behavior…was wondering if anyone can explain this?
inputs (each line un-commented individually for outputs)
def move_right(self):
print(self.nodes[-2])
# self.nodes[-1] = [self.nodes[-1][0] + 1, self.nodes[-1][1]] # situation 1
# self.nodes[-1][0] = 1 # situation 2
# self.nodes[-1][0] += 1 # situation 3
print(self.nodes[-2])
situation 1 out: (expected)
[2, 3]
[2, 3]
----------
[3, 3]
[3, 3]
----------
...
situation 2 out: (unexpected…why does it set (-2) also?)
[2, 3]
[1, 3]
----------
[1, 3]
[1, 3]
----------
...
situation 3 out: (unexpected… original implementation)
[2, 3]
[3, 3]
----------
[3, 3]
[4, 3]
----------
...
full code: import pygamefrom pygame import K_RIGHT, K_LEFT, K_UP, K_DOWN, K_ESCAPEfro - Pastebin.com
please don’t go assaulting it for the poor design choices,
this is far from its final form, there’s more to each class that I have yet to implement, just was curious about this
Thanks to anyone who has any insight, I can’t figure what I might’ve done wrong with the syntax
It just doesn’t make sense to me yet