Hello guest, if you read this it means you are not registered. Click here to register in a few simple steps, you will enjoy all features of our Forum.
Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Basic RPG
#1
This is a basic RPG where you fight Goblins and Giant Rats until you die. I will make this a part of a larger more interesting game in the future maybe. The stat calculations are unbalanced, and it's really unpolished, but it's basically just my way of trying to teach myself Python.

import random
import sys

#Enemies
goblin = {}
goblin['Name'] = 'Goblin'
goblin['Health'] = 50.0
goblin['Agility'] = 40.0
goblin['Strength'] = 35.0
goblin['Level'] = 1

giant_rat = {}
giant_rat['Name'] = 'Giant Rat'
giant_rat['Health'] = 40.0
giant_rat['Agility'] = 55.0
giant_rat['Strength'] = 30.0
giant_rat['Level'] = 1

def Death():
    print("You have died!")
    print("GAME OVER")
    exit_game = raw_input()
    sys.exit()	

def Fight(enemy):
    print("All of a sudden a " + str(enemy['Name']) + " attacks you!")
    Enemy = enemy.copy()
    while Enemy['Health'] > 0:
        Attack = False
        A = raw_input("Type A to attack the " + str(Enemy['Name']) + ". ")
        if A == "Attack" or "attack" or "A" or "a":
            Attack = True
        else:
            pass
        if Attack == True:
            print("You swing at the " + str(Enemy['Name']) + " with your sword!...")
            Player_attack_lands = False
            Enemy_attack_lands = False
            if (Enemy['Agility'] - player['Agility'] + (random.random() - random.random()) * 100) <= 0:
                Player_attack_lands = True
            else:
                pass
            if Player_attack_lands == True:
                print("You strike the " + str(Enemy['Name']) + "!")
                Enemy['Health'] = Enemy['Health'] - player['Strength']
                print("You've done " + str(player['Strength']) +" damage! ")
                print("The " + str(Enemy['Name']) + " now has " + str(Enemy['Health']) + " left. ")
                if Enemy['Health'] <= 0:
                    pass
                else:
                    print("The " + str(Enemy['Name']) + " launches an attack at you! ")
                    if (Enemy['Agility'] - player['Agility'] + (random.random() - random.random()) * 100) <= 0:
                        Enemy_attack_lands = True
                    else:
                        print("The " + str(Enemy['Name']) + " missed! ")
                    if Enemy_attack_lands == True:
                        print("The " + str(Enemy['Name']) + " lands its attack! ")
                        player['Health'] = player['Health'] - Enemy['Strength']
                        print("The " + str(Enemy['Name']) + " deals " + str(Enemy['Strength']) + " damage to you. ")
                        print("You have " + str(player['Health']) + " Health left. ")
                        if player['Health'] <= 0:
                            Death()
                        else:
                            pass
                    else:
                        pass      
            else:
                print("You miss!")
                print("The " + str(Enemy['Name']) + " launches an attack at you! ")
                if (Enemy['Agility'] - player['Agility'] + (random.random() - random.random()) * 100) <= 0:
                    Enemy_attack_lands = True
                else:
                    print("The " + str(Enemy['Name']) + " missed! ")
                if Enemy_attack_lands == True:
                    print("The " + str(Enemy['Name']) + " lands its attack! ")
                    player['Health'] = player['Health'] - Enemy['Strength']
                    print("The " + str(Enemy['Name']) + " deals " + str(Enemy['Strength']) + " damage to you. ")
                    print("You have " + str(player['Health']) + " Health left. ")
                    if player['Health'] <= 0:
                        Death()
                    else:
                        pass
    print("You've killed the " + str(enemy['Name']) + "!")

#Character Creaion
player={}
player['Name'] = raw_input("Please enter your character's name. ")
player['Sex'] = raw_input("Please enter your character's sex. ")
player['Experience'] = 0
player['Level'] = 1
if player['Sex'] == 'female' or 'f' or 'F':
    player['Sex'] = 'Female'
elif player['Sex'] == 'male' or 'm' or 'M':
    player['Sex'] = 'Male'
else:
    pass
