manhattanxl

Juego de memoria, Python, Kivy

Apr 11th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.87 KB | None | 0 0
  1. # PARA QUE EL JUEGO CORRA, DEBERA INDICAR EN LA CARPETA RELATIVA
  2. # "imgs" LAS IMAGENES QUE DESEA QUE FORMEN PARTE DEL JUEGO (8 IMAGENES
  3. # NUMERADAS DEL 1 AL 8 Y CON EXTENSION PNG) Y UNA IMAGEN DE
  4. # NOMBRE wait.png
  5. # Desarrollado como parte del voluntariado android UNAJ.
  6. # Contacto afperrens@gmail.com
  7. __version__ = "1.0"
  8. import kivy
  9. from kivy.app import App
  10. from kivy.uix.boxlayout import BoxLayout
  11. from kivy.uix.gridlayout import GridLayout
  12. from kivy.uix.button import Button
  13. from kivy.uix.label import Label
  14. from kivy.uix.screenmanager import ScreenManager, Screen, NoTransition
  15. from kivy.uix.image import AsyncImage, Image
  16. from kivy.animation import Animation
  17. from kivy.clock import Clock
  18. import random
  19. import time
  20.  
  21. puntaje = 0
  22.  
  23. class principal(App):
  24.     intentos = 0
  25.     aciertos = 0
  26.    
  27.     # COMENTADO, SI SE LLAMA DESDE UN GESTOR DE VENTANAS, DESCOMENTAR ESTA PARTE
  28.     # QUITAR BUILD Y EL LLAMADO A PRINCIPAL AL PIE DE ESTE ARCHIVO
  29.     #def __init__(self,**kwargs):
  30.         #creo la pantalla de juego
  31.         #super(principal,self).__init__(**kwargs)
  32.     def build(self):
  33.         contenedor = GridLayout(cols=4,rows=5)
  34.         self.contenedorImg = [BoxLayout(orientation='horizontal') for x in range(16)]
  35.         for i in range(16):
  36.             self.contenedorImg[i].add_widget(Image(source='imgs/wait.png'))
  37.             contenedor.add_widget(self.contenedorImg[i])
  38.         # agrego widgets de control
  39.         self.variosLabel = Label(text='')
  40.         self.puntajeLabel = Label(text='puntaje')
  41.         self.tiempoLabel = Label(text='tiempo')
  42.         self.hacerButton = Button(text='iniciar',on_press=self.mezclar)
  43.         contenedor.add_widget(self.variosLabel)
  44.         contenedor.add_widget(self.puntajeLabel)
  45.         contenedor.add_widget(self.tiempoLabel)
  46.         contenedor.add_widget(self.hacerButton)
  47.         #self.add_widget(contenedor)
  48.         return contenedor
  49.        
  50.     def mezclar(self,instance):
  51.         # mezclo los elementos y los cargo a las imagenes
  52.         arrImg = []
  53.         for i in range(8): arrImg.append("imgs/" + str(i+1) + ".png")
  54.         for i in range(8): arrImg.append("imgs/" + str(i+1) + ".png")
  55.         random.shuffle(arrImg)
  56.         for i in range(16):
  57.             self.contenedorImg[i].children[0].source = arrImg[i]
  58.         self.cuentaAtras(5,self.taparGrilla)
  59.    
  60.     def cuentaAtras(self,tiempo,funcion):
  61.         # countdown que ejecuta funcion cuando llega a cero
  62.         # # # # # # # # # # # # # # # # # # # # # # # # #
  63.         # deberia ser al reves, primero cuenta, luego imprime
  64.         # para que la pila de python no este con 10 funciones
  65.         # en la pila al llamar taparGrilla si no solo con uno
  66.         if(tiempo > 0):
  67.             self.tiempoLabel.text = str(tiempo)
  68.             Clock.schedule_once(lambda dt: self.cuentaAtras(tiempo-1,funcion),1)
  69.         else:
  70.             print "llego"
  71.             funcion()
  72.    
  73.     def taparGrilla(self):
  74.         # convierto todas las imagenes en botones
  75.         for i in range(16):
  76.             temp = self.contenedorImg[i].children[0].source
  77.             self.contenedorImg[i].remove_widget(self.contenedorImg[i].children[0])
  78.             self.contenedorImg[i].add_widget(Button(text='?',on_press=self.clickInterrogante))
  79.             self.contenedorImg[i].children[0].source = temp # le paso el nombre de img
  80.             self.contenedorImg[i].children[0].idImg = i # le paso la posicion de la imagen
  81.         self.hacerButton.disabled = True
  82.         # con instanciaActual determino si el boton es el primero en ser
  83.         # presionado (setea una imagen) o el segundo (usarlo y pasarlo a "")
  84.         self.instanciaActual = ["",0,"",0]
  85.    
  86.     def clickInterrogante(self,instance):
  87.         # cuando se hace click se determina si se hizo click sobre uno o
  88.         # sobre el segundo y determina si se eligio correctamente
  89.         self.intentos += 1
  90.         # primer boton
  91.         if(self.instanciaActual[0] == ""):
  92.             self.instanciaActual[0] = instance.source
  93.             self.instanciaActual[1] = instance.idImg
  94.             self.contenedorImg[instance.idImg].remove_widget(self.contenedorImg[instance.idImg].children[0])
  95.             self.contenedorImg[instance.idImg].add_widget(Image(source=instance.source))
  96.         # segundo boton
  97.         else:
  98.             self.contenedorImg[instance.idImg].remove_widget(self.contenedorImg[instance.idImg].children[0])
  99.             self.contenedorImg[instance.idImg].add_widget(Image(source=instance.source))
  100.             # uso instanciaActual en vez de lambda por que en python parece
  101.             # que no se pueden enviar muchos parametros en lambda
  102.             self.instanciaActual[2] = instance.source
  103.             self.instanciaActual[3] = instance.idImg
  104.             if(instance.source == self.instanciaActual[0]):
  105.                 self.variosLabel.text = "acerto!"
  106.                 self.aciertos += 1
  107.                 self.puntajeLabel.text = str(self.aciertos)
  108.                 self.instanciaActual = ["",0,"",0]
  109.             else:
  110.                 self.switchBotones()
  111.                 self.cuentaAtras(1,self.volverBoton)
  112.                 self.variosLabel.text = "no acerto!"
  113.  
  114.  
  115.  
  116.     def volverBoton(self):
  117.         # vuelve a poner las imagenes en botones
  118.         self.contenedorImg[self.instanciaActual[3]].add_widget(Button(text="?",on_press=self.clickInterrogante))
  119.         self.contenedorImg[self.instanciaActual[3]].children[0].source = self.instanciaActual[2]
  120.         self.contenedorImg[self.instanciaActual[3]].children[0].idImg = self.instanciaActual[3]
  121.         self.contenedorImg[self.instanciaActual[3]].remove_widget(self.contenedorImg[self.instanciaActual[3]].children[1])
  122.  
  123.         self.contenedorImg[self.instanciaActual[1]].add_widget(Button(text="?",on_press=self.clickInterrogante))
  124.         self.contenedorImg[self.instanciaActual[1]].children[0].source = self.instanciaActual[0]
  125.         self.contenedorImg[self.instanciaActual[1]].children[0].idImg = self.instanciaActual[1]
  126.         self.contenedorImg[self.instanciaActual[1]].remove_widget(self.contenedorImg[self.instanciaActual[1]].children[1])
  127.         self.instanciaActual = ["",0,"",0]
  128.         self.switchBotones()
  129.    
  130.     def switchBotones(self):
  131.         # alterna disabled de los botones para que no se puedan presionar
  132.         if(self.instanciaActual[0] != ""):
  133.             for i in range(16):
  134.                 self.contenedorImg[i].children[0].disabled = True
  135.         else:
  136.             for i in range(16):
  137.                 self.contenedorImg[i].children[0].disabled = False
  138.        
  139.     def salir(self,instance):
  140.         print "aca se cierra el juego"
  141.  
  142. if __name__ == '__main__':
  143.     principal().run()
Add Comment
Please, Sign In to add comment