You are viewing our Forum Archives. To view or take place in current topics click here.
Python Text Based Game
Posted:

Python Text Based GamePosted:

MarKehh
  • TTG Master
Status: Offline
Joined: Dec 31, 200914Year Member
Posts: 892
Reputation Power: 38
Status: Offline
Joined: Dec 31, 200914Year Member
Posts: 892
Reputation Power: 38
Sup guys been awhile since I posted something on here, but here it goes. I've just started learning Python and have been working on this Text Based Game that I saw on youtube. It's a work in progress and loaded with some errors, but it's a great learning process for me. Currently you can fight, gain gold, and buy a single sword. I plan to expand on it and add more features as I continue to learn python such as the save and load function. It's really basic and I know some of it is broke, but again it's a work in progress.
Picture
[ Register or Signin to view external links. ]
EDITED 10-25-2016 with new code!
import sys
import os
import random
import math

###Fix the armor issue(its not setting max health above 100? maybe another if like levels?) Find out how to incorporate a level up system, add more levels below, dont thin the if's run well while in the class...
weapons = {"Great Sword":40,"God Sword":0}
armor = {"Knight Shield":30}
levels = {"1":0,"2":50,"3":100,"4":150,"5":200}

class Player:
    def __init__(self, name):
        self.name = name
        self.base_attack = 10
        self.gold = 100
        self.pots = 10
        self.weap = ["Rusty Sword"]
        self.curweap = "Rusty Sword"
        self.base_shield = ["Rusty Shield"]
        self.curarmor = "Rusty Shield"
        self.protection = 0
        self.xp = 0
        self.maxhealth = 100
        self.health = self.maxhealth
             
       
    @property
    def attack(self):
        attack = self.base_attack
        if self.curweap == "Rusty Sword":
            attack += 5
        if self.curweap == "Great Sword":
            attack += 15
        if self.curweap == "God Sword":
            attack += 100
           
        return attack
   

        if self.curarmor == "Rusty Shield":
            self.maxhealth = self.maxhealth + self.protection
       

class Goblin:
    def __init__(self, name):
        self.name = name
        self.maxhealth = 50
        self.health = self.maxhealth
        self.attack = 5
        self.goldgain = 10
        self.xpgain = 50
GoblinIG = Goblin("Goblin")

class Zombie:
    def __init__(self, name):
        self.name = name
        self.maxhealth = 70
        self.health = self.maxhealth
        self.attack = 5
        self.goldgain = 15
        self.xpgain = 50
ZombieIG = Zombie("Zombie")

       
def main():
    os.system("clear")
    print ("Welcome to the dungeon!")
    print ("1} - Start")
    print ("2} - Load")
    print ("3} - Exit")
   
    option = input("--> ")
    if option == "1":
        start()
    elif option == "2":
        load()
    elif option == "3":
        sys.exit()
    else:
        main()
       
def load():
    global PlayerIG
    print ("This will load stats from user.txt")
    option = input("Press enter to load...")
    ###Load the file
    with open("user.txt") as f:
        lines = [x.replace("\n","") for x in f.readlines()]
    ###Load saved player data
    PlayerIG = Player(lines[0])
    PlayerIG.name = lines[1]
    PlayerIG.health = int(lines[2])
    PlayerIG.base_attack = int(lines[3])
    PlayerIG.gold = int(lines[4])
    PlayerIG.pots = int(lines[5])
    PlayerIG.curweap = (lines[6])
    PlayerIG.xp = int((lines[7]))
    PlayerIG.level = (lines[8])
    PlayerIG.curarmor = (lines[9])

    start1()
   
def save():
    print ("Do you want to save and exit?")
    option = input("1. Yes\n2. No")
    if option == "1":
    ###Open the user.txt file
        with open("user.txt","w") as f:
            ###Set necessary vars as strings
            shealth = str(PlayerIG.health)
            sgold = str(PlayerIG.gold)
            sattack = str(PlayerIG.base_attack)
            spots = str(PlayerIG.pots)
            sxp = str(PlayerIG.xp)
            slevel = str(PlayerIG.level)
            ###Write the stats to user.txt
            f.write(str(PlayerIG) + "\n")
            f.write(str(PlayerIG.name) + "\n")
            f.write(shealth + "\n")
            f.write(sattack + "\n")
            f.write(sgold + "\n")
            f.write(spots + "\n")
            f.write(str(PlayerIG.curweap) + "\n")
            f.write(sxp + "\n")
            f.write(slevel + "\n")
            f.write(PlayerIG.curarmor)
        print ("Your stats have been saved!")
        main()
    else:
        print ("No")
        start1()
   
