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
|
# MathQuest.py
# Author: Tyler Bragdon
# Contributors: Eric Mallon
#
# Description: The initial starting screen for the Math Quest project, giving
# the player options to play through the entire game or select an individual
# level to play from the level select screen.
import tower, levelone, leveltwo, levelthree, levelfour, levelfive, race, util
import random, os.path
import util
import pygame
import pygame.font
from pygame.locals import *
def run():
#Must initialize pygame before doing anything else
pygame.init()
random.seed()
display_flags = DOUBLEBUF
#XO laptop is 1200x900 resolution
width, height = 1200, 900
if pygame.display.mode_ok((width, height), display_flags ):
screen = pygame.display.set_mode((width, height), display_flags)
clock = pygame.time.Clock()
run = 1
pygame.display.set_caption('Math Quest')
clicked = 0
menuLocation = 0 #The variable representing which screen of the main menu the player is on - 0 is the main menu
mouseRect = pygame.Rect(pygame.mouse.get_pos(),(5,5)) #A rectangle representing the current mouse position
#load the images used
bgImage = util.load_image('fork.bmp')
bgButton = util.load_image('mainButton.jpg')
#Code to set up the initial screen (Stores as surface)
mainButtons = [pygame.Rect(400,300,400,100),pygame.Rect(400,450,400,100),pygame.Rect(400,600,400,100)] #Button locations
mainButtonText = ["Start Game","Level Select","Exit"] #Button text
mainScreen = pygame.Surface((width,height)) #Surface the background/buttons of the main screen are stored
mainScreen.blit(bgImage,(0,0))
titleFont = pygame.font.Font(None, 150)
mainScreen.blit(titleFont.render("Math Quest",1,(5,5,5)),(300,100))
#Goes through every button, drawing it to the screen and adding its text
for i in range(len(mainButtons)):
(x,y) = mainButtons[i].topleft
mainScreen.blit(bgButton,(x,y),(0,0,50,100)) #draws the left portion of the button images
#draws the middle portion of the button images
middle = 50
while middle < (mainButtons[i].width - 50):
mainScreen.blit(bgButton,(x+middle,y),(50,0,50,100))
middle = middle+50
mainScreen.blit(bgButton,(x+middle,y),(100,0,50,100)) #draws right portion of button
# Adds the button text to the surface, with text centered in the button
font = pygame.font.Font(None, 80)
displayBox = font.render(mainButtonText[i], 1, (30,30,150))
textSize = font.size(mainButtonText[i])
mainScreen.blit(displayBox,(mainButtons[i].topleft[0]+(400-textSize[0])/2,mainButtons[i].topleft[1]+(100-textSize[1])/2))
#Set up the level select screen (Also stores as surface)
levelSelectButtons = [pygame.Rect(175,150,400,100),pygame.Rect(625,150,400,100),pygame.Rect(175,300,400,100),pygame.Rect(625,300,400,100),pygame.Rect(175,450,400,100),pygame.Rect(625,450,400,100),pygame.Rect(175,600,400,100),pygame.Rect(625,600,400,100),pygame.Rect(175,750,850,100)]
levelSelectButtonText = ["Bridge Builder","Map","Dungeon","Watch","Race","Tower Gates","Tower Climber","Boss","Back"]
levelSelectScreen = pygame.Surface((width,height))
levelSelectScreen.blit(bgImage,(0,0))
titleFont = pygame.font.Font(None, 120)
levelSelectScreen.blit(titleFont.render("Level Select",1,(5,5,5)),(335,35))
for i in range(len(levelSelectButtons)):
pygame.draw.rect(levelSelectScreen,[175,0,0],levelSelectButtons[i])
(x,y) = levelSelectButtons[i].topleft
levelSelectScreen.blit(bgButton,(x,y),(0,0,50,100))
middle = 50
while middle < (levelSelectButtons[i].width - 50):
levelSelectScreen.blit(bgButton,(x+middle,y),(50,0,50,100))
middle = middle+50
levelSelectScreen.blit(bgButton,(x+middle,y),(100,0,50,100))
font = pygame.font.Font(None, 80)
displayBox = font.render(levelSelectButtonText[i], 1, (30,30,150))
textSize = font.size(levelSelectButtonText[i])
levelSelectScreen.blit(displayBox,(levelSelectButtons[i].topleft[0]+(levelSelectButtons[i].width-textSize[0])/2,levelSelectButtons[i].topleft[1]+(100-textSize[1])/2))
screen.blit(mainScreen,(0,0))
pygame.display.flip()
while run:
mouseRect = pygame.Rect(pygame.mouse.get_pos(),(5,5))
events = pygame.event.get()
for event in events:
#User decides to exit program
if event.type == QUIT:
run = 0 #stop running
#Whenever the mouse is clicked, clicked is turned to true
if event.type == pygame.MOUSEBUTTONDOWN:
clicked = 1
if clicked == 1:
clicked = 0
if menuLocation == 0: #On the main screen
clickedButton = mouseRect.collidelist(mainButtons)
if clickedButton != -1:
if clickedButton == 0: #Starts the game from the beginning and plays through all levels
levelone.run()
levelfour.run()
leveltwo.run()
levelthree.run()
tower.run()
elif clickedButton == 1: #Goes to level select screen
menuLocation = 1
screen.blit(levelSelectScreen,(0,0))
elif clickedButton == 2: #Exits program
run = 0
elif menuLocation == 1: #On the level select screen
clickedButton = mouseRect.collidelist(levelSelectButtons)
if clickedButton != -1:
print "Button %d clicked"%(clickedButton)
if clickedButton == 0: #Bridge Level
levelone.run()
if clickedButton == 1: #Map Level
leveltwo.run()
if clickedButton == 2: #Dungeon Level
levelfive.run()
if clickedButton == 3: #Watch Level
levelfour.run()
if clickedButton == 4: #Race Level
race.run()
if clickedButton == 5: #Tower Gate Level
levelthree.run()
if clickedButton == 6: #Tower Climbing level
tower.run()
if clickedButton == 7: #Boss level (not yet implemented)
pass
if clickedButton == 8: #Back button
#Sets the menu location to that of the main menu, draws surface
menuLocation = 0
screen.blit(mainScreen,(0,0))
pygame.display.flip()
clock.tick(40) #limits to 40 FPS
pygame.quit()
#Main method simply calls run one
def main():
run()
if __name__ == '__main__': main()
|