Python循环调用冻结
我试图创建一个游戏服务器来学习,但我遇到了一个问题,我的更新函数每(0.05)秒运行一次,没有调用任何传递变量的函数Python循环调用冻结,python,twisted,Python,Twisted,我试图创建一个游戏服务器来学习,但我遇到了一个问题,我的更新函数每(0.05)秒运行一次,没有调用任何传递变量的函数 def update(self): print("Match update: %s" % (str(self))) if (self.pendingShutdown): cancelShutdown = True for player in self.players: if player.protocol =
def update(self):
print("Match update: %s" % (str(self)))
if (self.pendingShutdown):
cancelShutdown = True
for player in self.players:
if player.protocol == None:
cancelShutdown = False
if (time() > self.shutdownTime):
print("Time elapsed, shutting down match")
#self.quit()
else:
self.sendlocations()
def sendlocations(self):
print("ran new send locations")
for player in self.players:
print(player.posX)
for r in range(0, len(self.players)):
matchPlayer = self.players[r]
#equation for distance between players
x = (player.posX - matchPlayer.posX)**2
y = (player.posY - matchPlayer.posY)**2
distance = math.sqrt(x+y)
#ending equation
if (distance <= 1000):
if (matchPlayer.protocol):
#finding the player index(SENDING PLAYER)
index = 0
for i in matchPlayer.match.players:
if(i.playerId == player.playerId):
matchPlayer.protocol.sendPlayerMoved(index, player.posX, player.posY, player.posZ)
index = index + 1
print("Player moved %s " % (index))
def sendPlayerMoved(self, playerIndex, posX, posY, posZ):
message = MessageWriter()
message.writeByte(MESSAGE_PLAYER_MOVED)
message.writeInt(playerIndex)
message.writeInt(posX)
message.writeInt(posY)
message.writeInt(posZ)
#self.log("Sent PLAYER_MOVED %d %d" % (playerIndex, posX))
self.sendMessage(message)
def更新(自):
打印(“匹配更新:%s”%(str(self)))
如果(自挂起关闭):
cancelShutdown=True
对于self.players中的玩家:
如果player.protocol==无:
cancelShutdown=False
如果(time()>self.shutdown):
打印(“经过的时间,关闭匹配”)
#self.quit()
其他:
self.sendlocations()
def发送位置(自身):
打印(“运行新发送位置”)
对于self.players中的玩家:
打印(player.posX)
对于范围内的r(0,len(self.players)):
matchPlayer=self.players[r]
#运动员之间的距离方程式
x=(player.posX-matchPlayer.posX)**2
y=(player.posY-matchPlayer.posY)**2
距离=数学sqrt(x+y)
#结束方程
如果(距离)很难帮助你。你的例子不完整我们无法推断sendPlayerMoved
的作用。当然,有三个嵌套循环无助于服务器的响应。是的,我注意到,在我试图找出问题的时候,我将尝试减少循环。还编辑了问题并添加了sendPlayerMoved。