Código
Snake C / C ++
Tudo o que você precisa fazer é copiar e colar o código / ** * Snake * Implementação do clássico jogo Snake * onde a cobra se move constantemente e o jogador * tem que evitar interseções com ela mesma e bordas. * Quando a cobra come comida, ela cresce e acelera um pouco. * * 23 de junho de 2016 * por Sergey Royz e hunter Buzzell * / # include / * Joystick PINs * / # define VRX A0 # define VRY A1 # define SW 2 / * Display PINs * / # define CLK 8 # define CS 9 # define DIN 10 # define SIZE 8 # define ADVANCE_DELAY 20int cobra [SIZE * SIZE] [2]; int comprimento; int food [2], v [2]; bool is_game_over =false; long current_time; long prev_advance; int blink_count; const short messageSpeed =5; LedControl lc =LedControl (DIN, CLK, CS, 1); void init_game () {prev_advance =current_time =0; blink_count =3; metade int =TAMANHO / 2; comprimento =SIZE / 3; para (int i =0; i =SIZE) {delay (1000); showGameOverMessage (); return true; } if (head [1] <0 || head [1]> =SIZE) {delay (1000); showGameOverMessage (); return true; } para (int i =0; i =0; i--) {cobra [i + 1] [0] =cobra [i] [0]; cobra [i + 1] [1] =cobra [i] [1]; } cobra [0] [0] + =v [0]; cobra [0] [1] + =v [1]; return false;} void setup () {pinMode (SW, INPUT_PULLUP); pinMode (VRX, INPUT); pinMode (VRY, INPUT); attachInterrupt (digitalPinToInterrupt (SW), reiniciar, RISING); lc.shutdown (0, falso); lc.setIntensity (0, 8); init_game (); render ();} void loop () {if (! is_game_over) {clearScreen (); render (); if (current_time - prev_advance> ADVANCE_DELAY) {is_game_over =advance (); prev_advance =current_time; }} else {while (blink_count> 0) {clearScreen (); atraso (300); render (); atraso (300); blink_count--; }} readControls (); current_time ++;} void restart () {init_game (); is_game_over =false;} void readControls () {int dx =map (analogRead (VRX), 0, 906, 2, -2); int dy =map (analogRead (VRY), 0, 906, -2, 2); if (dx! =0) {dx =dx / abs (dx);} if (dy! =0) {dy =dy / abs (dy);} if (dy! =0 &&v [0]! =0 ) {v [0] =0; v [1] =dy; } if (dx! =0 &&v [1]! =0) {v [0] =dx; v [1] =0; }} const PROGMEM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