Python Forum
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Basic RPG
#8
Hi!
I've copied your code and modified it, I'm added some features like level_up issues and new fight logic. I'm a beginner with python and this exercise help me a lot, even I tried to do it using class but I wasn't successful on it. But you now what? I'm still happy with it.

import random
import sys
import time

player = {}
rat = {"name": "Rat", "experience": 15, "attack": 15, "defense": 5, "health": 20}
snake = {"name": "Snake", "experience": 10, "attack": 18, "defense": 5, "health": 15}
dragon = {"name": "Dragon", "experience": 150, "attack": 50, "defense": 40, "health": 1000}
next_level = [20, 40, 80, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000]


def stats():
    print("Current Stats:")
    print("Name: " + str(player["name"]))
    print("Level: " + str(player["level"]))
    print("Experience: " + str(player["experience"]))
    print("Current Health: " + str(player["health"]))
    print("Max Health: " + str(player["max_health"]))
    print("Defense: " + str(player["defense"]))
    print("Attack: " + str(player["attack"]))
    print("-" * 160)


def character():
    player["name"] = input("Enter your character's name. ")
    player["experience"] = 0
    player["level"] = 1
    player["attack"] = 30
    player["defense"] = 10
    player["health"] = 150
    player["max_health"] = player["health"]


def saving(file_name):
    save = open(str(file_name) + ".txt", "w+")
    save.write(str(player["name"]) + "\n")
    save.write(str(player["health"]) + "\n")
    save.write(str(player["max_health"]) + "\n")
    save.write(str(player["defense"]) + "\n")
    save.write(str(player["attack"]) + "\n")
    save.write(str(player["level"]) + "\n")
    save.write(str(player["experience"]) + "\n")
    save.close()
    print("saving successful!")


def loading(file_name):
    try:
        load = open(str(file_name) + ".txt", "r")
    except:
        print("Sorry this save file doesn't exist.")
        return
    if load.mode == "r":
        contents = load.readlines()
        name = contents[0]
        list_name = list(name)
        del list_name[-1]
        joined_name = ''.join(list_name)

        health = contents[1]
        list_health = list(health)
        del list_health[-1]
        joined_health = ''.join(list_health)
        int_health = int(joined_health)

        max_health = contents[2]
        list_healthmax = list(max_health)
        del list_healthmax[-1]
        joined_healthmax = ''.join(max_health)
        int_max_health = int(joined_healthmax)

        defense = contents[3]
        list_agility = list(defense)
        del list_agility[-1]
        joined_agility = ''.join(list_agility)
        int_defense = int(joined_agility)

        attack = contents[4]
        list_strength = list(attack)
        del list_strength[-1]
        joined_strength = ''.join(list_strength)
        int_attack = int(joined_strength)

        level = contents[5]
        list_level = list(level)
        del list_level[-1]
        joined_level = ''.join(list_level)
        int_level = int(joined_level)

        experience = contents[6]
        list_experience = list(experience)
        del list_experience[-1]
        joined_experience = ''.join(list_experience)
        int_experience = int(joined_experience)

        player["name"] = joined_name
        player["health"] = int_health
        player["max_health"] = int_max_health
        player["defense"] = int_defense
        player["attack"] = int_attack
        player["level"] = int_level
        player["experience"] = int_experience

        print("loading successful!")


def rest():
    player["health"] = player["max_health"]
    print("Your health is full.")


def is_alive() -> bool:
    return player["health"] > 0


def level_up():
    for i in next_level:
        if player["experience"] >= i:
            print("-" * 160)
            player["level"] += 1
            player["max_health"] += 15
            player["attack"] += 5
            player["defense"] += 4
            print("You level up!")
            rest()
            print("-" * 160)
            next_level.pop(0)


def fight(enemy):
    print("All of a sudden a " + str(enemy['name']) + " attacks you!")
    creature = enemy.copy()
    turn: int = 0
    while creature["health"] > 0 and is_alive():
        turn += 1
        print("")
        if turn % 2 == 1:
            print("It is your turn to attack.")
            time.sleep(1)
            hit = random.randint(1, player["attack"]) - random.randint(1, player["defense"])
            if hit <= 0:
                print("You miss...")
            else:
                creature["health"] -= hit
                print("You hit " + str(hit) + " on the enemy")
        else:
            print("It is enemy turn to attack.")
            time.sleep(1)
            hit = random.randint(1, creature["attack"]) - random.randint(1, player["defense"])
            if hit <= 0:
                print("The enemy miss...")
            else:
                player["health"] -= hit
                print("The enemy hits " + str(hit) + " on you")

        if creature["health"] > 0 and not is_alive():
            print("GAME OVER! " + str(player["name"]) + " died.")
            sys.exit()

        if is_alive() and creature["health"] <= 0:
            print("")
            print("You won the battle.")
            print("")
            player["experience"] += creature["experience"]
            print("You won " + str(creature["experience"]) + " experience")
            level_up()


def main():
    character()
    print("Hail Adventurer! Welcome to the MyRPG! Please fulfil the information before start the game")
    print("Type (H)elp for more information")
    while True:
        menu = input(">").lower()
        if menu in ["help", "h"]:
            print("Commands: (H)elp, (R)est, (E)xplore, s(T)ats, (S)ave, (L)oad, (Q)uit")
        elif menu in ["rest", "r"]:
            rest()
        elif menu in ["explore", "e"]:
            shuffle = random.randint(1, 10)  
            if shuffle <= 5:
                fight(snake)
            else:
                fight(rat)
        elif menu in ["stats", "t"]:
            stats()
        elif menu in ["save", "s"]:
            salvar = input("Type the file name to save: ")
            saving(salvar)
        elif menu in ["load", "l"]:
            carregar = input("Type the file name to load: ")
            loading(carregar)
        elif menu in ["quit", "q"]:
            print("You quit the game.")
            sys.exit()
        else:
            print("Invalid Key, please try again")


main()
Reply


Messages In This Thread
Basic RPG - by Iskuss - Dec-04-2017, 02:38 AM
RE: Basic RPG - by Larz60+ - Dec-04-2017, 03:55 AM
RE: Basic RPG - by Iskuss - Dec-07-2017, 06:05 AM
RE: Basic RPG - by nilamo - Dec-11-2017, 09:13 PM
RE: Basic RPG - by Iskuss - Dec-12-2017, 06:00 AM
RE: Basic RPG - by mpd - Dec-12-2017, 12:50 PM
RE: Basic RPG - by Windspar - Dec-12-2017, 03:14 PM
RE: Basic RPG - by uribrasil - Apr-25-2020, 09:16 PM
RE: Basic RPG - by Larz60+ - Apr-25-2020, 09:19 PM
RE: Basic RPG - by uribrasil - Apr-25-2020, 09:21 PM
RE: Basic RPG - by Larz60+ - Apr-25-2020, 09:24 PM
RE: Basic RPG - by uribrasil - Apr-25-2020, 09:26 PM
RE: Basic RPG - by asiaphone12 - May-08-2020, 04:59 AM
RE: Basic RPG - by Larz60+ - May-08-2020, 08:39 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020