二手产品经理

二手产品经理

THIS IS RENO

在线学Python100天 - 第4周第七天

Catbox

问题#

  1. 今天的任务是编写一个角色对战游戏,需要综合运用之前学到的知识,很有挑战。
  2. 创建一个角色生成程序,一个对战程序。让用户自定义生成 2 个角色,然后调用角色对战。最后宣布哪个角色胜利。

#

CODE#

import random, os, time

again = "yes"


def sided(n1, n2):
  n1 = random.randint(1, n1)
  n2 = random.randint(1, n2)
  n3 = n1 * n2
  return n3


def health():
  n1 = sided(6, 12) / 2 + 10
  return n1


def srtength():
  n1 = sided(6, 12) / 2 + 12
  return n1


def kill(h, s):
  return h - s


os.system("clear")
print("角色对战游戏")
uname1 = input("输入角色的名称: \n")
utype1 = input("输入角色属性(人类、精灵、巫师、兽人)\n")
health1 = health()
srtength1 = srtength()
print("血量: ", health1)
print("力量: ", srtength1)
uname2 = input("它的对手是谁? \n")
utype2 = input("输入角色属性(人类、精灵、巫师、兽人)\n")
health2 = health()
srtength2 = srtength()
print("血量: ", health2)
print("力量: ", srtength2)

time.sleep(3)
os.system("clear")
print("对战时间!")
health0 = 1
i = 1
while health0 >= 0:
  time.sleep(1)
  sided1 = sided(1, 12)
  sided2 = sided(1, 12)
  if sided1 > sided2:
    print(uname1, "掷出了", sided1, "点")
    print(uname2, "掷出了", sided2, "点")
    health0 = kill(health2, srtength1)
    health2 = health0
    print(sided1, "赢得了第", i, "回合,他对", uname2, "造成了", srtength1, "点伤害", uname2,
          "还有", health0, "点健康度")
    if health0 < 0:
      print(uname1, "赢得了最终胜利!")
      break
    i += 1
  elif sided2 > sided1:
    print(uname1, "掷出了", sided1, "点")
    print(uname2, "掷出了", sided2, "点")
    print(uname2, "赢得了第", i, "回合,他对", uname1, "造成了", srtength2, "点伤害", uname1,
          "还有", health0, "点健康度")
    health0 = kill(health1, srtength2)
    health1 = health0
    if health0 < 0:
      print(uname2, "赢得了最终胜利!")
      break
    i += 1
  else:
    print(uname1, "掷出了", sided1, "点")
    print(uname2, "掷出了", sided2, "点")
    print("这一回合他们打平了。再来!")
    print("---")
    i += 1
    continue

加载中...
此文章数据所有权由区块链加密技术和智能合约保障仅归创作者所有。