社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

python进入一个无限循环,这个循环什么也不做

Oscar Arranz • 7 年前 • 1878 次点击  

我不知道为什么会这样,但基本上在python结束一段时间之后,它什么也不做(但程序没有完成),我可以看到程序正在使用处理器,就像它是一个无限循环一样。

我不知道为什么会发生,以前没有发生过,我想我没有改变导致这个问题的函数。

代码:

import random


class Gato:

    movimiento = 0
    posicion = [0, 0]

    def spawn(self):
        self.posicion = [random.randint(0, 4), random.randint(0, 4)]

    def moverse(self, direccion):
        movido = False

        while not movido:
            if direccion == "sur":
                if self.posicion[1] == 4:
                    print("No puedes salir de los limites del mapa")
                    direccion = input("Vuelve a moverte en una dirección (norte, este, oeste)")
                else:
                    self.posicion[1] += 1
                    self.movimiento += 1
                    movido = True
            elif direccion == "norte":
                if self.posicion[1] == 0:
                    print("No puedes salir de los limites del mapa")
                    direccion = input("Vuelve a moverte en una dirección (sur, este, oeste)")
                else:
                    self.posicion[1] -= 1
                    self.movimiento += 1
                    movido = True
            elif direccion == "este":
                if self.posicion[0] == 4:
                    print("No puedes salir de los limites del mapa")
                    direccion = input("Vuelve a moverte en una dirección (norte, sur, oeste)")
                else:
                    self.posicion[0] += 1
                    self.movimiento += 1
                    movido = True
            elif direccion == "oeste":
                if self.posicion[0] == 0:
                    print("No puedes salir de los limites del mapa")
                    direccion = input("Vuelve a moverte en una dirección (norte, sur, este)")
                else:
                    self.posicion[0] -= 1
                    self.movimiento += 1
                    movido = True
            else:
                print("Esa no es una direccion válida")
                direccion = input("Vuelve a moverte en una dirección (norte, sur, este)")

    def detectarcolisionraton(self, raton):
        if self.posicion == raton.getposicion():
            return True
        else:
            return False

    def detectarcolisionqueso(self, queso):
        if self.posicion == queso.getposicion():
            return True
        else:
            return False

    def getmovimientos(self):
        return self.movimiento

    def checklimites(self, dir):
        if self.posicion[1] == 4 & dir == 0:
            print("No puedes salir de los limites del mapa")
        elif self.posicion[1] == 0 & dir == 1:
            print("No puedes salir de los limites del mapa")
        elif self.posicion[0] == 0 & dir == 2:
            print("No puedes salir de los limites del mapa")
        elif self.posicion[0] == 4 & dir == 3:
            print("No puedes salir de los limites del mapa")


class Raton:

    posicion = [0, 0]

    def spawn(self):
        self.posicion = [random.randint(0, 4), random.randint(0, 4)]

    def getposicion(self):
        return self.posicion

    def moverse(self, gato):
        xory = random.randint(0, 1)
        rand = random.randint(0, 1)
        movido = False

        while not movido:
            if gato.getmovimientos() % 3 == 0:
                if xory == 0:
                    if rand == 0:
                        if self.posicion[0] == 4:
                            xory = random.randint(0, 1)
                            rand = random.randint(0, 1)
                        else:
                            self.posicion[0] += 1
                            movido = True
                    else:
                        if self.posicion[0] == 0:
                            xory = random.randint(0, 1)
                            rand = random.randint(0, 1)
                        else:
                            self.posicion[0] -= 1
                            movido = True
                else:
                    if rand == 0:
                        if self.posicion[1] == 4:
                            xory = random.randint(0, 1)
                            rand = random.randint(0, 1)
                        else:
                            self.posicion[1] += 1
                            movido = True
                    else:
                        if self.posicion[1] == 0:
                            xory = random.randint(0, 1)
                            rand = random.randint(0, 1)
                        else:
                            self.posicion[1] -= 1
                            movido = True

    def pista(self, gato):
        if self.posicion[0] == gato.posicion[0] and self.posicion[1] > gato.posicion[1]:
            print("El raton está al Sur")
        elif self.posicion[0] > gato.posicion[0] and self.posicion[1] > gato.posicion[1]:
            print("El raton está al Sureste")
        elif self.posicion[0] > gato.posicion[0] and self.posicion[1] == gato.posicion[1]:
            print("El raton está al Este")
        elif self.posicion[0] > gato.posicion[0] and self.posicion[1] < gato.posicion[1]:
            print("El raton está al Noreste")
        elif self.posicion[0] == gato.posicion[0] and self.posicion[1] < gato.posicion[1]:
            print("El raton está al Norte")
        elif self.posicion[0] < gato.posicion[0] and self.posicion[1] < gato.posicion[1]:
            print("El raton está al Noroeste")
        elif self.posicion[0] < gato.posicion[0] and self.posicion[1] == gato.posicion[1]:
            print("El raton está al Oeste")
        elif self.posicion[0] > gato.posicion[0] and self.posicion[1] < gato.posicion[1]:
            print("El raton está al Suroeste")


class Queso:

    posicion = [0, 0]

    def spawn(self):
        self.posicion = [random.randint(0, 4), random.randint(0, 4)]

    def getposicion(self):
        return self.posicion


def imprimirmapa():
    for y in range(0, 5):
        for x in range(0, 5):
            print("[", end="", flush=True)
            if gato.posicion[0] == x and gato.posicion[1] == y:
                print("O", end="", flush=True)
            else:
                print("X", end="", flush=True)

            if x == 4:
                print("]")
            else:
                print("]", end="", flush=True)


gato = Gato()
raton = Raton()
queso = Queso()

gato.spawn()
raton.spawn()
queso.spawn()

while gato.posicion[0] == raton.posicion[0] and gato.posicion[1] == raton.posicion[1]:
    print("Spawn Failed!")
    raton.spawn()


ganado = False

while not ganado:
    raton.pista(gato)
    imprimirmapa()
    gato.moverse(input("Introduce la dirección en que te quieres mover (norte, sur, este, oeste)"))
    if gato.detectarcolisionraton(raton):
        print("Has ganado")
        ganado = True
    elif gato.detectarcolisionqueso(queso):
        queso.posicion = [-1, -1]
        print("Encontraste el QUESO")
        raton.pista(gato)
        gato.moverse(input("Introduce la dirección en que te quieres mover (norte, sur, este, oeste)"))
        raton.pista(gato)
        gato.moverse(input("Introduce la dirección en que te quieres mover (norte, sur, este, oeste)"))
        raton.pista(gato)
        queso.spawn()
        gato.moverse(input("Introduce la dirección en que te quieres mover (norte, sur, este, oeste)"))
    raton.moverse(gato)
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/41101
文章 [ 1 ]  |  最新文章 7 年前