def start():
    os.system("clear")
    print ("Hello! What is your name?")
    option = input("--> ")
    global PlayerIG
    PlayerIG = Player(option)
    start1()
   
def start1():
    os.system("clear")
   
    if PlayerIG.xp in range(0,49):
        PlayerIG.level = "1"
    if PlayerIG.xp in range(50,99):
        PlayerIG.level = "2"
        levelup()
        print ("You leveled up here is 50 gold!")
    if PlayerIG.xp in range(100,149):
        PlayerIG.level = "3"
    if PlayerIG.xp in range(150,149):
        PlayerIG.level = "4"
    if PlayerIG.xp in range(200,350):
        PlayerIG.level = "5"
    if PlayerIG.curarmor == "Rusty Shield":
        PlayerIG.protection = "10"
    if PlayerIG.curarmor == "Knight Shield":
        PlayerIG.protection = "20"
    print ("Hello: %s" % PlayerIG.name)
    print ("Level: %d with %d xp!" % (int(PlayerIG.level), int(PlayerIG.xp)))
    print ("Attack: %i" % PlayerIG.attack)
    print ("Gold: %d" % PlayerIG.gold)
    print ("Current Weapons: %s" % PlayerIG.curweap)
    print ("Current Armor: %s" % PlayerIG.curarmor)
    print ("Protection: %d" % int(PlayerIG.protection))
    print ("Potions: %d" % PlayerIG.pots)
    print ("Health: %i/%i" % (PlayerIG.health, PlayerIG.maxhealth))
    print ("1. Fight")
    print ("2. Store")
    print ("3. Save")
    print ("4. Exit")
    option = input("-->")
    if option == "1":
        prefight()
    elif option == "2":
        store()
    elif option == "3":
        save()
    elif option == "4":
        sys.exit()
    else:
        start1()
       
def prefight():
    global enemy
    enemynum = random.randint(1, 2)
    if enemynum == "1":
        enemy = GoblinIG
    else:
        enemy = ZombieIG
    fight()

def fight():
    os.system("clear")
    print ("%s  vs  %s" % (PlayerIG.name, enemy.name))
    print ("%s's Health: %d/%d  %s's Health: %i/%i" % (PlayerIG.name, PlayerIG.health, PlayerIG.maxhealth, enemy.name, enemy.health, enemy.maxhealth))
    print ("Potions %i\n" % PlayerIG.pots)
    print ("Protection: %d" % int(PlayerIG.protection))
    print ("1. Attack")
    print ("2. Drink Potion")
    print ("3. Run")
    option = input("")
    if option == "1":
        attack()
    elif option == "2":
        drinkpot()
    elif option == "3":
        run()
    else:
        fight()
       
def attack():
    os.system("clear")
    PAttack = random.randint(math.floor(PlayerIG.attack / 2), PlayerIG.attack)
    EAttack = random.randint(math.floor(enemy.attack / 2), enemy.attack)
    if PAttack == PlayerIG.attack / 2:
        print ("You missed!")
    else:
        enemy.health -= PAttack
        print ("You deal %i damage" % PAttack)
    option = input("")
    if enemy.health <=0:
        win()
    os.system("clear")
    if EAttack == enemy.attack/2:
        print ("The enemy missed!")
    else:
        PlayerIG.health -= EAttack
        print ("The enemy deals %i damage" % EAttack)
    option = input("")
    if PlayerIG.health <= 0:
        dead()
    else:
        fight()

def drinkpot():
    os.system("clear")
    if PlayerIG.pots == 0:
        print ("You dont have any potions")
    else:
        PlayerIG.health += 50
        PlayerIG.pots -= 1
        if PlayerIG.health > PlayerIG.maxhealth:
            PlayerIG.health = PlayerIG.maxhealth
        print ("You drank a potion")
        option = input("")
        fight()
       
       
