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()