Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTyler B <eldrac@eldrac-laptop.(none)>2009-07-12 01:56:36 (GMT)
committer Tyler B <eldrac@eldrac-laptop.(none)>2009-07-12 01:56:36 (GMT)
commit70a488b2e54f7b9a5e8fe2b3d9a36e412a94cd21 (patch)
treeaa723acf5e4846b79d50028c6cc83ac343f046c9
Initial addition of MathQuest levels and files
-rw-r--r--data/TowerBackground2.bmpbin0 -> 2430054 bytes
-rw-r--r--data/TowerWindow.bmpbin0 -> 15254 bytes
-rw-r--r--data/WallTile.bmpbin0 -> 7654 bytes
-rw-r--r--data/brick_wall_bg.bmpbin0 -> 3240054 bytes
-rw-r--r--data/bridge_bg_2.bmpbin0 -> 3240054 bytes
-rw-r--r--data/bridge_controls_3.bmpbin0 -> 288054 bytes
-rw-r--r--data/bridge_piece3 - Copy.bmpbin0 -> 2934 bytes
-rw-r--r--data/bridge_piece3.bmpbin0 -> 2934 bytes
-rw-r--r--data/fork.bmpbin0 -> 3240054 bytes
-rw-r--r--data/grass.bmpbin0 -> 15054 bytes
-rw-r--r--data/horrid_door.bmpbin0 -> 615474 bytes
-rw-r--r--data/house2_map.bmpbin0 -> 4198 bytes
-rw-r--r--data/house_map.bmpbin0 -> 7950 bytes
-rw-r--r--data/lake_bg.bmpbin0 -> 3240054 bytes
-rw-r--r--data/map.bmpbin0 -> 3124054 bytes
-rw-r--r--data/map_foreground2.bmpbin0 -> 990134 bytes
-rw-r--r--data/numberPlate.bmpbin0 -> 10934 bytes
-rw-r--r--data/parchment.bmpbin0 -> 589654 bytes
-rw-r--r--data/sky.jpgbin0 -> 34853 bytes
-rw-r--r--data/smrpgmid.midbin0 -> 18909 bytes
-rw-r--r--data/stonehedge_map.bmpbin0 -> 15134 bytes
-rw-r--r--data/tower_map.bmpbin0 -> 35254 bytes
-rw-r--r--data/tower_outside.bmpbin0 -> 3240054 bytes
-rw-r--r--data/tower_outside_controls.bmpbin0 -> 1146374 bytes
-rw-r--r--data/wall.bmpbin0 -> 7654 bytes
-rw-r--r--data/watch3.bmpbin0 -> 981054 bytes
-rw-r--r--data/watch_broken2.bmpbin0 -> 1041654 bytes
-rw-r--r--data/watch_drop_bg.bmpbin0 -> 3240054 bytes
-rw-r--r--data/wave.bmpbin0 -> 1242 bytes
-rw-r--r--data/wave2.bmpbin0 -> 978 bytes
-rw-r--r--data/wood_drop.aiffbin0 -> 39734 bytes
-rw-r--r--levelfive.py147
-rw-r--r--levelfour.py141
-rw-r--r--levelone.py275
-rw-r--r--levelthree.py274
-rw-r--r--leveltwo.py167
-rw-r--r--race.py461
-rw-r--r--start.py10
-rw-r--r--template.py44
-rw-r--r--tower.py639
-rw-r--r--util.py199
41 files changed, 2357 insertions, 0 deletions
diff --git a/data/TowerBackground2.bmp b/data/TowerBackground2.bmp
new file mode 100644
index 0000000..fdaf3e8
--- /dev/null
+++ b/data/TowerBackground2.bmp
Binary files differ
diff --git a/data/TowerWindow.bmp b/data/TowerWindow.bmp
new file mode 100644
index 0000000..8e015c5
--- /dev/null
+++ b/data/TowerWindow.bmp
Binary files differ
diff --git a/data/WallTile.bmp b/data/WallTile.bmp
new file mode 100644
index 0000000..a93ba50
--- /dev/null
+++ b/data/WallTile.bmp
Binary files differ
diff --git a/data/brick_wall_bg.bmp b/data/brick_wall_bg.bmp
new file mode 100644
index 0000000..17c442a
--- /dev/null
+++ b/data/brick_wall_bg.bmp
Binary files differ
diff --git a/data/bridge_bg_2.bmp b/data/bridge_bg_2.bmp
new file mode 100644
index 0000000..cbfd7bd
--- /dev/null
+++ b/data/bridge_bg_2.bmp
Binary files differ
diff --git a/data/bridge_controls_3.bmp b/data/bridge_controls_3.bmp
new file mode 100644
index 0000000..53ab1cc
--- /dev/null
+++ b/data/bridge_controls_3.bmp
Binary files differ
diff --git a/data/bridge_piece3 - Copy.bmp b/data/bridge_piece3 - Copy.bmp
new file mode 100644
index 0000000..68989fb
--- /dev/null
+++ b/data/bridge_piece3 - Copy.bmp
Binary files differ
diff --git a/data/bridge_piece3.bmp b/data/bridge_piece3.bmp
new file mode 100644
index 0000000..6ad7793
--- /dev/null
+++ b/data/bridge_piece3.bmp
Binary files differ
diff --git a/data/fork.bmp b/data/fork.bmp
new file mode 100644
index 0000000..91cc32a
--- /dev/null
+++ b/data/fork.bmp
Binary files differ
diff --git a/data/grass.bmp b/data/grass.bmp
new file mode 100644
index 0000000..d07dff1
--- /dev/null
+++ b/data/grass.bmp
Binary files differ
diff --git a/data/horrid_door.bmp b/data/horrid_door.bmp
new file mode 100644
index 0000000..7b91b23
--- /dev/null
+++ b/data/horrid_door.bmp
Binary files differ
diff --git a/data/house2_map.bmp b/data/house2_map.bmp
new file mode 100644
index 0000000..dca52b7
--- /dev/null
+++ b/data/house2_map.bmp
Binary files differ
diff --git a/data/house_map.bmp b/data/house_map.bmp
new file mode 100644
index 0000000..31797be
--- /dev/null
+++ b/data/house_map.bmp
Binary files differ
diff --git a/data/lake_bg.bmp b/data/lake_bg.bmp
new file mode 100644
index 0000000..dfe037d
--- /dev/null
+++ b/data/lake_bg.bmp
Binary files differ
diff --git a/data/map.bmp b/data/map.bmp
new file mode 100644
index 0000000..1a65b39
--- /dev/null
+++ b/data/map.bmp
Binary files differ
diff --git a/data/map_foreground2.bmp b/data/map_foreground2.bmp
new file mode 100644
index 0000000..005adc5
--- /dev/null
+++ b/data/map_foreground2.bmp
Binary files differ
diff --git a/data/numberPlate.bmp b/data/numberPlate.bmp
new file mode 100644
index 0000000..7c91d4e
--- /dev/null
+++ b/data/numberPlate.bmp
Binary files differ
diff --git a/data/parchment.bmp b/data/parchment.bmp
new file mode 100644
index 0000000..2b72149
--- /dev/null
+++ b/data/parchment.bmp
Binary files differ
diff --git a/data/sky.jpg b/data/sky.jpg
new file mode 100644
index 0000000..37d7c6b
--- /dev/null
+++ b/data/sky.jpg
Binary files differ
diff --git a/data/smrpgmid.mid b/data/smrpgmid.mid
new file mode 100644
index 0000000..ab80262
--- /dev/null
+++ b/data/smrpgmid.mid
Binary files differ
diff --git a/data/stonehedge_map.bmp b/data/stonehedge_map.bmp
new file mode 100644
index 0000000..35d1dd5
--- /dev/null
+++ b/data/stonehedge_map.bmp
Binary files differ
diff --git a/data/tower_map.bmp b/data/tower_map.bmp
new file mode 100644
index 0000000..16da85e
--- /dev/null
+++ b/data/tower_map.bmp
Binary files differ
diff --git a/data/tower_outside.bmp b/data/tower_outside.bmp
new file mode 100644
index 0000000..2ab4b68
--- /dev/null
+++ b/data/tower_outside.bmp
Binary files differ
diff --git a/data/tower_outside_controls.bmp b/data/tower_outside_controls.bmp
new file mode 100644
index 0000000..e743d14
--- /dev/null
+++ b/data/tower_outside_controls.bmp
Binary files differ
diff --git a/data/wall.bmp b/data/wall.bmp
new file mode 100644
index 0000000..8c46027
--- /dev/null
+++ b/data/wall.bmp
Binary files differ
diff --git a/data/watch3.bmp b/data/watch3.bmp
new file mode 100644
index 0000000..e5cff2d
--- /dev/null
+++ b/data/watch3.bmp
Binary files differ
diff --git a/data/watch_broken2.bmp b/data/watch_broken2.bmp
new file mode 100644
index 0000000..a1a038c
--- /dev/null
+++ b/data/watch_broken2.bmp
Binary files differ
diff --git a/data/watch_drop_bg.bmp b/data/watch_drop_bg.bmp
new file mode 100644
index 0000000..4d2e939
--- /dev/null
+++ b/data/watch_drop_bg.bmp
Binary files differ
diff --git a/data/wave.bmp b/data/wave.bmp
new file mode 100644
index 0000000..08f276d
--- /dev/null
+++ b/data/wave.bmp
Binary files differ
diff --git a/data/wave2.bmp b/data/wave2.bmp
new file mode 100644
index 0000000..320d67d
--- /dev/null
+++ b/data/wave2.bmp
Binary files differ
diff --git a/data/wood_drop.aiff b/data/wood_drop.aiff
new file mode 100644
index 0000000..84f982d
--- /dev/null
+++ b/data/wood_drop.aiff
Binary files differ
diff --git a/levelfive.py b/levelfive.py
new file mode 100644
index 0000000..82f6cca
--- /dev/null
+++ b/levelfive.py
@@ -0,0 +1,147 @@
+import random, os.path
+import util
+
+import pygame
+import pygame.font
+from pygame.locals import *
+
+#THE GRAPHICS ON THIS LEVEL ARE SO UGLY. FIX THEM.
+#intro sucks too, players should have the numbers beforehand
+#at least the concept is good =(
+
+#level concept: player has a scrap of paper with numbers on it.
+# these numbers correspond to the answer of math problems
+# that show the correct door among several choices, leading the
+# way through a maze in the cellar
+
+# current state: backend for creating problems is in, and displaying them
+# on the piece of parchment. didnt implement the event handling code because
+# that will change depending on what images i get
+
+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')
+
+
+ background = util.load_image('brick_wall_bg.bmp')
+ door_one = util.load_image('horrid_door.bmp',1)
+ numbers_list_bg = util.load_image('parchment.bmp',1)
+ screen.blit(background,(0,0))
+ screen.blit(door_one, (600-350/2,900-585+10))
+ intro_string = "We have chased Count Mathenstein down into this cellar, and you can hear him mumbling some numbers to himself in the distance, they are probably important, lets write them down!"
+ intro_two = "The numbers sound like this..."
+
+ #remove
+ dev_note = "<<DEV NOTE>> By the way, this level looks HORRIBLE and is not anything like what it will look like. I cant find graphics for the level though, so I just coded it and left the bad placeholders in for now. It is a proof of concept/backend as opposed to a full level at the moment. Ideally, players will be traveling through a maze of boxes/barrels in the cellar of a building chasing after the Count, choosing their path based on a sheet of answers to math problems that they took from the Count that tells the correct way through"
+ util.ok_box(dev_note,(400,300),700,200,screen)
+
+ screen.blit(background,(0,0))
+
+
+
+ util.ok_box(intro_string, (400,300),400,80,screen)
+ screen.blit(background,(0,0))
+ screen.blit(door_one, (600-350/2,900-585+10))
+
+
+
+
+ draw = 1
+ answers = [0,0,0,0,0,0]
+ num_doors = [0,0,0,0,0,0]
+ answer_spot = [0,0,0,0,0,0]
+ numbers_string = ""
+ answer_num = [0,0,0,0,0,0]
+ #division maze?
+ for i in range(0,5):
+ answers[i] = [random.randint(2,9),random.randint(5,20),random.randint(2,9),random.randint(5,20), random.randint(2,9),random.randint(5,20)]
+ num_doors[i] = random.randint(2,3)
+ if num_doors[i] == 2:
+ answer_spot[i] = random.randint(0,1)
+ else:
+ answer_spot[i] = random.randint(0,2)
+ answer_num[i] = answers[i][answer_spot[i]*2+1]
+ numbers_string += " %s" % (answer_num[i])
+ intro_two += numbers_string
+
+
+ util.ok_box(intro_two,(500,300),200,80,screen)
+ screen.blit(background,(0,0))
+ door = 0
+
+
+ while run:
+ events = pygame.event.get()
+ for event in events:
+ #User decides to exit program
+ if event.type == QUIT:
+ run = 0 #stop running
+ pygame.quit()
+
+ #endevents
+ if draw == 1:
+ font = pygame.font.Font(None, 24)
+ screen.blit(background,(0,0))
+ if num_doors[door] == 2:
+ screen.blit(door_one,(200,100))
+ text_rect = pygame.Rect((250,300),(120,50))
+ door_one_text = "%i / %i = __" % (answers[door][0]*answers[door][1], answers[door][0])
+ text_box = util.render_textrect(door_one_text,font,text_rect,(255,255,255),0,0)
+ screen.blit(text_box,text_rect.topleft)
+
+ screen.blit(door_one,(650,100))
+ text_rect = pygame.Rect((700,300),(120,50))
+ door_one_text = "%i / %i = __" % (answers[door][2]*answers[door][3], answers[door][2])
+ text_box = util.render_textrect(door_one_text,font,text_rect,(255,255,255),0,0)
+ screen.blit(text_box,text_rect.topleft)
+ if num_doors[door] == 3:
+ screen.blit(door_one, (50,100))
+ text_rect = pygame.Rect((100,300),(120,50))
+ door_one_text = "%i / %i = __" % (answers[door][0]*answers[door][1], answers[door][0])
+ text_box = util.render_textrect(door_one_text,font,text_rect,(255,255,255),0,0)
+ screen.blit(text_box,text_rect.topleft)
+
+ screen.blit(door_one, (450,100))
+ text_rect = pygame.Rect((500,300),(120,50))
+ door_one_text = "%i / %i = __" % (answers[door][2]*answers[door][3], answers[door][2])
+ text_box = util.render_textrect(door_one_text,font,text_rect,(255,255,255),0,0)
+ screen.blit(text_box,text_rect.topleft)
+
+ screen.blit(door_one, (850,100))
+ text_rect = pygame.Rect((900,300),(120,50))
+ door_one_text = "%i / %i = __" % (answers[door][4]*answers[door][5], answers[door][4])
+ text_box = util.render_textrect(door_one_text,font,text_rect,(255,255,255),0,0)
+ screen.blit(text_box,text_rect.topleft)
+ screen.blit(numbers_list_bg,(800,900-440))
+ text_rect = pygame.Rect((900,900-360),(70,300))
+ font = pygame.font.Font(None, 48)
+ text_box = util.render_textrect(numbers_string[1:],font,text_rect,(255,0,0),0,0)
+ text_box.set_colorkey(text_box.get_at((0, 0)))
+ screen.blit(text_box,text_rect.topleft)
+
+
+
+
+ pygame.display.flip()
+
+ clock.tick(40) #limits to 40 FPS
+
+
+def main():
+ run()
+
+#runs main if called via other naming convention
+if __name__ == '__main__': main()
diff --git a/levelfour.py b/levelfour.py
new file mode 100644
index 0000000..4e1e4e8
--- /dev/null
+++ b/levelfour.py
@@ -0,0 +1,141 @@
+import random, os.path
+import util
+
+import pygame
+import pygame.font
+from pygame.locals import *
+import time
+import math
+
+#to do: possibly change to 5 minute intervals for ease of reading
+# put am/pm on the watches themselves
+
+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')
+
+
+
+ background = util.load_image('watch_drop_bg.bmp')
+
+ watch1 = util.load_image('watch_broken2.bmp')
+ colorkey = watch1.get_at((0, 0))
+ watch1.set_colorkey(colorkey)
+
+ watch2 = util.load_image('watch3.bmp')
+ colorkey = watch2.get_at((0, 0))
+ watch2.set_colorkey(colorkey)
+
+ bg_draw = 1
+ state = (4,1)
+ screen.blit(background,(0,0))
+ intro_string = "We are finally out of the cellar, but now Count Mathenstein is nowhere in sight! But look, he dropped his watch...and it broke when it fell, so it still is showing the time it was when he was here! How long has it been since the Count was last here?"
+ util.ok_box(intro_string,(300,300),600,70,screen)
+ current_hours = int(time.strftime("%H"))
+ current_minutes = int(time.strftime("%M"))
+ random_hours = random.randint(0,current_hours)
+ if random_hours == current_hours and current_minutes > 20:
+ random_minutes = random.randint(10,current_minutes)
+ else:
+ random_minutes = random.randint(0,60)
+ if current_hours % 12 == current_hours and current_hours != 12:
+ am_pm_current = "AM"
+ else:
+ am_pm_current = "PM"
+
+ if random_hours % 12 == random_hours and random_hours != 12:
+ am_pm_broken = "AM"
+ else:
+ am_pm_broken = "PM"
+
+ watch3_mid = (333,441)
+ broken_watch = (880,510)
+ line_length_1 = 100
+ cur_hours = current_hours
+ current_hours = current_hours % 12
+ if current_hours == 0:
+ current_hours = 12
+ elapsed_time = (cur_hours - random_hours)*60+current_minutes-random_minutes
+ print "answer in case its hard to tell for debugging:"
+ print " current time: "
+ print current_hours
+ print current_minutes
+ print am_pm_current
+ print "random time"
+ print random_hours
+ print random_minutes
+ print am_pm_broken
+ print "elapsed: "
+ print elapsed_time
+
+ while run:
+ events = pygame.event.get()
+ for event in events:
+ #User decides to exit program
+ if event.type == QUIT:
+ run = 0 #stop running
+ pygame.quit()
+
+
+ if bg_draw == 1:
+ bg_draw = 0
+ screen.blit(background,(0,0))
+ screen.blit(watch1,(630,100))
+ screen.blit(watch2, (100,100))
+ font = pygame.font.Font(None, 24)
+
+ pygame.draw.line(screen, (0,0,0),watch3_mid,( watch3_mid[0]+50*math.cos(math.radians(90-30*current_hours)), watch3_mid[1]-50*math.sin(math.radians(90-30*current_hours))) ,8)
+ pygame.draw.line(screen, (0,0,0),watch3_mid, (watch3_mid[0]+100*math.cos(math.radians(90-6*current_minutes)), watch3_mid[1]-100*math.sin(math.radians(90-6*current_minutes))), 2)
+
+ pygame.draw.line(screen, (0,0,0),broken_watch, (broken_watch[0]+50*math.cos(math.radians(90-30*random_hours)),broken_watch[1]-50*math.sin(math.radians(90-30*random_hours)) ) ,8)
+ pygame.draw.line(screen, (0,0,0),broken_watch, (broken_watch[0]+100*math.cos(math.radians(90-6*random_minutes)), broken_watch[1]-100*math.sin(math.radians(90-6*random_minutes))), 2)
+
+ text_rect = pygame.Rect((400,800),(30,30))
+ text_box = util.render_textrect(am_pm_current,font,text_rect,(255,255,255),0,0)
+ screen.blit(text_box,text_rect.topleft)
+
+ text_rect = pygame.Rect((900,800),(30,30))
+ text_box = util.render_textrect(am_pm_broken,font,text_rect,(255,255,255),0,0)
+ screen.blit(text_box,text_rect.topleft)
+
+ elapsed_guess = util.ask(screen,"How long has it been since his watch broke? (HH:MM)", 450,800,400)
+ print "hmm"
+ hours_guess = int(elapsed_guess[0:elapsed_guess.find(':')])
+ minutes_guess = int(elapsed_guess[elapsed_guess.find(':')+1:len(elapsed_guess)])
+ guess = hours_guess*60+minutes_guess
+ print "guess"
+ print guess
+
+ if int(guess) == elapsed_time or elapsed_guess == "quit":
+ print "win"
+ util.ok_box("Good job, now we know how long it has been since he was here!",(500,300),200,50,screen)
+ run = 0
+ else:
+ bg_draw = 1
+
+
+
+ pygame.display.flip()
+
+ clock.tick(40) #limits to 40 FPS
+
+
+def main():
+ print "Running Tower"
+ run()
+
+#runs main if called via other naming convention
+if __name__ == '__main__': main()
diff --git a/levelone.py b/levelone.py
new file mode 100644
index 0000000..9581e85
--- /dev/null
+++ b/levelone.py
@@ -0,0 +1,275 @@
+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(os.path.join('data', "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
+ pygame.quit()
+ 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(os.path.join('data', "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(os.path.join('data', "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(os.path.join('data', "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
+
+def main():
+ print "Running bridge lvl one"
+ run()
+
+#runs main if called via other naming convention
+if __name__ == '__main__': main()
diff --git a/levelthree.py b/levelthree.py
new file mode 100644
index 0000000..7c7b577
--- /dev/null
+++ b/levelthree.py
@@ -0,0 +1,274 @@
+import random, os.path
+import util
+import random, os.path, math
+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('OLPC Math Game')
+
+
+ #level 3 var init
+ state = (3,1)
+ num_loaded = 0
+ lvl_three_background = util.load_image('tower_outside.bmp')
+ lvl_three_controls = util.load_image('tower_outside_controls.bmp')
+ #lvl_three_clue_bg = util.load_image('tower_clue_bg.bmp')
+ tower_code = [0,0,0,0,0,0,0,0,0]
+ user_code_guess = [0,0,0,0,0,0,0,0,0]
+ loaded = 0
+ start_clue = random.randint(0,3)
+
+ #gen code
+ c = random.randint(33,99)
+ b = random.randint(33,99)
+ a = c*b
+ tower_code[0] = int(a / 1000)
+ tower_code[1] = int((a % 1000) / 100)
+ tower_code[2] = int((a % 100) / 10)
+ tower_code[3] = int((a % 10) / 1)
+
+ place_1 = random.randint(0,3)
+ #tens, ones, tenths, hundreths
+ lvl_3_digit_1 = random.randint(1000,9999)
+ if place_1 == 0:
+ tower_code[4] = int(lvl_3_digit_1 / 1000)
+ if place_1 == 1:
+ tower_code[4] = int((lvl_3_digit_1 % 1000) / 100)
+ if place_1 == 2:
+ tower_code[4] = int((lvl_3_digit_1 % 100) / 10)
+ if place_1 == 3:
+ tower_code[4] = int((lvl_3_digit_1 % 10) / 1)
+
+ place_2 = random.randint(0,3)
+ #tens, ones, tenths, hundreths
+ lvl_3_digit_2 = random.randint(1000,9999)
+ if place_2 == 0:
+ tower_code[5] = int(lvl_3_digit_2 / 1000)
+ if place_2 == 1:
+ tower_code[5] = int((lvl_3_digit_2 % 1000) / 100)
+ if place_2 == 2:
+ tower_code[5] = int((lvl_3_digit_2 % 100) / 10)
+ if place_2 == 3:
+ tower_code[5] = int((lvl_3_digit_2 % 10) / 1)
+
+ random_spot = random.randint(0,3)
+ #start, middle, end
+ random_pat_num = random.randint(-6,9)
+ random_start_val = random.randint(34,68)
+ pattern = (random_start_val + random_pat_num, random_start_val + random_pat_num*2, random_start_val + random_pat_num*3, random_start_val + random_pat_num*4)
+ tower_code[6] = int(pattern[random_spot] / 10)
+ tower_code[7] = int(pattern[random_spot] % 10)
+ lock = 0
+ done = 0
+ door_rect = pygame.Rect((270,350),(65,65))
+
+
+
+ while run:
+ events = pygame.event.get()
+ for event in events:
+ #User decides to exit program
+ if event.type == QUIT:
+ run = 0 #stop running
+ pygame.quit()
+ if state == (3,1):
+ 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 = (3,2)
+ if state == (3,2):
+ #prevents clues from scrolling super fast
+ if lock > 0:
+ lock = lock - 1
+
+ key_pressed = pygame.key.get_pressed()
+ if key_pressed[K_2]:
+ loaded = 1
+ num_loaded = 2
+ if key_pressed[K_3]:
+ loaded = 1
+ num_loaded = 3
+ if key_pressed[K_5]:
+ loaded = 1
+ num_loaded = 5
+ if key_pressed[K_6]:
+ loaded = 1
+ num_loaded = 6
+ if key_pressed[K_7]:
+ loaded = 1
+ num_loaded = 7
+ if key_pressed[K_8]:
+ loaded = 1
+ num_loaded = 8
+ if key_pressed[K_9]:
+ loaded = 1
+ num_loaded = 9
+ if key_pressed[K_1]:
+ loaded = 1
+ num_loaded = 1
+ if key_pressed[K_4]:
+ loaded = 1
+ num_loaded = 4
+
+ if event.type == pygame.MOUSEBUTTONDOWN:
+
+
+ (x, y) = pygame.mouse.get_pos()
+ #if x > 41 and x < 104
+ #41, 104, 138
+ #piece loaded = x - 41
+ #41,137, 170
+
+ #these two lines of code rock
+ #description: takes the position of your mouse, subtracts the area to the left of the first button
+ #from the x coord, mods that by the distance from the start of the screen to the start of the 2nd button
+ #minus the start of the screen to the first button, and if that is less then the distance to the end of
+ #the first button plus one minus the distance from the start of the screen to the first button and then
+ #checks to make sure the x componenet of the mouse is between the start of the first button and the end
+ #of the last button and the y is between the top/bottom of the buttons
+
+ # and if all thats true, it sets the number being carried to the int of the ceil of the x minus the
+ #distance between the start of the screen and the first button divided by the distance to the start
+ #of the second button minus the distance to the start of the first button
+
+ if (x-41) % (138-41) < (104+1-41) and x > 41 and x < 877+34+(104-41) and y > 803 and y < 867:
+ num_loaded = int(math.ceil((x-41)/(138-41)))
+ loaded = 1
+ if loaded == 1:
+ if (x-41) % (170-41) < (137+1-41) and x > 41 and x < 1162 and y > 611 and y < 707:
+ user_code_guess[int(math.ceil((x-41)/(170-41)))] = num_loaded
+ loaded = 0
+
+ if x > 1000 and x < 1160 and y > 804 and y < 864:
+ if lock == 0:
+ lock = 20
+ start_clue = (start_clue + 1) % 4
+ if done == 1 and door_rect.collidepoint((x,y)) == 1:
+ run = 0
+
+
+ if state == (3,1):
+ #lvl_three_background = util.load_image('tower_outside.bmp')
+ screen.blit(lvl_three_background, (0, 0))
+ font = pygame.font.Font(None, 24)
+ intro_string = "The path ends at a tall tower, but the door is locked! Enter the password and click on the door to enter the tower."
+ 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 == (3,2):
+
+ screen.blit(lvl_three_background, (0, 0))
+
+ screen.blit(lvl_three_controls, (7,900-322))
+
+ #display 0-9
+ font = pygame.font.Font(None, 72)
+ for i in range(0,10):
+ screen.blit(font.render("%s" %i, 1, (255,255,255)), (60+i*96,810))
+ for i in range(0,8):
+ screen.blit(font.render("%s" %user_code_guess[i], 1, (255,255,255)), (77+i*128,635))
+
+ font = pygame.font.Font(None, 32)
+ screen.blit(font.render("Next Clue", 1, (255,255,255)), (50+9*96+115,820))
+
+ (x, y) = pygame.mouse.get_pos()
+ if loaded == 1:
+ screen.blit(font.render("%s" % num_loaded, 1, (255,255,255)), (x+10,y+10))
+
+ #screen.blit(lvl_three_clue_bg,(600,150))
+
+ font = pygame.font.Font(None, 32)
+ text_rect = pygame.Rect((620,170),(440,170))
+ text_rect = pygame.Rect((620,70),(440,170))
+ clue_string = ""
+ lvl_3_digit_1 += 0.0
+ lvl_3_digit_2 += 0.0
+ if start_clue == 0:
+ clue_string = "The first four numbers in the code are the answer to this problem: %s * %s = __" % (c,b)
+ if start_clue == 1:
+ if place_1 == 0:
+ place_1_name = "tens"
+ if place_1 == 1:
+ place_1_name = "ones"
+ if place_1 == 2:
+ place_1_name = "tenths"
+ if place_1 == 3:
+ place_1_name = "hundreths"
+ clue_string = "The fifth number in the code is the number in the %s place of this number: %s" % (place_1_name,lvl_3_digit_1/100)
+ if start_clue == 2:
+ if place_2 == 0:
+ place_2_name = "tens"
+ if place_2 == 1:
+ place_2_name = "ones"
+ if place_2 == 2:
+ place_2_name = "tenths"
+ if place_2 == 3:
+ place_2_name = "hundreths"
+ clue_string = "The sixth number in the code is the number in the %s place of this number: %s" % (place_2_name,lvl_3_digit_2/100)
+
+
+ if start_clue == 3:
+ if random_spot == 0:
+ clue_string = "The seventh and eighth numbers are the number missing from this pattern: __, %s, %s, %s" % (pattern[1],pattern[2],pattern[3])
+ if random_spot == 1:
+ clue_string = "The seventh and eighth numbers are the number missing from this pattern: %s, __, %s, %s" % (pattern[0],pattern[2],pattern[3])
+ if random_spot == 2:
+ clue_string = "The seventh and eighth numbers are the number missing from this pattern: %s, %s, __, %s" % (pattern[0],pattern[1],pattern[3])
+ if random_spot == 3:
+ clue_string = "The seventh and eighth numbers are the number missing from this pattern: %s, %s, %s, __" % (pattern[0],pattern[1],pattern[2])
+
+
+
+
+ text_box = util.render_textrect(clue_string,font,text_rect,(255,255,255),1,1)
+ text_box.set_colorkey((0,0,0))
+ font = pygame.font.Font(None, 118)
+ if user_code_guess == tower_code:
+ screen.blit(font.render("O", 1, (0,255,0)), (60+8*128,622))
+ done = 1
+ else:
+ screen.blit(font.render("X", 1, (255,0,0)), (63+8*128,622))
+ done = 0
+
+
+ #text_box.set_alpha(0)
+ screen.blit(text_box,text_rect.topleft)
+
+ pygame.display.flip()
+
+ clock.tick(40) #limits to 40 FPS
+
+
+def main():
+ print "Running Tower"
+ run()
+
+#runs main if called via other naming convention
+if __name__ == '__main__': main()
diff --git a/leveltwo.py b/leveltwo.py
new file mode 100644
index 0000000..b89013a
--- /dev/null
+++ b/leveltwo.py
@@ -0,0 +1,167 @@
+import random, os.path
+import util
+
+import pygame
+import pygame.font
+from pygame.locals import *
+import string
+
+
+
+
+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 two setup
+ background = util.load_image('fork.bmp')
+ map_bg = util.load_image('map.bmp')
+ map_foreground = util.load_image('map_foreground2.bmp')
+
+ house1 = util.load_image('house_map.bmp')
+ colorkey = house1.get_at((0, 0))
+ house1.set_colorkey(colorkey)
+
+ tower = util.load_image('tower_map.bmp')
+ tower.set_colorkey((255,255,255))
+ tower.convert_alpha()
+
+
+
+ house2 = util.load_image('house2_map.bmp')
+ colorkey = house2.get_at((0, 0))
+ house2.set_colorkey(colorkey)
+
+ stonehedge = util.load_image('stonehedge_map.bmp')
+ colorkey = stonehedge.get_at((0, 0))
+ stonehedge.set_colorkey(colorkey)
+
+ map_image = [house1,house2,stonehedge]
+ screen.blit(background,(0,0))
+ state = (2,1)
+ bg_draw=1
+
+ map_points = [(480,230), (715,295),(812,218), (818,307), (812,451)]
+
+ win = 0
+
+ rand_array = [random.randint(0,2),random.randint(0,2),random.randint(0,2),random.randint(0,2),random.randint(0,2)]
+ tower_spot = random.randint(0,4)
+ tower_xy = map_points[tower_spot]
+ win_coord = ["f3","e6","f7","e7","c7"]
+ while run:
+ events = pygame.event.get()
+ for event in events:
+ #User decides to exit program
+ if event.type == QUIT:
+ run = 0 #stop running
+ pygame.quit()
+ if state == (2,1):
+ 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 = (2,2)
+ if state == (2,3):
+ 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:
+ run = 0
+
+ #end events
+ if state == (2,1): #level one, intro
+ #background = load_image('bridge_bg_2.bmp')
+ screen.blit(background, (0, 0))
+ font = pygame.font.Font(None, 24)
+ intro_string = "There is a fork in the road; it is a good thing we picked up that map! Find the tower on the map by entering the closest coordinant"
+ 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 == (2,2):
+ if bg_draw==1:
+ screen.blit(background,(0,0))
+ bg_draw=0
+ map_bg.set_colorkey((0,0,0))
+ screen.blit(map_bg, (100,100))
+ font = pygame.font.Font(None, 24)
+ screen.blit(map_foreground,(279,200))
+
+
+
+ screen.blit(tower,tower_xy)
+ for i in range(0,5):
+ if map_points[i] != tower_xy:
+ screen.blit(map_image[rand_array[i]],map_points[i])
+
+ pygame.draw.line(screen, [0,0,0], (275,200+13*40),(915,13*40+200),7)
+ pygame.draw.line(screen, [0,0,0], (275,200),(275,720),7)
+ for i in range(0,7): # down
+ y_coord = font.render("%s" % chr(i+65), 1, (255, 0, 0))
+ screen.blit(y_coord,(255,670-i*80))
+ pygame.draw.line(screen, [0,0,0], (275,i*80+200),(915,i*80+200))
+ for i in range(0,9): # across
+ x_coord = font.render("%s" % i, 1, (255, 0, 0))
+ screen.blit(x_coord,(270+i*80,725))
+ pygame.draw.line(screen, [0,0,0], (275+i*80,200),(275+i*80,720))
+ text_rect = pygame.Rect(500,815,300,30)
+ text_box = util.render_textrect("Hint: The red dot is on coordinent a3",font,text_rect,(255,255,255),0,0)
+ #text_box.set_colorkey((0,0,0))
+ screen.blit(text_box,text_rect.topleft)
+ pygame.draw.circle(screen, (255,0,0),(515,678),10)
+ coord = util.ask(screen,"What coordinant is the tower closest to?",500,775,300)
+ if coord == win_coord[tower_spot]:
+ state = (2,3)
+ else:
+ bg_draw=1
+
+
+ if state == (2,3):
+ screen.blit(background, (0, 0))
+ font = pygame.font.Font(None, 24)
+ intro_string = "Great job finding the tower on the map! Now we know which path to take...and it's the scary one"
+ 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))
+
+ pygame.display.flip()
+
+ clock.tick(40) #limits to 40 FPS
+
+
+def main():
+ run()
+
+#runs main if called via other naming convention
+if __name__ == '__main__': main()
diff --git a/race.py b/race.py
new file mode 100644
index 0000000..9fbf50a
--- /dev/null
+++ b/race.py
@@ -0,0 +1,461 @@
+import random, os.path, math
+import util
+
+import pygame
+import pygame.font
+from pygame.locals import *
+play_pos = 0
+def onscreen(other_pos):
+ global play_pos
+ top = player_pos+600
+ bottom = player_pos-50
+ if other_pos > top or other_pos < bottom:
+ return 0
+ return 1
+
+def gen_question():
+ operator = random.randint(0,3)
+ if operator == 0:
+ #subtraction
+ first_num = random.randint(0,100)
+ second_num = random.randint(0,first_num)
+ answer = first_num-second_num
+ oper = "-"
+
+ elif operator == 1:
+ #addition
+ first_num = random.randint(0,100)
+ second_num = random.randint(0,100)
+ oper = "+"
+ answer = first_num+second_num
+
+ elif operator == 2:
+ #multiplication
+ first_num = random.randint(0,12)
+ second_num = random.randint(0,12)
+ answer = first_num*second_num
+
+ oper = "*"
+ elif operator == 3:
+ #division
+ answer = random.randint(1,12)
+ second_num = random.randint(1,12)
+ first_num = answer*second_num
+ oper = "/"
+
+
+
+ return (answer,first_num,second_num,oper)
+
+
+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')
+
+ global player_pos
+ #draw grass
+ #pygame.draw.rect(screen,(0,255,0),(0,0,1200,900))
+ #draw water
+ #pygame.draw.polygon(screen,(0,131,164),((350,0),(850,0),(900,900),(300,900),(350,0)))
+
+
+ #draw gradiant
+ # x1 = 0
+ #x2 = 1200
+ #a = (0,130,165)
+ ##b = (117,181,227)
+ #b = (200,230,255)
+ #y1 = 200
+ #y2 = 900
+ #h = 700
+ #rate = (float((b[0]-a[0])/h),(float(b[1]-a[1])/h),(float(b[2]-a[2])/h))
+ #for line in range(y1,y2):
+ # color = (min(max(a[0]+(rate[0]*line),0),255),min(max(a[1]+(rate[1]*line),0),255), min(max(a[2]+(rate[2]*line),0),255))
+ # pygame.draw.line(screen,color,(x1,line),(x2,line))
+ #
+ #pygame.draw.polygon(screen,(100,255,100),((0,100),(400,100),(0,750),(0,100)))
+ #pygame.draw.polygon(screen,(100,255,100),((1200,100),(800,100),(1200,750),(1200,100)))
+
+
+
+ #draw sky
+ # pygame.draw.rect(screen,(100,100,255),(0,0,1200,200))
+
+
+ #redo this so we dont have to store so many, should be easy, just append each time one falls offscreen negatively
+ warning_string = "This level is a work in progress, currently you hit the buttons at the top to speed up your racer (I dont have a graphic made for it yet, so its the house on the left), to try to win a race. There is no end condition yet, and a lot of the graphics arent working quite right."
+ util.ok_box(warning_string,(400,400),600,300,screen)
+
+ markers = []
+ waves_array=[]
+ for i in range(0,100000):
+ y = random.randint(0,80)
+ if y==0:
+ x = random.randint(100,1000)
+ #xh = random.sample((200,1000,500),1)
+ #x = xh[0]
+ waves_array.append((x,i))
+ if i % 400 == 0 and i > 400:
+ markers.append([i,0])
+ click_button = 0
+ tick = 0
+ player_pos=0
+ bad_one_pos=0
+ bad_one_vel=4
+ player_vel=3
+ start_banner = 15
+ player_x = 500
+ player_pos_draw = 800
+ bg_image = util.load_image("lake_bg.bmp")
+ player_image = util.load_image("house2_map.bmp",1)
+ player = (player_image,(player_x,player_pos_draw))
+ wave_image = util.load_image("wave.bmp",1)
+ marker_image = util.load_image("bridge_piece3.bmp")
+ wave_rect = pygame
+ draw_list = []
+
+
+ bad_one_image = util.load_image("house_map.bmp",1)
+ bad_one = (bad_one_image,(700,800))
+
+ screen.blit(bg_image,(0,0))
+ #pygame.draw.line(screen,(117,184,215),(300,205),(0,800))
+ pygame.draw.line(screen,(255,255,255),(200,203),(0,800))
+
+ #800-203/1000-1200
+ #-597/200
+
+ ###questions
+
+ #4.N.10 Select and use appropriate operations (addition, subtraction, multiplication, and division) to solve problems, including those involving money.
+
+
+
+
+ quest_rect = pygame.Rect(0,0,200,40)
+ answer_one_rect = pygame.Rect(0,40,50,60)
+ answer_two_rect = pygame.Rect(50,40,50,60)
+ answer_three_rect = pygame.Rect(100,40,50,60)
+ answer_four_rect = pygame.Rect(150,40,50,60)
+
+ new_question = 1
+ player_vel = 2
+ bad_one_vel = 2
+
+ ###
+
+ #pygame.draw.line(screen,(117,184,215),(900,205),(1200,800))
+ pygame.draw.line(screen,(255,255,255),(1000,203),(1200,800))
+ pygame.display.flip()
+ #print waves_array
+ #change = 1
+ #stone = [util.load_image("stonehedge_map.bmp",1),[0,400]]
+ #draw_list.append(stone)
+ while run:
+ events = pygame.event.get()
+ for event in events:
+ #User decides to exit program
+ if event.type == QUIT:
+ run = 0 #stop running
+ pygame.quit()
+ click_button = 0
+ if tick > 0:
+ tick = tick - 1
+ if tick == 0:
+
+ if event.type == pygame.MOUSEBUTTONDOWN:
+
+ (x, y) = pygame.mouse.get_pos()
+ if answer_one_rect.collidepoint((x,y)):
+ click_button = 1
+ if answer_two_rect.collidepoint((x,y)):
+ click_button = 2
+ if answer_three_rect.collidepoint((x,y)):
+ click_button = 3
+ if answer_four_rect.collidepoint((x,y)):
+ click_button = 4
+ tick+=20
+ #print click_button
+
+
+
+
+ update_rects = []
+
+ #figure out what we need to draw
+ #first draw over the old stuff
+ for i in draw_list:
+ screen.blit(bg_image,i[1],pygame.Rect(i[1],i[0].get_size()))
+ new_rect = pygame.Rect(i[1],i[0].get_size())
+ update_rects.append(new_rect)
+
+
+
+
+ #draw_list.append(stone)
+
+ # if stone[1][0] == 900:
+ # change = -1
+ # if stone[1][0] == 100 and change == -1:
+ # change = 1
+ # stone[1][0] += change
+
+ draw_list = []
+
+ #update contestants positions
+
+
+
+
+ player_plus_vel = 2
+ bad_plus_vel = 1
+ player_minus_vel = 2
+ bad_minus_vel = 1
+ if click_button != 0:
+
+ #print "old vel " + str(player_vel)
+ if click_button == 1:
+ if question[1] + question[2] == question[0]:
+ new_question =1
+ player_vel+= player_plus_vel
+ bad_one_vel+=bad_plus_vel
+ else:
+ player_vel-=player_minus_vel
+ bad_one_vel-= bad_minus_vel
+ if click_button == 2:
+ if question[1] - question[2] == question[0]:
+ new_question =1
+ player_vel+= player_plus_vel
+ bad_one_vel+=bad_plus_vel
+ else:
+ player_vel-=player_minus_vel
+ bad_one_vel-= bad_minus_vel
+ if click_button == 3:
+ if question[1] * question[2] == question[0]:
+ new_question =1
+ player_vel+= player_plus_vel
+ bad_one_vel+=bad_plus_vel
+ else:
+ player_vel-=player_minus_vel
+ bad_one_vel-= bad_minus_vel
+ if click_button == 4:
+ if question[1] / question[2] == question[0]:
+ new_question =1
+ player_vel+= player_plus_vel
+ bad_one_vel+=bad_plus_vel
+ else:
+ player_vel-=player_minus_vel
+ bad_one_vel-= bad_minus_vel
+
+ if player_vel < 0:
+ player_vel = 0
+ if bad_one_vel < 0:
+ bad_one_vel = 0
+
+ click_button = 0
+ # print "new vel " + str(player_vel)
+
+ if new_question == 1:
+ question = gen_question()
+ question_string = "" + str(question[1]) + " " + " _ " + " " + str(question[2]) + " = " + str(question[0])
+ #print question_string
+ font2 = pygame.font.Font(None, 36)
+
+ #rawr = util.render_textrect(question_string,font,quest_rect,(255,255,255),(0,0,0),1,1)
+ #rawr21 = (rawr,quest_rect.topleft())
+ #draw_list.append(rawr21)
+
+ #draw_list.append( )
+ #draw_list.append((util.render_textrect(str(question[4]),font,answer_one_rect,(255,255,255),0,1,1),answer_one_rect.topleft))
+ #draw_list.append((util.render_textrect(str(question[5]),font,answer_two_rect,(255,255,255),(0,0,0),1,1),answer_two_rect.topleft))
+ #draw_list.append((util.render_textrect(str(question[6]),font,answer_three_rect,(255,255,255),(0,0,0),1,1),answer_three_rect.topleft))
+ #draw_list.append((util.render_textrect(str(question[7]),font,answer_four_rect,(255,255,255),(0,0,0),1,1),answer_four_rect.topleft))
+
+ #screen.blit(util.render_textrect(question_string,font,quest_rect,(255,255,255),(0,0,0),1,1),quest_rect.topleft)
+ #screen.blit(util.render_textrect(str(question[4]),font,answer_one_rect,(255,255,255),(0,0,0),1,1),answer_one_rect.topleft)
+ #screen.blit(util.render_textrect(str(question[5]),font,answer_two_rect,(255,255,255),(0,0,0),1,1),answer_two_rect.topleft)
+ #screen.blit(util.render_textrect(str(question[6]),font,answer_three_rect,(255,255,255),(0,0,0),1,1),answer_three_rect.topleft)
+ #screen.blit(util.render_textrect(str(question[7]),font,answer_four_rect,(255,255,255),(0,0,0),1,1),answer_four_rect.topleft)
+
+ quest_string_rect = util.render_textrect(question_string,font2,quest_rect,(255,255,255),0,1)
+ font2 = pygame.font.Font(None, 72)
+ add_rect = util.render_textrect("+",font2,answer_one_rect,(255,255,255),0,1,1)
+ sub_rect = util.render_textrect("-",font2,answer_two_rect,(255,255,255),0,1,1)
+ mult_rect = util.render_textrect("*",font2,answer_three_rect,(255,255,255),0,1,1)
+ divide_rect = util.render_textrect(u"\u00F7",font2,answer_four_rect,(255,255,255),0,1,1)
+
+
+
+ #quest_string_rect.set_colorkey(0)
+
+ screen.blit(quest_string_rect,(0,0))
+ screen.blit(add_rect,answer_one_rect.topleft)
+ screen.blit(sub_rect,answer_two_rect.topleft)
+ screen.blit(mult_rect,answer_three_rect.topleft)
+ screen.blit(divide_rect,answer_four_rect.topleft)
+
+
+ pygame.draw.rect(screen,(255,0,0),answer_one_rect,5)
+ pygame.draw.rect(screen,(255,0,0),answer_two_rect,5)
+ pygame.draw.rect(screen,(255,0,0),answer_three_rect,5)
+ pygame.draw.rect(screen,(255,0,0),answer_four_rect,5)
+
+
+ #draw_list.append((utilx,quest_rect.topleft))
+
+ #pygame.draw.line(screen,(0,0,0),(0,0),(500,10))
+ #print "WTF"
+ pygame.display.flip()
+
+ #update_rects.append(quest_rect)
+ #update_rects.append(answer_one_rect)
+ #update_rects.append(answer_two_rect)
+ #update_rects.append(answer_three_rect)
+ #update_rects.append(answer_four_rect)
+ new_question = 0
+
+
+ #question_string +=
+
+
+
+
+ #player_vel = 0
+ #bad_one_vel = 0
+
+
+ player_pos=player_pos+player_vel
+ bad_one_pos = bad_one_pos+bad_one_vel
+ if bad_one_pos - player_pos > 100:
+ bad_one_vel+=1
+ if bad_one_vel - player_pos < -100:
+ bad_one_vel-=1
+ player_pos_draw = 800-player_vel
+ player = (player_image,(500,player_pos_draw))
+
+ if onscreen(bad_one_pos):
+
+ bad_one_pos_draw = 800+(player_pos-bad_one_pos)
+ bad_one = (bad_one_image,(600,bad_one_pos_draw))
+ draw_list.append(bad_one)
+
+ #now draw new frame
+
+
+ draw_list.append(player)
+
+ k = 0
+ for i in markers:
+ y_pos = i[0]
+ x_pos = i[1]
+ if onscreen(y_pos):
+ markers[k][1] += 3
+ #x_1 = 300-markers[k][1]
+ #print x_pos
+ y_loc = 800+(player_pos-y_pos)
+ font_size = 12+14*(y_loc-200)/600
+ #if font_size != 8:
+ # print font_size
+ font = pygame.font.Font(None, font_size)
+ text_rect = pygame.Rect((300-x_pos,800+(player_pos-y_pos)),(10+font_size,10+font_size))
+ text_box = util.render_textrect(str(k),font,text_rect,(255,255,255),1,1)
+
+ x_pos = (-1*(800+(player_pos-y_pos))+800)/3-12
+ #draw_list.append((marker_image,(x_pos,800+(player_pos-y_pos))))
+ draw_list.append((text_box,((x_pos+2,802+(player_pos-y_pos)))))
+ x_pos = (-1*(800+(player_pos-y_pos))+800)/(-3)+1200
+
+
+ #draw_list.append((marker_image,(x_pos,800+(player_pos-y_pos))))
+
+
+ draw_list.append((text_box,((x_pos+2,802+(player_pos-y_pos)))))
+ if y_pos > player_pos+800:
+ break
+ k+=1
+
+ for i in waves_array:
+ wave_x = i[0]
+ wave_y = i[1]
+ if onscreen(wave_y):
+
+ #pygame.draw.line(screen,(255,255,0),(wave_x,203),((wave_x-200)*3/2,800))
+ #print wave_x
+ if wave_x == 200 or wave_x == 600:
+ wave_x +=1
+
+ wave_x = int(((-1*(800+(player_pos-wave_y)))-203)/((203-800)/(-wave_x+(wave_x-200)*3.0/2.0))+wave_x)
+ if wave_x-500 > 0:
+ wave_x-=int(math.fabs(x-600)/400.0*150)
+ elif 700-wave_x>0:
+ wave_x+=int(math.fabs(x-600)/400.0*150)
+
+ y_loc = 800+(player_pos-wave_y)
+ size_scale = (y_loc-200)/600.0
+ #print size_scale
+ if size_scale < 0:
+ size_scale = 0
+ wave_height = 10+int(28*size_scale)
+ wave_width = 20+int(50*size_scale)
+
+ wave_x-=wave_width/2
+
+ #print wave_width
+ wave_image = pygame.Surface((wave_width,wave_height))
+ pygame.draw.ellipse(wave_image,(200,200,255),pygame.Rect((0,0),(wave_width,wave_height)))
+ pygame.draw.ellipse(wave_image,(100,255,100),pygame.Rect((0,0),(wave_width,wave_height)),5)
+
+ wave_image.blit(bg_image,(0,wave_height/2),pygame.Rect( (wave_x,800+(player_pos-wave_y)) ,(wave_width,3+wave_height)))
+ wave_image.set_alpha(100)
+ wave_image.set_colorkey(wave_image.get_at((0, 0)))
+
+ #wave_image_2 = scaleUp(wave_image,50+((800+(player_pos-wave_y))-203)/(900-203)*50)
+ draw_list.append((wave_image,(wave_x,800+(player_pos-wave_y))))
+ #if wave_x - player_pos < 600 and wave_x - player_pos > -100:
+ # draw_list.append(((wave_image),(wave_x,wave_y),(wave_image.get_size())))
+ if i[1] > player_pos+800:
+ break
+
+ #pygame.draw.line(screen,(117,184,215),(300,205),(0,800))
+ pygame.draw.line(screen,(255,255,255),(200,205),(0,800))
+
+ #pygame.draw.line(screen,(117,184,215),(900,205),(1200,800))
+ pygame.draw.line(screen,(255,255,255),(1000,205),(1200,800))
+ for i in draw_list:
+ screen.blit(i[0],(i[1]))
+ #new_rect = pygame.Rect(b,c)
+ new_rect = pygame.Rect(i[1],i[0].get_size())
+ update_rects.append(new_rect)
+
+
+
+
+
+
+
+
+ #pygame.display.flip()
+ #print "updating rects"
+ pygame.display.update(update_rects)
+
+ clock.tick(40) #limits to 40 FPS
+
+
+def main():
+ run()
+
+#runs main if called via other naming convention
+if __name__ == '__main__': main()
diff --git a/start.py b/start.py
new file mode 100644
index 0000000..dfd9cac
--- /dev/null
+++ b/start.py
@@ -0,0 +1,10 @@
+import tower, levelone, leveltwo, levelthree, levelfour, util
+
+def main():
+ levelone.run()
+ levelfour.run()
+ leveltwo.run()
+ levelthree.run()
+ tower.run()
+
+if __name__ == '__main__': main()
diff --git a/template.py b/template.py
new file mode 100644
index 0000000..706d38e
--- /dev/null
+++ b/template.py
@@ -0,0 +1,44 @@
+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('OLPC Math Game')
+
+
+ while run:
+ events = pygame.event.get()
+ for event in events:
+ #User decides to exit program
+ if event.type == QUIT:
+ run = 0 #stop running
+ pygame.quit()
+
+
+ pygame.display.flip()
+
+ clock.tick(40) #limits to 40 FPS
+
+
+def main():
+ run()
+
+#runs main if called via other naming convention
+if __name__ == '__main__': main()
diff --git a/tower.py b/tower.py
new file mode 100644
index 0000000..be61962
--- /dev/null
+++ b/tower.py
@@ -0,0 +1,639 @@
+import random, os.path
+import util
+
+import pygame
+import pygame.font
+from pygame.locals import *
+
+#Global variables:
+
+#List representing the range of the numbers that are in the gaps on each level, the first level has numbers from 0~20 and so on
+levelRanges = [(0,20),(10,30),(20,40),(30,50),(45,65),(60,80),(65,85),(80,100)]
+
+#Generates a tower "floor", given the amount of holes in the floor and the height on the screen it will be placed on
+#Returns a list of 4 things each representing different aspects of the floor:
+# 1. A list of the rectangles which make up the "walls" of the floor"
+# 2. A list of the rectangles which represent the gaps between the floor walls
+# 3. A list of the rectangles which will contain the numbers (all within the walls, on either side of the gaps)
+# 4. A list representing which number boxes are associated with each gap
+
+def generateFloor(holes, height, type = 1):
+ walls, gaps = [], []
+ numBoxes = []
+ rect = []
+ gapPattern = []
+ if holes == 1:
+ holeSize = random.randint(10,50)*10
+ holeStart = random.randint(0,70-holeSize/10)*10
+
+ walls.append(pygame.Rect(150,height,100+holeStart,50))
+ walls.append(pygame.Rect(250+holeStart+holeSize,height,800-holeStart-holeSize,50))
+
+ gaps.append(pygame.Rect(250+holeStart, height, holeSize, 20))
+
+ numBoxes.append(pygame.Rect(155+holeStart, height+5, 90, 40))
+ numBoxes.append(pygame.Rect(255+holeStart+holeSize,height+5,90,40))
+
+ gapPattern = [(0,1)]
+
+ elif holes == 2 and type == 1:
+
+ hole1Size = random.randint(10,24)*10
+ hole2Size = random.randint(10,24)*10
+ centerSize = random.randint(22,70-hole1Size/10-hole2Size/10)*10
+ holeStart = random.randint(0,70-hole1Size/10-hole2Size/10-centerSize/10)*10
+
+ #print "Hole 1 Size:%d\nHole 2 Size: %d\n Center Size: %d\nHole Start: %d"%(hole1Size,hole2Size,centerSize,holeStart)
+
+ walls.append(pygame.Rect(150,height,100+holeStart,50))
+ walls.append(pygame.Rect(250+holeStart+hole1Size,height,centerSize,50))
+ walls.append(pygame.Rect(250+holeStart+hole1Size+centerSize+hole2Size,height,800-(holeStart+hole1Size+centerSize+hole2Size),50))
+
+ gaps.append(pygame.Rect(250+holeStart,height,hole1Size,20))
+ gaps.append(pygame.Rect(250+holeStart+hole1Size+centerSize,height,hole2Size,20))
+
+ numBoxes.append(pygame.Rect(155+holeStart,height+5,90,40))
+ numBoxes.append(pygame.Rect(255+holeStart+hole1Size,height+5,90,40))
+ numBoxes.append(pygame.Rect(155+holeStart+hole1Size+centerSize,height+5,90,40))
+ numBoxes.append(pygame.Rect(255+holeStart+hole1Size+centerSize+hole2Size,height+5,90,40))
+
+ gapPattern = [(0,1),(2,3)]
+
+ elif holes == 2 and type != 1:
+ #Randomly assigns hole sizes
+ hole1Size = random.randint(10,50)*10
+ hole2Size = random.randint(10,60-(hole1Size/10))*10
+ holeStart = random.randint(0,60-(hole1Size+hole2Size)/10)*10
+
+ walls.append(pygame.Rect(150,height,100+holeStart,50))
+ walls.append(pygame.Rect(250+holeStart+hole1Size,height,100,50))
+ walls.append(pygame.Rect(350+holeStart+hole1Size+hole2Size,height,700-holeStart-hole1Size-hole2Size,50))
+
+ gaps.append(pygame.Rect(250+holeStart, height, hole1Size, 20))
+ gaps.append(pygame.Rect(350+holeStart+hole1Size, height, hole2Size, 20))
+
+ numBoxes.append(pygame.Rect(155+holeStart,height+5,90,40))
+ numBoxes.append(pygame.Rect(255+holeStart+hole1Size,height+5,90,40))
+ numBoxes.append(pygame.Rect(355+holeStart+hole1Size+hole2Size,height+5,90,40))
+
+ gapPattern = [(0,1),(1,2)]
+
+ elif holes == 3 and type == 1:
+ holeStart = random.randint(0,2)*10
+ hole1Size = random.randint(0,1)*10
+ hole2Size = random.randint(0,2)*10
+ hole3Size = random.randint(0,1)*10
+ centerSize = random.randint(0,3)*10
+
+ if random.randint(0,1) == 1:
+ walls.append(pygame.Rect(150,height,100+holeStart,50))
+ walls.append(pygame.Rect(350+holeStart+hole1Size,height,210+centerSize,50))
+ walls.append(pygame.Rect(660+holeStart+hole1Size+centerSize+hole2Size,height,100,50))
+ walls.append(pygame.Rect(860+holeStart+hole1Size+centerSize+hole2Size+hole3Size,height,190-(holeStart+hole1Size+centerSize+hole2Size+hole3Size),50))
+
+ gaps.append(pygame.Rect(250+holeStart,height,100+hole1Size,20))
+ gaps.append(pygame.Rect(560+holeStart+hole1Size+centerSize,height,100+hole2Size,20))
+ gaps.append(pygame.Rect(760+holeStart+hole1Size+centerSize+hole2Size,height,100+hole3Size,20))
+
+ numBoxes.append(pygame.Rect(155+holeStart,height+5,90,40))
+ numBoxes.append(pygame.Rect(355+holeStart+hole1Size,height+5,90,40))
+ numBoxes.append(pygame.Rect(465+holeStart+hole1Size+centerSize,height+5,90,40))
+ numBoxes.append(pygame.Rect(665+holeStart+hole1Size+centerSize+hole2Size,height+5,90,40))
+ numBoxes.append(pygame.Rect(865+holeStart+hole1Size+centerSize+hole2Size+hole3Size,height+5,90,40))
+
+ gapPattern = [(0,1),(2,3),(3,4)]
+
+ else:
+ walls.append(pygame.Rect(150,height,100+holeStart,50))
+ walls.append(pygame.Rect(350+holeStart+hole1Size,height,100,50))
+ walls.append(pygame.Rect(550+holeStart+hole1Size+hole2Size,height,210+centerSize,50))
+ walls.append(pygame.Rect(860+holeStart+hole1Size+centerSize+hole2Size+hole3Size,height,190-(holeStart+hole1Size+centerSize+hole2Size+hole3Size),50))
+
+ gaps.append(pygame.Rect(250+holeStart,height,100+hole1Size,20))
+ gaps.append(pygame.Rect(450+holeStart+hole1Size,height,100+hole2Size,20))
+ gaps.append(pygame.Rect(760+holeStart+hole1Size+centerSize+hole2Size,height,100+hole3Size,20))
+
+ numBoxes.append(pygame.Rect(155+holeStart,height+5,90,40))
+ numBoxes.append(pygame.Rect(355+holeStart+hole1Size,height+5,90,40))
+ numBoxes.append(pygame.Rect(555+holeStart+hole1Size+hole2Size,height+5,90,40))
+ numBoxes.append(pygame.Rect(665+holeStart+hole1Size+centerSize+hole2Size,height+5,90,40))
+ numBoxes.append(pygame.Rect(865+holeStart+hole1Size+centerSize+hole2Size+hole3Size,height+5,90,40))
+
+ gapPattern = [(0,1),(1,2),(3,4)]
+
+ elif holes == 3 and type != 1:
+
+ holeStart = random.randint(0,10)*10
+ hole1Size = random.randint(0,15-holeStart/10)*10
+ hole2Size = random.randint(0,17-(holeStart+hole1Size)/10)*10
+ hole3Size = random.randint(0,20-(holeStart+hole1Size+hole2Size)/10)*10
+
+ walls.append(pygame.Rect(150,height,100+holeStart,50))
+ walls.append(pygame.Rect(350+holeStart+hole1Size,height,100,50))
+ walls.append(pygame.Rect(550+holeStart+hole1Size+hole2Size,height,100,50))
+ walls.append(pygame.Rect(750+holeStart+hole1Size+hole2Size+hole3Size,height,300-(holeStart+hole1Size+hole2Size+hole3Size),50))
+
+ gaps.append(pygame.Rect(250+holeStart,height,100+hole1Size,20))
+ gaps.append(pygame.Rect(450+holeStart+hole1Size,height,100+hole2Size,20))
+ gaps.append(pygame.Rect(650+holeStart+hole1Size+hole2Size,height,100+hole3Size,20))
+
+ numBoxes.append(pygame.Rect(155+holeStart,height+5,90,40))
+ numBoxes.append(pygame.Rect(355+holeStart+hole1Size,height+5,90,40))
+ numBoxes.append(pygame.Rect(555+holeStart+hole1Size+hole2Size,height+5,90,40))
+ numBoxes.append(pygame.Rect(755+holeStart+hole1Size+hole2Size+hole3Size,height+5,90,40))
+
+ gapPattern = [(0,1),(1,2),(2,3)]
+
+ elif holes == 4:
+ walls.append(pygame.Rect(150,height,100,50))
+ walls.append(pygame.Rect(350,height,100,50))
+ walls.append(pygame.Rect(550,height,100,50))
+ walls.append(pygame.Rect(750,height,100,50))
+ walls.append(pygame.Rect(950,height,100,50))
+
+ gaps.append(pygame.Rect(250,height,100,20))
+ gaps.append(pygame.Rect(450,height,100,20))
+ gaps.append(pygame.Rect(650,height,100,20))
+ gaps.append(pygame.Rect(850,height,100,20))
+
+ numBoxes.append(pygame.Rect(155,height+5,90,40))
+ numBoxes.append(pygame.Rect(355,height+5,90,40))
+ numBoxes.append(pygame.Rect(555,height+5,90,40))
+ numBoxes.append(pygame.Rect(755,height+5,90,40))
+ numBoxes.append(pygame.Rect(955,height+5,90,40))
+
+ gapPattern = [(0,1),(1,2),(2,3),(3,4)]
+
+ return (walls, gaps, numBoxes, gapPattern)
+
+
+
+def generateScreen(background, levelNum):
+
+ #defines the locations of the outer walls
+ leftWall = pygame.Rect(100,0,50,900)
+ rightWall = pygame.Rect(1050,0,50,900)
+ bottomWall = pygame.Rect(100,850,1000,50)
+
+ #loads up the sky graphic, selects a random portion
+ skyGraphic = util.load_image('sky.jpg')
+ background.blit(skyGraphic,(0,0),(random.randint(0,600),0,100,900))
+ background.blit(skyGraphic,(1100,0),(random.randint(0,600),0,100,900))
+
+ if levelNum == 0:#only draw grass on the bottom level!
+ grass = util.load_image('grass.bmp')
+ background.blit(grass,(0,850))
+ background.blit(grass,(1100,850))
+
+ #Generates/saves the two middle floors of the tower
+ if levelNum == 0:
+ floor1size = random.sample([1,1,2,2,2,2,2],1)[0]
+ floor2size = random.sample([1,2,2,2,2,2,3,3,3,3,4],1)[0]
+ floor1type = random.randint(0,1)
+ floor2type = random.randint(0,1)
+
+ else:
+ floor1size = random.sample([1,2,2,2,2,2,3,3,3,3,3,4,4],1)[0]
+ floor2size = random.sample([1,2,2,2,2,2,3,3,3,3,3,4,4],1)[0]
+ floor1type = random.randint(0,1)
+ floor2type = random.randint(0,1)
+
+ floor1 = generateFloor(floor1size,500,floor1type)
+ floor2 = generateFloor(floor2size,150,floor2type)
+
+ floor1Numbers = len(floor1[2])
+
+ centerWalls = util.merge(floor1[0],floor2[0])
+ gaps = util.merge(floor1[1],floor2[1])
+ numbers = util.merge(floor1[2],floor2[2])
+
+ gapPattern = floor1[3]
+
+ for pair in floor2[3]:
+ gapPattern.append((pair[0]+floor1Numbers, pair[1]+floor1Numbers))
+
+ floorNumbers = []
+ gapNumbers = []
+
+ #Goes through every number box (around each gap), and randomly assigns a number to it, based on the number range of the current level
+ for i in range(len(numbers)):
+ floorNumbers.append(random.randint(levelRanges[levelNum][0],levelRanges[levelNum][1]))
+
+ walls = [leftWall, rightWall, bottomWall]
+ walls = util.merge(walls,centerWalls)
+
+ #Loads the two background image files into surfaces
+ towerWallTile = util.load_image('WallTile.bmp')
+ windowTile = util.load_image('TowerWindow.bmp')
+
+ #Prints each wall tile
+ for i in range(18):
+ for j in range(18):
+ background.blit(towerWallTile, (150+50*i,50*j))
+
+ #Draws the four windows on the screen
+ background.blit(windowTile, (400,300))
+ background.blit(windowTile, (750,300))
+ background.blit(windowTile, (400,650))
+ background.blit(windowTile, (750,650))
+
+ #loads the image for the walls
+ wallSprite = util.load_image('wall.bmp')
+
+ #Draws the walls
+ for wall in walls:
+ pygame.draw.rect(background, [100,100,100], wall)
+ if wall.width == 50: #draws the vertical walls
+ x = wall.topleft[0]
+ y = wall.topleft[1]
+ while y < wall.height:
+ background.blit(wallSprite, (x,y))
+ y = y + 50
+ else: #draws the horizontal walls
+ x = wall.topleft[0]
+ y = wall.topleft[1]
+ #Checks if the horizontal wall touches the right one, if so adds a small bit of tile first, to make sure it tiles properly on the right side
+ if wall.topright[0] == 1050:
+ if wall.width%50 != 0:
+ background.blit(wallSprite,(x,y),pygame.Rect(50-(wall.width%50),0,wall.width%50,50))
+ x = x + wall.width % 50
+ while x < wall.topright[0]:
+ background.blit(wallSprite, (x,y))
+ x = x + 50
+ if wall.topright[0] - x < 50:
+ background.blit(wallSprite,(x,y),pygame.Rect(0,0,wall.topright[0] - x,50))
+ x = wall.topright[0]
+
+
+ #Goes through every gap and associates the numbers with it, also checks the randomized numbers to make sure they are passable, if not generates a new one
+ index = 0
+ for gap in gaps:
+ #makes sure there is a difference of at least two around each gap so there is room to go through
+ while -1 <= floorNumbers[gapPattern[index][0]] - floorNumbers[gapPattern[index][1]] <= 1:
+ floorNumbers[gapPattern[index][1]] = random.randint(levelRanges[levelNum][0],levelRanges[levelNum][1])
+ gapNumbers.append((floorNumbers[gapPattern[index][0]],floorNumbers[gapPattern[index][1]]))
+ index = index+1
+
+ index = 0
+ numberPlate = util.load_image('numberPlate.bmp') #The background image behind the numbers
+
+ #Draws all the numbers/its background image around every gap
+ for numBox in numbers:
+ numberText = stringFactor(floorNumbers[index])
+
+ #Sets up the font which will be used in the box containing the number
+ fontSize = 40
+ font = pygame.font.Font(None, fontSize)
+
+ displayBox = font.render(numberText, 1, (50,50,50))
+ textSize = font.size(numberText)
+
+ background.blit(numberPlate, numBox.topleft)
+ background.blit(displayBox, (numBox.topleft[0]+(90-textSize[0])/2,numBox.topleft[1]+(40-textSize[1])/2))
+
+ index = index+1
+
+ background.convert()
+ return (background,walls,gaps,gapNumbers)
+
+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)
+
+ #display background
+ background = pygame.Surface(screen.get_size())
+ background = background.convert()
+ background.fill((200, 200, 200))
+
+
+ heroNumber = random.randint(3,17)
+ levelNum = 0
+
+ level = generateScreen(background,levelNum)
+ background = level[0]
+ walls = level[1]
+ gaps = level[2]
+ gapNumbers = level[3]
+
+ #Grid, for display purposes
+ grid = 0
+ if grid == 1:
+ for i in range(24):
+ pygame.draw.line(background, [0,0,0], (50*i,0),(50*i,900))
+ for i in range(18):
+ pygame.draw.line(background, [0,0,0], (0,50*i),(1200,50*i))
+
+
+ screen.blit(background, (0, 0))
+ pygame.display.flip()
+
+
+ hero = pygame.Rect(600, 700, 70, 100) #The rectangle representing the hero
+
+ dropRects = []
+ dropValues = []
+ dropStrings = []
+
+ updateGaps = 1 #The variable that stores if the gaps need to be updated(set to 1 whenever the heroes number changes), also need to update at start
+
+ #Sets the movement speeds for the character, in pixels per loop increment (game runs at 40 FPS)
+ sideSpeed = 10
+ upSpeed = 20
+ gravity = 10
+ xchange = 0
+ ychange = gravity
+
+ clock = pygame.time.Clock()
+
+ shownGap1, shownGap2 = None, None #Variables will represent gaps that will be displayed (if they get hit or stepped on)
+ fallWait, topHoleWait = 0, 0 #Variables representing timers for if the hero is falling or a gap is being displayed because it was stepped on
+
+ run = 1 #Variable representing if the game is running
+ runCounter = 0 #The number of run loops the game has gone through
+
+ #The main game loop of the program
+ while run:
+ runCounter = runCounter+1
+ events = pygame.event.get()
+ for event in events:
+ #User decides to exit program
+ if event.type == QUIT:
+ run = 0 #stop running
+
+ #Triggers when a key is first pressed down
+ elif event.type == pygame.KEYDOWN:
+ if event.key == 273: #up
+ ychange += -upSpeed
+ elif event.key == 274: #down
+ ychange += 0
+ elif event.key == 275: #right
+ xchange += sideSpeed
+ elif event.key == 276: #left
+ xchange += -sideSpeed
+ elif event.unicode == "+":
+ heroNumber = heroNumber+1
+ updateGaps = 1
+ elif event.unicode == "-":
+ heroNumber = heroNumber-1
+ updateGaps = 1
+
+ #Triggers when a pressed down key is no longer being pressed
+ elif event.type == pygame.KEYUP:
+ if event.key == 273:
+ ychange += +upSpeed
+ elif event.key == 274:
+ ychange += 0
+ elif event.key == 275:
+ xchange += -sideSpeed
+ elif event.key == 276:
+ xchange += +sideSpeed
+
+ #Sees if the lists storing the gaps which are impassable need to be updated
+ if updateGaps != 0:
+ updateGaps = 0
+ print "Updating Gaps"
+
+ impassableGaps = [] #The list that will store all the gaps that are not currently passable because of the heroes current number
+ impassableWalls = util.merge([],walls) #Combined list representing impassable gaps and all walls
+
+ #Goes through every gap on the screen and checks if it is impassable or not, if so adds to appropriate lists
+ for i in range(len(gaps)):
+ if not(gapNumbers[i][0]<=heroNumber<=gapNumbers[i][1] or gapNumbers[i][0]>=heroNumber>=gapNumbers[i][1]):
+ if hero.collidelist([gaps[i]]) == -1: #makes sure the hero isn't in the gap currently (prevents it from getting stuck)
+ impassableGaps.append(gaps[i])
+ impassableWalls.append(pygame.Rect(gaps[i][0],gaps[i][1],gaps[i][2],1))
+
+ #Start of hero movement/collision detection block
+ if fallWait != 0: #The hero is falling, so movement is not based on arrow keys pressed
+ fallWait = fallWait - 1
+ tempXChange = 0 #hero does not move left or right, just falls based o gravity
+ tempYChange = gravity
+ if fallWait <= 20: #After done falling for a while stops showing the gap which was hit
+ shownGap1 = None
+ else: #hero is not falling, movement based on the modifiers from the arrow keys.
+ shownGap1 = None
+ tempXChange = xchange
+ tempYChange = ychange
+
+ tempHero = hero.move(tempXChange,tempYChange)
+
+ if tempHero.collidelist(impassableWalls) == -1: #no collision problems, hero moves normally
+ hero = tempHero
+ else:
+ tempHero = hero.move(tempXChange,0) # first checks if collision is from vertical movement,
+ if tempHero.collidelist(impassableWalls) == -1: # if it is, move hero horizontally only
+ hero = tempHero
+ else:
+ tempHero = hero.move(0,tempYChange) # otherwise, checks if collision is from horizontal movement
+ if tempHero.collidelist(impassableWalls) == -1:
+ hero = tempHero
+
+
+ #If collison comes from both horizontal and verical movements, the hero does not get moved
+
+ #checks if the hero hits a gap they cannot pass through from the bottom
+ if hero.collidelist(impassableGaps) != -1:
+ fallWait = 60 #Sets the variable representing the fact that the hero is falling, because they went the wrong way
+ shownGap1 = impassableGaps[hero.collidelist(impassableGaps)]
+
+ #checks if the hero is standing on top of a gap they cannot pass through
+ tempHero = hero.inflate(-70,0)
+ tempHero = tempHero.move(0,1)
+ if tempHero.collidelist(impassableGaps) != -1:
+ topHoleWait = 5 #gap hero is standing on stays for 5 increments after it's off
+ gap = impassableGaps[tempHero.collidelist(impassableGaps)]
+ shownGap2 = pygame.Rect(gap.topleft,(gap.width,20))
+
+ #re-draws the background
+ screen.blit(background, (0, 0))
+ pygame.draw.rect(screen, [255,50,50], hero) #draws the hero
+
+ #Checks if any impassable gaps have been hit or stepped on and thus need to be displayed
+ if shownGap1 != None:
+ pygame.draw.rect(screen, [250,100,100,50], shownGap1)
+ if topHoleWait != 0:
+ topHoleWait = topHoleWait - 1
+ pygame.draw.rect(screen, [250,100,100,50], shownGap2)
+
+ #Code which spawns a drop which contains a number, only called every 100 loops
+ if runCounter%75 == 0:
+ #for i in range(1):
+ newDrop = generateDropNumber(heroNumber,levelNum)
+ #print "spawn bubble %d"%(runCounter),newDrop[2]
+ dropRects.append(pygame.Rect(random.randint(150,850),0,50,50))
+ dropValues.append(newDrop[0:2])
+ dropStrings.append(newDrop[2])
+
+
+
+ #Checks if hero has hit any drops
+ if hero.collidelist(dropRects) != -1:
+ i = hero.collidelist(dropRects)
+
+ if dropValues[i][0] == 0: #hit an addition drop
+ if heroNumber + dropValues[i][1] < 1000:
+ heroNumber = heroNumber + dropValues[i][1]
+ dropRects.pop(i)
+ dropValues.pop(i)
+ dropStrings.pop(i)
+ updateGaps = 1
+
+ elif dropValues[i][0] == 1: #hit a subtraction drop
+ if -1000 < heroNumber - dropValues[i][1]:
+ heroNumber = heroNumber - dropValues[i][1]
+ dropRects.pop(i)
+ dropValues.pop(i)
+ dropStrings.pop(i)
+ updateGaps = 1
+
+ elif dropValues[i][0] == 2: #Multiplication drop
+ if -1000 < heroNumber*dropValues[i][1] < 1000:
+ heroNumber = heroNumber * dropValues[i][1]
+ dropRects.pop(i)
+ dropValues.pop(i)
+ dropStrings.pop(i)
+ updateGaps = 1
+
+ elif dropValues[i][0] == 3: #Division drop
+ if heroNumber%dropValues[i][1] == 0:
+ heroNumber = heroNumber / dropValues[i][1]
+ dropRects.pop(i)
+ dropValues.pop(i)
+ dropStrings.pop(i)
+ updateGaps = 1
+
+ #Goes through every drop currently on screen (starting with most recently spawned), draws it to the screen with the appropriate number, and moves it down
+ dropIndexs = range(len(dropRects))
+ dropIndexs.reverse()
+ for i in dropIndexs:
+ #dropTextRect = dropRects[i].inflate(20,-25)
+ #pygame.draw.rect(screen, [102,255,255], dropRects[i])
+ font = pygame.font.Font(None,42)
+ dropBox = font.render(dropStrings[i], 1, (50,50,230))
+ textSize = font.size(dropStrings[i])
+ screen.blit(dropBox, (dropRects[i].topleft[0]+(50-textSize[0])/2,dropRects[i].topleft[1]+(50-textSize[1])/2))
+ dropRects[i] = dropRects[i].move(0,2)
+ if dropRects[i].topleft[1]>=900: #Removes from all applicable lists if no longer on screen
+ dropRects.pop(i)
+ dropValues.pop(i)
+ dropStrings.pop(i)
+
+ #Sets up and displays the box which contains the number the hero currently has
+ numBox = pygame.Rect(0,0,60,30)
+ hero_number_box = util.render_textrect(str(heroNumber),pygame.font.Font(None, 40),numBox,(50,50,50),[250,250,250],0,1)
+ screen.blit(hero_number_box, (hero.topleft[0]+5,hero.topleft[1]+35))
+ pygame.display.flip() #updates screen
+
+ #Made it off the top of the screen, must advance to next level
+ if hero.topleft[1] <= -80:
+ levelNum = levelNum+1
+ if levelNum == 8: #Made it to the top of the tower!
+ victoryCinematic(screen)
+ break
+
+ #Copy copies the second floor displayed on the screen so it can be shown as the very bottom on the next level
+ for gap in gaps:
+ pygame.draw.rect(background,[250,100,100,50], gap)
+
+ copy = background.subsurface(pygame.Rect(100,150,1000,50)).copy()
+
+ #Clears background, generate new level/get it saved into variables
+ background.fill((200, 200, 200))
+ level = generateScreen(background,levelNum)
+ background = level[0]
+ walls = level[1]
+ gaps = level[2]
+ gapNumbers = level[3]
+
+ #Clear the falling numbers from the screen
+ dropRects, dropValues, dropStrings = [], [], []
+
+ #Reset heros vertical location
+ hero = pygame.Rect(hero.topleft[0],700,70,100)
+
+ #As the level has changed, gaps need to be updated again
+ updateGaps = 1
+
+ background.blit(copy, (100,850))
+ print "Now on level %d."%(levelNum)
+
+ #print clock
+ clock.tick(40) #limits to 40 FPS
+
+
+def main():
+ print "Running Tower"
+ run()
+
+
+#Given a number, returns a string representing the number, randomly formated into a short math equasion
+def stringFactor(number):
+ #The list which represents the string posibilities which will be chosen from later
+ choices = []
+
+ if number == 0: #special case if the number is 0, string possibilites are 0 times a number from 2~12
+ for i in range(13)[1:]:
+ choices.append("%dx%d"%(0,i))
+ choices.append("%dx%d"%(i,0))
+ else: # Number is non-zero
+ #Checks if the number is a multiple of 2~12x2~12, adds all possibilities to list
+ for i in range(13)[2:]:
+ if number%i == 0:
+ if 1 < number/i <= 12:
+ choices.append("%dx%d"%(i,number/i))
+
+
+ #Sees if no possibilities found yet:
+ if len(choices) == 0:
+ #Adds the number itself to choices multiple times to make that choice more likely
+ for i in range(6):
+ choices.append("%d"%(number))
+ #choices.append(u"%d\u00F75"%(number*5)) #Five times the number divided by 5
+ choices.append(u"%d\u00F72"%(number*2)) # Double the number divided by 2
+ if number<100:
+ choices.append("%dx10+%d"%(number/10,number%10))#expanded notation
+
+ #selects a single entry at random from the possible choices, and returns the string
+ return random.sample(choices,1)[0]
+
+
+# Executed once the player reaches the top of the tower
+def victoryCinematic(screen):
+
+ util.ok_box("You have reached the top of the tower!", (450,400), 300, 50, screen)
+
+def generateDropNumber(heroNumber,level):
+ #Randomly selects if the drop will have addition, subtraction, multiplication, or division, addition and subtraction being more likely
+ type = random.sample([0,0,0,0,0,0,1,1,1,1,1,1],1)[0]
+ levelAverage = (levelRanges[level][0]+levelRanges[level][1])/2
+ returnString = ""
+ if type == 0: #Addition drop
+ number = random.randint(2,15)
+ if heroNumber-levelAverage < -30:
+ number = random.sample([number,-number-heroNumber+levelAverage],1)[0]
+ returnString = "+%d"%(number)
+ elif type == 1: #Subtraction drop
+ number = random.randint(2,9)
+ if heroNumber-levelAverage > 30:
+ number = random.sample([number,number+heroNumber-levelAverage],1)[0]
+ returnString = "-%d"%(number)
+ elif type == 2: #Multiplication drop
+ number = random.randint(2,5)
+ returnString = "x%d"%(number)
+ elif type == 3: #Division drop
+ number = random.randint(2,5)
+ returnString = u"\u00F7%d"%(number)
+
+ return (type,number,returnString)
+
+#runs main if called via other naming convention
+if __name__ == '__main__': main() \ No newline at end of file
diff --git a/util.py b/util.py
new file mode 100644
index 0000000..778d2e9
--- /dev/null
+++ b/util.py
@@ -0,0 +1,199 @@
+import pygame
+import pygame.font
+from pygame.locals import *
+import random, os.path
+import math
+import string
+
+def load_image(file, trans = 0):
+ "loads an image, prepares it for play"
+ file = os.path.join('data', file)
+ try:
+ surface = pygame.image.load(file)
+ if trans == 1:
+ surface.set_colorkey(surface.get_at((0, 0)))
+ except pygame.error:
+ raise SystemExit, 'Could not load image "%s" %s'%(file, pygame.get_error())
+ return surface.convert()
+
+def ok_box(string, location, width, height, screen):
+ clock = pygame.time.Clock()
+ pygame.init()
+ font = pygame.font.Font(None, 24)
+ intro_string = string
+ text_rect = pygame.Rect(location,(width,height+40))
+ text_box = render_textrect(intro_string,font,text_rect,(255,255,255),0,0)
+ screen.blit(text_box,text_rect.topleft)
+ (x_coord,y_coord) = location
+ ok_box = pygame.Rect((x_coord+width/2-15,y_coord+height+5),(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))
+ run = 1
+ while run:
+ events = pygame.event.get()
+ for event in events:
+ if event.type == QUIT:
+ run = 0 #stop running
+ pygame.quit()
+ if event.type == pygame.MOUSEBUTTONDOWN:
+ (x, y) = pygame.mouse.get_pos()
+ if x > x_coord+width/2-15 and x < x_coord+width/2+15 and y > y_coord+height+5 and y < y_coord+height+35:
+ run = 0
+ return 1
+ pygame.display.flip()
+ clock.tick(20)
+
+
+def render_textrect(string, font, rect, text_color, background_color, trans, justification=0):
+ """Returns a surface containing the passed text string, reformatted
+ to fit within the given rect, word-wrapping as necessary. The text
+ will be anti-aliased.
+
+ Takes the following arguments:
+
+ string - the text you wish to render. \n begins a new line.
+ font - a Font object
+ rect - a rectstyle giving the size of the surface requested.
+ text_color - a three-byte tuple of the rgb value of the
+ text color. ex (0, 0, 0) = BLACK
+ background_color - a three-byte tuple of the rgb value of the surface.
+ trans - set to 0 if you want the background to be shown
+ justification - 0 (default) left-justified
+ 1 horizontally centered
+ 2 right-justified
+
+ Returns the following values:
+
+ Success - a surface object with the text rendered onto it.
+ Failure - raises a TextRectException if the text won't fit onto the surface.
+ """
+
+ import pygame
+
+ final_lines = []
+
+ requested_lines = string.splitlines()
+
+ # Create a series of lines that will fit on the provided
+ # rectangle.
+
+ for requested_line in requested_lines:
+ if font.size(requested_line)[0] > rect.width:
+ words = requested_line.split(' ')
+ # if any of our words are too long to fit, return.
+ for word in words:
+ if font.size(word)[0] >= rect.width:
+ raise TextRectException, "The word " + word + " is too long to fit in the rect passed."
+ # Start a new line
+ accumulated_line = ""
+ for word in words:
+ test_line = accumulated_line + word + " "
+ # Build the line while the words fit.
+ if font.size(test_line)[0] < rect.width:
+ accumulated_line = test_line
+ else:
+ final_lines.append(accumulated_line)
+ accumulated_line = word + " "
+ final_lines.append(accumulated_line)
+ else:
+ final_lines.append(requested_line)
+
+ # Let's try to write the text out on the surface.
+
+ surface = pygame.Surface(rect.size)
+ if trans == 0:
+ surface.fill(background_color)
+ # CHANGED HERE, this make the BG transparent XXXXXXXXXXXXXXXXXXXXXXXXX transparent stuff here XXXXXXXXXX
+ #surface.set_colorkey(background_color)
+ # CHANGED
+
+ accumulated_height = 0
+ for line in final_lines:
+ if accumulated_height + font.size(line)[1] >= rect.height:
+ raise TextRectException, "Once word-wrapped, the text string was too tall to fit in the rect."
+ if line != "":
+ tempsurface = font.render(line, 1, text_color)
+ if justification == 0:
+ surface.blit(tempsurface, (5, accumulated_height+5))
+ elif justification == 1:
+ surface.blit(tempsurface, ((rect.width - tempsurface.get_width()) / 2, accumulated_height))
+ elif justification == 2:
+ surface.blit(tempsurface, (rect.width - tempsurface.get_width(), accumulated_height))
+ else:
+ raise TextRectException, "Invalid justification argument: " + str(justification)
+ accumulated_height += font.size(line)[1]
+
+ return surface
+
+
+
+#Takes two lists as arguments, returns list with both merged
+def merge(list1, list2):
+ for element in list2:
+ list1.append(element)
+ return list1
+
+def get_key():
+ while 1:
+ clock = pygame.time.Clock()
+ event = pygame.event.poll()
+ if event.type == QUIT:
+ run = 0 #stop running
+ pygame.quit()
+ if event.type == KEYDOWN:
+ keys = (event.key,event.unicode)
+ return keys
+ else:
+ pass
+ clock.tick(20)
+
+
+def display_box(screen, message,x,y,length):
+ "Print a message in a box in the middle of the screen"
+ fontobject = pygame.font.Font(None,18)
+ #pygame.draw.rect(screen, (0,0,0),((screen.get_width() / 2) - 100,(screen.get_height() / 2) - 10, 200,20), 0)
+ #pygame.draw.rect(screen, (255,255,255), ((screen.get_width() / 2) - 102,(screen.get_height() / 2) - 12, 204,24), 1)
+ pygame.draw.rect(screen, (0,0,0),(x,y, length,20), 0)
+ pygame.draw.rect(screen, (255,255,255), (x-2,y-2, length+4,24), 1)
+ if len(message) != 0:
+ screen.blit(fontobject.render(message, 1, (255,255,255)),(x+2,y+3))
+ pygame.display.flip()
+
+def ask(screen, question,x,y,length):
+ clock = pygame.time.Clock()
+ "ask(screen, question) -> answer"
+ pygame.font.init()
+ current_string = []
+ display_box(screen, question + ": " + string.join(current_string,""),x,y,length)
+ while 1:
+ events = pygame.event.get()
+ for event in events:
+ if event.type == QUIT:
+ run = 0 #stop running
+ pygame.quit()
+ inkey = get_key()
+ if inkey[0] == K_BACKSPACE:
+ #if inkey == u"\u2408":
+ #print "back space"
+ current_string = current_string[0:-1]
+ elif inkey[0] == K_RETURN:
+ #print "enter"
+ break
+ elif inkey == K_MINUS:
+ current_string.append("_")
+ #elif inkey <= 127:
+ else:
+ #current_string.append(chr(inkey))
+ current_string.append(inkey[1])
+ display_box(screen, question + ": " + string.join(current_string,""),x,y,length)
+ clock.tick(20)
+ return string.join(current_string,"")
+
+
+