951947697 发表于 2020-11-6 19:13:31

循环停不下来

import random as r
import math

map_x =
map_y =
def nit(list1):
    list1 += 2
    list1 -= 2
   

class Soldiers:
    def __init__(self):
      self.power = 5
      self.x = r.randint(0,100)
      self.y = r.randint(0,100)

    def move(self):
      new_x = self.x + r.choice()
      new_y = self.y + r.choice()

      if new_x < map_x:
            self.x = 0 - (new_x - 0)
            self.power -= 1

      elif new_x > map_x:
            self.x = map_x - (new_x - (map_x))
            self.power -= 1

      else:
            self.x = new_x


      if new_y < map_y:
            self.y = 0 - (new_y - 0)
            self.power -= 1

      elif new_y > map_y:
            self.y = map_y - (new_y - map_y)
            self.power -= 1

      else:
            self.y =new_y

      return (self.x, self.y)

      

class Shooter(Soldiers):
    def __init__(self):
      self.x = r.randint(0,100)
      self.y = r.randint(0,100)
      super().__init__()
      self.power = 2


    def move(self):
      new_x = self.x + r.choice()
      new_y = self.y + r.choice()
      

class Lord_Voldemort(Soldiers):
    def __init__(self):
      self.x = r.randint(0,100)
      self.y = r.randint(0,100)
      super().__init__()
      self.power = 5

    def move(self):
      new_x = self.x + r.choice()
      new_y = self.y + r.choice()


class Medic(Soldiers):
    def __init__(self):
      self.x = r.randint(0,100)
      self.y = r.randint(0,100)
      super().__init__()
      self.power = 3
      if self.power > 5:
            self.power = 5
      
    def move(self):
      new_x = self.x + r.choice()
      new_y = self.y + r.choice()

      if new_x < map_x:
            self.x = 0 - (new_x - 0)
            self.power -= 1

      elif new_x > map_x:
            self.x = map_x - (new_x - map_x)
            self.power -= 1

      else:
            self.x = new_x


      if new_y < map_y:
            self.y = 0 - (new_y - 0)
            self.power -= 1

      elif new_y > map_y:
            self.y = map_y - (new_y - map_y)
            self.power -= 1

      else:
            self.y =new_y
            
      self.power += 1
      return (self.x, self.y)




soldier = Soldiers()

shooter = Shooter()

lord_voldemort = Lord_Voldemort()

medic = Medic()

unit =[]
unit.append(soldier)
unit.append(shooter)
unit.append(lord_voldemort)
unit.append(medic)




while True:

    if not soldier.power and soldier in unit:
      
      print('普通士兵被淘汰了')

    if not shooter.power and shooter in unit:
      
      print('射手被淘汰了')

    if not lord_voldemort.power and lord_voldemort in unit:
      
      print('伏地魔被毒死了')

    if not medic.power and medic in unit:
      
      print('医疗兵被淘汰了')

    soldier_pos = soldier.move()
    shooter_pos = shooter.move()
    lord_voldemort_pos = lord_voldemort.move()
    medic_pos = medic.move()

    if soldier_pos == shooter_pos and shooter in unit:
      print('射手被普通士兵击杀!')
      shooter.power = 0
            
    if lord_voldemort_pos == soldier_pos and soldier in unit:
      print('普通士兵被伏地魔偷袭了!')
      soldier.power = 0

    if lord_voldemort_pos == medic_pos and medic in unit:
      print('医疗兵被伏地魔偷袭了!')
      medic.power = 0


    if lord_voldemort_pos == shooter_pos and shooter in unit:
      print('射手被伏地魔偷袭了!')
      shooter.power = 0
            
    if math.sqrt((shooter.x - medic.x)**2+(shooter.y - medic.y)**2) >= 2 and math.sqrt((shooter.x - medic.x)**2+(shooter.y - medic.y)**2) <=4 and medic in unit:
      print('医疗兵被射手击杀了!!')
      medic.power = 0

    if math.sqrt((shooter.x - soldier.x)**2+(shooter.y - soldier.y)**2) >= 2 and math.sqrt((shooter.x - soldier.x)**2+(shooter.y - soldier.y)**2) <=4 and soldier in unit:
      print('普通士兵被射手击杀了!!')
      soldier.power = 0
      
    if not soldier.power and soldier in unit:
      unit.remove(soldier)
      print('普通士兵被淘汰了')

    if not shooter.power and shooter in unit:
      unit.remove(shooter)
      print('射手被淘汰了')

    if not lord_voldemort.power and lord_voldemort in unit:
      unit.remove(lord_voldemort)
      print('伏地魔被毒死了')

    if not medic.power and medic in unit:
      unit.remove(medic)
      print('医疗兵被淘汰了')

    for each_unit in unit[:]:

      if len(unit) == 1:
            print('游戏结束,%s存活了下来!!!' % each_unit)
          break




代码中即使有break语句也停不下下来!
为啥呢??



      

Brick_Porter 发表于 2020-11-6 19:23:22

你的break是嵌套在for循环里面的,所以停止for循环,外层的while不会停止,我建议你在while循环开始前创建一个变量move_on = True,然后while move_on:,这样写while循环,在for循环中只要符合退出条件就move_on = False,这样就能停下游戏了

Brick_Porter 发表于 2020-11-6 19:30:58

如图,使用move_on这个状态位控制程序的运行

LuLD 发表于 2020-11-6 19:32:00

可能是吃了炫迈口香糖{:10_279:}

951947697 发表于 2020-11-6 19:41:49

Brick_Porter 发表于 2020-11-6 19:30
如图,使用move_on这个状态位控制程序的运行

谢谢大神!!!感谢感谢!!!!!
页: [1]
查看完整版本: 循环停不下来