二手产品经理

二手产品经理

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

載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。