Python LoopingCall冻结

时间:2018-08-19 09:59:52

标签: 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 == 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)

我能够调用“ self.sendlocations()”,并且当我没有通过if(distance <=) 该功能可以正确打印出“运行新的发送位置”行,但是添加此行后,该行将不再显示。添加这些行之后,“更新”功能也会冻结。我不知道为什么会这样。

0 个答案:

没有答案