1
2
3
4 """
5 [menu.py]
6 Clase principal que ejecuta el programa en entorno gráfico.
7 """
8
9 __author__ = "Victor Ramirez de la Corte"
10 __date__ = "23/04/2009"
11 __version__ = "PyRubik v0.6.5"
12
13
14 import pygame, sys, os
15 from language import *
16 from pygame.locals import *
17 from scramble import *
18 from timer import *
19 import capturar
20 import timer
21 import cam
22 import bd
23
24
25
27 fullname = os.path.join(folder, name)
28 image = pygame.image.load(fullname)
29 image = image.convert_alpha()
30 return image
31
32
34 """
35 Insertar una cadena en pygame.
36 cadena: string que quieres insertar
37 font: font de pygame
38 color: rgb(r,g,b)
39 backgroung: imagen de fondo
40 window = ventana en la que quieres escribir
41 pos: posicion en la que quieres colocar el texto
42 update: tipo rect el cual es el que se actualiza
43 """
44 fuente = font.render(cadena, True, color)
45 window.blit(background, (0,0))
46 window.blit(fuente, pos)
47 pygame.display.update(update)
48
49
50 -def captureKeys(cadena, font, color, background, window, pos = (0,0),update = None):
51 """
52 Capturamos keys desde pygame e insertamos la captura en pygame.
53 cadena: string que quieres insertar
54 font: font de pygame
55 color: rgb(r,g,b)
56 backgroung: imagen de fondo
57 window = ventana en la que quieres escribir
58 pos: posicion en la que quieres colocar el texto
59 update: tipo rect el cual es el que se actualiza
60 """
61 pygame.event.clear()
62 string = ""
63 while 1:
64 insertStringInBackground(cadena + string, font, color, background, window, pos, update)
65 keys = pygame.event.wait()
66 if keys.type == KEYDOWN and keys.key == K_BACKSPACE:
67 string = string[:len(string)-1]
68 elif keys.type == KEYDOWN and keys.key == K_RETURN:
69 break
70 elif keys.type == KEYDOWN and keys.key == K_ESCAPE:
71 string = ""
72 break
73 elif keys.type == KEYDOWN and keys.key == K_SPACE:
74 string += " "
75 elif keys.type == KEYDOWN:
76 key = keys.key
77 string += pygame.key.name(key)
78 pygame.event.clear()
79 return string
80
81
82 -def pintarCubo(window, capas, numberOrColor = 1, colores = None):
83 """pinta las capas de self.capas con pygame"""
84
85 m = (95, 95, 95, 190, 285, 0)
86 n = (0, 95, 190, 95, 95, 95)
87 a = (0, 30, 60)
88
89
90 for x in range(len(capas)):
91 for w in range(len(capas[x])):
92 if numberOrColor == 0:
93 pygame.draw.rect(window, colores[int(capas[x][w])][2],\
94 (a[w%3]+m[x], a[w//3]+n[x], 30, 30))
95 else:
96 pygame.draw.rect(window, capas[x][w], (a[w%3]+m[x], a[w//3]+n[x], 30, 30))
97
98 """
99 def opcionesThemes(themes):
100 archivo = open("theme/%s/opt.txt" % themes, "r")
101 opc = archivo.readlines()
102
103 color = opc[0][opc[0].index("=")+1:opc[0].index("\n")]
104 colorSel = opc[1][opc[1].index("=")+1:opc[1].index("\n")]
105 sizeFont = opc[2][opc[2].index("=")+1:opc[2].index("\n")]
106 sizeFontScr = opc[3][opc[3].index("=")+1:opc[3].index("\n")]
107 sizeFontClock = opc[4][opc[4].index("=")+1:opc[4].index("\n")]
108 center = opc[5][opc[5].index("=")+1:opc[5].index("\n")]
109 pos = opc[5][opc[5].index("=")+1:opc[5].index("\n")]
110 return color, colorSel, sizeFont, sizeFontClock, center, pos
111 """
112
114
118
119
120
121 pygame.font.init()
122
123 self.window = pygame.display.set_mode(size)
124 pygame.display.set_caption("PyRubik")
125 pygame.display.set_icon(load_image("DATOS", "icon.png"))
126 themes = os.listdir('themes')
127 themes.sort()
128 themes = themes[1:]
129 font = "themes/%s" % themes[theme]
130 background = "themes/%s" % themes[theme]
131 self.background = load_image(background, "background.png")
132 self.font = pygame.font.Font(os.path.join(font, "font.ttf") , sizeFont)
133 self.fontScr = pygame.font.Font(os.path.join(font, "font.ttf") , sizeFontScr)
134 self.fontTime = pygame.font.Font(os.path.join(font, "font.ttf") , 80)
135 self.window.blit(self.background, (0,0))
136 self.options = lll.menu[:]
137 self.options[5] = self.options[5].partition("<")[0] + "<%s>" % themes[theme]
138 idiomas = os.listdir('mo')[:]
139 idiomas.sort()
140 idiomas = idiomas[1:]
141
142
143 reloj = pygame.time.Clock()
144
145
146 opt = 0
147 l = 1
148 cont = 0
149
150 while 1:
151
152 pygame.display.flip()
153 noWait = pygame.event.pump()
154 keyinput = pygame.key.get_pressed()
155 if self.window.get_size() != (size):
156 self.window = pygame.display.set_mode(size)
157
158
159
160 outMenu = []
161 for k in range(len(self.options)):
162 if opt == k:
163 outMenu.append(self.font.render(self.options[k], True, colorSel))
164 else:
165 outMenu.append(self.font.render(self.options[k], True, color))
166 self.window.blit(outMenu[k], (pos[0], pos[1] + k*pos[2]))
167
168
169 self.window.blit(self.background, (0,0))
170 for z in range(len(outMenu)):
171
172 if center == True:
173 f_rect = outMenu[z].get_rect()
174 f_rect.move_ip(pos[0], pos[1] + z*pos[2])
175 f_rect.centerx = self.window.get_rect().centerx
176 else:
177
178 f_rect = (pos[0], pos[1] + z*pos[2])
179 self.window.blit(outMenu[z], f_rect)
180
181
182 if opt == 5 and (keyinput[K_RIGHT] or keyinput[K_LEFT]):
183 if keyinput[K_RIGHT]:
184 theme += 1
185 if theme > len(themes) - 1:
186 theme = 0
187 elif keyinput[K_LEFT]:
188 theme -= 1
189 if theme < 0:
190 theme = len(themes) - 1
191
192 self.options = lll.menu[:]
193 self.options[5] = self.options[5].partition("<")[0] + "<%s>" % themes[theme]
194 font = "themes/%s/font.ttf" % themes[theme]
195 background = "themes/%s/background.png" % themes[theme]
196 mmm = Menu(color=(255,155,0,155), theme = theme, lang = lang)
197
198
199 if (keyinput[K_RIGHT] or keyinput[K_LEFT]) and opt == 3 or lang == 0:
200 event = pygame.event.wait()
201 if event.type == KEYDOWN and keyinput[K_LEFT]:
202 l -= 1
203 if l < 0:
204 l = len(idiomas) - 1
205 elif event.type == KEYDOWN and keyinput[K_RIGHT]:
206 l += 1
207 if l > len(idiomas) - 1:
208 l = 0
209 lll.translate(idiomas[l])
210 self.options = lll.menu
211 self.options[5] += "<%s>" % themes[theme]
212 for k in range(len(self.options)):
213 outMenu.insert(k, (self.font.render(self.options[k], True, color)))
214 self.window.blit(outMenu[k], (pos[0], pos[1] + k*pos[2]))
215 outMenu = outMenu[:len(self.options)]
216 lang = 1
217 reloj.tick(100)
218 pygame.time.delay(30)
219
220
221 if keyinput[K_DOWN] or keyinput[K_UP]:
222 event = pygame.event.wait()
223 if event.type == KEYDOWN and keyinput[K_DOWN]:
224 opt += 1
225 if opt >= len(self.options):
226 opt = 0
227 elif event.type == KEYDOWN and keyinput[K_UP]:
228 opt -= 1
229 if opt < 0:
230 opt = len(self.options)-1
231
232
233
234
235 if keyinput[K_RETURN] and opt == 0:
236 alg = Scramble()
237 scra = self.fontScr.render("Scramble: "+alg.scramble(), True, color)
238 self.window.blit(self.background, (0,0))
239 self.window.blit(scra, (20,250))
240 pygame.display.update()
241
242
243 string = captureKeys(lll.insertEsp, self.fontScr,\
244 color, self.background, self.window, (20,300), update=((20,300),(760,20)))
245 stringT = string
246 try: string = int(string)
247 except: string = 5
248
249 while 1:
250
251 self.window.blit(self.background, (0,0))
252 insertStringInBackground(lll.cadenaMenu[0], self.fontScr, color,\
253 self.background, self.window, pos=(20,350), update=((20,350),(760,40)))
254 insertStringInBackground(lll.cadenaMenu[1], self.fontScr, color,\
255 self.background, self.window, pos=(20,400), update=((20,400),(760,40)))
256
257 evento5 = pygame.event.wait()
258 if evento5.type == KEYDOWN and (evento5.key == K_RETURN or evento5.key == K_SPACE):
259
260 for st in range(string, 0, -1):
261 insertStringInBackground(str(st), self.fontTime, color,\
262 self.background, self.window, pos=(350,500), update=((350,500),(350,80)))
263 time.sleep(1)
264 tim = time.time()
265 tempX = 0
266 pygame.event.clear()
267
268
269 while not tempX:
270 tiempo = time.time() - tim
271 tiempoStr = str(tiempo)
272 posP = tiempoStr.index(".")
273 cadenaTime = tiempoStr[:posP] + tiempoStr[posP:posP+3]
274 cadT = self.fontTime.render(cadenaTime, True, color)
275 self.window.blit(self.background, (0,0))
276 self.window.blit(cadT, (350,500))
277 pygame.display.update(((350,500),(350,80)))
278 pygame.event.pump()
279 keyinput = pygame.key.get_pressed()
280 tempX = keyinput[K_SPACE]
281
282 reloj.tick(100)
283 pygame.time.delay(30)
284
285 pygame.event.clear()
286
287
288 save = captureKeys(lll.save + ": ", self.fontScr, color,\
289 self.background, self.window, pos = (20,400), update=((20,400),(760,20)))
290 if lll.yes.count(save.lower()) > 0:
291 bbdd = bd.BD()
292 bbdd.guardar(tiempo, "3x3x3")
293 bbdd.close()
294 insertStringInBackground(lll.saved, self.font,\
295 color, self.background, self.window, pos = (100,350))
296 scra = self.fontScr.render("Scramble: "+alg.scramble(), True, color)
297 self.window.blit(self.background, (0,0))
298 self.window.blit(scra, (20,250))
299 pygame.display.update((20,250),(760,20))
300
301 elif evento5.type == KEYDOWN and evento5.key == K_ESCAPE:
302 break
303
304
305
306 if keyinput[K_RETURN] and opt == 2:
307 pass
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322 elif keyinput[K_RETURN] and opt == 1:
323 captura = capturar.Capturar()
324 cadena = "(1).[" + lll.optSolve[1] + "], (2)." + lll.optSolve[0]
325 cadenaFont = self.fontScr.render(cadena, True, color)
326 self.window.blit(self.background, (0,0))
327 self.window.blit(cadenaFont, (0,300))
328 pygame.display.update()
329
330 while 1:
331 event2 = pygame.event.wait()
332 if event2.type == KEYDOWN and event2.key == K_1:
333 paleta12 = pygame.image.load("DATOS/paleta12.png")
334 cubo2D = pygame.image.load("DATOS/cube2D.png")
335 break
336 elif event2.type == KEYDOWN and event2.key == K_ESCAPE:
337 pygame.event.clear()
338 break
339 elif event2.type == KEYDOWN and event2.key == K_2:
340 break
341
342 if event2.key == K_1:
343 self.window.blit(self.background, (0,0))
344 insertStringInBackground(lll.finish, self.fontScr, color,\
345 self.background, self.window, pos=(50,550))
346 self.window.blit(paleta12, (20,290))
347 self.window.blit(cubo2D, (400,300))
348
349 xPos = (492, 492, 492, 582, 672, 402)
350 yPos = (302, 392, 482, 392, 392, 392)
351 subPos = (0, 30, 60)
352 posCentros = (4,13,22,31,40,49)
353 selColor = (255,255,255)
354 colorCentro = set()
355 rects = [[],[]]
356
357 for x in range(6):
358 for w in range(9):
359 rects[0].append(pygame.Rect((subPos[w%3]+xPos[x],subPos[w//3]+yPos[x]),(26,26)))
360 rects[1].append((0,0,w+1))
361 pygame.display.update()
362
363 while 1:
364 click = pygame.event.wait()
365 if click.type == MOUSEBUTTONDOWN:
366 mousePos = pygame.mouse.get_pos()
367 if (20 <= mousePos[0] <= 220) and (290 <= mousePos[1] <= 440):
368 selColor = self.window.get_at(pygame.mouse.get_pos())
369 print "Color: ",selColor
370 elif (400 <= mousePos[0] <= 760) and (300 <= mousePos[1] <= 570):
371 for rect in rects[0]:
372 if (rect[0] <= mousePos[0] <= (rect[0] + rect[2]))\
373 and (rect[1] <= mousePos[1] <= (rect[1] + rect[3])):
374 pygame.draw.rect(self.window, selColor, rect)
375 numColoresUsados = rects[1].count(selColor)
376 print numColoresUsados
377
378 if numColoresUsados < 9:
379 rects[1][rects[0].index(rect)] = selColor
380 pygame.display.update(rect)
381 else:
382 print "hay mas de nueve colores ya"
383
384
385 if click.type == KEYDOWN and click.key == K_RETURN:
386 break
387 cont = 0
388 self.capas = [[],[],[],[],[],[]]
389 for xx in range(6):
390 for yy in range(9):
391 self.capas[xx].append(rects[1][cont])
392 cont += 1
393 print self.capas
394
395
396 """
397 #TODO USAR FOTO DE CUBO CON NUMEROS Y MIRAR SI SE PUEDEN UNIR MAIN Y MENU
398 cont = 0
399 for capas in captura.capas:
400 for cara in range(6):
401 cadenaCol = captura.colores[cara][0] + str(captura.colores[cara][1])
402 insertStringInBackground(cadenaCol, self.fontScr,
403 color, self.background, self.window, (20,250+cara*25),\
404 update = ((20,250+cara*25),(760,20)))
405
406 apto = False
407 string = ""
408 colorComprueba = (0,255,0)
409 while not apto and cont <= 6:
410 insertStringInBackground(lll.insertColor+": "+string,\
411 self.fontScr,colorComprueba, self.background,\
412 self.window, (300,250+cont*25),\
413 update = ((300,250+cont*25),(400,20)))
414 keys = pygame.event.wait()
415 if keys.type == KEYDOWN and keys.key == K_BACKSPACE:
416 string = string[:len(string)-1]
417 elif keys.type == KEYDOWN and keys.key == K_RETURN:
418 #añadiendo colores
419 if len(string) == 9 and colorComprueba == (0,255,0):
420 for numColor in string:
421 captura.colores[int(numColor)][1] -=1
422 cadenaColor = captura.colores[int(numColor)][0]
423 capas.append(int(cadenaColor[0]))
424 apto = True
425 cont += 1
426 elif keys.type == KEYDOWN and keys.key == K_ESCAPE:
427 string = ""
428 break
429 elif keys.type == KEYDOWN and keys.key == K_SPACE:
430 string += " "
431 elif keys.type == KEYDOWN:
432 key = keys.key
433 string += pygame.key.name(key)
434 #comprobamos los colores
435 existe = False
436 for caracter in range(len(string)):
437 for contColor in range(6):
438 if string[caracter] == str(contColor):
439 existe = existe+True
440 for contColor in range(5):
441 print (captura.colores[contColor][1]-string.count(str(contColor))) < 0
442 if ((captura.colores[contColor][1]-string.count(str(contColor))) < 0) or\
443 string.isdigit() == False or not existe or len(string)>9:
444 colorComprueba = (255,0,0)
445 else: colorComprueba = (0,255,0)
446 pygame.event.clear()
447 """
448
449
450 if event2.key == K_2:
451 cadena = "(1).[2 " + lll.fotos + "] u (2). 6 " + lll.fotos + " 3. usar import cube3D"
452 cadenaFont = self.fontScr.render(cadena, True, color)
453 self.window.blit(self.background, (0,0))
454 self.window.blit(cadenaFont, (0,300))
455 pygame.display.update()
456 pygame.event.clear()
457 while 1:
458 pygame.event.clear()
459 event3 = pygame.event.wait()
460 if event3.type == KEYDOWN and event3.key == K_2:
461 malla = 2
462 break
463 elif event3.type == KEYDOWN and event3.key == K_ESCAPE:
464 pygame.event.clear()
465 break
466 elif event3.type == KEYDOWN and event3.key == K_1:
467 malla = 1
468 break
469 if event3.key != K_ESCAPE:
470 cam.CapturarDesdeCam(malla)
471 if event3.key == K_2:
472 captura.capturarColores6Capas()
473 else:
474 captura.sacarColoresDeImagen()
475 ColorOrNumber = 1
476
477
478
479 elif keyinput[K_RETURN] and opt == 4:
480 bbdd = bd.BD()
481 self.window.blit(self.background, (0,0))
482 pygame.display.update()
483
484 for intSta in range(len(lll.statistic)):
485 insertStringInBackground(lll.statistic[intSta], self.fontScr,
486 color, self.background, self.window, (20,250+intSta*25),\
487 update = ((20,250+intSta*25),(760,20)))
488
489 pygame.event.clear()
490 otraConsulta = True
491 while otraConsulta:
492 mostrar = False
493 event4 = pygame.event.wait()
494 if event4.type == KEYDOWN and event4.key == K_2:
495 media = captureKeys("Average of: ", self.fontScr,\
496 color, self.background, self.window, (40,400),\
497 update=((40,400),(760,20)))
498 try: media = int(media)
499 except: media = 5
500 consultaStr = bbdd.consultarMediaDe(media)
501 mostrar = True
502 elif event4.type == KEYDOWN and event4.key == K_3:
503 consultaStr = bbdd.consultarRecord()
504 mostrar = True
505 elif event4.type == KEYDOWN and event4.key == K_4:
506 tipo = captureKeys("Tipo: ", self.fontScr,\
507 color, self.background, self.window, (40,400),\
508 update=((40,400),(760,20)))
509 consultaStr = bbdd.consultarTiemposDe(str(tipo))
510 mostrar = True
511 elif event4.type == KEYDOWN and event4.key == K_1:
512 consultaStr = bbdd.consultarTodo()
513 mostrar = True
514 elif event4.type == KEYDOWN and event4.key == K_ESCAPE:
515 break
516
517 if mostrar:
518 insertStringInBackground(consultaStr,\
519 self.fontScr,color, self.background, self.window,\
520 (40,400),update = ((40,400),(760,20)))
521 insertStringInBackground(lll.consulta, self.fontScr,\
522 color, self.background, self.window, (40,450),\
523 update=((40,450),(760,20)))
524
525 v = pygame.locals
526 while 1:
527 event4 = pygame.event.wait()
528 if event4.type == KEYDOWN and (event4.key == \
529 getattr(v, 'K_%s' % lll.no[1]) or event4.key == K_ESCAPE ):
530 otraConsulta = False
531 break
532 elif event4.type == KEYDOWN and event4.key == getattr(v, 'K_%s' % lll.yes[1]):
533 otraConsulta = True
534 self.window.blit(self.background, (0,0))
535 pygame.display.update(((40,400),(760,100)))
536 break
537 reloj.tick(100)
538 pygame.time.delay(30)
539 bbdd.close()
540 pygame.event.clear()
541
542
543
544 elif keyinput[K_RETURN] and opt == 6:
545 self.window.blit(self.background, (0,0))
546 pygame.display.update()
547
548 for intCre in range(len(lll.credits)):
549 insertStringInBackground(lll.credits[intCre], self.fontScr,
550 color, self.background, self.window, (20,250+intCre*40),\
551 update = ((20,250+intCre*40),(760,40)))
552
553 x = False
554 while not x:
555 event = pygame.event.wait()
556 x = (event.type == KEYDOWN and event.key == K_ESCAPE)
557
558
559 elif keyinput[K_RETURN] and opt == 7:
560 sys.exit()
561
562
563 if keyinput[K_f]:
564 pygame.display.toggle_fullscreen()
565
566 reloj.tick(100)
567 pygame.time.delay(30)
568
569
570