Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/dev/pacmath.activity/gameMain.py
blob: d861bd7cf739d95e0b9cd616461e6421acb16d47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#! /usr/bin/env python
"""
Creates a game with maze and sprites and updates game
@license: U{http://creativecommons.org/licenses/by-sa/3.0/us/}
@var game: The instance of gameMain
@var done: when game is done it will equal true
@var BASE_LIVES: the number of lives with witch a player starts
@var MAZE_SIZE: size of maze
@var MAZE_DRAW_FRAME: size of maze to be drawn
"""

from pacmanMovement import pacmanMovement
from GhostMovement import ghostMovement
from mazeSetup import mazeSetup
from questionGenerator import questionGenerator
from question import question
import random
import pygame
import sys

# GAME VARIABLES
MAZE_SIZE = 25
MAZE_DRAW_FRAME = 25
BASE_LIVES = 3

class gameMain:
    def __init__(self):
        """
        Constructor
        """
        # 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.operation = random.sample('x+-/', 1)
        self.questGen = questionGenerator( self.operation, 2, 12 )  
        self.lives = BASE_LIVES
        self.score = 0
        self.level = 1

    def gameOver(self):
        """
        Called to restart the game at level 1 when a player loses
        """
        self.level = 1
        self.lives = BASE_LIVES
        self.score = 0
        self.screen.fill((0,0,0))
	self.operation = random.sample('x+-/', 1)
        self.questGen = questionGenerator( self.operation, 2, 12 ) 
        self.QandA.updateLives(self.screen, self.lives)
        self.QandA.updateScore(self.screen, self.score)
        self.QandA.updateLevel(self.screen, self.level)
        self.resetQuestion()

    def resetQuestion(self):
        """
        Generates and uses a new question set with a new operation
        """
        self.maze = mazeSetup(self.screen, MAZE_SIZE) # create an instance of the
                                            # maze->call the constructor
	self.operation = random.sample('x+-/', 1)
        self.questGen = questionGenerator( self.operation, 2, 12 ) 
        self.QandA = question(self.screen, self.questGen.getQuestionSet())
        self.ghost1 = ghostMovement( 0, self.QandA.questions[0])
        self.ghost2 = ghostMovement( 1, self.QandA.questions[1])
        self.ghost3 = ghostMovement( 2, self.QandA.questions[2])
        self.ghost4 = ghostMovement( 3, self.QandA.questions[3])
        self.pacman = pacmanMovement()
        
        self.ghosts = [ self.ghost1, self.ghost2, self.ghost3, self.ghost4 ]
        self.nextQuestion()

    def nextQuestion(self):
        """
        Updates the game to move on to a new question, if no more are available
        in the current set, this calls resetQuestion and moves the player to
        the next level
        """
        if self.QandA.nextQuestion():
            self.wrongGhosts = pygame.sprite.Group()
            self.correctGhosts = pygame.sprite.Group()
            
            
            for i in range(len(self.ghosts)):
                if not (i == self.QandA.getAnswerIndex() ):
                    self.wrongGhosts.add( self.ghosts[i] )
                else:
                    self.correctGhosts.add( self.ghosts[i] )
        else:
            self.level += 1
            self.resetQuestion()
        
    def update(self, event):
        """
        Updates the maze and sprites given an event
        @type event: pygame.event.Event
        @param event: directional key
        """
        alreadyCollided = False
        # anything the needs to be update in the game loop
        # such as the players, screen.blit, etc.
        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)
        alreadyCollided = self.checkCollisions()
        pacmanEvent = pacmanMovement.update(self.pacman, self.screen, event, MAZE_SIZE, self.maze)
        if not alreadyCollided:
            self.checkCollisions()
        if pacmanEvent == 1:
            self.score += 1
            if self.score == 100:
                self.score = 1
                self.lives += 1
                self.QandA.updateLives(self.screen, self.lives)
            self.QandA.updateScore(self.screen, self.score)
        elif pacmanEvent == 2:
            self.freeze(30)

    def freeze(self, time):
        """
        Frezzes all ghosts for a given amount of time
        @type time: number
        @param time: The number of update calls to stay still for
        """
        self.ghost1.freeze(time)
        self.ghost2.freeze(time)
        self.ghost3.freeze(time)
        self.ghost4.freeze(time)

    def checkCollisions(self):
        """
        Checks for and handles collisions between ghosts and the player
        @rtype: boolean
        @return: if there was a collision
        """
        for ghost in pygame.sprite.spritecollide(self.pacman, self.correctGhosts,False):
            self.nextQuestion()
            self.QandA.updateLevel(self.screen, self.level)
            ghost.eaten()
            return True

        for ghost in pygame.sprite.spritecollide(self.pacman, self.wrongGhosts, False):
            self.lives -= 1
            if self.lives < 0:
                self.gameOver()
            else:
                self.QandA.updateLives(self.screen, self.lives)
                self.QandA.updateScore(self.screen, self.score)
                ghost.eaten()
                return True
        return False

    def pause(self):
        """
        Places the pause overlay over the game screen
        """
        self.QandA.drawPaused(self.screen)
        

    def unpause(self):
        """
        Removes the pause overlay
        """
        self.QandA.drawQuestion(self.screen)

    def loop(self):
        """
        Main game loop, handles user input and timing
        """
        done = False
        paused = False
        self.resetQuestion()
        self.QandA.updateScore(self.screen, self.score)
        self.QandA.updateLives(self.screen, self.lives)
        self.QandA.updateLevel(self.screen, self.level)
        
        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