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>How about storing your variables(for example p1direction and p2direction) in a vector(player_directions) indexed by the player number and using a loop access it, for example:</p>

<pre><code>number_of_players  = 2
playersXS = function_that_fills_playersXS() # return a vector containing your p1XS and p2XS  variables in a vector

for player_number in xrange(number_of_players):
    if player_directions[player_number]=='right' and playersXS[player_number]&lt; Attributes[player_number][1]:
        playersXS[player_number]+=Attributes[player_number][0]
</code></pre>

python loops variables pygame