DEV Community

loading...

Dragon game

Karen Molina
・2 min read

Hello!
Ok, let's start! Now, we can create a new game, you know the same book.
I'm gonna put the code first, then I gotta explain it.

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 dragons id friendly")
    print("and will share his treasure with you. The other dragon")
    print("is greedy and hungry, and will eat you in sight")

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(chooseCave):
    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 open his jaws...")
    print()
    time.sleep(2)

    friendlyCave = random.randint(1,2)

    if chooseCave == 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()
Enter fullscreen mode Exit fullscreen mode

In this program we use two modules: random and time module.

Currently we use functions, the function is a way to say what to do and how. In python the functions has been writing with the statement defines or def. So, we have the firts block to code inside a function called displayIntro.
Inside displayIntro function, we write a few print methods to introduce the user inside the game.

Then we have the chooseCave function. Inside it, we put a empty varibale. Below we can write a while loop. Inside it, we have to compar two options or values: 1 and 2. Those values, are going to be the user options.

The next function we have is called checkCave. In this function we have to write a twice print methods to start the story. Then, we have to question to user which are the way to choose. Immediately we have to do the comparison between his choose and a random number. This is why we use random module. To choose randomdly two options: number 1 or 2. The options that we return to user, depends that return the random module.

Finnaly, we have to do another function to know if the user want to continue or left the game.

Discussion (1)

Collapse
faranaiki profile image
Muhammad Faran Aiki

Well, a little suggestion for me....

You can import like,

import random, time
Enter fullscreen mode Exit fullscreen mode

You can use,

# You can use """ this """ interline.
print("""You are in a land full of dragons, In front of you,
you see two caves. In one cave, the dragons id friendly
and will share his treasure with you. The other dragon
is greedy and hungry, and will eat you in sight""")
Enter fullscreen mode Exit fullscreen mode

And you can use,

while playAgain.lower() in ("yes", "y", "ye"): # This is simpler.
    # ...
Enter fullscreen mode Exit fullscreen mode

Avoid,

    print("A large dragon jumps out in front of you! HE open his jaws...")
    print()
Enter fullscreen mode Exit fullscreen mode

Rather, use,

    print("A large dragon jumps out in front of you! HE open his jaws...\n")
Enter fullscreen mode Exit fullscreen mode