在非常简单的Python游戏中创建战斗系统

时间:2014-03-06 20:47:43

标签: python python-2.7 while-loop

所以这是代码

# TEST.PY

import sys
import random

class Fight(object):
    def enter(self):
        print "\n", "-" * 10
        print "There are two muchachos:"
        print "MUCHACHO1"
        print "MUCHACHO2"
        print "One of them looks wounded or something."

        your_hit_points = 20
        muchacho1_hit_points = 6
        muchacho2_hit_points = 11
        muchacho1 = True
        muchacho2 = True


        while your_hit_points > 0 or (not muchacho1 and not muchacho2):
            print "\n", "-" * 10
            your_attack = random.randint(4,12)
            muchacho1_attack = random.randint(1,4)
            muchacho2_attack = random.randint(4,8)


            attack = int(raw_input("Type 1 to attack MUCHACHO1, 2 to attack MUCHACHO2 >"))

            if attack == 1:
                muchacho1_hit_points - your_attack
                print "You hit MUCHACHO1 for %d hit points." % your_attack

                if muchacho1_hit_points <= 0 and muchacho1:
                    muchacho1 = False
                    print "MUCHACHO1 killed!"
                else:
                    pass

            elif attack == 2:
                muchacho2_hit_points - your_attack
                print "You hit MUCHACHO2 for %d hit points." % your_attack

                if muchacho2_hit_points <= 0 and muchacho2:
                    muchacho2 = False
                    print "MUCHACHO2 killed!"
                else:
                    pass

            else:
                print "DOES NOT COMPUTE"
                pass

            your_hit_points - muchacho1_attack
            print "MUCHACHO1 hit you for %d points, you have %d hit points left." % (muchacho1_attack, your_hit_points)

            your_hit_points - muchacho2_attack
            print "MUCHACHO2 hit you for %d points, you have %d hit points left." % (muchacho2_attack, your_hit_points)

        exit(1)

    a_fight = Fight()
    a_fight.enter()

我没什么困难。基本上WHILE循环永远不会完成,似乎每个人的生命点都没有减去。我有一种感觉,因为我已经缺少了一些非常基本的东西,因为我已经编写了几个小时,所以我可能看不到简单的东西。

我知道通过使用更多的类或函数可以做得更好但是现在我想这样做(对于80多个char行也很抱歉)。

2 个答案:

答案 0 :(得分:1)

我认为您要做的是muchacho1_hit_points -= your_attack,而muchacho2则相同。现在你只是丢弃减去的结果。

答案 1 :(得分:0)

我不是说它已经完成或我拥有最好的代码风格,但我对你的代码进行了一些改进(顺便说一句好游戏)。 我看到的一些错误: 你的循环是错误的(当你或至少有一个muchacho还活着的时候你应该运行循环)。 每次受到伤害时都需要覆盖muchacho1_hit_points / muchacho2_hit_points。 你需要在每轮之前检查muchacho是否还活着。 这个类没有意义,你可以只使用一个函数。 在python中pass什么都不做,在你的情况下可以忽略;它通常在声明稍后使用的容器类时使用。 您可以做的一些改进:如果用户没有输入整数(它现在崩溃),则捕获异常

import random

class Fight(object):
  def enter(self):
    print "\n", "-" * 10
    print "There are two muchachos:"
    print "MUCHACHO1"
    print "MUCHACHO2"
    print "One of them looks wounded or something."

    your_hit_points = 20
    muchacho1_hit_points = 6
    muchacho2_hit_points = 11
    muchacho1 = True
    muchacho2 = True

    while your_hit_points > 0 and (muchacho1 or muchacho2):
      print "\n", "-" * 10
      your_attack = random.randint(4,12)
      muchacho1_attack = random.randint(1,4)
      muchacho2_attack = random.randint(4,8)

      attack = int(raw_input("Type 1 to attack MUCHACHO1, 2 to attack MUCHACHO2 >"))
      if attack == 1:
        if muchacho1:
          muchacho1_hit_points = muchacho1_hit_points - your_attack
          print "You hit MUCHACHO1 for %d hit points." % your_attack
          if muchacho1_hit_points <= 0:
            muchacho1 = False
            print "MUCHACHO1 killed!"
        else:
          print "MUCHACHO 1 is already dead!"
      elif attack == 2:
        if muchacho2:
          muchacho2_hit_points = muchacho2_hit_points - your_attack
          print "You hit MUCHACHO2 for %d hit points." % your_attack
          if muchacho2_hit_points <= 0:
            muchacho2 = False
            print "MUCHACHO2 killed!"
        else:
          print "MUCHACHO 2 is already dead!"
      else:
        print "DOES NOT COMPUTE"

      if muchacho1:
        your_hit_points = your_hit_points - muchacho1_attack
        print ("MUCHACHO1 hits you for %d points, you have %d"
           " hit points left." %(muchacho1_attack, your_hit_points))
        if your_hit_points <= 0:
          print 'You are dead'
          break
      if muchacho2:
        your_hit_points = your_hit_points - muchacho2_attack
        print ("MUCHACHO2 hits you for %d points, you have %d"
            " hit points left." % (muchacho2_attack, your_hit_points))
        if your_hit_points <= 0:
          print 'You are dead'

a_fight = Fight()
a_fight.enter()
相关问题