Dragon Realm (text-based game)

This is the place for queries that don't fit in any of the other categories.

Dragon Realm (text-based game)

Postby dazuza360 » Wed May 15, 2013 12:04 am

Hey forum,
I just started Python Programming as a hobby recently. I was learning how to make a text-based game called Dragon Realm.

Basically it is a game where there are 2 caves, one with a friendly dragon that gives you treasure, and another cave with a hungry dragon that eats you. The game allows the player to choose a cave and randomly decides whether the player gets the treasure or gets eaten.

Here's the source code:

Code: Select all
import random
import time

def displayIntro():
    print('You are in a land full of dragons. In front of you,')
    print('you see two caves. In one cave, the dragon is friendly')
    print('and will share his treasure with you. The other dragon')
    print('is greedy and hungry, and will eat you on sight.')
    print()

def chooseCave():
    cave = ''
    while cave != '1' and cave != '2':
        print('Which cave will you go into? (1 or 2)')
        cave = input()

    return cave

def checkCave(chosenCave):
    print('You approach the cave...')
    time.sleep(2)
    print('It is dark and spooky...')
    time.sleep(2)
    print('A large dragon jumps out in front of you! He opens his jaws and...')
    print()
    time.sleep(2)

    friendlyCave = random.randint(1, 2)

    if chosenCave == str(friendlyCave):
         print('Gives you his treasure!')
    else:
         print('Gobbles you down in one bite!')

playAgain = 'yes'
while playAgain == 'yes' or playAgain == 'y':

    displayIntro()

    caveNumber = chooseCave()

    checkCave(caveNumber)

    print('Do you want to play again? (yes or no)')
    playAgain = input()


So i've followed along with this and now I want to add more to this game by adding more options for the player, like more caves for example.
How would I be able to do this?

If you could show me the code to answer my question or show me a good tutorial that would be most helpful.
Thanks in advance! :D
dazuza360
 
Posts: 5
Joined: Tue May 14, 2013 10:31 am

Re: Dragon Realm (text-based game)

Postby NotAnotherNeo » Wed May 15, 2013 11:59 am

Don't be vague. If someone is going to help you, they want to know what the exact problem is.

If there isn't a problem, then they're probably not going to help you.

sorry bro, just read the code and think about how to add more caves, this one is really simple.
NotAnotherNeo
 
Posts: 5
Joined: Sun May 12, 2013 4:04 am

Re: Dragon Realm (text-based game)

Postby Kebap » Wed May 15, 2013 12:53 pm

I could have sworn I had seen this thread before, with already some answers suggesting maybe using a list for more than 2 rooms.
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 282
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: Dragon Realm (text-based game)

Postby Mekire » Wed May 15, 2013 2:36 pm

Kebap wrote:I could have sworn I had seen this thread before, with already some answers suggesting maybe using a list for more than 2 rooms.
He originally posted this in the Game Development section. I told him that it would probably be more suitable here as it wasn't really a game development question but rather a python basics question. I also suggested a couple things and essentially told him he needed to experiment and post back after he tried a few things.

@op: this is still my suggestion. Try to add more accepted user inputs; try adding a wider range of random numbers to generate. Make a different room appear on a new random number. Experiment. Once you can't figure out why the approach you are taking isn't working, THEN post back and include what you have tried.

This will be a much more productive experience for all involved parties.

-Mek
User avatar
Mekire
 
Posts: 809
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Dragon Realm (text-based game)

Postby metulburr » Wed May 15, 2013 4:28 pm

I could have sworn I had seen this thread before, with already some answers suggesting maybe using a list for more than 2 rooms.


That was my fault. I went to move his thread and made a shadow thread. Deleted the shadow thread, and it deleted both the original thread and the shadow thread. I messaged the OP and told him to go ahead and repost
New Users, Read This
version Python 3.3.2 and 2.7.5, tkinter 8.5, pyqt 4.8.4, pygame 1.9.2 pre
OS Ubuntu 13.04, Mint 11, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
User avatar
metulburr
 
Posts: 1088
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Dragon Realm (text-based game)

Postby dazuza360 » Thu May 16, 2013 12:49 am

Hey guys this is what I came up with that seemed to achieve what I wanted. It was a bit long-winded and ugly, but I think it still works out sorta.

Code: Select all
#Dragon Realm Game
import random
import time

def displayIntro():
    print('You are on a planet full of dragons. In front of you, you see two caves.')
    time.sleep(2)
    print('In one cave, the dragon is friendly and will share his treasure with you.')
    time.sleep(2)
    print('The other dragon is greedy and hungry, and will eat you on sight!')
    time.sleep(2)
   
def chooseCave():
    cave = ''
    while cave != '1' and cave != '2' and cave !='3':
        print('Which cave will you enter into?(1, 2 or 3)')
        cave = input()

    return cave

def checkCave(chosenCave):
    def firstCave():
        if randCave == 1:
            print('The dragon greets you and gives you his treasure!')
        elif randCave == 2:
            print('The dragon crushes you with it\'s powerful jaws and eats you!')
        else:
            print('You realise the cave is empty. Your mind is playing tricks on you ...')

    def secondCave():
        if randCave == 1:
            print('The dragon greets you and gives you his treasure!')
        elif randCave == 2:
            print('The dragon crushes you with it\'s powerful jaws and eats you!')
        else:
            print('You realise the cave is empty. Your mind is playing tricks on you ...')

    def thirdCave():
        if randCave == 1:
            print('The dragon greets you and gives you his treasure!')
        elif randCave == 2:
            print('The dragon crushes you with it\'s powerful jaws and eats you!')
        else:
            print('You realise the cave is empty. Your mind is playing tricks on you ...')

    print('You approach the cave...')
    time.sleep(2)
    print('It is dark and spooky...')
    time.sleep(2)
    print('You notice a huge shadow looms over you...')
    time.sleep(3)
    print('A large dragon appears in front of you! He opens his jaws and...')
    time.sleep(4)
    print()

    randCave = random.randint(1,3)
   
    if chosenCave == 1:
        firstCave()
    elif chosenCave == 2:
        secondCave()
    else:
        thirdCave()
   
playAgain = 'yes'
while playAgain == 'yes' or playAgain == 'YES' or playAgain == 'y':
    print()
    displayIntro()

    caveNumber = chooseCave()
    checkCave(caveNumber)

    print('Do you want to play again? (yes or no)')
    playAgain = input()


@Mekire: Thanks for your input it really helped me get thinking :)
I might add some more options now that I know how to.

If any of you guys have some ideas or recommendations please feel free to post 'em up for me.

-dazuza
dazuza360
 
Posts: 5
Joined: Tue May 14, 2013 10:31 am

Re: Dragon Realm (text-based game)

Postby metulburr » Thu May 16, 2013 12:58 am

If any of you guys have some ideas or recommendations please feel free to post 'em up for me.

here are some

you need to read up on some string methods, like str.lower() for example:
Code: Select all
playAgain = input('Player again?')

if playAgain.lower()[0] == 'y':
    print('redo')
else:
    print('quitting')


and learn the 'in' operator:
Code: Select all
l = [1,2,3]
num = input('number?')
if int(num) in l:
    print('you number is in the list')
else:
    print('wrong')
New Users, Read This
version Python 3.3.2 and 2.7.5, tkinter 8.5, pyqt 4.8.4, pygame 1.9.2 pre
OS Ubuntu 13.04, Mint 11, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
User avatar
metulburr
 
Posts: 1088
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Dragon Realm (text-based game)

Postby dazuza360 » Thu May 16, 2013 1:42 am

@metulburr: Cool thanks for the tip. I know i've got a lot of reading up and learning to do :lol:
dazuza360
 
Posts: 5
Joined: Tue May 14, 2013 10:31 am

Re: Dragon Realm (text-based game)

Postby Vik2015 » Sat May 18, 2013 9:05 pm

Hello, I have modified your code a bit. ( If you find any errors, please say, because I have Python 2.7 and maybe I missed something)

Code: Select all
#Dragon Realm Game
import random
import time

def displayIntro():
    print('You are on a planet full of dragons. In front of you, you see two caves.')
    time.sleep(2)
    print('In one cave, the dragon is friendly and will share his treasure with you.')
    time.sleep(2)
    print('The other dragon is greedy and hungry, and will eat you on sight!')
    time.sleep(2)
   
def chooseCave():
    cave = ''
    caveList = ['1','2','3']
    while cave not in caveList:
        print('Which cave will you enter into?(1, 2 or 3)')
        cave = input()

    return cave

def checkCave(chosenCave):

    print('You approach the cave...')
    time.sleep(2)
    print('It is dark and spooky...')
    time.sleep(2)
    print('You notice a huge shadow looms over you...')
    time.sleep(3)
    print('A large dragon appears in front of you! He opens his jaws and...')
    time.sleep(4)
    print()

    randCave = random.randint(1,3)
   
    if randCave == 1:
        print('The dragon greets you and gives you his treasure!')
    elif randCave == 2:
        print('The dragon crushes you with it\'s powerful jaws and eats you!')
    else:
        print('You realise the cave is empty. Your mind is playing tricks on you ...')
   
playAgain = 'yes'
while playAgain.lower() in 'yes':
    print()
    displayIntro()

    caveNumber = chooseCave()
    checkCave(caveNumber)

    print('Do you want to play again? (y/N)')
    playAgain = input()

BTW, I have removed functions: firstCave(), secondCave() and thirdCave() because they were the same :) so it was just waste of lines i think :).
Vik2015
 
Posts: 15
Joined: Sat May 18, 2013 8:53 pm
Location: Computer

Re: Dragon Realm (text-based game)

Postby Vik2015 » Sun May 19, 2013 10:46 am

I have posted the code here cuz I am not sure if my PM was sent to you. #for admins: if my PM was sent to dazuza360, then decline this post :) just i still have PM in Outbox, not sure if I sent it...
Code: Select all
# Dragon Realm Game
import random
import time

def displayIntro():
    print('You are on a planet full of dragons. In front of you, you notice some caves.')
    time.sleep(2)
    print('In one cave, the dragon is friendly and will share his treasure with you.')
    time.sleep(2)
    print('Another cave holds a greedy and hungry dragon, and will eat you on sight!')
    time.sleep(2)
    print('Some caves are a mystery...')
    time.sleep(2)
    print('You must also be careful to look out for traps!')
    time.sleep(2)
   
def chooseCave():
    cave = ''
    caveList = ['1','2','3','4']
    while cave not in caveList: # means, if cave is not in caveList ( it doesn't equals to any of caveList's variables
        #example: if cave == 1: then it won't print('Which cave...'), but it will return cave
        #actually "while cave not in caveList:" means the same as "while cave !=1 or cave !=2 or cave !=3 or cave !=4:"
        #hope, I expained normally ( English is not my native lang xD )
        print('Which cave will you enter into?')
        cave = raw_input()
         
    return cave

def checkCave(chosenCave):

    def randCave():
        if randomCave == 1:
          print('The dragon approaches you from the shadows...')
          print('... and presents you with a treasure box!')
          print('Confused, you leave the cave wondering how you managed to escape with your life and with treasure.')

        elif randomCave == 2:
          print('You freeze in your spot as you see a dragon licking its lips with eagerness to devour its next meal!')
          print('With a quick swipe it pins you down as you helplessly squirm under the powerful claws.')
          time.sleep(8)
          print('The dragon eats you!')

        elif randomCave == 3:
          print('You realise the cave is empty. Your mind is playing tricks on you ...')

        else:
          print('This cave is empty, except for a large box in the middle of the room.')
          print('Suddenly, the floor below you starts to rumble! Before you can do anything,')
          print('the floor below you gives way and you fall to your death!')

    randomCave = random.randint(1,4)

    print('You approach the cave...')
    time.sleep(2)
    print('It is dark and spooky...')
    time.sleep(2)

    #I wanna show you how to make it faster:
    caveName = {'1':'first','2':'second','3':'third','4':'fourth'} # creating a dictionary with some values.
    #You will need them for printing which cave do you enter
    #If you don't know what is a dictionary, look here: http://docs.python.org/3.1/tutorial/datastructures.html#dictionaries

    # and now...
    print('You enter the %s cave...'%caveName[chosenCave])
    # Ok... Now i will try to explain. %s is used to assing strings(integers and so on) to it. it is equal to:
    # print('You enter the',caveName[chosenCave],'cave...') If you didn't understood, don't worry it was hard for me too. Look here: http://docs.python.org/release/2.3.5/lib/typesseq-strings.html
    # BTW, I am not sure if it will work with Python 3... if so, replace line print('You enter the %s cave...'%caveName[chosenCave]) with
    # print ('You enter the',caveName[chosenCave],'cave...')

    # Now... what does 'caveName[chosenCave]' means???
    # Look, you have some variables and their values. For exaple, '1' is variable and its value is 'first'. So when you call (look 1 line up)...
    # You take VALUE of VARIABLE. When you call you give variable chosenCave, and get its value in the dictionary
    # Again, read more on http://docs.python.org/3.1/tutorial/datastructures.html#dictionaries
    randCave()
   
    '''#remove here and down to make it work
    if chosenCave == '1':
        print('You enter the first cave...')
        randCave()

    elif chosenCave == '2':
        print('You enter the second cave...')
        randCave()
       
    elif chosenCave == '3':
        print('Your enter the third cave...')
        randCave()
       
    else:
        print('You enter the fourth cave...')
        randCave()
    '''#remove here and upper to make it work
   
playAgain = 'y' # I made it 'y' just to show you how it works:
while playAgain.lower() in 'yes': # playAgain.lower() means all letter will be in the lower case.
    # ... in 'yes' means that if playAgain.lower() equals part of 'yes' ( or whole word ) it will return True.
    print()
    displayIntro()

    caveNumber = chooseCave()
    checkCave(caveNumber)

    print('Do you want to play again? (y/n)')
    playAgain = raw_input()

# I think, thats all... If you find any errors or bugs, please say me ( cuz as i said i use Python 2.7, and i need to replace some stuff to test
# scripts, so i may fogot to replace something back... :D
# Cheers
# -Vik2015 ;)
Vik2015
 
Posts: 15
Joined: Sat May 18, 2013 8:53 pm
Location: Computer

Re: Dragon Realm (text-based game)

Postby Kebap » Sun May 19, 2013 1:53 pm

Vik2015 wrote: #for admins: if my PM was sent to dazuza360, then decline this post :) just i still have PM in Outbox, not sure if I sent it...

I am pretty sure this means that you have sent it but it just was not read yet.

By the way, you can decline your own posts, if you feel it necessary. Aint nobody gonna check PMs then delete postings.
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 282
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: Dragon Realm (text-based game)

Postby Vik2015 » Sun May 19, 2013 1:56 pm

Oh... Thx a lot! :D
Vik2015
 
Posts: 15
Joined: Sat May 18, 2013 8:53 pm
Location: Computer


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 2 guests