Advertisement
Guest User

Untitled

a guest
Nov 7th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Programa: Snake com Arduino Uno
  2. //Alteracoes e adaptacoes: FILIPEFLOP
  3. //
  4. //Baseado no codigo original de Abhinav Faujdar *****/
  5.  
  6. #include <Adafruit_GFX.h>
  7. #include <Adafruit_PCD8544.h>
  8. #include <SPI.h>
  9.  
  10. //Inicializa o display
  11. Adafruit_PCD8544 display = Adafruit_PCD8544(8, 9, 10, 11, 12);
  12.  
  13. #define LEFT 7
  14. #define DOWN 5
  15. #define RIGHT 4
  16. #define UP 6
  17. #define speakerPin 17
  18.  
  19. //Verifica em qual direcao a cobra esta se movendo
  20. boolean dl = false, dr = false, du = false, dd = false;
  21.  
  22. int x[50], y[50], i, slength, tempx = 10, tempy = 10, xx, yy;
  23. unsigned int high;
  24. uint8_t bh, bl;
  25. int xegg, yegg;
  26. int freq, tb;
  27. int l, r, u, d, p;
  28. unsigned long time = 280, beeptime = 50;
  29. int score = 0, flag = 0;
  30.  
  31. void setup()
  32. {
  33.   Serial.begin(9600);
  34.   display.begin();
  35.  
  36.   //Informacoes iniciais display
  37.   display.setContrast(50);
  38.   display.clearDisplay();
  39.   display.drawRoundRect(0, 0, 84 , 25, 1, 2);
  40.   display.setTextSize(2);
  41.   display.setTextColor(BLACK);
  42.   display.setCursor(12, 6);
  43.   display.println("SNAKE");
  44.   display.setTextSize(1);
  45.   display.setTextColor(BLACK);
  46.   display.setCursor(12, 29);
  47.   display.println("FILIPEFLOP");
  48.   display.setCursor(13, 29);
  49.   display.println("FILIPEFLOP");
  50.   display.setCursor(0, 40);
  51.   display.println("filipeflop.com");
  52.  
  53.   display.display();
  54.   delay(2000);
  55.  
  56.   pinMode(LEFT, INPUT);
  57.   pinMode(RIGHT, INPUT);
  58.   pinMode(UP, INPUT);
  59.   pinMode(DOWN, INPUT);
  60.   pinMode(speakerPin, OUTPUT);
  61.   digitalWrite(LEFT, HIGH);
  62.   digitalWrite(RIGHT, HIGH);
  63.   digitalWrite(UP, HIGH);
  64.   digitalWrite(DOWN, HIGH);
  65.  
  66.   slength = 8;
  67.   xegg = (display.width()) / 2;
  68.   yegg = (display.height()) / 2;
  69.   display.clearDisplay();
  70.   display.drawRect(0, 0, 84, 48, 1);
  71.   display.setCursor(4, 2);
  72.   display.print("P:     R:");
  73.   display.setCursor(22, 2);
  74.   display.print(score);
  75.   display.setCursor(64, 2);
  76.   display.print(high);
  77.   display.drawRect(0, 0, 84, 11, 1);
  78.   //Define coordenadas iniciais
  79.   for (i = 0; i <= slength; i++)
  80.   {
  81.     x[i] = 25 - 3 * i;
  82.     y[i] = 30;
  83.   }
  84.   for (i = 0; i < slength; i++)  //Draw the snake
  85.   {
  86.     display.drawCircle(x[i], y[i], 1, BLACK);
  87.   }
  88.   display.display();
  89.   dr = true;
  90. }
  91.  
  92. void loop()
  93. {
  94.   movesnake();
  95. }
  96.  
  97. void movesnake()
  98. {
  99.   //Leitura do valor dos botoes
  100.   l = digitalRead(LEFT);
  101.   d = digitalRead(DOWN);
  102.   r = digitalRead(RIGHT);
  103.   u = digitalRead(UP);
  104.  
  105.   if (flag == 0)
  106.   {
  107.     direct();
  108.   }
  109.  
  110.   if (millis() % time == 0)
  111.   {
  112.     if (flag == 0)
  113.     {
  114.       if (dr == true) {
  115.         tempx = x[0] + 3;
  116.         tempy = y[0];
  117.       }
  118.       if (dl == true) {
  119.         tempx = x[0] - 3;
  120.         tempy = y[0];
  121.       }
  122.       if (du == true) {
  123.         tempy = y[0] - 3;
  124.         tempx = x[0];
  125.       }
  126.       if (dd == true) {
  127.         tempy = y[0] + 3;
  128.         tempx = x[0];
  129.       }
  130.     }
  131.     flag = 0;
  132.     //Verifica se a cobra esta nas mesmas coordenadas do ovo
  133.     checkgame();
  134.     checkegg();
  135.  
  136.     //Altera as coordenadas de todos os pontos da cobra
  137.     for (i = 0; i <= slength; i++)
  138.     {
  139.       xx = x[i];
  140.       yy = y[i];
  141.       x[i] = tempx;
  142.       y[i] = tempy;
  143.       tempx = xx;
  144.       tempy = yy;
  145.     }
  146.     //Desenha a cobra e o ovo nas novas coordenadas
  147.     drawsnake();
  148.   }
  149. }
  150.  
  151. void checkgame()
  152. {
  153.   for (i = 1; i < slength; i++)
  154.   {
  155.     //Verifica se o recorde foi batido e
  156.     //mostra na tela o novo valor
  157.     high = (((0xff00 + bh) << 8) + bl);
  158.     if (score > high)
  159.     {
  160.       high = score;
  161.       bh = (high >> 8);
  162.       bl = high & 0xff;
  163.       display.fillRect(61, 1, 20, 9, 0);
  164.       display.setCursor(65, 2);
  165.       display.print(high);
  166.     }
  167.     //Verifica se tocou nas bordas
  168.     if ((x[0] <= 1 || x[0] >= 83) || (y[0] <= 12 || y[0] >= 47) || (x[i] == x[0] && y[i] == y[0]) )
  169.     {
  170.       //Jogo terminado. Mostra informacoes na tela
  171.       display.clearDisplay();
  172.       display.fillRoundRect(0, 0, 84 , 31, 1, 2);
  173.       display.setTextColor(WHITE);
  174.       display.setTextSize(2);
  175.       display.setCursor(18, 1);
  176.       display.print("GAME");
  177.       display.setCursor(18, 16);
  178.       display.print("OVER");
  179.       display.setTextColor(BLACK);
  180.       display.setTextSize(1);
  181.       display.setCursor(12, 33);
  182.       display.print("PLACAR");
  183.       display.setCursor(60, 33);
  184.       display.print(score);
  185.       display.setCursor(12, 41);
  186.       display.print("RECORDE");
  187.       display.setCursor(60, 41);
  188.       display.print(high);
  189.       display.display();
  190.       //Aguarda 5 segundos e reinicia o jogo
  191.       delay(5000);
  192.  
  193.       //Apaga a tela
  194.       display.clearDisplay();
  195.       //Retorna aos valores iniciais
  196.       slength = 8;
  197.       score = 0;
  198.       time = 280;
  199.       redraw();
  200.     }
  201.   }
  202. }
  203.  
  204. void checkegg()      //Snake meets egg
  205. {
  206.   //Verifica se a cobra está nas mesmas coordenadas do ovo
  207.   if (x[0] == xegg or x[0] == (xegg + 1) or x[0] == (xegg + 2) or x[0] == (xegg - 1))
  208.   {
  209.     if (y[0] == yegg or y[0] == (yegg + 1) or y[0] == (yegg + 2) or y[0] == (yegg - 1))
  210.     {
  211.       //Incrementa a cobra em uma posição, incrementa o placar e
  212.       //aumenta a velocidade do jogo, diminuindo o tempo
  213.       score += 1;
  214.       display.fillRect(21, 1, 20, 9, 0);
  215.       display.setCursor(22, 2);
  216.       display.print(score);
  217.       slength += 1;
  218.       if (time >= 90)
  219.       {
  220.         time -= 10;
  221.       }
  222.       display.fillRect(xegg, yegg, 3, 3, WHITE);
  223.       display.display();
  224.       beep(35, beeptime);
  225.       //Cria novo ovo em posicao aleatoria
  226.       xegg = random(2, 80);
  227.       yegg = random(15, 40);
  228.     }
  229.   }
  230. }
  231.  
  232. void direct()
  233. {
  234.   //Altera o movimento caso uma tecla tenha sido pressionada
  235.   if (l == LOW and dr == false)
  236.   {
  237.     dl = true; du = false; dd = false;
  238.     //Salva novas coordenadas em Tempx, Tempy
  239.     tempx = x[0] - 3;
  240.     tempy = y[0];
  241.     flag = 1;
  242.   }
  243.   else if (r == LOW and dl == false)
  244.   {
  245.     dr = true; du = false; dd = false;
  246.     tempx = x[0] + 3;
  247.     tempy = y[0];
  248.     flag = 1;
  249.   }
  250.   else if (u == LOW and dd == false)
  251.   {
  252.     du = true; dl = false; dr = false;
  253.     tempy = y[0] - 3;
  254.     tempx = x[0];
  255.     flag = 1;
  256.   }
  257.   else if (d == LOW and du == false)
  258.   {
  259.     dd = true; dl = false; dr = false;
  260.     tempy = y[0] + 3;
  261.     tempx = x[0];
  262.     flag = 1;
  263.   }
  264. }
  265.  
  266. void drawsnake()
  267. {
  268.   display.fillRect(xegg, yegg, 3, 3, BLACK);
  269.   display.drawCircle(x[0], y[0], 1, BLACK);
  270.   display.drawCircle(x[slength], y[slength], 1, WHITE);
  271.   display.display();
  272. }
  273.  
  274. void redraw()
  275. {
  276.   display.drawRect(0, 0, 84, 48, 1);
  277.   display.drawRect(0, 0, 84, 48, 1);
  278.   display.setCursor(4, 2);
  279.   display.print("P:     R:");
  280.   display.drawRect(0, 0, 84, 11, 1);
  281.   display.fillRect(21, 1, 20, 9, 0);
  282.   display.setCursor(22, 2);
  283.   display.print(score);
  284.   display.fillRect(61, 1, 20, 9, 0);
  285.   display.setCursor(65, 2);
  286.   display.print(high);
  287.  
  288.   xegg = (display.width()) / 2;
  289.   yegg = (display.height()) / 2;
  290.   dl = false, dr = false, du = false, dd = false;
  291.   dr = true;
  292.   display.setCursor(4, 2);
  293.   display.print("P:     R:");
  294.   display.drawRect(0, 0, 84, 11, 1);
  295.   //Retorna as coordenadas iniciais
  296.   for (i = 0; i <= slength; i++)
  297.   {
  298.     x[i] = 25 - 3 * i;
  299.     y[i] = 30;
  300.   }
  301.   tempx = 33 - 3 * i;
  302.   tempy = 30;
  303.   display.display();
  304. }
  305.  
  306. void beep (int freq, long tb)
  307. {
  308.   int x;
  309.   long delayAmount = (long)(500 / freq);
  310.   long loopTime = (long)(tb / (delayAmount * 2));
  311.   for (x = 0; x < loopTime; x++)
  312.   {
  313.     digitalWrite(speakerPin, HIGH);
  314.     delay(delayAmount);
  315.     digitalWrite(speakerPin, LOW);
  316.     delay(delayAmount);
  317.   }
  318.   delay(2);
  319. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement