用非常简单的Python gam创建战斗系统

2024-10-01 13:40:52 发布

您现在位置:Python中文网/ 问答频道 /正文

所以这就是密码

# 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多个字符行也很抱歉)。在


Tags: andyouforyourifrandompointsprint
2条回答

我想你想做的是muchacho1_hit_points -= your_attack,对{}也是一样。现在你只需要丢弃减法的结果。在

我不是说它已经完成或者我有最好的代码风格,但是我对你的代码做了一些改进(顺便说一句,这个游戏不错)。 我看到了一些虫子: 你的循环是错误的(你应该在你或者至少有一个傻瓜还活着的时候运行这个循环)。 你需要在每次受到伤害的时候重写muchacho1_hit_points/muchacho2_hit_points。 你需要在每轮比赛前检查穆查乔是否还活着。 这个类没有意义,您可以使用函数。 在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()

相关问题 更多 >