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