Components and supplies
Arduino Mega 2560
Jumper wires (generic)
32x16 RGB LED Matrix Panel
Analog joystick (Generic)
Project description
Code
LED_Panel_Snake_Game.ino
arduino
1/* 2 Version 1.0 August, 2021 3 Copyright (c) 2021 Korneel Verraes 4 https://lego-projecten.webnode.nl/ 5 6 LED Panel Snake Game: 7 A joystick with analog output VRx and VRy is respectively connected to pin A3 and A4 8 An LED Panel 16x32 is connected according to the general scheme (see: https://learn.adafruit.com/32x16-32x32-rgb-led-matrix/new-wiring) 9 10 Accompanying video: https://youtu.be/G16ej5IbYTQ 11*/ 12 13#include <RGBmatrixPanel.h> 14 15//#define CLK 8 // USE THIS ON ARDUINO UNO, ADAFRUIT METRO M0, etc. 16//#define CLK A4 // USE THIS ON METRO M4 (not M0) 17#define CLK 11 // USE THIS ON ARDUINO MEGA 18#define OE 9 19#define LAT 10 20#define A A0 21#define B A1 22#define C A2 23 24#define F2(progmem_ptr) (const __FlashStringHelper *)progmem_ptr 25 26const int VRx = A3; 27const int VRy = A4; 28const char str[] PROGMEM = "GAME OVER"; 29const char str2[] PROGMEM = "YOUR SCORE WAS:"; 30 31int xPosition = 0; 32int yPosition = 0; 33int mapX = 0; 34int mapY = 0; 35 36//snake start x-coordinates 37int x[512] = {5, 4, 3, 2}; 38 39//snake start y-coordinates 40int y[512] = {7, 7, 7, 7}; 41int snakeDirection = 0; 42int lastDirection = 0; 43 44//snake start length 45int snakeLength = 4; 46boolean food = false; 47int foodX = 0; 48int foodY = 0; 49unsigned long previousTime = 0; 50int score = 0; 51 52//declare a lower value to make the snake move faster 53int difficulty = 150; 54 55RGBmatrixPanel matrix(A, B, C, CLK, LAT, OE, false); 56 57void setup() { 58 Serial.begin(9600); 59 pinMode(VRx, INPUT); 60 pinMode(VRy, INPUT); 61 matrix.begin(); 62 matrix.setTextWrap(false); 63 matrix.fillScreen(0); 64 65 //to make sure that totally random food is generated, A15 is a disconnected pin! 66 randomSeed(analogRead(A15)); 67} 68 69void loop() { 70 xPosition = analogRead(VRx); 71 yPosition = analogRead(VRy); 72 mapX = map(xPosition, 0, 1023, -10, 10); 73 mapY = map(yPosition, 0, 1023, -10, 10); 74 if (mapX > 4 && lastDirection != 2) snakeDirection = 1; 75 if (mapX < -4 && lastDirection != 1) snakeDirection = 2; 76 if (mapY < -4 && lastDirection != 4) snakeDirection = 3; 77 if (mapY > 4 && lastDirection != 3) snakeDirection = 4; 78 79 //turn off the last pixel 80 matrix.drawPixel(x[snakeLength - 1], y[snakeLength - 1], matrix.Color333(0, 0, 0)); 81 if (millis() - previousTime >= difficulty) { 82 //snake moves right 83 if (snakeDirection == 1) { 84 //check if the snake doesn't exceed the right edge 85 if (x[0] == 31) { 86 gameOver(); 87 return; 88 } 89 snakeCalculator(); 90 x[0]++; 91 } 92 //snake moves left 93 if (snakeDirection == 2) { 94 //check if the snake doesn't exceed the left edge 95 if (x[0] == 0) { 96 gameOver(); 97 return; 98 } 99 snakeCalculator(); 100 x[0]--; 101 } 102 //snake moves down 103 if (snakeDirection == 3) { 104 //check if the snake doesn't exceed the bottom edge 105 if (y[0] == 0) { 106 gameOver(); 107 return; 108 } 109 snakeCalculator(); 110 y[0]--; 111 } 112 //snake moves up 113 if (snakeDirection == 4) { 114 //check if the snake doesn't exceed the top edge 115 if (y[0] == 15) { 116 gameOver(); 117 return; 118 } 119 snakeCalculator(); 120 y[0]++; 121 } 122 lastDirection = snakeDirection; 123 previousTime = millis(); 124 } 125 126 //the snake is generated here 127 for (int i = 0; i < snakeLength; i++) { 128 matrix.drawPixel(x[i], y[i], matrix.Color333(0, 7, 0)); //green 129 } 130 131 //check if the snake eats food 132 if (foodX == x[0] && foodY == y[0]) { 133 snakeLength++; 134 food = false; 135 } 136 137 //check if there is food on the display 138 if (food == false) foodGenerator(); 139 140 //check if the snake doesn't touch himself 141 for (int i = 1; i < snakeLength; i++) { 142 if (x[0] == x[i] && y[0] == y[i]) gameOver(); 143 } 144} 145 146//calcultate the new coordinates 147void snakeCalculator() { 148 for (int i = snakeLength; i > 0; i--) { 149 //shift every x-coordinate in the array 150 x[i] = x[i - 1]; 151 } 152 for (int i = snakeLength; i > 0; i--) { 153 //shift every y-coordinate in the array 154 y[i] = y[i - 1]; 155 } 156} 157 158//generate food in random places 159void foodGenerator() { 160 foodX = random(32); 161 foodY = random(16); 162 163 //check if food isn't generated on the snake, otherwise try again (return) 164 for (int i = 0; i < snakeLength; i++) { 165 if (foodX == x[i] && foodY == y[i]) return; 166 } 167 matrix.drawPixel(foodX, foodY, matrix.Color333(7, 0, 0)); //red 168 food = true; 169} 170 171//game over screen 172void gameOver() { 173 //blink the current snake and food position 5 times 174 for (int j = 0; j < 5; j++) { 175 matrix.fillScreen(0); 176 delay(200); 177 for (int i = 0; i < snakeLength; i++) { 178 matrix.drawPixel(x[i], y[i], matrix.Color333(0, 7, 0)); //green 179 } 180 matrix.drawPixel(foodX, foodY, matrix.Color333(7, 0, 0)); //red 181 delay(200); 182 } 183 184 //scroll "GAME OVER" over the display 185 for (int i = 31; i > -120; i--) { 186 matrix.fillScreen(0); 187 matrix.setTextSize(2); 188 matrix.setCursor(i, 1); 189 matrix.setTextColor(matrix.Color333(7, 2, 0)); //orange 190 matrix.print(F2(str)); 191 delay(10); 192 } 193 194 //scroll "YOUR SCORE WAS" over the display 195 for (int i = 31; i > -192; i--) { 196 matrix.fillScreen(0); 197 matrix.setTextSize(2); 198 matrix.setCursor(i, 1); 199 matrix.setTextColor(matrix.Color333(7, 2, 0)); //orange 200 matrix.print(F2(str2)); 201 delay(10); 202 } 203 scoreCalculator(); 204 delay(5000); 205 newGame(); 206} 207 208//calculate your score 209void scoreCalculator() { 210 matrix.setTextSize(1); 211 matrix.setTextColor(matrix.Color333(7, 2, 0)); //orange 212 213 //if the score exists of only 1 digit 214 if (snakeLength / 10 == 0) { 215 matrix.setCursor(13, 4); 216 matrix.print(snakeLength); 217 } 218 219 //if the score exists of 2 digits 220 else if (snakeLength / 100 == 0) { 221 matrix.setCursor(10, 4); 222 223 //separate the first digit and display 224 matrix.print(snakeLength / 10); 225 matrix.setCursor(17, 4); 226 227 //separate the second digit and display 228 matrix.print(snakeLength % 10); 229 } 230 231 //if the score exists of 3 digits 232 else { 233 //separate the first digit and display 234 score = snakeLength / 100; 235 snakeLength -= score * 100; 236 matrix.setCursor(7, 4); 237 matrix.print(score); 238 239 //separate the second digit and display 240 score = snakeLength / 10; 241 snakeLength -= score * 10; 242 matrix.setCursor(13, 4); 243 matrix.print(score); 244 245 //separate the third digit and display 246 score = snakeLength; 247 matrix.setCursor(19, 4); 248 matrix.print(score); 249 } 250} 251 252//reset all variables to start a new game 253void newGame() { 254 matrix.fillScreen(0); 255 x[0] = 5; x[1] = 4; x[2] = 3; x[3] = 2; 256 y[0] = 7; y[1] = 7; y[2] = 7; y[3] = 7; 257 snakeDirection = 0; 258 lastDirection = 0; 259 snakeLength = 4; 260 food = false; 261 262 // to make sure that totally random food is generated, A15 is a disconnected pin! 263 randomSeed(analogRead(A15)); 264} 265
LED_Panel_Snake_Game.ino
arduino
1/* 2 Version 1.0 August, 2021 3 Copyright (c) 2021 Korneel Verraes 4 5 https://lego-projecten.webnode.nl/ 6 7 LED Panel Snake Game: 8 A joystick 9 with analog output VRx and VRy is respectively connected to pin A3 and A4 10 An 11 LED Panel 16x32 is connected according to the general scheme (see: https://learn.adafruit.com/32x16-32x32-rgb-led-matrix/new-wiring) 12 13 14 Accompanying video: https://youtu.be/G16ej5IbYTQ 15*/ 16 17#include <RGBmatrixPanel.h> 18 19//#define 20 CLK 8 // USE THIS ON ARDUINO UNO, ADAFRUIT METRO M0, etc. 21//#define CLK A4 22 // USE THIS ON METRO M4 (not M0) 23#define CLK 11 // USE THIS ON ARDUINO MEGA 24#define 25 OE 9 26#define LAT 10 27#define A A0 28#define B A1 29#define C A2 30 31#define 32 F2(progmem_ptr) (const __FlashStringHelper *)progmem_ptr 33 34const int VRx = 35 A3; 36const int VRy = A4; 37const char str[] PROGMEM = "GAME OVER"; 38const 39 char str2[] PROGMEM = "YOUR SCORE WAS:"; 40 41int xPosition = 0; 42int yPosition 43 = 0; 44int mapX = 0; 45int mapY = 0; 46 47//snake start x-coordinates 48int 49 x[512] = {5, 4, 3, 2}; 50 51//snake start y-coordinates 52int y[512] = {7, 7, 53 7, 7}; 54int snakeDirection = 0; 55int lastDirection = 0; 56 57//snake start 58 length 59int snakeLength = 4; 60boolean food = false; 61int foodX = 0; 62int 63 foodY = 0; 64unsigned long previousTime = 0; 65int score = 0; 66 67//declare 68 a lower value to make the snake move faster 69int difficulty = 150; 70 71RGBmatrixPanel 72 matrix(A, B, C, CLK, LAT, OE, false); 73 74void setup() { 75 Serial.begin(9600); 76 77 pinMode(VRx, INPUT); 78 pinMode(VRy, INPUT); 79 matrix.begin(); 80 matrix.setTextWrap(false); 81 82 matrix.fillScreen(0); 83 84 //to make sure that totally random food is generated, 85 A15 is a disconnected pin! 86 randomSeed(analogRead(A15)); 87} 88 89void loop() 90 { 91 xPosition = analogRead(VRx); 92 yPosition = analogRead(VRy); 93 mapX 94 = map(xPosition, 0, 1023, -10, 10); 95 mapY = map(yPosition, 0, 1023, -10, 10); 96 97 if (mapX > 4 && lastDirection != 2) snakeDirection = 1; 98 if (mapX < -4 && 99 lastDirection != 1) snakeDirection = 2; 100 if (mapY < -4 && lastDirection != 4) 101 snakeDirection = 3; 102 if (mapY > 4 && lastDirection != 3) snakeDirection = 4; 103 104 105 //turn off the last pixel 106 matrix.drawPixel(x[snakeLength - 1], y[snakeLength 107 - 1], matrix.Color333(0, 0, 0)); 108 if (millis() - previousTime >= difficulty) 109 { 110 //snake moves right 111 if (snakeDirection == 1) { 112 //check 113 if the snake doesn't exceed the right edge 114 if (x[0] == 31) { 115 gameOver(); 116 117 return; 118 } 119 snakeCalculator(); 120 x[0]++; 121 } 122 123 //snake moves left 124 if (snakeDirection == 2) { 125 //check if the 126 snake doesn't exceed the left edge 127 if (x[0] == 0) { 128 gameOver(); 129 130 return; 131 } 132 snakeCalculator(); 133 x[0]--; 134 } 135 136 //snake moves down 137 if (snakeDirection == 3) { 138 //check if the 139 snake doesn't exceed the bottom edge 140 if (y[0] == 0) { 141 gameOver(); 142 143 return; 144 } 145 snakeCalculator(); 146 y[0]--; 147 } 148 149 //snake moves up 150 if (snakeDirection == 4) { 151 //check if the snake 152 doesn't exceed the top edge 153 if (y[0] == 15) { 154 gameOver(); 155 156 return; 157 } 158 snakeCalculator(); 159 y[0]++; 160 } 161 162 lastDirection = snakeDirection; 163 previousTime = millis(); 164 } 165 166 167 //the snake is generated here 168 for (int i = 0; i < snakeLength; i++) { 169 170 matrix.drawPixel(x[i], y[i], matrix.Color333(0, 7, 0)); //green 171 } 172 173 174 //check if the snake eats food 175 if (foodX == x[0] && foodY == y[0]) { 176 177 snakeLength++; 178 food = false; 179 } 180 181 //check if there is food 182 on the display 183 if (food == false) foodGenerator(); 184 185 //check if the 186 snake doesn't touch himself 187 for (int i = 1; i < snakeLength; i++) { 188 if 189 (x[0] == x[i] && y[0] == y[i]) gameOver(); 190 } 191} 192 193//calcultate the new 194 coordinates 195void snakeCalculator() { 196 for (int i = snakeLength; i > 0; i--) 197 { 198 //shift every x-coordinate in the array 199 x[i] = x[i - 1]; 200 } 201 202 for (int i = snakeLength; i > 0; i--) { 203 //shift every y-coordinate in the 204 array 205 y[i] = y[i - 1]; 206 } 207} 208 209//generate food in random places 210void 211 foodGenerator() { 212 foodX = random(32); 213 foodY = random(16); 214 215 //check 216 if food isn't generated on the snake, otherwise try again (return) 217 for (int 218 i = 0; i < snakeLength; i++) { 219 if (foodX == x[i] && foodY == y[i]) return; 220 221 } 222 matrix.drawPixel(foodX, foodY, matrix.Color333(7, 0, 0)); //red 223 food 224 = true; 225} 226 227//game over screen 228void gameOver() { 229 //blink the current 230 snake and food position 5 times 231 for (int j = 0; j < 5; j++) { 232 matrix.fillScreen(0); 233 234 delay(200); 235 for (int i = 0; i < snakeLength; i++) { 236 matrix.drawPixel(x[i], 237 y[i], matrix.Color333(0, 7, 0)); //green 238 } 239 matrix.drawPixel(foodX, 240 foodY, matrix.Color333(7, 0, 0)); //red 241 delay(200); 242 } 243 244 //scroll 245 "GAME OVER" over the display 246 for (int i = 31; i > -120; i--) { 247 matrix.fillScreen(0); 248 249 matrix.setTextSize(2); 250 matrix.setCursor(i, 1); 251 matrix.setTextColor(matrix.Color333(7, 252 2, 0)); //orange 253 matrix.print(F2(str)); 254 delay(10); 255 } 256 257 258 //scroll "YOUR SCORE WAS" over the display 259 for (int i = 31; i > -192; i--) 260 { 261 matrix.fillScreen(0); 262 matrix.setTextSize(2); 263 matrix.setCursor(i, 264 1); 265 matrix.setTextColor(matrix.Color333(7, 2, 0)); //orange 266 matrix.print(F2(str2)); 267 268 delay(10); 269 } 270 scoreCalculator(); 271 delay(5000); 272 newGame(); 273} 274 275//calculate 276 your score 277void scoreCalculator() { 278 matrix.setTextSize(1); 279 matrix.setTextColor(matrix.Color333(7, 280 2, 0)); //orange 281 282 //if the score exists of only 1 digit 283 if (snakeLength 284 / 10 == 0) { 285 matrix.setCursor(13, 4); 286 matrix.print(snakeLength); 287 288 } 289 290 //if the score exists of 2 digits 291 else if (snakeLength / 100 == 292 0) { 293 matrix.setCursor(10, 4); 294 295 //separate the first digit and display 296 297 matrix.print(snakeLength / 10); 298 matrix.setCursor(17, 4); 299 300 //separate 301 the second digit and display 302 matrix.print(snakeLength % 10); 303 } 304 305 306 //if the score exists of 3 digits 307 else { 308 //separate the first digit 309 and display 310 score = snakeLength / 100; 311 snakeLength -= score * 100; 312 313 matrix.setCursor(7, 4); 314 matrix.print(score); 315 316 //separate the 317 second digit and display 318 score = snakeLength / 10; 319 snakeLength -= 320 score * 10; 321 matrix.setCursor(13, 4); 322 matrix.print(score); 323 324 325 //separate the third digit and display 326 score = snakeLength; 327 matrix.setCursor(19, 328 4); 329 matrix.print(score); 330 } 331} 332 333//reset all variables to start 334 a new game 335void newGame() { 336 matrix.fillScreen(0); 337 x[0] = 5; x[1] = 338 4; x[2] = 3; x[3] = 2; 339 y[0] = 7; y[1] = 7; y[2] = 7; y[3] = 7; 340 snakeDirection 341 = 0; 342 lastDirection = 0; 343 snakeLength = 4; 344 food = false; 345 346 // 347 to make sure that totally random food is generated, A15 is a disconnected pin! 348 349 randomSeed(analogRead(A15)); 350} 351
Downloadable files
led_panel_y2iCkknLcj.png
led_panel_y2iCkknLcj.png
Comments
Only logged in users can leave comments
legoev3projects
0 Followers
•2 Projects
1
1
Arduino Snake Game with 32x16 LED Panel | Arduino Project Hub
warner_6
3 years ago
It isnt working for the arduino uno because the file is too big