-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquiz.py
executable file
·200 lines (160 loc) · 8.75 KB
/
quiz.py
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
#History Mansion - A history slider game
#quiz.py Created By Terry Clark
#Released under a "Simplified BSD" License
import pygame, os, sys, random, submitScore, winLoseDisplay
from context import *
from pygame.locals import *
class quiz:
def __init__(self):
#Screen Size
self.WINDOWWIDTH = (1100)
self.WINDOWHEIGHT = (700)
#Set In Game colors and assets
self.WHITE = ( 255, 255, 255 )
self.BLACK = ( 0, 0, 0 )
self.BROWN = ( 180, 120, 40 )
self.BGCOLOR = self.BLACK
self.BGIMAGE = pygame.image.load("Assets/images/Pictures/mansion/fireplace.jpg")
self.BORDERCOLOR = self.BROWN
self.BUTTONTEXTCOLOR = self.WHITE
#Quiz Variables
self.CHAR = ('flo','geng','ghandi','henry','queen','cleo')
self.randQuestSel = random.choice(range(23))
self.titleInstruct = "Complete all questions to submit your score!"
self.instruct = "Select a person below....."
self.questNum = 1
self.quizScore = 0
self.percent = 0
#Option buttons
self.EXIT_SURF, self.EXIT_RECT = self.makeText('EXIT', self.BUTTONTEXTCOLOR, 30, 10, self.WINDOWHEIGHT - 40)
self.quizTitle, self.quizTitleRect = self.makeTextCenter("QUIZ!", self.WHITE, 40, (self.WINDOWWIDTH/2), 100)
self.titleInstruct, self.titleInstructRect = self.makeTextCenter(self.titleInstruct, self.WHITE, 30, self.WINDOWWIDTH/2, 150)
self.quizInstruct, self.quizInstructRect = self.makeTextCenter(self.instruct, self.WHITE, 30, self.WINDOWWIDTH/2, 330)
self.screen = pygame.display.get_surface()
#This is to display the player name to the screen whilst they type
self.__playerName = ""
#This is for the highscore submit
self.puzOrQuiz = "quiz"
#/---Event loop function---\#
def update(self, dt):
for event in pygame.event.get():
if event.type == MOUSEBUTTONUP:
if self.EXIT_RECT.collidepoint(event.pos):
if top() is self:
pop()
#User presses character images to answer question
for z in range(6):
if self.ansImgRectList[z-1].collidepoint(event.pos):
self.checkAns(self.CHAR[z-1])
#End quiz conditional
if self.questNum > 22:
push(submitScore.submitScore(self.quizScore, self.puzOrQuiz))
self.questNum = 1
self.quizScore = 0
self.percent = 0
#Escaping the game
elif event.type == KEYUP:
if event.key == K_ESCAPE:
if top() is self:
pop()
elif event.type == QUIT:
sys.exit()
#/-----Draw to screen-----\#
def draw(self, dt):
#Display background
self.screen.blit(self.BGIMAGE, (0, 0))
pygame.draw.rect(self.screen, self.BLACK, (0, 180, self.WINDOWWIDTH, 300))
self.scoreText, self.scoreTextRect = self.makeTextCenter("Score: " + str(self.quizScore), self.WHITE, 40, (self.WINDOWWIDTH/2), 500)
#Display quest and answer
self.QandAList()
self.displayAnswers()
#Game text and buttons
self.screen.blit(self.quizTitle, self.quizTitleRect)
self.screen.blit(self.titleInstruct, self.titleInstructRect)
self.screen.blit(self.quizInstruct, self.quizInstructRect)
self.screen.blit(self.scoreText, self.scoreTextRect)
self.screen.blit(self.EXIT_SURF, self.EXIT_RECT)
pygame.display.flip()
#/-----In Game Functions-----\#
def think(self, dt):
self.update(dt)
self.draw(dt)
def QandAList(self):
self.questList = { #cleo
0 : ("Who ruled Egypt between 51 BC and 30 BC?", "cleo"),
1 : ("Who had a daughter called Pharaoh Ptolemy XII Auletes?", "cleo"),
2 : ("Whose father was called King Ptolemy XII?", "cleo"),
3 : ("Who died after their son had committed suicide?", "cleo"),
#geng
4 : ("Who was born around 1162 AD and died at the age of 65?", "geng"),
5 : ("Who conquered nearly 12 millions square miles of territory?", "geng"),
6 : ("Who was originally named Temujin?", "geng"),
7 : ("Who killed over 40 million people?", "geng"),
#ghandi
8 : ("Which person was born on October 2 1869 in Porbandar, India?", "ghandi"),
9 : ("Who fought for the independence of India?", "ghandi"),
10 : ("Who went to Law School in London?", "ghandi"),
11 : ("Which person was killed on January 30, 1948?", "ghandi"),
#henry
12 : ("Which person was born in 1491, the Tudor Age?", "henry"),
13 : ("Who had six wives and was the King of England?", "henry"),
14 : ("Who chopped off peoples heads when they were cross with them?", "henry"),
15 : ("Which person died in 1547?", "henry"),
#queen
16 : ("Who was born in 1819?", "queen"),
17 : ("Which person became Queen in 1837 when they was 18?", "queen"),
18 : ("Who had a the Victorian Age named after them?", "queen"),
19 : ("Who died in January 1901 living to see the start of the 20th century?", "queen"),
#flo
20 : ("Who was born in 1820, prior to the steam railway?", "flo"),
21 : ("Who was the founder of modern nursing?", "flo"),
22 : ("Who showed that cleaner hospitals helped sick people get better?", "flo"),
23 : ("Which person died in 1910?", "flo"),
}
questNumText, questNumTextRect = self.makeTextCenter("Question " + str(self.questNum) + " of 22:", self.WHITE, 30, self.WINDOWWIDTH/2, 220)
quest, questRect = self.makeTextCenter(self.questList[self.randQuestSel][0], self.WHITE, 25, self.WINDOWWIDTH/2, 260)
self.screen.blit(questNumText, questNumTextRect)
self.screen.blit(quest, questRect)
def displayAnswers(self):
imgLocation = "Assets/images/Pictures/quiz_images/"
x = 0
xadj = 140
y = 350
self.ansImgList = []
self.ansImgRectList = []
for z in range(6):
self.ansImgList.append("self.ansImg" + str(z-1))
self.ansImgRectList.append("self.ansImgRect" + str(z-1))
for z in range(6):
self.ansImgList[z-1], self.ansImgRectList[z-1] = self.imgClickSurf(imgLocation + self.CHAR[z-1] + ".jpg", (x + xadj), y)
x += 140
self.screen.blit(self.ansImgList[z-1], self.ansImgRectList[z-1])
def imgClickSurf (self, img, top, left):
image = pygame.image.load(os.path.join(img))
imageRect = image.get_rect()
imageRect.topleft = (top, left)
return (image, imageRect)
def checkAns(self, char):
if char == self.questList[self.randQuestSel][1]:
self.quizScore += 10
self.randQuestSel = random.choice(range(23))
self.questNum += 1
isCorrect = True
push(winLoseDisplay.winLoseDisplay(isCorrect))
else:
self.questNum += 1
self.randQuestSel = random.choice(range(23))
isCorrect = False
push(winLoseDisplay.winLoseDisplay(isCorrect))
def makeText(self, text, color, size, top, left):
font = pygame.font.SysFont("monospace", size, bold=True, italic = False)
textSurf = font.render(text, True, color)
textRect = textSurf.get_rect()
textRect.topleft = (top, left)
return (textSurf, textRect)
def makeTextCenter(self, text, color, size, centerx, height):
font = pygame.font.SysFont("monospace", size, bold=True, italic = False)
textSurf = font.render(text, True, color)
textRect = textSurf.get_rect()
textRect.center = (centerx, height)
return (textSurf, textRect)