diff options
author | Michael Kitson <msk5293@rit.edu> | 2010-11-04 06:07:49 (GMT) |
---|---|---|
committer | Michael Kitson <msk5293@rit.edu> | 2010-11-04 06:07:49 (GMT) |
commit | 091b7667dedc31c3088afe87c824d6cdfa3b6c5e (patch) | |
tree | 5522924065aee4e1ecff3044f90a84b8acec93c3 | |
parent | 9b99dc886e712608eb19c01f3f1c6248d5ade99b (diff) |
Questions are now randomly generated in questionGenerator.py
-rw-r--r-- | dev/pacmath.activity/gameMain.py | 19 | ||||
-rw-r--r-- | dev/pacmath.activity/question.py | 22 | ||||
-rw-r--r-- | dev/pacmath.activity/questionGenerator.py | 52 |
3 files changed, 66 insertions, 27 deletions
diff --git a/dev/pacmath.activity/gameMain.py b/dev/pacmath.activity/gameMain.py index 8a2e1e4..974f14f 100644 --- a/dev/pacmath.activity/gameMain.py +++ b/dev/pacmath.activity/gameMain.py @@ -10,6 +10,7 @@ Creates a game with maze and sprites and updates game from pacmanMovement import pacmanMovement from GhostMovement import ghostMovement from mazeSetup import mazeSetup +from questionGenerator import questionGenerator from question import question import pygame import sys @@ -51,22 +52,16 @@ screen = pygame.display.set_mode((900, 700)) # size = olpcgames.ACTIVITY.game_size pygame.display.set_caption('PacMath') screen.fill((0,0,0)) -quests = [("5 x 4 = ?", "20") - , ("2 x 4 = ?", "8") - , ("3 x 7 = ?", "21") - , ("8 x 5 = ?", "40") - , ("1 x 6 = ?", "6") - , ("9 x 2 = ?", "18") - , ("11 x 3 = ?", "33") - , ("12 x 12 = ?", "144")] +questGen = questionGenerator( 'x', 2, 12 ) +quests = questGen.getQuestionSet() # make an instance of this class maze = mazeSetup(screen, MAZE_SIZE) #create an instance of the maze->call the constructor QandA = question(screen, quests) -ghost1 = ghostMovement((screen.get_rect().x, screen.get_rect().y), 0, QandA.q1) -ghost2 = ghostMovement((screen.get_rect().x, screen.get_rect().y), 1, QandA.q2) -ghost3 = ghostMovement((screen.get_rect().x, screen.get_rect().y), 2, QandA.q3) -ghost4 = ghostMovement((screen.get_rect().x, screen.get_rect().y), 3, QandA.q4) +ghost1 = ghostMovement((screen.get_rect().x, screen.get_rect().y), 0, QandA.questions[0]) +ghost2 = ghostMovement((screen.get_rect().x, screen.get_rect().y), 1, QandA.questions[1]) +ghost3 = ghostMovement((screen.get_rect().x, screen.get_rect().y), 2, QandA.questions[2]) +ghost4 = ghostMovement((screen.get_rect().x, screen.get_rect().y), 3, QandA.questions[3]) pacman = pacmanMovement((screen.get_rect().x, screen.get_rect().y)) game = gameMain() diff --git a/dev/pacmath.activity/question.py b/dev/pacmath.activity/question.py index 2f8af0b..ac05b0e 100644 --- a/dev/pacmath.activity/question.py +++ b/dev/pacmath.activity/question.py @@ -31,21 +31,13 @@ class question: @param quests: array a questions with answers """ # Draw the answers and questions - self.question = quests - self.q1 = random.randint(0, 3) - self.q3 = random.randint(0, 3) - while self.q3 == self.q1: - self.q3 = random.randint(0, 3) - self.q2 = random.randint(4, 7) - self.q4 = random.randint(4, 7) - while self.q4 == self.q2: - self.q4 = random.randint(4, 7) + self.questions = quests - self.drawAnswer(screen, self.question[self.q1][1], 1) - self.drawAnswer(screen, self.question[self.q2][1], 2) - self.drawAnswer(screen, self.question[self.q3][1], 3) - self.drawAnswer(screen, self.question[self.q4][1], 4) - self.drawQuestion(screen, self.question[self.q2][0]) + self.drawAnswer(screen, self.questions[0][1], 1) + self.drawAnswer(screen, self.questions[1][1], 2) + self.drawAnswer(screen, self.questions[2][1], 3) + self.drawAnswer(screen, self.questions[3][1], 4) + self.drawQuestion(screen, self.questions[random.randint(0,3)][0]) # Draws the answer in a ghost colored rectangle @@ -101,4 +93,4 @@ class question: # Draws the question under maze q = font.render(question, 1, (94,246,0)) - screen.blit(q, (0, 640))
\ No newline at end of file + screen.blit(q, (0, 640)) diff --git a/dev/pacmath.activity/questionGenerator.py b/dev/pacmath.activity/questionGenerator.py new file mode 100644 index 0000000..918b15c --- /dev/null +++ b/dev/pacmath.activity/questionGenerator.py @@ -0,0 +1,52 @@ +#! /usr/bin/env python +""" +Creates random questions for PacMath +@license: U{http://creativecommons.org/licenses/by-sa/3.0/us/} +@var done: when game is done it will equal true +@var MAZE_SIZE: size of maze +@var MAZE_DRAW_FRAME: size of maze to be drawn +""" +from random import randint +import pygame +import sys + +class questionGenerator: + def __init__(self, operation, minOp, maxOp): + """ + Constructor + """ + self.minOp = minOp + self.maxOp = maxOp + self.operation = operation + + def getQuestion(self): + op1 = randint(self.minOp, self.maxOp) + op2 = randint(self.minOp, self.maxOp) + + if( self.operation == 'x' or self.operation == '*' or self.operation == 'X' ): + answer = op1*op2 + return (str(op1) + " x " + str(op2) + " = ?", str(answer)) + else: + return null + def getQuestionSet(self): + questions = [] + attempts = 0 + while len(questions) < 4 and attempts < 100: + toAdd = self.getQuestion() + notDuplicate = True + for i in questions: + if toAdd[1] == i[1]: + notDuplicate = False + attempts += 1 + if notDuplicate: + questions.append( toAdd ) + + if attempts >= 100: + print "Bad question set, not enough questions could be generated" + print "Minimum Operand: " + str(self.maxOp) + print "Maximum Operand: " + str(self.minOp) + print "Operation: " + str(self.operation) + raise SystemExit + return questions + + |