SoI’m trying to code a game that renders in the terminal. It is a grid-based system, which uses list[int, int] (vector) for positions, directions etc. my map is a 5×5 and I’m basically trying to implement an equation I’ve come up with so that the player won’t be able to move beyond the borders. For example, if the player prompts to move 7 (6, 0) positions to the right from the starting position (0, 0), the player shall not move that far, but will move at least 5 positions to reach the border (4, 0).
Before I begin explaining what I’ve done, let me tell you that I have made my own class inheriting the list and just modified the dunder methods __mul_, __add_ and __sub_, which in turn makes for a vector-like class. Now, the equation is: newPosition + moveDir*(worldSize - newPosition)
. The newPosition
is the position which I’m looking to move to. moveDir
is the direction in which the player will be moving. worldSize
is the previously mentioned size of the world (5×5). The world size subtracted by the new position seems to work just fine. However when I multiply it by the move direction the result is a list that contains two things, an empty list and the worldSize - newPosition
result. Finally, if I change the code, sometimes it just gives me an TypeError:
line 18, in __rmul__
return [x*y for x,y in zip(self, value)]
^^^^^^^^^^^^^^^^
TypeError: 'int' object is not iterable
I have no idea how to fix this. I’m thinking that I have to use a Vector2 from pygame.math but I’m not too sure and I’m not looking forward to breaking the game.
Drelpy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.