The following code is a simple text-based RPG game where you can move between rooms and fight with monsters until you defeat the boss.
import random
import sys
class player(object):
name = "Hero Arius"
hp = 200
power = 20
armor = 20
class gnome(object):
name = "Flimp the Gnome"
hp = 10
power = 1
armor = 3
loot = random.randint(0, 2)
class strongerGnome(object):
name = "Flimp+ the Gnome"
hp = 20
power = 2
armor = 6
loot = random.randint(0, 2)
class goblin(object):
name = "Driekol the Goblin"
hp = 30
power = 4
armor = 4
loot = random.randint(0, 2)
class strongerGoblin(object):
name = "Driekol+ the Goblin"
hp = 60
power = 8
armor = 8
loot = random.randint(0, 2)
class minotaurus(object):
name = "Gratus the Minotaurus"
hp = 120
power = 10
armor = 3
loot = random.randint(0, 2)
class strongerMinotaurus(object):
name = "Gratus+ the Minotaurus"
hp = 240
power = 20
armor = 6
loot = random.randint(0, 2)
class wizard(object):
name = "Gandalf the Wizard"
hp = 480
power = 40
armor = 2
loot = random.randint(0, 2)
hero = player()
flimp = gnome()
strongerFlimp = strongerGnome()
driekol = goblin()
strongerDriekol= strongerGoblin()
gratus = minotaurus()
strongerGratus = strongerMinotaurus()
gandalf = wizard()
class boss(object):
name = "Leoric the Skeletonking"
hp = 960
power = 60
armor = 60
def gameOver(character, points):
if character.hp <= 0:
print(f"\n You are dead!")
print(f"\n Thanks for playing!")
print(f"\n Points: {points}")
writeScore(points)
quit()
def gameWin(points):
print(f"\n You defeated Leoric the Skeletonking!")
print(f"\n You successfully rescued the world! Congrats!")
print(f"\n Points: {points}")
writeScore(points)
exit()
def writeScore(points):
f = open("score.txt", "a")
name = input("Enter your name: ")
print(f"Player name: {name}\nPlayer points: {points}\n", file=f)
f.close()
def loot():
loots = ["Sword", "Armor"]
lootChance = random.randint(0, 2)
lootDrop = loots[lootChance]
return lootDrop
def lootEffect(lootDrop, character):
if lootDrop == "Sword":
character.power = character.power + 10
print("You got a new sword!")
print("Power increased by 10.")
print(f"Your power is now: {character.power}")
return character
elif lootDrop == "Armor":
character.armor = character.armor + 10
print("You got a new shield!")
print("Armor increased by 10.")
print(f"Your armor is now: {character.armor}")
return character
def battle(points: int) -> int:
global enemy
if current_room == "Terrifying Mine":
enemy = flimp
elif current_room == "Tunnel of Hell":
enemy = strongerFlimp
elif current_room == "Deceptive Cave":
enemy = driekol
elif current_room == "Illusion Cave":
enemy = strongerDriekol
elif current_room == "Unstable Vortex":
enemy = gratus
elif current_room == "Imaginary Labyrinth":
enemy = strongerGratus
else:
enemy = gandalf
print(f"{enemy.name} showed up!")
print("You have two options:")
while enemy.hp >= 0:
choice = input("\n [1] - Attack\n [2] - Retreat\n ")
if choice == "1":
print(f"\n{'-' * 27}")
print(f"{hero.name} swung his sword, attacking {enemy.name}!")
hitchance = random.randint(0, 10)
if hitchance > 3:
enemy.hp = enemy.hp - hero.power
print(f"You wound the enemy, the enemy's life: {enemy.hp} hp")
if enemy.hp > 1:
hero.hp = round(hero.hp - (enemy.power / hero.armor))
print(f"{enemy.name} is striking back, it has wounded you! Health: {hero.hp} hp")
print(f"{'-' * 27}")
gameOver(hero, points)
else:
if enemy.name == "Flimp the Gnome":
points += 5
elif enemy.name == "Flimp+ the Gnome":
points += 10
elif enemy.name == "Driekol the Goblin":
points += 15
elif enemy.name == "Driekol+ the Goblin":
points += 30
elif enemy.name == "Gratus the Minotaurus":
points += 60
elif enemy.name == "Gratus+ the Minotaurus":
points += 120
elif enemy.name == "Gandalf the Wizard":
points += 240
print(f"You have defeated the enemy: {enemy.name}")
print(f"{'-' * 27}")
lootDrop = loot()
print(f"\n{'-' * 27}")
print(f"You have acquired an item: {lootDrop}")
lootEffect(lootDrop, hero)
print(f"{'-' * 27}")
return points
else:
print("Your sword slips from your hand, you missed the attack!")
print(f"{enemy.name} takes this opportunity and seriously injures you!")
hero.hp = hero.hp - enemy.power
print(f"Health: {hero.hp} hp")
print(f"{'-' * 27}")
gameOver(hero, points)
elif choice == "2":
print(f"\n{'-' * 27}")
runchance = random.randint(1, 10)
if runchance > 4:
print("You have successfully escaped!")
print(f"{'-' * 27}")
sys.exit()
else:
print("You try to run away, but you slip and fall!")
print("You try to defend yourself, but fail, so the enemy wounds you badly!")
hero.hp -= enemy.power
print(f"Health: {hero.hp} hp")
print(f"{'-' * 27}")
else:
print(f"\n{'-' * 27}")
print("The number is not allowed! Please enter only 1 or 2!")
print(f"{'-' * 27}")
def boss_battle(points: int) -> int:
enemy = boss()
print("The arch-enemy of the world,", enemy.name, "showed up!")
print("You have two options:")
while enemy.hp > 0:
choice = input("\n [1] - Attack\n [2] - Retreat\n ")
if choice == "1":
print(f"\n{'-' * 27}")
print(f"{hero.name} swung his sword, attacking {enemy.name}-t!")
hitchance = random.randint(0, 10)
if hitchance > 3:
enemy.hp = enemy.hp - hero.power
print(f"You wound the enemy, the enemy's life: {enemy.hp} hp")
if enemy.hp > 1:
hero.hp = round(hero.hp - (enemy.power / hero.armor))
print(f"{enemy.name} is striking back, it has wounded you! Health: {hero.hp} hp")
print(f"{'-' * 27}")
gameOver(hero, points)
else:
if enemy.name == "Leoric the Skeletonking":
points += 480
print(f"You have defeated the enemy: {enemy.name}\n{'-' * 27}")
print(f"\n{'-' * 27}")
gameWin(hero)
return points
else:
print("Your sword slips from your hand, you missed the attack!")
print(f"{enemy.name} takes this opportunity and seriously injures you!")
hero.hp -= enemy.power
print(f"Health: {hero.hp} hp")
print(f"{'-' * 27}")
gameOver(hero, points)
elif choice == "2":
print(f"\n{'-' * 27}")
runchance = random.randint(1, 10)
if runchance > 4:
print("You have successfully escaped!")
print("Everyone is disappointed in you! You have run away from your duty and, therefore, the people continue to fear the terrible reign of Skeleton King Leoric!")
print("The world is infested with the Skeleton King and his followers! Leonic is on his way to take over the next world!\n The End!\n")
print(f"{'-' * 27}")
break
else:
print("You try to run away, but you slip and fall!")
print("You try to defend yourself, but fail, so the enemy wounds you badly!")
hero.hp -= enemy.power
print(f"Health: {hero.hp} hp")
print(f"{'-' * 27}")
gameOver(hero, points)
else:
print(f"\n{'-' * 27}")
print("The number is not allowed! Please enter only 1 or 2!")
print(f"{'-' * 27}")
def introduction():
print("\t\tWelcome chosen one, Hero Arius!\n\
Fate has chosen you as the hero to free the world from the terrible reign of Skeleton King Leoric!\n\
Get stronger through obstacles, defeat the evil's allies, and fight evil at the very end!\n\
Command: move [direction] (move north, east, west, south)\n")
input("Press ENTER to continue.")
rooms = {
'Start': {'North': 'Terrifying Mine'},
'Terrifying Mine': {'North': 'Tunnel of Hell'},
'Tunnel of Hell': {'East': 'Deceptive Cave'},
'Deceptive Cave': {'East': 'Illusion Cave'},
'Illusion Cave': {'East': 'Unstable Vortex'},
'Unstable Vortex': {'South': 'Imaginary Labyrinth'},
'Imaginary Labyrinth': {'South': 'Garden of the Wizard'},
'Garden of the Wizard': {'South': 'Bone Crusher Castle'},
'Bone Crusher Castle': {'Boss': 'Leoric the Skeletonking'}
}
current_room = "Start"
msg = ""
introduction()
totalpoints = 0
print(f"\n{'=' * 27}\nYou are here now: {current_room}\n{'=' * 27}")
while True:
user_input = input("Enter your move:\n")
next_move = user_input.split(' ')
action = next_move[0].title()
item = "Item"
direction = "null"
if len(next_move) > 1:
item = next_move[1:]
direction = next_move[1].title()
item = " ".join(item).title()
if action == "Move" or "M":
try:
current_room = rooms[current_room][direction]
msg = f"\n{'=' * 27}\n{hero.name} is heading to {direction}!\n{'=' * 27}"
print(msg)
print(f"\n{'=' * 27}\nYou are here now: {current_room}\n{'=' * 27}")
if "Boss" in rooms[current_room].keys():
totalpoints = boss_battle(totalpoints)
print(totalpoints)
break
else:
totalpoints = battle(totalpoints)
print(totalpoints)
except:
msg = f"\n{'=' * 27}\nYou can't go that way!\n{'=' * 27}"
print(msg)
elif action == "Exit":
break
else:
msg = "Invalid command!"
The code is working. I'm looking for tips in terms of coding styles, readability, and perhaps optimizations if any.
loot = random.randint(0, 2)
. That random call will get run once at class definition, which means that all your gnomes will have the same amount of loot as all other gnomes. If that isn't what you want, you'll need to move the randint call somewhere that gets run repeatedly, such as an__init__
method. \$\endgroup\$