Please help with this

mkharper mkharper at gmail.com
Wed Nov 13 06:09:54 EST 2013


On Wednesday, November 13, 2013 4:18:58 AM UTC, saad imran wrote:
> Could you point out any errors in my code:
> 
> 
> 
>  #This is a game where you have to escape a dragon.
> 
> # By Saad Imran
> 
> 
> 
> import random
> 
> import pygame
> 
> 
> 
> 
> 
> 
> 
> # Define questions and answers.
> 
> que1 = "4481 *2"
> 
> ans1 = "8962"
> 
> que2 = "457 * 21"
> 
> ans2 =  "9597"
> 
> que3 = "2345*23"
> 
> ans3 =  "53935"
> 
> que4 = "Who plays against the USA in golf's Walker Cup?"                                                   
> 
> ans4 = "britain"
> 
> que5 = "Which game is played in autumn using the fruit of the horse chestnut tree?"
> 
> ans5 = "conkers"
> 
> que6 = "From what country does Lego come?"
> 
> ans6 = "denmark"
> 
> que7 = "What term is used in cricket for the two men on the field who decide on whether batsmen are out, and signal for extras and boundaries?"
> 
> ans7 =  "umpire"
> 
> que8 = "What date was D-Day?"
> 
> ans8 = "06,06,1944"
> 
> que9 ="Who is called the 'Great One' in hockey history?"
> 
> ans9 = "wayne gretzky"
> 
> 
> 
> 
> 
> # intro
> 
> print("Welcome!")
> 
> print("")
> 
> name = input("What is your name? ")
> 
> print("")
> 
> print("You are in a cave and hear a dragon!",name)
> 
> print("")
> 
> print("Thinking you are doomed, you panic and run")
> 
> print("")
> 
> 
> 
> # Ask whether user wants to play or not
> 
> print("You run into a wizard who tells you that he will teleport you out of the cave if you solve his questions!")
> 
> print("")
> 
> game = input("Do you trust him?(y/n) ")
> 
> print("")
> 
> 
> 
> #if he doesn't game is ended
> 
> if game == "N" or game == "n":
> 
>     print("The dragon catches you and you are doomed!")
> 
>     gameloop = False
> 
>     loop = False
> 
> # if he does, game starts
> 
> 
> 
> else:
> 
>     gameLoop = True
> 
> 
> 
>     while gameLoop ==True:
> 
>     
> 
>         # Tell user how to write answers
> 
> 
> 
>         print("Please use all lower case letters. date format is dd,mm,yyyy")
> 
>         print("dragon is red, You are green!")
> 
> 
> 
> 
> 
> 
> 
>         # The following code intialises a pygame window where user can track progress of self and dragon
> 
>         # Needed to initialize pygame
> 
>         pygame.init()
> 
> 
> 
>         
> 
> 
> 
> 
> 
> 
> 
>         # define variable for user position
> 
>         userX =75
> 
>         userY =450
> 
> 
> 
> 
> 
> 
> 
>         # define variable for dragon position.
> 
>         dragonX = 0
> 
>         dragonY = 450
> 
>         
> 
>         
> 
>         
> 
>         
> 
> 
> 
>         
> 
>         
> 
>         
> 
>         # Define some Constants(colours and screen size)
> 
>         BLACK = (0, 0, 0)
> 
>         WHITE = (255, 255, 255)
> 
>         GREEN = (0, 255, 0)
> 
>         RED = (255, 0, 0)
> 
>             
> 
>         SCREEN_WIDTH = 700
> 
>         SCREEN_HEIGHT = 500
> 
> 
> 
>         # Create a screen 
> 
>         size = [SCREEN_WIDTH,SCREEN_HEIGHT]
> 
>         screen = pygame.display.set_mode(size)
> 
> 
> 
>         pygame.display.set_caption("dragonEscape Game")
> 
> 
> 
>         # Used to manage how fast the screen updates
> 
>         clock = pygame.time.Clock()
> 
> 
> 
>         #Loop until the user clicks the close button.
> 
>         loop = True
> 
> 
> 
>         # -------- Main Program Loop -----------
> 
>         while loop == True:
> 
>             # EVENT PROCESSING
> 
>             for event in pygame.event.get():    
> 
>                 if event.type == pygame.QUIT:   
> 
>                     loop = False                 
> 
> 
> 
>             #if user is caught
> 
> 
> 
>             if userX <= dragonX:
> 
>                 print("The dragon ate you for dinner!")
> 
>                 loop = False
> 
>                 gameLoop = False
> 
> 
> 
> 
> 
>             #if user wins
> 
>             if userX >= 680:
> 
>             
> 
>                 print("You win!")
> 
>                 loop = False
> 
>                 gameLoop = False
> 
>                 
> 
>             
> 
>          
> 
>             # DRAW COMMANDS
> 
>             screen.fill(WHITE)
> 
> 
> 
>         
> 
>             pygame.draw.rect(screen, GREEN, [userX,userY,50,50])
> 
>             pygame.draw.rect(screen, RED, [dragonX,dragonY,50,50])
> 
>             pygame.draw.line(screen, BLACK, [680,0],[680,500],40)
> 
> 
> 
> 
> 
>             #Code to generate random number
> 
> 
> 
>             questionSelection = random.randrange(1,10)
> 
> 
> 
> 
> 
>             # if statements that select question randomly based on random number generated and collects user answer
> 
>             print("")
> 
>             print("")
> 
>             print("")
> 
>             print("")
> 
>             print("")
> 
>             print("")
> 
>             if questionSelection == 1:
> 
>                 print(que1)
> 
>                 playerAnswer = input("What do you think the answer is?")
> 
>                 if playerAnswer ==ans1:
> 
>                     print("Correct!")
> 
>                     userX += 75
> 
>                 else:
> 
>                     print("UH-OH!")
> 
>                     dragonX += 75
> 
>                     
> 
>                     
> 
> 
> 
>             
> 
> 
> 
>             elif questionSelection == 2:
> 
>                 print(que1)
> 
>                 playerAnswer = input("What do you think the answer is?")
> 
>                 if playerAnswer ==ans2:
> 
>                     print("Correct!")
> 
>                     userX += 75
> 
>                 else:
> 
>                     print("UH-OH!")
> 
>                     dragonX += 75
> 
>                     
> 
>                     
> 
> 
> 
>             elif questionSelection == 3:
> 
>                 print(que1)
> 
>                 playerAnswer = input("What do you think the answer is?")
> 
>                 if playerAnswer ==ans3:
> 
>                     print("Correct!")
> 
>                     userX += 75
> 
>                 else:
> 
>                     print("UH-OH!")
> 
>                     dragonX += 75
> 
>                     
> 
> 
> 
>             elif questionSelection == 4:
> 
>                  print(que1)
> 
>                 playerAnswer = input("What do you think the answer is?")
> 
>                 if playerAnswer ==ans4:
> 
>                     print("Correct!")
> 
>                     userX += 75
> 
>                 else:
> 
>                     print("UH-OH!")
> 
>                     dragonX += 75
> 
>                     
> 
> 
> 
>             elif questionSelection == 5:
> 
>                 print(que1)
> 
>                 playerAnswer = input("What do you think the answer is?")
> 
>                 if playerAnswer ==ans5:
> 
>                     print("Correct!")
> 
>                     userX += 75
> 
>                 else:
> 
>                     print("UH-OH!")
> 
>                     dragonX += 75
> 
>                     
> 
>                     
> 
> 
> 
>             
> 
> 
> 
>             elif questionSelection == 6:
> 
>                 print(que1)
> 
>                 playerAnswer = input("What do you think the answer is?")
> 
>                 if playerAnswer ==ans6:
> 
>                     print("Correct!")
> 
>                     userX +=75
> 
>                 else:
> 
>                     print("UH-OH!")
> 
>                     dragonX += 75
> 
>                     
> 
> 
> 
>             elif questionSelection == 7:
> 
>                 print(que1)
> 
>                 playerAnswer = input("What do you think the answer is?")
> 
>                 if playerAnswer ==ans7:
> 
>                     print("Correct!")
> 
>                     userX += 75
> 
>                 else:
> 
>                     print("UH-OH!")
> 
>                     dragonX += 75
> 
>                     
> 
>             elif questionSelection == 8:
> 
>                 print(que1)
> 
>                 playerAnswer = input("What do you think the answer is?")
> 
>                 if playerAnswer ==ans8:
> 
>                     print("Correct!")
> 
>                     userX += 76
> 
>                 else:
> 
>                     print("UH-OH!")
> 
>                     dragonX += 75
> 
>                     
> 
> 
> 
>             else:
> 
>                 print(que9)
> 
>                 playerAnswer = input("What do you think the answer is?")
> 
>                 if playerAnswer == ans9:
> 
>                     print("Correct!")
> 
>                     userX += 25
> 
>                 else:
> 
>                     print("UH-OH!")
> 
>                     dragonX += 75
> 
>                     
> 
>                     
> 
>             print("")
> 
>             print("")
> 
>             print("")
> 
>             print("")
> 
>             print("")
> 
>             
> 
>             
> 
>             
> 
>             
> 
>             # Update Screen
> 
>             pygame.display.flip()
> 
> 
> 
>             # Set frames per second
> 
>             clock.tick(20)
> 
>         # -------- End of Main Program Loop --------
> 
> 
> 
> 
> 
>         # Close the window and quit.
> 
>         pygame.quit()
> 
> 
> 
> 
> 
> #game over, ending remarks.
> 
> print("Thanks for playing!")


