二手产品经理

二手产品经理

THIS IS RENO

Learn Python Online in 100 Days - Week 4, Day 7

Catbox

Problem#

  1. Today's task is to write a role-playing game that requires the comprehensive application of previously learned knowledge. It's quite challenging.
  2. Create a character generation program and a battle program. Let the user customize the generation of 2 characters, then call for a battle between the characters. Finally, announce which character wins.

#

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("Role-playing game")
uname1 = input("Enter the name of the character: \n")
utype1 = input("Enter the character attribute (human, elf, wizard, orc)\n")
health1 = health()
srtength1 = srtength()
print("Health: ", health1)
print("Strength: ", srtength1)
uname2 = input("Who is its opponent? \n")
utype2 = input("Enter the character attribute (human, elf, wizard, orc)\n")
health2 = health()
srtength2 = srtength()
print("Health: ", health2)
print("Strength: ", srtength2)

time.sleep(3)
os.system("clear")
print("Battle time!")
health0 = 1
i = 1
while health0 >= 0:
  time.sleep(1)
  sided1 = sided(1, 12)
  sided2 = sided(1, 12)
  if sided1 > sided2:
    print(uname1, "rolled", sided1, "points")
    print(uname2, "rolled", sided2, "points")
    health0 = kill(health2, srtength1)
    health2 = health0
    print(sided1, "won round", i, "and dealt", srtength1, "points of damage to", uname2,
          uname2, "has", health0, "health points left")
    if health0 < 0:
      print(uname1, "wins the final victory!")
      break
    i += 1
  elif sided2 > sided1:
    print(uname1, "rolled", sided1, "points")
    print(uname2, "rolled", sided2, "points")
    print(uname2, "won round", i, "and dealt", srtength2, "points of damage to", uname1,
          uname1, "has", health0, "health points left")
    health0 = kill(health1, srtength2)
    health1 = health0
    if health0 < 0:
      print(uname2, "wins the final victory!")
      break
    i += 1
  else:
    print(uname1, "rolled", sided1, "points")
    print(uname2, "rolled", sided2, "points")
    print("They tied this round. Let's go again!")
    print("---")
    i += 1
    continue

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.