So I'd like to get some help on this project i'm working on. Basically i'm doing a math game. There's three boxes where answer options are available. My problem is i don't know how to randomize where the correct answer is (it has to be in one position for it to work) Here is the code, and if anyone'd help me i'd be pleased.
Note: settings.py
is just a file where i've got variables defined such as WIDTH, HEIGHT etc.
import pygame, sys, random
from settings import *
class Game:
def __init__(self):
pygame.init()
pygame.display.set_caption(TITLE)
self.gameDisplay = pygame.display.set_mode((WIDTH, HEIGHT))
self.font = pygame.font.SysFont(None, 35, True)
self.plusorminus = random.choice(['+','-'])
self.No1 = random.randrange(1, 101)
self.No2 = random.randrange(1, 101)
self.random1 = str(random.randrange(1,101))
self.random2 = str(random.randrange(1, 101))
self.AnswerInput = None
self.score = 0
pygame.display.update()
self.clock = pygame.time.Clock()
self.GameLoop()
def displayText(self, text, color, coordinates):
self.text = self.font.render(text, 1, color)
self.gameDisplay.blit(self.text, coordinates)
def CalculateAnswer(self):
if self.plusorminus == '-':
if self.No1 > self.No2:
self.answer = self.No1 - self.No2
else:
self.answer = self.No2 - self.No1
else:
if self.No1 > self.No2:
self.answer = self.No1 + self.No2
else:
self.answer = self.No2 + self.No1
return str(self.answer)
def CheckAnswer(self):
if self.AnswerInput == 1:
self.displayText("Correct answer", RED, [WIDTH / 2 - 100, HEIGHT / 2 + 200])
elif self.AnswerInput == 0:
pass
def Update_Score(self):
self.displayText("Score: {}".format(self.score), WHITE, [10,10])
def CheckNewQuestion(self):
if self.AnswerInput == 1:
self.CheckAnswer()
#Regenerate the numbers
self.No1 = random.randrange(1, 101)
self.No2 = random.randrange(1, 101)
self.random1 = str(random.randrange(1, 101))
self.random2 = str(random.randrange(1, 101))
self.plusorminus = random.choice(['+', '-'])
self.CalculateAnswer() #Recalculate everything
self.DrawObjects() #Run function again
self.AnswerInput = None
def DrawObjects(self):
self.gameDisplay.fill(BLACK) #Fill screen black
#Draw text box
pygame.draw.rect(self.gameDisplay, WHITE, [WIDTH / 2 - 150, HEIGHT / 2 - 65, 300, 50])
#Write text
if self.No1 > self.No2:
self.displayText("What is {} {} {}?".format(self.No1, self.plusorminus, self.No2), RED, [WIDTH / 2 - 108, HEIGHT / 2 - 52])
else:
self.displayText("What is {} {} {}?".format(self.No2, self.plusorminus, self.No1), RED, [WIDTH / 2 - 108, HEIGHT / 2 - 52])
#Draw the three answer boxes
self.Answerbox1 = pygame.draw.rect(self.gameDisplay, WHITE,[WIDTH / 2 + 150, HEIGHT / 2 + 104, 150, 50]) # Randbox1
self.Answerbox2 = pygame.draw.rect(self.gameDisplay, WHITE,[WIDTH / 2 - 75, HEIGHT / 2 + 104, 150, 50]) # Randbox3
self.Answerbox3 = pygame.draw.rect(self.gameDisplay, WHITE,[WIDTH / 2 - 300, HEIGHT / 2 + 104, 150, 50]) # Ranbox3
#Write the answer options
self.displayText(str(self.answer), RED, [self.Answerbox1[0] + 60, self.Answerbox1[1] + 15])
self.displayText(str(self.random2), RED, [self.Answerbox2[0] + 60, self.Answerbox2[1] + 15])
self.displayText(str(self.random1), RED, [self.Answerbox3[0] + 60, self.Answerbox3[1] + 15])
def GameLoop(self):
print()
print(self.CalculateAnswer())
self.gameExit = False
while not self.gameExit:
for self.event in pygame.event.get():
if self.event.type == pygame.QUIT:
self.gameExit = True
elif self.event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
if self.Answerbox1.collidepoint(pos):
self.AnswerInput = 1
self.score += 10
self.CheckNewQuestion()
if self.Answerbox2.collidepoint(pos):
self.AnswerInput = 0
elif self.Answerbox3.collidepoint(pos):
self.AnswerInput = 0
else:
self.AnswerInput = 2
self.DrawObjects()
self.Update_Score()
self.CheckAnswer()
self.CheckNewQuestion()
pygame.display.update()
self.clock.tick(FPS)
pygame.quit()
sys.exit()
Game()
Aucun commentaire:
Enregistrer un commentaire