简体   繁体   English

Python2.7如何在循环中使用多重变量?

[英]Python2.7 how do I use multiples variables in a loop?

I'm making my own game with Python2.7 through the pygame libraby. 我正在通过pygame libraby用Python2.7制作自己的游戏。 It's a 1v1 combat game where players use the same keyboard. 这是一款1v1格斗游戏,玩家使用相同的键盘。

The game works in a main loop that is repeated 60times per second, every time the loop is executed, it calculates lots of things eg the position, problem is that I have 2 players, so I have to write the lines two times. 游戏在一个主循环中运行,该循环每秒重复执行60次,每次执行循环时,它都会计算很多事情,例如位置,问题是我有2个玩家,因此我必须写两次行。

Example here: 这里的例子:

if p1direction == 'right' and p1XS < p1Attributes[1]: p1XS += p1Attributes[0] 如果p1direction =='right'并且p1XS <p1Attributes [1]:p1XS + = p1Attributes [0]

and: 和:

if p2direction == 'right' and p2XS < p2Attributes[1]: p2XS += p2Attributes[0] 如果p2direction =='正确'并且p2XS <p2Attributes [1]:p2XS + = p2Attributes [0]

See the differences p1 and p2, they are variables that belongs to Player 1 and Player 2 respectively. 看到差异p1和p2,它们是分别属于玩家1和玩家2的变量。

I just want to find a solution to not write every time the same lines just for p2. 我只想找到一个解决方案,以免每次只为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... 我在考虑for函数,因此我什至可以轻松添加播放器,但在这种情况下我不知道该怎么做...

Can someone help me ? 有人能帮我吗 ? :) Please :) 请

Create a class player. 创建一个班级播放器。 Then add the attributes of each player to the class. 然后将每个玩家的属性添加到类中。 Instantiate your class with player 1 and 2. 使用玩家1和2实例化您的课程。

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

playerOne = Player()
playerTwo = Player()

direction = playerOne.direction

If you haven't used classes yet, I wouldn't recommend using them though. 如果您还没有使用过类,那么我不建议您使用它们。 Inheritance can get pretty nasty... 继承会变得很讨厌...

Hope that helped, Narusan 希望能有所帮助,Narusan

EDIT: If you haven't used classes in Python yet, I recommend catching up there first and then continuing your game development. 编辑:如果您还没有在Python中使用过类,我建议先赶上那里,然后再继续您的游戏开发。 I have programmed several games in pygame as well, and classes come in very hand. 我也已经在pygame中编写了几款游戏,而且课堂也很方便。 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). 实际上,如果不使用适当的类(或无尽的if子句和for循环,会使一切变得非常缓慢),创建pygame游戏几乎是不可能的。

Wish you all the best of luck 祝你好运

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: 如何将变量(例如p1direction和p2direction)存储在由玩家编号索引的向量(player_directions)中,并使用循环访问它,例如:

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]< Attributes[player_number][1]:
        playersXS[player_number]+=Attributes[player_number][0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM