Saturday, January 29, 2011

Dragon Realm's Game

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!");
 print("Other one is greedy and Kill you!");
 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 and open the jaw...");
 print();
 time.sleep(2);
 
 friendlyCave = random.randint(1,2);
 
 if chosenCave == str(friendlyCave):
  print("Give 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? y/n");
 playAgain = input();
http://inventwithpython.com/chapter6.html

2 comments:

  1. I LIKE THE INFORMATION YOU PUT IN THE TEXT TO CODE
    NICE CODE
    I HOPE I COULD PLAY YOUR GAME
    GOOD JOB
    NICE WORK
    DRAGON REALM CODE LOOKS EASY TO CODE BECAUSE OF YOU

    ReplyDelete
  2. Hiya can you help me figure out why this doesn't work because I can't see the error. It just comes up, asks for the cave number but then does not execute the if or else statement, but instead just asks for the cave number again.

    #This is the code for a game called Dragon Realm-A text-based RPG.
    import random
    #This imports a module called random, which is used to make random selections.
    import time
    #This imports a module called time, which is used for controlling time within
    #the module.
    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()
    """
    This defines a new command called displayIntro. When the new command is called
    upon, the code which follows it (That is indented) is executed. In this case,
    it is just some simple print functions.
    """
    def chooseCave():
    cave = ''
    while cave != '1' and cave != '2':
    print('Which cave will you go into? (1 or 2)')
    cave=input()

    return cave
    """
    Once again, a new function is being defined. This time, however, it has different
    code following it. Firstly, a new variable, called "cave", has been defined, and
    set as nothing. Following this is a while statement, which tells the computer
    to execute the following indented code for as long as the while condition is
    being met. In this case, as long as "cave" does not equal 1 and does not equal
    2, the computer displays the message, which cave will you go into (1 or 2).
    """
    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!')
    """
    This code defines yet another new function, called checkCave. This time, there is
    another few pieces of previously unseen code, in the form of the use of the time
    module, as well as if, else, str and random.randint. Firstly, the time module is
    being used in the code time.sleep (2). This tells the computer to wait, or sleep
    for 2 seconds before executing the next piece of code. Next, there is a new variable
    called friendlyCave, followed by a random.randint. This tells the computer to
    pick a random integer (Using the random module) in the range of numbers placed
    in the brackets, in this case being 1, 2.

    Following this is an if statement. This tells the computer that should a certain
    criteria be met, then the code which is indented after it should be executed. The
    else statement then tells the computer that if the criteria is not met, then the
    code following it should be executed instead.
    """
    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()
    """
    This piece of code assigns another variable called playAgain, which is set to yes.
    After this, there is another while statement, telling the computer to execute the
    following code as long as the variable playAgain is equivalent to either yes or
    y. In this while statement, we see the functions defined earlier in the code being
    called upon. This means that if the condtions of the while statement are met, the
    computer then executes the code following the new functions.
    """

    ReplyDelete