def run():
    os.system("clear")
    runnum = int(random.randint(1, 3))
    if runnum == 1:
        print ("You got away")
        option = input("-> ")
        enemy.health = enemy.maxhealth
        start1()
    else:
        print ("You failed to get away!")
        option = input("-> ")
        os.system("clear")
        EAttack = enemy.attack
        if EAttack == enemy.attack/2:
            print ("The enemy missed!")
        else:
            PlayerIG.health -= EAttack
            print ("The enemy deals %i damage" % EAttack)
    option = input("")
    if PlayerIG.health <= 0:
        dead()
    else:
        fight()
   
def win():
    PlayerIG.gold += enemy.goldgain
    PlayerIG.xp += int(enemy.xpgain)
    enemy.health = enemy.maxhealth
    pot_chance = random.randint(1,3)
    if pot_chance > 1:
        PlayerIG.pots += 1
        print ("Ooo you found a potion!\nYou now have %i potions!" % PlayerIG.pots)
    print ("You have defated the %s" % enemy.name)
    print ("You received %i gold" % enemy.goldgain)
    option = input("")
    start1()
   
def dead():
    enemy.health = enemy.maxhealth
    print ("You have died")
    option = input("")
    main()
   
   
def store():
    os.system("clear")
    print ("Welcome to the shop!")
    print ("\nWhat would you like to buy?\n")
    print ("1. Great Sword")
    print ("2. Knight Shield")
    print ("Back")
    print ("")
    option = input("")
   
    if option in weapons:
        if PlayerIG.gold >= weapons[option]:
            os.system("clear")
            PlayerIG.gold -= weapons[option]
            PlayerIG.weap.append(option)
            PlayerIG.curweap = option
            print ("You have bought %s" % option)
            option = input("")
            store()
    if option in armor:
        if PlayerIG.gold >= armor[option]:
            os.system("clear")
            PlayerIG.gold -= armor[option]
            PlayerIG.base_shield.append(option)
            PlayerIG.curarmor = option
            print ("You have bought %s" % option)
            option = input("")
            store()
           
        else:
            os.system("clear")
            print ("You don't have enough gold!")
            option = input("")
            store()
           
    elif option == ("Back"):
        start1()
    else:
        os.system("clear")
        print ("That item does not exist")
        option = input("")
        store()

def levelup():
    PlayerIG.gold += 50

   
main()



Last edited by MarKehh ; edited 1 time in total
#2. Posted:
-Deano
  • Rated Awesome
Status: Offline
Joined: Aug 19, 201014Year Member
Posts: 5,243
Reputation Power: 532
Status: Offline
Joined: Aug 19, 201014Year Member
Posts: 5,243
Reputation Power: 532
Quite a cool game. I remember making one as my first assignment at university.
I had added additional options such as "Light Attack, Heavy Attack, Counter Attack", "Use Powerup", etc. which would be cool to introduce to your game.
#3. Posted:
MarKehh
  • TTG Master
Status: Offline
Joined: Dec 31, 200914Year Member
Posts: 892
Reputation Power: 38
Status: Offline
Joined: Dec 31, 200914Year Member
Posts: 892
Reputation Power: 38
-Deano wrote Quite a cool game. I remember making one as my first assignment at university.
I had added additional options such as "Light Attack, Heavy Attack, Counter Attack", "Use Powerup", etc. which would be cool to introduce to your game.

Thanks, i do plan on adding more weapons and even armor that the user can buy to have more defense. I myself am starting a CS degree at my University in a week.
#4. Posted:
FOV
  • Summer 2018
Status: Offline
Joined: Aug 31, 201113Year Member
Posts: 1,883
Reputation Power: 8098
Motto: The extent of the observable world that is seen at any given moment.
Motto: The extent of the observable world that is seen at any given moment.
Status: Offline
Joined: Aug 31, 201113Year Member
Posts: 1,883
Reputation Power: 8098
Motto: The extent of the observable world that is seen at any given moment.
May I ask where you found this tutorial? I'm a senior in high school taking an advanced placement college computer science class and pretty soon we're going to be assigned a project where we need to code something, and that game looks like something I'd be interested in making.
#5. Posted:
MarKehh
  • TTG Master
Status: Offline
Joined: Dec 31, 200914Year Member
Posts: 892
Reputation Power: 38
Status: Offline
Joined: Dec 31, 200914Year Member
Posts: 892
Reputation Power: 38
Just saw this now, I am sending you a pm.
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.