ROSALES_RUIZ

PING PONG CON MARCADOR DE PUNTAJE

Nov 3rd, 2015
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.71 KB | None | 0 0
  1. INSTITUTO POLITECNICO NACIONAL
  2. ESCUELA SUPERIOR DE INGENIERIA MECANICA Y ELECTRICA
  3. UNIDAD ZACATENCO
  4.  
  5. ING. EN CONTROL Y AUTOMATIZACION
  6. ASIGNATURA: INTERFASES Y MICROCONTROLADORES
  7. M.C LUIS ARTURO BENITEZ VELAZQUEZ
  8. GRUPO 7AM1
  9.  
  10. AUTORES:
  11. ROSALES HERNANDEZ JOSE EDUARDO
  12. RUIZ ESTUDILLO MARIA FERNANDA
  13. ***********************************************************************************
  14.  
  15. #include "LedControl.h"
  16. #include "LiquidCrystal.h"
  17.  
  18.  
  19. LedControl lc=LedControl(8,7,6,1); //Declaraci'on del objeto de control de la matriz
  20. int POT=A5;                        //Control del jugador 1
  21. int POT2=A4;                       //Control del jugador 2
  22. int POT_IN;                        //Variable analogica del jugador 1
  23. int POT_IN2;                       //Variable analogica del jugador 2
  24. int x;                             //Coord x de la pelota
  25. int y;                             //Coord y de la pelota
  26. int x_a;                           //Coord x anterior
  27. int y_a;                           //Coord y anterior
  28. int x_a1;
  29. int angulo;                        //Angulo de movimiento
  30. int j1x;                           //Posicion central de la barra 1
  31. int j2x;                           //Posicion central de la barra 2
  32. int j1;                            //Marcador del jugador 1
  33. int j2;                            //Marcador del jugador 2
  34. int boton=9;                       //Pin del boton de inicio
  35. int enclave=0;                     //Variable de enclave
  36. int inicio=0;                      //Variable de inicio
  37. int punt=5;                        //Puntaje ganador
  38.  
  39. //LCD
  40. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
  41. int player1=j1;
  42. int player2=j2;
  43.  
  44.  
  45. void setup()
  46. {
  47.   Serial.begin(9600);
  48.  
  49.   lc.shutdown(0,false);          //Encendido del Driver
  50.   lc.setIntensity(0,10);         //Intensidad de brillo de la matriz
  51.  
  52.   lcd.begin(16, 2);
  53.   lcd.setCursor(0,0);
  54.   lcd.print("IPN    ESIME ZAC");
  55.   lcd.setCursor(0,1);
  56.   lcd.print("P I N G  P O N G");
  57.   TransPort(5);
  58.   lcd.clear();
  59.   lcd.setCursor(0,0);
  60.   lcd.print("ROSALES H. LALO");
  61.   lcd.setCursor(0,1);
  62.   lcd.print("RUIZ E. MAFER");
  63.   TransPort(5);
  64.   lcd.clear();
  65.  
  66.   pinMode(POT, INPUT);
  67.   pinMode(POT2,INPUT);
  68.   pinMode(boton,INPUT);
  69.  
  70. //  for(int i=0;i<=7;i++)          //Apagado de toda la matriz
  71. //    for(int j=0;j<=7;j++)
  72. //      lc.setLed(0,i,j,false);
  73.   Serial.begin(9600);
  74.  
  75. }
  76.  
  77. void loop()
  78. {
  79.   while(enclave==0)                //Estado de Inicio
  80.   {
  81.     inicio=digitalRead(boton);
  82.     lc.setColumn(0,7,B00000000);   //Carita Feliz
  83.     lc.setColumn(0,6,B00100100);
  84.     lc.setColumn(0,5,B00100100);
  85.     lc.setColumn(0,4,B00000000);
  86.     lc.setColumn(0,3,B01000010);
  87.     lc.setColumn(0,2,B01000010);
  88.     lc.setColumn(0,1,B00111100);
  89.     lc.setColumn(0,0,B00000000);
  90.     // LCD: Presione Start
  91.     lcd.setCursor(0,0);
  92.     lcd.print("P I N G  P O N G");
  93.     lcd.setCursor(0,1);
  94.     lcd.print("   Push START  ");
  95.  
  96.    
  97.       if(inicio==1)                  //Inicio del Juego
  98.     {
  99.       lcd.clear();
  100.       enclave++;
  101.       for(int i=0;i<=7;i++)        //Apagado de toda la Matriz
  102.         for(int j=0;j<=7;j++)
  103.           lc.setLed(0,i,j,false);
  104.       reset();
  105.     }
  106.   }
  107.   while(enclave>=1)                 //Estado de Juego
  108.   {
  109.     Pelota();
  110.     lcdwhileplay();
  111.   }
  112. }
  113.  
  114. void Jugador1()
  115. {
  116.   POT_IN=analogRead(POT);
  117.   j1x=map(POT_IN,0,1025,1,7);
  118.  
  119.   lc.setLed(0,0,j1x+1,true);
  120.   lc.setLed(0,0,j1x,true);
  121.   lc.setLed(0,0,j1x-1,true);
  122.  
  123.   for(int i=0;i<=7;i++)
  124.     if(i!=j1x&&i!=j1x-1&&i!=j1x+1)
  125.       lc.setLed(0,0,i,false);
  126. }
  127.  
  128. void Jugador2()
  129. {
  130.   POT_IN2=analogRead(POT2);
  131.   j2x=map(POT_IN2,0,1025,1,7);
  132.  
  133.   lc.setLed(0,7,j2x+1,true);
  134.   lc.setLed(0,7,j2x,true);
  135.   lc.setLed(0,7,j2x-1,true);
  136.  
  137.   for(int i=0;i<=7;i++)
  138.     if(i!=j2x&&i!=j2x-1&&i!=j2x+1)
  139.       lc.setLed(0,7,i,false);
  140. }
  141.  
  142. void Pelota()
  143. {
  144.   CalcAngulo();
  145.   lc.setLed(0,x,y,true);
  146.   lc.setLed(0,x_a,y_a,false);
  147.  
  148.   delay(75);
  149.   Jugador1();
  150.   Jugador2();
  151.  
  152.   delay(75);
  153.   Jugador1();
  154.   Jugador2();
  155.  
  156.   delay(75);
  157.   x_a=x;
  158.   y_a=y;
  159. }
  160.  
  161. void CalcAngulo()
  162. {
  163.   if(x==6)
  164.   {
  165.     if((angulo==225||angulo==315)&&(y==j2x+1||y==j2x-1))
  166.       angulo=90;
  167.     else if(angulo==225&&y==j2x)
  168.       angulo=135;
  169.     else if(angulo==315&&y==j2x)
  170.       angulo=45;
  171.     else if(angulo==270&&(y==j2x+1||y==j2x||y==j2x-1))
  172.       angulo=random(0,2) == 0 ? 45:135;
  173.     else if(angulo==225&&y==j2x+2)
  174.       angulo=45;
  175.     else if(angulo==315&&y==j2x-2)
  176.       angulo=135;
  177.     else if(y!=j2x+1||y!=j2x||y!=j2x-1)
  178.       {
  179.         if(angulo==225)
  180.           lc.setLed(0,x+1,y-1,true);
  181.         if(angulo==270)
  182.           lc.setLed(0,x+1,y,true);
  183.         if(angulo==315)
  184.           lc.setLed(0,x+1,y+1,true);
  185.         //lc.setLed(0,x,y,false);
  186.            
  187.         j1=j1+1;
  188.        
  189.         delay(500);
  190.         if(j1>=punt)
  191.         {
  192.           enclave=0;
  193.           winner(1);
  194.           Trans();
  195.           delay (3000);
  196.           return;
  197.         }      
  198.         x=1;
  199.         y=random(1,6);
  200.         angulo=random(0,2) == 0 ? 225 : 315;
  201.       }
  202.   }
  203.  
  204.   else if(x==1)
  205.   {
  206.     if((angulo==45||angulo==135)&&(y==j1x+1||y==j1x-1))
  207.       angulo=270;
  208.     else if(angulo==135&&y==j1x)
  209.       angulo=225;
  210.     else if(angulo==45&&y==j1x)
  211.       angulo=315;
  212.     else if(angulo==90&&(y==j1x+1||y==j1x||y==j1x-1))
  213.       angulo=random(0,2) == 0 ? 225:315;
  214.     else if(angulo==135&&y==j1x+2)
  215.       angulo=315;
  216.     else if(angulo==45&&y==j1x-2)
  217.       angulo=225;
  218.     else if(y!=j1x+1||y!=j1x||y!=j1x-1)
  219.       {
  220.         if(angulo==45)
  221.           lc.setLed(0,x-1,y+1,true);
  222.         if(angulo==90)
  223.           lc.setLed(0,x-1,y,true);
  224.         if(angulo==135)
  225.           lc.setLed(0,x-1,y-1,true);
  226.         //lc.setLed(0,x,y,false);
  227.        
  228.         j2=j2+1;
  229.        
  230.         Serial.println(j2);
  231.         delay(500);
  232.         if(j2>=punt)
  233.         {
  234.           enclave=0;
  235.           winner(2);          
  236.           Trans();
  237.           delay (3000);
  238.           return;
  239.         }
  240.         x=6;
  241.         y=random(1,6);
  242.         angulo=random(0,2) == 0 ? 45 : 135;
  243.       }
  244.   }
  245.   if(y==7)
  246.   {
  247.     if(angulo==45)
  248.       angulo=135;
  249.     else if(angulo==315)
  250.       angulo=225;
  251.   }
  252.   else if(y==0)
  253.   {
  254.     if(angulo==135)
  255.       angulo=45;
  256.     else if(angulo==225)
  257.       angulo=315;
  258.   }
  259.   MovimientoPelota();
  260. }
  261.  
  262. void MovimientoPelota()
  263. {
  264.     if(angulo==45)
  265.   {
  266.     x=x-1;
  267.     y=y+1;
  268.   }
  269.   else if(angulo==90)
  270.   {
  271.     x=x-1;
  272.   }
  273.   else if(angulo==135)
  274.   {
  275.     x=x-1;
  276.     y=y-1;
  277.   }
  278.   else if(angulo==225)
  279.   {
  280.     x=x+1;
  281.     y=y-1;
  282.   }
  283.   else if(angulo==270)
  284.   {
  285.     x=x+1;
  286.   }
  287.   else if(angulo==315)
  288.   {
  289.     x=x+1;
  290.     y=y+1;
  291.   }
  292. }
  293.  
  294. void reset()
  295. {
  296.   j1=0;
  297.   j2=0;
  298.   x=random(3,4);
  299.   y=random(2,6);
  300.   angulo=random(0,1) == 0 ? 90 : 270;
  301. }
  302.  
  303. void Trans()
  304. {
  305.   for(int i=0;i<=7;i++)
  306.     for(int j=0;j<=7;j++)
  307.     {
  308.       lc.setLed(0,i,j,true);
  309.       delay(20);
  310.     }
  311.   for(int i=0;i<=7;i++)
  312.     for(int j=0;j<=7;j++)
  313.     {
  314.       lc.setLed(0,i,j,false);
  315.       delay(20);
  316.     }
  317. }
  318.  
  319. void lcdwhileplay()
  320. {
  321.   lcd.setCursor(0,0);
  322.   lcd.print("PLAYER1");
  323.  
  324.   lcd.setCursor(4,1);
  325.   lcd.print(j1);
  326.  
  327.   lcd.setCursor(8,0);
  328.   lcd.print("PLAYER2");
  329.    
  330.   lcd.setCursor(12,1);
  331.   lcd.print(j2);
  332. }
  333.  
  334. void winner(int p)
  335. {
  336.   lcd.clear();
  337.   lcd.setCursor(0,0);
  338.   lcd.print("W I N N E R");
  339.   lcd.setCursor(0,1);
  340.   if(p==1)
  341.   {
  342.     lcd.print("P L A Y E R  1");
  343.   }
  344.   else if(p==2)
  345.   {
  346.     lcd.print("P L A Y E R  2");
  347.   }
  348. }
  349.  
  350. void TransPort(int a)
  351. {
  352.   int j=0;
  353.   a=a*2;
  354.   while(j<=a){
  355.     for(int i=0;i<=6;i=i+2){
  356.       lc.setColumn(0,i,B10101010);
  357.       lc.setColumn(0,i+1,B01010101);
  358.     }
  359.     delay(250);
  360.     for(int i=0;i<=6;i=i+2){
  361.       lc.setColumn(0,i+1,B10101010);
  362.       lc.setColumn(0,i,B01010101);
  363.     }
  364.     delay(250);
  365.     j++;
  366.   }
  367. }
Advertisement
Add Comment
Please, Sign In to add comment