Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/dev/pacmath.activity/question.py
blob: 67eed88488e52a7f2cb5102926049cb091a98af0 (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
#! /usr/bin/env python
"""
Set up questions and answers with draw functions
@license: U{http://creativecommons.org/licenses/by-sa/3.0/us/}
"""
import pygame
import random

class question:
    """
    @var A: Properties of answer box [color, (left, top), center, ghost location]
    @var B: Properties of answer box [color, (left, top), center, ghost location]
    @var C: Properties of answer box [color, (left, top), center, ghost location]
    @var D: Properties of answer box [color, (left, top), center, ghost location]
    @var SIZE: Size of answer box
    @var QUESTION_COLOR: the rgb color in which question text is written
    """
    # Properties of rectangles: [color, (left, top), center, ghost location]
    A = [(0, 180, 255), (625, 0), (735, 15), "./images/ghost.png"]
    B = [(253, 0, 1), (625, 79), (735, 94), "./images/ghost2.png"]
    C = [(255, 124, 0), (625, 158), (735, 173), "./images/ghost3.png"]
    D = [(255, 0, 192), (625, 237), (735, 250), "./images/ghost4.png"]
    
    # Size of rectangles
    SIZE = (275, 75)
    
    QUESTION_COLOR = (94,246,0)
    
    # Default
    def __init__ (self, screen, quests):
        """
        Randomly selects which questions are going to be used and draws the answer
        @param screen: the screen in which we will draw
        @param quests: array a questions with answers
        """
        # Draw the answers and questions
        self.questions = quests
        self.answerIndex = random.randint(0,3)
        self.question = self.questions[self.answerIndex][0]
            
        self.drawAnswers(screen)
        self.drawQuestion(screen)
        self.pacman = pygame.image.load("./images/pacmanr.png").convert_alpha()
        
    def drawAnswers(self, screen):
        """
        Draws all answers in their color coded boxes
        @param screen: The screen on which to draw
        """
        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):
        """
        Draws answers in a color coded box
        @param screen: the screen in which we will draw
        @param answer: answer to be drawn
        @param choice: which choice from 1-4
        """
        # Font used
        font = pygame.font.Font(None, 72)
        
        # Set properties of rectangle based on choice
        if choice == 1:
            color = self.A[0]
            pos = self.A[1]
            center = self.A[2]
            ghost = self.A[3]
        elif choice == 2:
            color = self.B[0]
            pos = self.B[1]
            center = self.B[2]
            ghost = self.B[3]
        elif choice == 3:
            color = self.C[0]
            pos = self.C[1]
            center = self.C[2]
            ghost = self.C[3]
        elif choice == 4:
            color = self.D[0]
            pos = self.D[1]
            center = self.D[2]
            ghost = self.D[3]
        
        # Create and draw rectangle, answer and ghost right of maze
        ans = font.render(answer, 1, color)
        rect = (pos, self.SIZE)
        pygame.draw.rect(screen, color, rect, 3)
        g = pygame.image.load(ghost).convert_alpha()
        screen.blit(ans, center)
        screen.blit(g, (pos[0] + 4, pos[1] + 4))
        
    # Draws the question
    def drawQuestion(self, screen):
        """
        Draws the question to the screen
        @param screen: the screen in which we will draw
        """
        # Font used
        font = pygame.font.Font(None, 72)
        
        # Draws the question under maze
        q = font.render(self.question, 1, self.QUESTION_COLOR)
        self.clearBottom(screen)
        screen.blit(q, (0, 640))
        pygame.display.flip()
    def drawPaused(self, screen):
        """
        Writes 'Paused' over where the question is usually printed
        @param screen: the screen on which we will draw
        """
        # 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):
        """
        Draws a black box over the question-holding part of the screen
        """
        blackBox = pygame.Surface((600,60))
        blackBox.fill((0,0,0))
        screen.blit(blackBox, (0,640))

    def getAnswerIndex(self):
        """
        Returns the integer index of the correct answer
        @return the index
        """
        return self.answerIndex

    # lives box is 35 px tall, ghosts are 25px square
    def updateLives(self, screen, count):
        """
        Redraws the lives display onto the screen
        @param screen: the screen on which to draw
        @param count: the number of lives we have
        """
        self.clearLives(screen)
        offset = 0
        for i in range(count):
            offset += 30
            screen.blit(self.pacman, (900-offset, 640))
        pygame.display.update(pygame.Rect(900-offset, 640, offset, 30))

    def clearLives(self, screen):
        """
        Draws a black box over the lives display so we may redraw them
        @param screen: the screen on which to draw
        """
        blackBox = pygame.Surface((300,30))
        blackBox.fill((0,0,0))
        screen.blit(blackBox, (600,640))
        pygame.display.update(pygame.Rect(600, 640, 300, 30))
    
    def updateScore(self, screen, score):
        """
        Redraws the score onto the screen
        @param screen: the screen on which we will draw
        @param score: the score to be drawn
        """
        self.clearScore(screen)
        
        text = pygame.font.Font(None, 36).render( "Score: " + str(score), 1, self.QUESTION_COLOR )
        screen.blit(text, (900-text.get_width(), 670))
        pygame.display.update(pygame.Rect(900-text.get_width(), 670, text.get_width(), 30))

    def clearScore(self, screen):
        """
        Draws a black box over the score display so that we may redraw it
        @param screen: the screen on which to draw
        """
        blackBox = pygame.Surface((300,30))
        blackBox.fill((0,0,0))
        screen.blit(blackBox, (600,670))
        pygame.display.update(pygame.Rect(600, 670, 300, 30))