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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
import random, os.path
import util
import math
import os.path
import pygame
import pygame.font
from pygame.locals import *
#to do: replace OK boxes
# possibly reword description (johns feedback)
# change mechanic to limit the number of times you can use wood pieces
# aka instead of a hard cap of total used, cap pieces used to most efficient solution
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('OLPC Math Game')
#level one setup code
state = (1,1)
#bg music
#pygame.mixer.music.load("smrpgmid.mid")
#pygame.mixer.music.play(9)
bridge_max_length = 20
bridge_lengths = [random.randint(1,5),random.randint(1,5),random.randint(1,5)]
bridge_piece = util.load_image('bridge_piece3.bmp')
piece_loaded = 0
amount_loaded = 0
lvl_one_background = util.load_image('bridge_bg_2.bmp')
#load controls
bridge_controls = util.load_image('bridge_controls_3.bmp')
#Display The Background
screen.blit(lvl_one_background, (0, 0))
while run:
events = pygame.event.get()
for event in events:
#User decides to exit program
if event.type == QUIT:
run = 0 #stop running
if state == (1,1): #we are in level one, setup/intro
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_loc = list(event.pos)
(x, y) = pygame.mouse.get_pos()
if x > 100+220-15 and x < 100+220+15 and y > 80 and y < 110:
state = (1,2)
if state == (1,2): #we are in level one, main level
#level one state two (main level) event code
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_loc = list(event.pos)
(x, y) = pygame.mouse.get_pos()
if x > 308 and x < 350 and y > 837 and y < 879:
piece_loaded = 1
amount_loaded = 2
if x > 401 and x < 445 and y > 837 and y < 879:
piece_loaded = 1
amount_loaded = 3
if x > 493 and x < 542 and y > 837 and y < 879:
piece_loaded = 1
amount_loaded = 5
if piece_loaded:
if x > 386 and x < bridge_lengths[0]*24+386 and y > 318 and y < 358:
#wood_sound = pygame.mixer.Sound("wood_drop.aiff") #Waterside by nobuo uematsu from blue dragon
#wood_sound.play()
piece_loaded = 0
bridge_lengths[0] += amount_loaded
if x > 386 and x < bridge_lengths[1]*24+386 and y > 358 and y < 398:
#wood_sound = pygame.mixer.Sound("wood_drop.aiff") #Waterside by nobuo uematsu from blue dragon
#wood_sound.play()
piece_loaded = 0
bridge_lengths[1] += amount_loaded
if x > 386 and x < bridge_lengths[2]*24+386 and y > 398 and y < 438:
#wood_sound = pygame.mixer.Sound("wood_drop.aiff")#Waterside by nobuo uematsu from blue dragon
#wood_sound.play()
piece_loaded = 0
bridge_lengths[2] += amount_loaded
key_pressed = pygame.key.get_pressed()
if key_pressed[K_2]:
piece_loaded = 1
amount_loaded = 2
if key_pressed[K_3]:
piece_loaded = 1
amount_loaded = 3
if key_pressed[K_5]:
piece_loaded = 1
amount_loaded = 5
#end of level one, state two event handling code
if state == (1,4): # used too much wood, level one
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_loc = list(event.pos)
(x, y) = pygame.mouse.get_pos()
if x > 200+90-15 and x < 200+90+15 and y > 140 and y < 170:
piece_loaded = 0
bridge_lengths = [random.randint(1,5),random.randint(1,5),random.randint(1,5)]
amount_loaded = 0
state = (1,2)
if state == (1,3): # beat the level, level one
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_loc = list(event.pos)
(x, y) = pygame.mouse.get_pos()
if x > 200+90-15 and x < 200+90+15 and y > 140 and y < 170:
piece_loaded = 0
bridge_lengths = [random.randint(1,5),random.randint(1,5),random.randint(1,5)]
amount_loaded = 0
run = 0
#end of events
if state == (1,1): #level one, intro
#background = load_image('bridge_bg_2.bmp')
screen.blit(lvl_one_background, (0, 0))
font = pygame.font.Font(None, 24)
intro_string = "Our hero has come to a river, but the bridge is broken! You must rebuild the bridge to cross. There is only just enough wood though, so don't waste any!"
text_rect = pygame.Rect((100,20),(440,100))
text_box = util.render_textrect(intro_string,font,text_rect,(255,255,255),0,0)
screen.blit(text_box,text_rect.topleft)
ok_box = pygame.Rect((100+220-15,80),(30,30))
ok_box_surf = pygame.Surface(ok_box.size)
ok_box_surf.fill((255,255,255))
screen.blit(ok_box_surf,ok_box.topleft)
ok_text = font.render("OK",1,(0,0,0))
(x,y) = ok_box.topleft
x += 3
y += 5
screen.blit(ok_text,(x,y))
if state == (1,2): #level one, main level
#bridge pieces counters
font = pygame.font.Font(None, 36)
top_plank_display = font.render("%s/20" % bridge_lengths[0], 1, (255, 255, 255))
middle_plank_display = font.render("%s/20" % bridge_lengths[1], 1, (255, 255, 255))
bottom_plank_display = font.render("%s/20" % bridge_lengths[2], 1, (255, 255, 255))
total_planks_used = bridge_lengths[0]+bridge_lengths[1]+bridge_lengths[2]
total_plank_display = font.render("%s/60" % total_planks_used, 1, (255, 255, 255))
#redraw screen
#background = load_image('bridge_bg_2.bmp')
screen.blit(lvl_one_background, (0, 0))
screen.blit(bridge_controls,(280,900-150))
(x, y) = pygame.mouse.get_pos()
#draw bridge
for i in range(3):
j = bridge_lengths[i]
for k in range(j):
screen.blit(bridge_piece,(388+k*24,320+i*40))
#shows piece in hand
if piece_loaded:
for i in range(amount_loaded):
screen.blit(bridge_piece,(x+i*24,y))
#bridge pieces counters
screen.blit(top_plank_display, (825,760))
screen.blit(middle_plank_display, (825,793))
screen.blit(bottom_plank_display, (825,822))
screen.blit(total_plank_display, (825,854))
if bridge_lengths[0] == 20 and bridge_lengths[1] == 20 and bridge_lengths[2] == 20:
state = (1,3)
if total_planks_used > 60:
state = (1,4)
if state == (1,3): #level one, end screen
font = pygame.font.Font(None, 36)
#background = load_image('bridge_bg_2.bmp')
#bridge_controls = load_image('bridge_controls_3.bmp')
screen.blit(lvl_one_background, (0, 0))
screen.blit(bridge_controls,(280,900-150))
#draw bridge
for i in range(3):
j = bridge_lengths[i]
for k in range(j):
screen.blit(bridge_piece,(388+k*24,320+i*40))
#bridge pieces counters
top_plank_display = font.render("%s/20" % bridge_lengths[0], 1, (255, 255, 255))
middle_plank_display = font.render("%s/20" % bridge_lengths[1], 1, (255, 255, 255))
bottom_plank_display = font.render("%s/20" % bridge_lengths[2], 1, (255, 255, 255))
total_planks_used = bridge_lengths[0]+bridge_lengths[1]+bridge_lengths[2]
total_plank_display = font.render("%s/60" % total_planks_used, 1, (255, 255, 255))
screen.blit(top_plank_display, (825,760))
screen.blit(middle_plank_display, (825,793))
screen.blit(bottom_plank_display, (825,822))
screen.blit(total_plank_display, (825,854))
font = pygame.font.Font(None, 24)
congrats_string = "Good job! You fixed the bridge. Now continue to the next level"
text_rect = pygame.Rect((200,80),(180,100))
text_box = util.render_textrect(congrats_string,font,text_rect,(255,255,255),0,0)
screen.blit(text_box,text_rect.topleft)
ok_box = pygame.Rect((200+90-15,140),(30,30))
ok_box_surf = pygame.Surface(ok_box.size)
ok_box_surf.fill((255,255,255))
screen.blit(ok_box_surf,ok_box.topleft)
ok_text = font.render("OK",1,(0,0,0))
(x,y) = ok_box.topleft
x += 3
y += 5
screen.blit(ok_text,(x,y))
if state == (1,4):
font = pygame.font.Font(None, 36)
#background = load_image('bridge_bg_2.bmp')
#bridge_controls = load_image('bridge_controls_3.bmp')
screen.blit(lvl_one_background, (0, 0))
screen.blit(bridge_controls,(280,900-150))
#draw bridge
for i in range(3):
j = bridge_lengths[i]
for k in range(j):
screen.blit(bridge_piece,(388+k*24,320+i*40))
#bridge pieces counters
top_plank_display = font.render("%s/20" % bridge_lengths[0], 1, (255, 255, 255))
middle_plank_display = font.render("%s/20" % bridge_lengths[1], 1, (255, 255, 255))
bottom_plank_display = font.render("%s/20" % bridge_lengths[2], 1, (255, 255, 255))
total_planks_used = bridge_lengths[0]+bridge_lengths[1]+bridge_lengths[2]
total_plank_display = font.render("%s/60" % total_planks_used, 1, (255, 255, 255))
screen.blit(top_plank_display, (825,760))
screen.blit(middle_plank_display, (825,793))
screen.blit(bottom_plank_display, (825,822))
screen.blit(total_plank_display, (825,854))
font = pygame.font.Font(None, 24)
congrats_string = "Oh, no! You used too many pieces of wood, try again."
text_rect = pygame.Rect((200,80),(180,100))
text_box = util.render_textrect(congrats_string,font,text_rect,(255,255,255),0,0)
screen.blit(text_box,text_rect.topleft)
ok_box = pygame.Rect((200+90-15,140),(30,30))
ok_box_surf = pygame.Surface(ok_box.size)
ok_box_surf.fill((255,255,255))
screen.blit(ok_box_surf,ok_box.topleft)
ok_text = font.render("OK",1,(0,0,0))
(x,y) = ok_box.topleft
x += 3
y += 5
screen.blit(ok_text,(x,y))
pygame.display.flip()
clock.tick(40) #limits to 40 FPS
#end run
pygame.quit()
def main():
print "Running bridge lvl one"
run()
#runs main if called via other naming convention
if __name__ == '__main__': main()
|