Please help with this

S

saad imran

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!")
 
G

Gary Herron

Could you point out any errors in my code:
<Snip 280+ lines of code.>

Nope.



You've got to do *some* of the work if you expect free volunteer help
from people around here. Take the time to tell us what you expect this
program to do, what actually happens when you run it, what errors you
get. If you get Python traceback, cut and paste it into an email.
Tell us how you have tried to fix the problem, whatever that may be.
Give some hint where in the nearly 300 lines of code the problem may be
occurring.

You can get lots of free volunteer help from this group, but the
question, as you've asked it, is a misuse (or even an *abuse*) of this
group.

Gary Herron
 
M

Mark Lawrence

<Snip 280+ lines of code.>

Nope.



You've got to do *some* of the work if you expect free volunteer help
from people around here. Take the time to tell us what you expect this
program to do, what actually happens when you run it, what errors you
get. If you get Python traceback, cut and paste it into an email. Tell
us how you have tried to fix the problem, whatever that may be. Give
some hint where in the nearly 300 lines of code the problem may be
occurring.

You can get lots of free volunteer help from this group, but the
question, as you've asked it, is a misuse (or even an *abuse*) of this
group.

Gary Herron

It's my belief that the above is way OTT given the dross we've had to
put up with from a certain Greek individual over the last few months.
Why isn't the OP here being spoon fed?
 
D

Dave Angel

Could you point out any errors in my code:
que1 = "4481 *2"
ans1 = "8962"
que2 = "457 * 21"
ans2 = "9597"

These values should all be in a single named structure, probably a
list of tuples. Then all that duplicated code could be condensed into
a single block.

Is that the error you wanted us to find?
 
S

Steven D'Aprano

Could you point out any errors in my code:

Hi Saad, and welcome!

As others have pointed out, you'll have better results if you tell us
what errors you have, what result you actually expected, and what you
have tried to do to fix it.

If you just want a general code review, it is best to make that clear by
saying you have no known errors.

Also, unfortunately not everyone here is familiar with PyGame. For PyGame
questions, you may be better asking for help on a PyGame forum or mailing
list. We'll help if we can, but we're not experts.

I haven't gone through your entire program in detail, but one small thing
stands out:

# intro
print("Welcome!")
print("")

You don't need to print the empty string to get a blank like. You can
either call print with no arguments:

print("Welcome!")
print()


or you can manually add an extra newline to the string:

print("Welcome!\n")


Hope this helps,
 
M

mkharper

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,

You have and odd "gameloop". All others are "gameLoop".


Michael
 
M

mkharper

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!")

Also....

Instead of asking the question 8 times, how about using a couple of dictionaries for the questions and answers with the answers include the coordinates like this:-

QUE = {1: "4481 *2",
2: "457 * 21",
3: "2345*23",
4: "Who plays against the USA in golf's Walker Cup?",
5: "Which game is played in autumn using the fruit of the horse chestnut tree?",
6: "From what country does Lego come?",
7: "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?",
8: "What date was D-Day?",
9: "Who is called the 'Great One' in hockey history?", }

ANS = {1: "8962;75;75",
2: "9597;75;75",
3: "53935;75;75",
4: "britain;75;75",
5: "conkers;75;75",
6: "denmark;75;75",
7: "umpire;75;75",
8: "06,06,1944;76;75",
9: "wayne gretzky;25;75", }

and then just test the question/answer with:

queSel = random.randrange(1, 10)

print("\n"*6)

print(QUE[queSel])
playerAnswer = input("What do you think the answer is?")
if playerAnswer == ANS[queSel].split(';')[0]:
print("Correct!")
USERX += ANS[queSel].split(';')[1]
else:
print("UH-OH!")
DRAGONX += ANS[queSel].split(';')[2]

print("\n"*5)


I'm sure there's better ways. This is just what came to mind.

Regards,


Michael
 
M

mkharper

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!"
 
P

Peter Otten

mkharper said:
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", }
# Code to generate random number
QUESEL = random.randrange(1, 10)

You can simplify that some more by putting question/answer pairs into a list

qa_pairs = [
("What is 4 x 2? ", "8"),
("What is 3 x 6? ", "18"),
#...
]

and then use random.choice() and tuple unpacking

question, answer = random.choice(qa_pairs)

if input(question) == answer: # python 2: replace input with raw_input
print("correct")
else:
print("UH-OH!")

You can nest the tuples if you really need other data

qa_pairs = [
("What is 4 x 2? ", ("8", 75, 75)),
("What is 3 x 6? ", ("18", 75, 75)),
#...
]

question, answer_and_offset = random.choice(qa_pairs)
answer, userx, dragonx = answer_and_offset
#...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top