manhattanxl

Juego de memoria, Python, Kivy

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