if player['Sex'] != 'Female' and player['Sex'] != 'Male':
    player['Sex'] = raw_input("Please enter your character's sex again. Please enter Male or Female. ")
else:
    pass
player['Strength'] = (random.random() * 100)
player['Agility'] = ((random.random() * 100) + 70) / 2
player['Health'] = (random.random() * 100 + 100)
#End Character Creation

while player['Health'] > 0:
    Fight(goblin)
    print("")
    Fight(giant_rat)
    print("")
Quote
#2
You scared me a bit. RPG was an old IBM language. I never liked it, and I'm thinking OH NO,
someones writing RPG in Python!

Thought I'd share
Quote
#3
Added Level up, Rest, Explore, Stats, and ability to save and load progress even after closing program.

import random
import sys

player = {} #Makes player{} dictionary global

#Enemies
goblin = {}
goblin['Name'] = 'Goblin'
goblin['Health'] = 50
goblin['Agility'] = 40
goblin['Strength'] = 25
goblin['Level'] = 1

giant_rat = {}
giant_rat['Name'] = 'Giant Rat'
giant_rat['Health'] = 40
giant_rat['Agility'] = 55
giant_rat['Strength'] = 20
giant_rat['Level'] = 1

feral_dog = {}
feral_dog['Name'] = 'Feral Dog'
feral_dog['Health'] = 50
feral_dog['Agility'] = 45
feral_dog['Strength'] = 20
feral_dog['Level'] = 1

def Death():
    print("You have died!")
    print("GAME OVER")
    player = {}
    exit_game = raw_input()
    Main() #Restarts game

def Stats():
    print("Current Stats:")
    print("Current Health: " + str(player['Health']))
    print("Max Health: " + str(player['Healthmax']))
    print("Agility: " + str(player['Agility']))
    print("Strength: " + str(player['Strength']))
    return("Level: " + str(player['Level']))

def Fight(enemy):
    print(" ")
    print("All of a sudden a " + str(enemy['Name']) + " attacks you!")
    Enemy = enemy.copy() #Copies enemy dictionary so the original doesn't get overwritten
    while Enemy['Health'] > 0:
        A = raw_input("Type A to attack the " + str(Enemy['Name']) + ". ") #Does player want to attack?
        if A in ["Attack", "attack", "A", "a"]:
            print("")
            print("You swing at the " + str(Enemy['Name']) + " with your sword!...")
            if (Enemy['Agility'] - player['Agility'] + (random.random() - random.random()) * 100) <= 0: #Do this if player hits enemy
                Enemy['Health'] = Enemy['Health'] - player['Strength'] #Enemy is hit and its health lowers
                print("")
                print("You strike the " + str(Enemy['Name']) + " and do " + str(player['Strength']) + " damage!")
                if Enemy['Health'] > 0: #Do this if enemy is still alive
                    print("The " + str(Enemy['Name']) + " now has " + str(Enemy['Health']) + " health left.")
                    print("")
                    print("The " + str(Enemy['Name']) + " launches an attack at you! ")
                    if (Enemy['Agility'] - player['Agility'] + (random.random() - random.random()) * 100) <= 0: #Enemy attacks player. Does the attack land?
                        print("The " + str(Enemy['Name']) + " lands its attack! ")
                        player['Health'] = player['Health'] - Enemy['Strength'] #Player is hit and his health lowers
                        print("")
                        print("The " + str(Enemy['Name']) + " deals " + str(Enemy['Strength']) + " damage to you. ")
                        if player['Health'] > 0:
                            print("You have " + str(player['Health']) + " Health left. ")
                        elif player['Health'] <= 0: #Player dies if he has 0 or less health
                            Death() #Calls Death() function
                    else:
                        print("The " + str(Enemy['Name']) + " missed! ")
                        print(" ")
            else: #Player misses the enemy
                print("")
                print("You miss!")
                print("The " + str(Enemy['Name']) + " launches an attack at you! ") #Enemy attack player
                if (Enemy['Agility'] - player['Agility'] + (random.random() - random.random()) * 100) <= 0: #Do this if enemy hits player?
                    print("The " + str(Enemy['Name']) + " lands its attack! ")
                    player['Health'] = player['Health'] - Enemy['Strength'] #Player is hit and his health lowers
                    print("The " + str(Enemy['Name']) + " deals " + str(Enemy['Strength']) + " damage to you. ")
                    if player['Health'] > 0:
                        print("You have " + str(player['Health']) + " Health left. ")
                        print(" ")
                    elif player['Health'] <= 0:
                        Death() #Calls Death() function
                else:
                    print("The " + str(Enemy['Name']) + " missed! ") #Enemy misses player
    print("")
    print("You've killed the " + str(enemy['Name']) + "!") #Enemy is killed
    player['Experience'] += Enemy['Level'] #Player gains experience
    print("You have gained " + str(Enemy['Level']) + " experience points.")
    print("Experience: " + str(player['Experience']) + "/" + str(player['Level'] + 9))
    if player['Experience'] >= player['Level'] + 9: #If player has enough xp to levels up
        Levelup() #Calls Levelup() function
        
