All Discussions > Steam Forums > Off Topic > Topic Details
Baggebo Sep 1, 2016 @ 2:04pm
Python Text based Adventure Game Help
So I'm trying to create a Text based adventure game but I somehow got confused on the way, there's a bit in the code where I want to create a combat system but somehow keep it seperate until I can Implement a storyline into the game so when the player encounters a enemy it goes it starts off the combat system code if it makes any sense xD can someone help?

If you can could you send me an edited version of this code where I can start to implement a storyline until the player encounters an enemy?

Here is the Code:

import sys
import os
import random

class Player:
def __init__(self, name):
self.name = name
self.maxhealth = 100
self.health = self.maxhealth
self.attack = 10
self.gold = 0
self.HPpots = 0

class Slime:
def __init__(self, name):
self.name = name
self.maxhealth = 30
self.health = self.maxhealth
self.attack = 5
self.goldvalue = 5
SlimeDQ = Slime("Slime")

class Skeleton:
def __init__(self, name):
self.name = name
self.maxhealth = 65
self.health = self.maxhealth
self.attack = 10
self.goldvalue = 10
SkeletonDQ = Skeleton("Skeleton")

def main():
os.system('cls')
print "Welcome To Dragon Quest!\n"
print "1|- Enter Dragon Quest!"
print "2|- Continue Your Previous Dragon Quest."
print "3|- Exit"
choice = raw_input("--> ")
if choice == "1":
start()
elif choice == "2":
pass
elif choice == "3":
sys.exit()
else:
main()

def start():
os.system('cls')
print "What is your name?"
choice = raw_input("Enter Your Name: ")
global PlayerDQ
PlayerDQ = Player(choice)
start1()

def start1():
os.system('cls')
print "Name: %s" %PlayerDQ.name
print "Attack: %i" %PlayerDQ.attack
print "Gold: %d" %PlayerDQ.gold
print "Health Potions: %d" %PlayerDQ.HPpots
print "Health: %i/%i"%(PlayerDQ.health, PlayerDQ.maxhealth)
print "1|- Fight"
print "2|- Item Shop"
print "3|- Save"
print "4|- Exit"
choice = raw_input("--> ")
if choice == "1":
PreFight()
elif choice == "2":
Store()
elif choice == "3":
pass
elif choice == "4":
sys.exit()
else:
start1()

def PreFight():
global enemy
enemynum = random.rand.int(1, 2)
if enemynum == 1:
enemy = SlimeDQ
else:
enemy = SkeletonDQ
Fight()

def Fight():
os.system('cls')
print "Player: %s vs %s"%(PlayerDQ.name, enemy.name)
print "Player Health: %d/%d %s Health: i%/%i"%(PlayerDQ.name, PlayerDQ.health, PlayerDQ.maxhealth, enemy.name, enemy.health, enemy.maxhealth)
print "Potions %i\n"%PlayerDQ.HPpots
print "1|- Attack"
print "2|- Use Health Potion"
print "3|- Escape"
choice = raw_input(' ')
if choice == '1':
attack()
elif choice == '2':
useHPpot()
elif choice == '3':
escape()
else:
fight()

def attack():
os.system('cls')
Pattack = random.randint(PlayerDQ.attack/2, PlayerDQ.attack)
Eattack = random.randint(enemy.attack/2, enemy.attack)
if Pattack == PlayerDQ.attack/2:
print "You miss the enemy!"
else:
enemy.health == Pattack
print "You deal %i damage to the enemy!" %Pattack
choice = rawinput(' ')
os.system('cls')

def useHPpot():
pass

def Store():
pass

def escape():
pass


main()
< >
Showing 1-3 of 3 comments
From what i understand you are trying to run from a seperate source when the player enters combat? I would use another python file and put all the combat code in there, then when you want to enter combat pass in the player and enemy as parameter into a function that is defined in the python file with your combat code. This way you only need to call one line from you main story line code and the combat code is hidden. Or did i misunderstand and this is not what you want to do?
The heading for the combat function might look like this:

def enterCombat(player,enemy):

Then all you have to do is write the rest of the functions for combat in that file, something like:

def attack(attacker,target):
target.health -= attacker.attack
print "%s lost %i health from %s , %s has %i health remaining" -->(target.name,attacker.attack,attacker.name,target.name,target.health)

*--> means auto wrap

There is most likely a few syntax errors as i have never done any python before and i made this short bit of code from looking at what you wrote and looking at the syntax and documentation online.

Edit: Lol --> makes a hyperlink on the steam client.
Last edited by Radical Left Lunatic; Sep 1, 2016 @ 2:42pm
Baggebo Sep 1, 2016 @ 3:39pm 
Originally posted by zoomdude111:
From what i understand you are trying to run from a seperate source when the player enters combat? I would use another python file and put all the combat code in there, then when you want to enter combat pass in the player and enemy as parameter into a function that is defined in the python file with your combat code. This way you only need to call one line from you main story line code and the combat code is hidden. Or did i misunderstand and this is not what you want to do?
The heading for the combat function might look like this:

def enterCombat(player,enemy):

Then all you have to do is write the rest of the functions for combat in that file, something like:

def attack(attacker,target):
target.health -= attacker.attack
print "%s lost %i health from %s , %s has %i health remaining" -->(target.name,attacker.attack,attacker.name,target.name,target.health)

*--> means auto wrap

There is most likely a few syntax errors as i have never done any python before and i made this short bit of code from looking at what you wrote and looking at the syntax and documentation online.

Edit: Lol --> makes a hyperlink on the steam client.
I suppose putting the combat system into another file would work and I can understand what your trying to say. I suppose each time I want an enemy encounter with the player I can kinda somehow switch to the combat system file and once it's done get it to switch back however how I would I get it to do that?
Originally posted by Ü DRAGON:
Originally posted by zoomdude111:
From what i understand you are trying to run from a seperate source when the player enters combat? I would use another python file and put all the combat code in there, then when you want to enter combat pass in the player and enemy as parameter into a function that is defined in the python file with your combat code. This way you only need to call one line from you main story line code and the combat code is hidden. Or did i misunderstand and this is not what you want to do?
The heading for the combat function might look like this:

def enterCombat(player,enemy):

Then all you have to do is write the rest of the functions for combat in that file, something like:

def attack(attacker,target):
target.health -= attacker.attack
print "%s lost %i health from %s , %s has %i health remaining" -->(target.name,attacker.attack,attacker.name,target.name,target.health)

*--> means auto wrap

There is most likely a few syntax errors as i have never done any python before and i made this short bit of code from looking at what you wrote and looking at the syntax and documentation online.

Edit: Lol --> makes a hyperlink on the steam client.
I suppose putting the combat system into another file would work and I can understand what your trying to say. I suppose each time I want an enemy encounter with the player I can kinda somehow switch to the combat system file and once it's done get it to switch back however how I would I get it to do that?

It would go back to the function you called it from once it was completed automatically, for example in main if i called

enterCombat(player,enemy)

after that function finished it would go back to main, i dont understand what you mean by "switching" between codes.Programming does not work that way, the python interpreter interprets each line of code in your program one line at a time, it does not matter where the line comes from as long as it has a valid line to interpret and quiting has not been requested it will continue to do so. Are you kind of new to programming?(not that there is anything wrong with that becuase we all got to start somewhere, it would just make sense that you would ask these kinds of questions if you where new to it).
< >
Showing 1-3 of 3 comments
Per page: 1530 50

All Discussions > Steam Forums > Off Topic > Topic Details
Date Posted: Sep 1, 2016 @ 2:04pm
Posts: 3