Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/levelthree.py
blob: 7c7b577ec0f5f76b9bd7af318020c8aa70545751 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
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()