OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Why can't I implement this equation to my game?

  • Thread starter Thread starter Drelpy
  • Start date Start date
D

Drelpy

Guest
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 5x5 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 (5x5). 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:

Code:
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.
<p>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 <em>5x5</em> 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 <strong>(6, 0)</strong> positions to the right from the starting position <strong>(0, 0)</strong>, the player shall not move that far, but will move at least 5 positions to reach the border <strong>(4, 0).</strong></p>
<p>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: <code>newPosition + moveDir*(worldSize - newPosition)</code> . The <code>newPosition</code> is the position which I'm looking to move to. <code>moveDir</code> is the direction in which the player will be moving. <code>worldSize</code> is the previously mentioned size of the world (5x5). 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 <code>worldSize - newPosition</code> result. Finally, if I change the code, sometimes it just gives me an TypeError:</p>
<pre><code>line 18, in __rmul__
return [x*y for x,y in zip(self, value)]
^^^^^^^^^^^^^^^^
TypeError: 'int' object is not iterable
</code></pre>
<p>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.</p>
 
Top