Components and supplies
SparkFun LED Array - 8x7
Male/Female Jumper Wires
Analog joystick (Generic)
Arduino UNO
Arduino Proto Shield
Apps and platforms
Arduino IDE
Project description
Code
Snake
c_cpp
All that you have to do is copy and paste the code
1/** 2 * Snake 3 * Implementation of the classical Snake game 4 * where the snake moved constantly and the player 5 * have to avoid intersections with itself and borders. 6 * When the snake eats food it grows and speeds up a little bit. 7 * 8 * 23 Jun 2016 9 * by Sergey Royz and hunter Buzzell 10 */ 11 12#include <LedControl.h> 13 14/* Joystick PINs */ 15#define VRX A0 16#define VRY A1 17#define SW 2 18 19/* Display PINs */ 20#define CLK 8 21#define CS 9 22#define DIN 10 23 24#define SIZE 8 25#define ADVANCE_DELAY 20 26 27 28int snake[SIZE*SIZE][2]; 29int length; 30int food[2], v[2]; 31bool is_game_over = false; 32long current_time; 33long prev_advance; 34int blink_count; 35const short messageSpeed = 5; 36 37 38 39LedControl lc = LedControl(DIN, CLK, CS, 1); 40 41void init_game() { 42 prev_advance = current_time = 0; 43 blink_count = 3; 44 int half = SIZE / 2; 45 length = SIZE / 3; 46 47 for (int i = 0; i < length; i++) { 48 snake[i][0] = half - 1; 49 snake[i][1] = half + i; 50 } 51 52 food[0] = half + 1; 53 food[1] = half - 1; 54 55 v[0] = 0; 56 v[1] = -1; 57} 58 59void render() { 60 for (int i = 0; i < length; i++) { 61 lc.setLed(0, snake[i][0], snake[i][1], 1); 62 } 63 lc.setLed(0, food[0], food[1], 1); 64} 65 66void clearScreen() { 67 for (int x = 0; x < SIZE; x++) { 68 for (int y = 0; y < SIZE; y++) { 69 lc.setLed(0, x, y, 0); 70 } 71 } 72} 73 74/** 75 * moves the snake forward 76 * returns true if the game is over 77 */ 78bool advance() { 79 int head[2] = {snake[0][0] + v[0], snake[0][1] + v[1]}; 80 81 if (head[0] < 0 || head[0] >= SIZE) { 82 83 delay(1000); 84 showGameOverMessage(); 85 return true; 86 } 87 88 if (head[1] < 0 || head[1] >= SIZE) { 89 90 delay(1000); 91 showGameOverMessage(); 92 return true; 93 } 94 95 for (int i = 0; i < length; i++) { 96 if (snake[i][0] == head[0] && snake[i][1] == head[1]) { 97 delay(1000); 98 showGameOverMessage(); 99 return true; 100 } 101 } 102 103 bool grow = (head[0] == food[0] && head[1] == food[1]); 104 if (grow) { 105 length++; 106 randomSeed(current_time); 107 food[0] = random(SIZE); 108 food[1] = random(SIZE); 109 } 110 111 for (int i = length - 1; i >= 0; i--) { 112 snake[i + 1][0] = snake[i][0]; 113 snake[i + 1][1] = snake[i][1]; 114 } 115 snake[0][0] += v[0]; 116 snake[0][1] += v[1]; 117 return false; 118} 119 120void setup() { 121 pinMode(SW, INPUT_PULLUP); 122 pinMode(VRX, INPUT); 123 pinMode(VRY, INPUT); 124 attachInterrupt(digitalPinToInterrupt(SW), restart, RISING); 125 126 lc.shutdown(0, false); 127 lc.setIntensity(0, 8); 128 129 init_game(); 130 render(); 131} 132 133void loop() { 134 if (!is_game_over) { 135 clearScreen(); 136 render(); 137 138 if (current_time - prev_advance > ADVANCE_DELAY) { 139 is_game_over = advance(); 140 prev_advance = current_time; 141 } 142 } else { 143 while (blink_count > 0) { 144 clearScreen(); 145 delay(300); 146 render(); 147 delay(300); 148 blink_count--; 149 150 } 151 } 152 readControls(); 153 current_time++; 154} 155 156void restart() { 157 init_game(); 158 is_game_over = false; 159} 160 161void readControls() { 162 int dx = map(analogRead(VRX), 0, 906, 2, -2); 163 int dy = map(analogRead(VRY), 0, 906, -2, 2); 164 if (dx != 0) {dx = dx / abs(dx);} 165 if (dy != 0) {dy = dy / abs(dy);} 166 167 if (dy != 0 && v[0] != 0) { 168 v[0] = 0; 169 v[1] = dy; 170 } 171 172 if (dx != 0 && v[1] != 0) { 173 v[0] = dx; 174 v[1] = 0; 175 } 176 177} 178 179const PROGMEM bool gameOverMessage[8][90] = { 180 {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,0,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0,0,1,1,1,1,0,0,0,1,1,0,0,0,0,0,0,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 187 {0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,1,1,0,0,1,1,0,0,1,1,0,0,0,1,1,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,1,1,0,0,0,0,1,1,1,1,1,1,0,0,1,1,0,0,1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0} 188}; 189void showGameOverMessage() { 190 for (int d = 0; d < sizeof(gameOverMessage[0]) - 7; d++) { 191 for (int col = 0; col < 8; col++) { 192 delay(messageSpeed); 193 for (int row = 0; row < 8; row++) { 194 // this reads the byte from the PROGMEM and displays it on the screen 195 lc.setLed(0, row, col, pgm_read_byte(&(gameOverMessage[row][col + d]))); 196 } 197 } 198 } 199} 200 201 202
Comments
Only logged in users can leave comments
Hunter1234
0 Followers
•0 Projects
Table of contents
Intro
18
0