Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAbbi Honeycutt <nemofreak@gmail.com>2010-11-06 04:57:30 (GMT)
committer Abbi Honeycutt <nemofreak@gmail.com>2010-11-06 04:57:30 (GMT)
commit89a7cd517d8225dd5f4496073f04334eaef7d1bd (patch)
treecfa7af5643f709be286c3d452312df08160d51e6
parent7f6d9d8c23f36f2eac5dadb52113bd1a1a8b34cb (diff)
parent190cc4241170351f3c12feda7ff740fd8c772e64 (diff)
Merge branch 'master' of gitorious.org:~classclownfish/pacmath/classclownfishs-pacmath
-rw-r--r--dev/pacmath.activity/gameMain.py110
-rw-r--r--dev/pacmath.activity/question.py46
-rw-r--r--dev/pacmath.activity/questionGenerator.py52
3 files changed, 145 insertions, 63 deletions
diff --git a/dev/pacmath.activity/gameMain.py b/dev/pacmath.activity/gameMain.py
index 8a2e1e4..df96ef4 100644
--- a/dev/pacmath.activity/gameMain.py
+++ b/dev/pacmath.activity/gameMain.py
@@ -10,12 +10,12 @@ 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
# GAME VARIABLES
-done = False
MAZE_SIZE = 25
MAZE_DRAW_FRAME = 25
@@ -26,8 +26,26 @@ class gameMain:
"""
# everything for setting up the game
temp = 0
+ pygame.init()
+ pygame.key.set_repeat(10, 50)
+ # make the screen however big the maze (maze needs to 625 by 625) is
+ self.screen = pygame.display.set_mode((900, 700))
+ # if olpcgames.ACTIVITY:
+ # size = olpcgames.ACTIVITY.game_size
+ pygame.display.set_caption('PacMath')
+ self.screen.fill((0,0,0))
+ self.questGen = questionGenerator( 'x', 2, 12 )
- def update(self, event, frame):
+ self.maze = mazeSetup(self.screen, MAZE_SIZE) # create an instance of the
+ # maze->call the constructor
+ self.QandA = question(self.screen, self.questGen.getQuestionSet())
+ self.ghost1 = ghostMovement((self.screen.get_rect().x, self.screen.get_rect().y), 0, self.QandA.questions[0])
+ self.ghost2 = ghostMovement((self.screen.get_rect().x, self.screen.get_rect().y), 1, self.QandA.questions[1])
+ self.ghost3 = ghostMovement((self.screen.get_rect().x, self.screen.get_rect().y), 2, self.QandA.questions[2])
+ self.ghost4 = ghostMovement((self.screen.get_rect().x, self.screen.get_rect().y), 3, self.QandA.questions[3])
+ self.pacman = pacmanMovement((self.screen.get_rect().x, self.screen.get_rect().y))
+
+ def update(self, event):
"""
Updates the maze and sprites given an event
@param event: directional key
@@ -35,55 +53,55 @@ class gameMain:
"""
# anything the needs to be update in the game loop
# such as the players, screen.blit, etc.
- ghostMovement.update(ghost1, screen, MAZE_SIZE, maze)
- ghostMovement.update(ghost2, screen, MAZE_SIZE, maze)
- ghostMovement.update(ghost3, screen, MAZE_SIZE, maze)
- ghostMovement.update(ghost4, screen, MAZE_SIZE, maze)
- pacmanMovement.update(pacman, screen, event, MAZE_SIZE, maze)
+ ghostMovement.update(self.ghost1, self.screen, MAZE_SIZE, self.maze)
+ ghostMovement.update(self.ghost2, self.screen, MAZE_SIZE, self.maze)
+ ghostMovement.update(self.ghost3, self.screen, MAZE_SIZE, self.maze)
+ ghostMovement.update(self.ghost4, self.screen, MAZE_SIZE, self.maze)
+ pacmanMovement.update(self.pacman, self.screen, event, MAZE_SIZE, self.maze)
pygame.display.update()
+
+ def pause(self):
+ """
+ Places the pause overlay over the game screen
+ """
+ self.QandA.drawPaused(self.screen)
+
-pygame.init()
-pygame.key.set_repeat(10, 50)
-# make the screen however big the maze (maze needs to 625 by 625) is
-screen = pygame.display.set_mode((900, 700))
-#if olpcgames.ACTIVITY:
-# 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")]
-
-# 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)
-pacman = pacmanMovement((screen.get_rect().x, screen.get_rect().y))
-game = gameMain()
+ def unpause(self):
+ self.QandA.drawQuestion(self.screen)
-#for only drawing every MAZE_DRAW_FRAME number of frames
-frame = 0
+ def loop(self):
+ done = False
+ paused = False
-# GAME LOOP
-while done == False:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- done = True
- elif event.type == pygame.KEYDOWN:
- if event.key == pygame.K_ESCAPE:
- done = True
- # slow things down
- pygame.time.delay(150)
- game.update(event, frame)
+ while done == False:
+ for event in pygame.event.get():
+ if event.type == pygame.QUIT:
+ done = True
+ elif event.type == pygame.KEYDOWN:
+ if event.key == pygame.K_ESCAPE:
+ done = True
+ elif event.key == pygame.K_p:
+ if paused:
+ paused = False
+ game.unpause()
+ else:
+ paused = True
+ game.pause()
+ pygame.time.delay(150)
+ # slow things down
+ if not paused:
+ pygame.time.delay(150)
+ game.update(event)
# once we exit the game loop we exit the game
+ sys.exit()
+
+############ END GAMEMAIN CLASS, STARTING CODE HERE ###########
+game = gameMain()
+game.loop()
sys.exit()
+
+# GAME LOOP
+
diff --git a/dev/pacmath.activity/question.py b/dev/pacmath.activity/question.py
index 2f8af0b..7c3b6b0 100644
--- a/dev/pacmath.activity/question.py
+++ b/dev/pacmath.activity/question.py
@@ -23,6 +23,8 @@ class question:
# Size of rectangles
SIZE = (275, 75)
+ QUESTION_COLOR = (94,246,0)
+
# Default
def __init__ (self, screen, quests):
"""
@@ -31,23 +33,19 @@ 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.question = self.questions[random.randint(0,3)][0]
- 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.drawAnswers(screen)
+ self.drawQuestion(screen)
+ def drawAnswers(self, screen):
+ 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)
+
# Draws the answer in a ghost colored rectangle
def drawAnswer(self, screen, answer, choice):
"""
@@ -90,7 +88,7 @@ class question:
screen.blit(g, (pos[0] + 4, pos[1] + 4))
# Draws the question
- def drawQuestion(self, screen, question):
+ def drawQuestion(self, screen):
"""
Draws the question
@param screen: the screen in which we will draw
@@ -100,5 +98,19 @@ class question:
font = pygame.font.Font(None, 72)
# Draws the question under maze
- q = font.render(question, 1, (94,246,0))
- screen.blit(q, (0, 640)) \ No newline at end of file
+ q = font.render(self.question, 1, self.QUESTION_COLOR)
+ self.clearBottom(screen)
+ screen.blit(q, (0, 640))
+ pygame.display.flip()
+ def drawPaused(self, screen):
+ # Font used
+ font = pygame.font.Font(None, 72)
+
+ q = font.render("Paused", 1, self.QUESTION_COLOR)
+ self.clearBottom(screen)
+ screen.blit(q, (0, 640))
+ pygame.display.flip()
+ def clearBottom(self, screen):
+ blackBox = pygame.Surface((900,60))
+ blackBox.fill((0,0,0))
+ screen.blit(blackBox, (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
+
+