def Levelup():
    player['Level'] += 1 #Increases player's level
    player['Experience'] = 0 #Resets player's xp to 0
    print("You have leveled up!")
    print("")
    raw_input("Press enter to continue. ")
    print("You are now level " + str(player['Level']) + "!")
    print("You have 5 points to spend on your Max Health, Agility, and Strength.")
    print("Your Max Health determines how much damage you can take without dying.")
    print("Your Agility determines how easily you hit your enemies and evade their attacks.")
    print("Your Strength determines how much damage your attacks do.")
    print(" ")
    print(Stats()) #Show player his current stats
    print("")
    raise_healthmax = 0
    raise_agility = 0
    raise_strength = 0 #Declares variables and allows while loop to start
    n = 0
    while raise_healthmax + raise_agility + raise_strength != 5: #Sum of stat points can't equal 5 for loop to start. Will cycle until sum equals 5 though.
        n += 1
        if n >1: #So that player sees this message if this loops more than once
            print("You have to spend exactly 5 points on your stats.")
        raise_healthmax = input("Max Health: ")
        while raise_healthmax not in [0, 1, 2, 3, 4, 5]: #If player enters an invalid number, have them try again.
            print("You must enter 0, 1, 2, 3, 4, or 5. Try again.")
            print("")
            raise_healthmax = input("Max Health: ")
            print(" ")
        raise_agility = input("Agility: ")
        while raise_agility not in [0, 1, 2, 3, 4, 5]: #If player enters an invalid number, have them try again.
            print("You must enter 0, 1, 2, 3, 4, or 5. Try again.")
            print("")
            raise_agility = input("Agility: ")
            print(" ")
        raise_strength = input("Strength: ")
        while raise_strength not in [0, 1, 2, 3, 4, 5]: #If player enters an invalid number, have them try again.
                print("You must enter 0, 1, 2, 3, 4, or 5. Try again.")
                print("")
                raise_strength = input("Strength: ")
                print(" ")
    player['Healthmax'] += raise_healthmax #Raise stats by player's desired amount
    player['Agility'] += raise_agility
    player['Strength'] += raise_strength
    print(Stats()) #Show player his new stats
    print(" ")
    
def Character_creation():
    player['Name'] = raw_input("Please enter your character's name. ")
    player['Sex'] = raw_input("Please enter your character's sex. ")
    player['Experience'] = 0
    player['Level'] = 1
    if player['Sex'] in ["female", "f", "F"]:
        player['Sex'] = 'Female'
    elif player['Sex'] in ["male", "m", "M"]:
        player['Sex'] = 'Male'
    if player['Sex'] != 'Female' and player['Sex'] != 'Male':
        player['Sex'] = raw_input("Please enter your character's sex again. Please enter Male or Female. ")
    player['Strength'] = int(round(((random.random() * 100 + 100) / 3))) #Sets stats
    player['Agility'] = int(round(((random.random() * 100 + 100) / 3)))
    player['Health'] = int(round(((random.random() * 100 + 150) / 2)))
    player['Healthmax'] = player['Health']

