Python2.7 how do I use multiples variables in a loop?
<p>I'm making my own game with Python2.7 through the pygame libraby.
It's a 1v1 combat game where players use the same keyboard.</p>

<p>The game works in a main loop that is repeated 60times per second, every time the loop is executed, it calculates lots of things e.g the position, problem is that I have 2 players, so I have to write the lines two times.</p>

<blockquote>
  <p>Example here:</p>
  
  <p>if p1direction == 'right' and p1XS &lt; p1Attributes[1]: p1XS +=
  p1Attributes[0]</p>
  
  <p>and:</p>
  
  <p>if p2direction == 'right' and p2XS &lt; p2Attributes[1]: p2XS +=
  p2Attributes[0]</p>
</blockquote>

<p>See the differences p1 and p2, they are variables that belongs to Player 1 and Player 2 respectively.</p>

<p>I just want to find a solution to not write every time the same lines just for p2. I was thinking about the for function so I can even add players easly but I don't know how to do it in this case...</p>

<p>Can someone help me ? :) Please</p>

<p>Create a class player.
Then add the attributes of each player to the class.
Instantiate your class with player 1 and 2. </p>

<pre><code>class Player():
    direction = "right"
    etc.
    def shoot(self):
        if self.direction == "right"
             shoot_right()

playerOne = Player()
playerTwo = Player()

direction = playerOne.direction
</code></pre>

<p>If you haven't used classes yet, I wouldn't recommend using them though. Inheritance can get pretty nasty...</p>

<p>Hope that helped,
Narusan</p>

<p>EDIT:
If you haven't used classes in Python yet, I recommend catching up there first and then continuing your game development. I have programmed several games in pygame as well, and classes come in very hand. In fact, it is almost impossible to create pygame games without using proper classes (or endless if-clauses and for-loops that will make everything super slow).</p>

<p>Wish you all the best of luck</p>

python loops variables pygame