Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
December 24th, 2012, 12:51 AM
Registered User

Join Date: Aug 2008
Posts: 6
Time spent in forums: 2 h 14 m 30 sec
Reputation Power: 0

Hello all,

I'm writing a math-based adventure game in the spirit of text-based adventure games from back in the day. So far I have things working pretty well. I'm planning on adding more randomness to the code, but this is just my prototype. I could use some advice on how best to make my code more elegant if anybody can offer suggestions.

I'm considering putting in a definition that asks the player if they want to play again when they die and it would reset the game.

edit:
This was written is Python 2.7.3

Code:
```#these are the required libraries to run the game.
import random
import sys

#These are character matrices. Currently unused.
#hero = [6,4,1]
#orc = [8,1,2]
#troll = [9,1,2]
#boss = [9,1,3]

#These are Global Variables
#p1,p2 are the parts of the randomly generated problem
p1=0
p2=0

attempt = 0
end = True
total = 0
#Score is currently unused. Score is an array that tracks score score = [easy,medium,hard] questions respectively.
score = [0,0,0]

#intro, rising and climax are the text prompts. I made them definitions so I can change them easily.
def intro():
print "The Black Knight of Kondor has kidnapped the Prince. Snatched from his cradle in the night! The king has offered a handsome reward for defeating the Black Knight and returning the babe safely to him. You set off and approach the entrance of the cave, but an orc crosses your path! He lunges at you, prepare for battle!"
print

def rising():
print "You enter the cave and see the armor of fallen knights who failed the challenge ahead. You can see there is a riddle on the wall. It reads "
print

def climax():
print "You solved the riddle and come face to face with the dark knight, defeat him!"
print

#randomint provides random numbers for easy, med and hard problems.
def randomint(a,b):
return random.randint(a,b)

print p1, ' + ', p2, ' = ?'
print

def easy():
global p1,p2
p1,p2 = randomint(1,4) , randomint(1,6)

def med():
global p1,p2
p1,p2 = randomint(1,4) , randomint(5,9)

#this is special for the riddle
def medriddle():
global p1,p2
p1,p2 = randomint(2,4) , randomint(5,10)

def hard():
global p1,p2
p1,p2 = randomint(14,19) , randomint(3,9)

#score tracking
def easypnt():
global score
score[0] = score[0] + 1

def medpnt():
global score
score[1] = score[1] + 1

def hardpnt():
global score
score[2] = score[2] + 1

def showscore():
print score[0], "easy points."
print score[1], "medium points."
print score[2], "hard points."

#these are the various stages of battle with the orc.
def orc1():
print "You successfully dodge the orc's attack. Counter!"
easy()
print
battlecheck(p1+p2)
easypnt()

def orc2():
print 'He swings at you again, but you side step the club.'
easy()
print
battlecheck(p1+p2)
easypnt()

def orc3():
print "He's getting tired and starting to stumble."
med()
print
battlecheck(p1+p2)
medpnt()

def orc4():
print "Finish him off!!!"
hard()
print
battlecheck(p1+p2)
hardpnt()

#these are the various stages of battle with the boss
def boss1():
print "You successfully dodge the black knight's attack. Counter!"
med()
print
battlecheck(p1+p2)
medpnt()

def boss2():
print 'He swings at you again, but you side step the giant axe.'
med()
print
battlecheck(p1+p2)
medpnt()

def boss3():
print "He's getting tired and starting to stumble."
hard()
print
battlecheck(p1+p2)
hardpnt()

def boss4():
print "Finish him off!!!"
hard()
print
battlecheck(p1+p2)
hardpnt()

#these checks determine whether the player successfully answered the questions
def battlecheck(ans):
if ans == attempt:
print "Hit!"
print
else:
print 'You missed! He counters and you fall to the ground. Who will save the prince?'
showscore()
raw_input ('Press Enter to quit')
sys.exit()

def riddlecheck(ans):
if ans == attempt:
print "You got it! The door slides open to reveal the  boss' chamber!"
medpnt()
print
else:
print 'You chose incorrectly! Arrows are launched as you run back to town.'
showscore()
raw_input ('Press Enter to quit.')
sys.exit()

#this is the battle with the first enemy
def orcbattle():
orc1()
orc2()
orc3()
orc4()
print

#this is the second challenge.
def riddle():
rising()
medriddle()
print "You went hunting with", p2, "arrows. You shot", p1, "and broke them. How many arrows do you have left?"
print
riddlecheck(p2-p1)

#this is the boss
def boss():
climax()
boss1()
boss2()
boss3()
boss4()
print "You make your way to the crib behind the defeated knight's throne only to find a note saying, 'The Prince is in another cave.'"
raw_input ('')
print
print "You find the next cave and approach it slowly and another orc jumps from the bushes! He lunges at you, prepare for battle!"

#this is the main while loop
intro()
while end == True:
orcbattle()
riddle()
boss()```

Last edited by jadedrakerider : December 24th, 2012 at 08:53 AM. Reason: Code accidentally Deleted

#2
December 24th, 2012, 05:39 AM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,157
Time spent in forums: 1 Month 3 Weeks 2 Days 8 h 41 m 50 sec
Reputation Power: 455
It's fun! Good game.

I didn't observe any errors for valid input. Considering only the play, and since this is python version 2, I recommend against the input function because it evaluates expressions. In other words, I can cut and paste the question. Not the word problem, of course.

Finish him off!!!
17 + 5 = ?

Hit!

Invalid expressions also raise an exception. I'd read from stdin and validate the string probably with try: except: statement.

I could not duplicate sys.exit() errors.
__________________
[code]Code tags[/code] are essential for python code!

#3
December 24th, 2012, 08:58 AM
Registered User

Join Date: Aug 2008
Posts: 6
Time spent in forums: 2 h 14 m 30 sec
Reputation Power: 0
Quote:
 Originally Posted by b49P23TIvg I could not duplicate sys.exit() errors.

You're right, that wasn't an actual error. It was just presented similarly to one in IDLE.

Thanks for the tip about input. I'm going to try to fix that.

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Help ending a without sys.exit()