def Chapter_01(): #Story
    print("You find yourself in a tavern. Around you, people are drinking and conversing with eachother. You sit by the fireplace, which is roaring with heat.")
    print("There are three people next to you that you can talk to: an elderly lady, a young bearded man , and a young blond haired man. Both of the men appear to be in their early twenties.")
    def dialogue_01_function():
        dialogue_01 = raw_input("Type 1 to talk to the lady, 2 to talk to the bearded man, and 3 to talk to the blonde man. ")
        if dialogue_01 == '1':
            print("one")
        elif dialogue_01 == '2':
            print("two")
        elif dialogue_01 == '3':
            print("three")
        else:
            dialogue_01_function()
    dialogue_01_function()

def Save(file_name):
    save = open(str(file_name) + ".txt", "w+")
    save.write(str(player['Name']) + "\n")
    save.write(str(player['Sex']) + "\n")
    save.write(str(player['Health']) + "\n")
    save.write(str(player['Healthmax']) + "\n")
    save.write(str(player['Agility']) + "\n")
    save.write(str(player['Strength']) + "\n")
    save.write(str(player['Level']) + "\n")
    save.write(str(player['Experience']) + "\n")
    save.close()

def Load(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)
        
        sex = contents[1]
        list_sex = list(sex)
        del list_sex[-1]
        joined_sex = ''.join(list_sex)
        
        health = contents[2]
        list_health = list(health)
        del list_health[-1]
        joined_health = ''.join(list_health)
        int_health = int(joined_health)
        
        healthmax = contents[3]
        list_healthmax = list(healthmax)
        del list_healthmax[-1]
        joined_healthmax = ''.join(healthmax)
        int_healthmax = int(joined_healthmax)
        
        agility = contents[4]
        list_agility = list(agility)
        del list_agility[-1]
        joined_agility = ''.join(list_agility)
        int_agility = int(joined_agility)
        
        strength = contents[5]
        list_strength = list(strength)
        del list_strength[-1]
        joined_strength = ''.join(list_strength)
        int_strength = int(joined_strength)
        
        level = contents[6]
        list_level = list(level)
        del list_level[-1]
        joined_level = ''.join(list_level)
        int_level = int(joined_level)
         
        experience = contents[7]
        list_experience = list(experience)
        del list_experience[-1]
        joined_experience = ''.join(list_experience)
        int_experience = int(joined_experience)

        player['Name'] = joined_name
        player['Sex'] = joined_sex
        player['Health'] = int_health
        player['Healthmax'] = int_healthmax
        player['Agility'] = int_agility
        player['Strength'] = int_strength
        player['Level'] = int_level
        player['Experience'] = int_experience
    
def Rest():
    player['Health'] = player['Healthmax']
    print("You have rested and your health has been restored.")

def Main():
    Character_creation()
    n = 0
    while player['Health'] > 0:
        n += 1
        if n == 1:
            print("Commands: Help, Rest, Explore, Stats, Save, Load")
        menu = raw_input("> ")
        if menu in ["Help", "help"]:
            print("Commands: Help, Rest, Explore, Stats, Save, Load")
        elif menu in ["Rest", "rest"]:
            Rest()
        elif menu in ["Stats", "stats"]:
            print(Stats())
        elif menu in ["Save", "save"]:
            file_name = raw_input("Name your save file. ")
            Save(file_name)
        elif menu in ["Load", "load"]:
            file_name = raw_input("What is the name of your save file? Don't include .txt ")
            Load(file_name)
        elif menu in ["Explore", "explore"]:
            shuffle = random.random() * 10 #Makes chance to fight all 3 enemies 1/3
            if shuffle <= 3:
                Fight(goblin)
            elif shuffle >= 7:
                Fight(giant_rat)
            else:
                Fight(feral_dog)
Main()
Quote
#4
Quote:
if menu in ["Help", "help"]:

That can be a little clearer, if you make menu always lowercase.  That way, the player can also do something wacky like "hELp" and it'll still work:
if menu.lower() == "help":
It's dangerous to go alone!  Take this: https://docs.python.org/3/library/index.html
Kevin Flynn is my spirit animal.
Quote
#5
Thanks, and I might end up doing:
if menu.lower() in ["help", "h"]:
And allow 1 letter abbreviations for all commands for ease of use.
Quote

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)