Hi Saad,

I've had a play and the following "does something".
(I'm running Python 2.7 so replaced input with raw_input.)
(User can use upper or lower case, just lower it before test.)
(I simplified the questions/answers so I could answer them.)

I'm out of time but hope the following helps.

Kind regards,


Michael


#!/usr/bin/env python
"""
    This is a game where you have to escape a dragon.
    By Saad Imran

"""

import random
import pygame

# Define questions and answers.

QUE = {1: "4 x 2",
       2: "3 x 6",
       3: "2 x 5",
       4: "6 / 2",
       5: "7 + 7",
       6: "8 - 3",
       7: "5 x 5",
       8: "4 / 1",
       9: "0 x 6", }

ANS = {1: "8;75;75",
       2: "18;75;75",
       3: "10;75;75",
       4: "3;75;75",
       5: "14;75;75",
       6: "5;75;75",
       7: "25;75;75",
       8: "4;75;75",
       9: "0;75;75", }

# intro
print("Welcome!\n")
NAME = raw_input("What is your name?: ")
print("\nYou are in a cave and hear a dragon %s!" % NAME)
print("Thinking you are doomed, you panic and run")

# Ask whether user wants to play or not
print("You run into a wizard who tells you that he will")
print("teleport you out of the cave if you solve his questions!\n")
GAME = raw_input("Do you trust him? (y/n): ")

#if he doesn't game is ended
if GAME.lower() == "n":
    print("The dragon catches you and you are doomed!\n")
    GAMELOOP = False
    LOOP = False

# if he does, game starts
else:
    GAMELOOP = True
    while GAMELOOP == True:
        print("(Dragon is red, You are green!)\n")

# Following code intialises a pygame window
# where user can track progress of self and dragon.
# Initialize pygame
        pygame.init()

# define variable for user position
        USERX = 75
        USERY = 450

# define variable for dragon position.
        DRAGONX = 0
        DRAGONY = 450
       
# Define some Constants(colours and screen size)
        BLACK = (0, 0, 0,)
        WHITE = (255, 255, 255,)
        GREEN = (0, 255, 0,)
        RED = (255, 0, 0,)
           
        SCREEN_WIDTH = 700
        SCREEN_HEIGHT = 500

# Create a screen
        SIZE = [SCREEN_WIDTH, SCREEN_HEIGHT]
        SCREEN = pygame.display.set_mode(SIZE)
        pygame.display.set_caption("dragonEscape Game")

# Used to manage how fast the screen updates
        CLOCK = pygame.time.Clock()

# Loop until the user clicks the close button.
        LOOP = True

# -------- Main Program Loop -----------
        while LOOP:
# EVENT PROCESSING
            for event in pygame.event.get():    
                if event.type == pygame.QUIT:  
                    LOOP = False                
# DRAW COMMANDS
            SCREEN.fill(WHITE)
            pygame.draw.rect(SCREEN, GREEN, [USERX, USERY, 50, 50])
            pygame.draw.rect(SCREEN, RED, [DRAGONX, DRAGONY, 50, 50])
            pygame.draw.line(SCREEN, BLACK, [680, 0], [680, 500], 40)
            pygame.display.flip()

# Code to generate random number
            QUESEL = random.randrange(1, 10)
# if statements that select question randomly based on random number generated
# and collects user answer.

            print("\n"*3)
            print(QUE[QUESEL])
            PLAYERANS = raw_input("What do you think the answer is? ")
            if PLAYERANS.lower() == ANS[QUESEL].split(';')[0].lower():
                print("Correct!")
                USERX += int(ANS[QUESEL].split(';')[1])
            else:
                print("UH-OH!")
                DRAGONX += int(ANS[QUESEL].split(';')[2])
            print "\n"*2 

# Update Screen
            pygame.display.flip()
            CLOCK.tick(20)

#if user is caught
            if USERX <= DRAGONX:
                print("The dragon ate you for dinner!")
                LOOP = False
                GAMELOOP = False
#if user wins
            if USERX >= 680:
                print("You win!")
                LOOP = False
                GAMELOOP = False

# Set frames per second
# --------- End of Main Program Loop ---------

        # Close the window and quit.
        pygame.quit()

#game over, ending remarks.
print "Thanks for playing!"




More information about the Python-list mailing list