Components and supplies
Buzzer
Pushbutton switch 12mm
Breadboard (generic)
MAXREFDES99# MAX7219 Display Driver Shield
Arduino Mega 2560
Project description
Code
asteroids game code
arduino
1/******************************************************** 2 * This is a simple game using the MAX72XX LED-Matrix. 3 * You are a star ship and you have to avoid contact with 4 * the asteroids falling down. 5 * 6 * (c) 2018 by Joshua Weßner 7 * 8 * 9 * 10 *******************************************************/ 11 12 13//LedControl by Eberhard Fahle 14// see: http://wayoda.github.io/LedControl/ 15#include "LedControl.h" 16 17/* 18 Now we need a LedControl to work with. 19 ***** These pin numbers will probably not work with your hardware ***** 20 pin 12 is connected to the DataIn 21 pin 11 is connected to the CLK 22 pin 10 is connected to LOAD 23 We have only a single MAX72XX. 24 */ 25LedControl lc=LedControl(12,11,10,1); 26 27 28/************************************************************** 29 * Global Variables 30 */ 31 32/* delay between updates of the display */ 33unsigned long delaytime=1000; 34 35/* to Display the score */ 36String scoreString; 37int score; 38int hundreds; 39String scoreStr; 40String scoreArr[] = {"" ,"" ,"" }; 41 42/* micro switches for control */ 43int pinLeft = 2; 44int pinRight = 3; 45volatile unsigned long buttonPressed; 46int buttonDelay = 150; //handle contact bounce 47 48volatile bool gameOver = false; 49 50/* "rythm" of the game, im milliseconds */ 51int tick; 52int tickCounter = 1; 53 54/* timestamp */ 55unsigned long now; 56 57/* display */ 58int ship; /* Initial ship position */ 59int columns[] = {0,0,0,0,0,0,0,0}; // 8 columns, every int is row 1-8. 60int randomInt; 61 62/************************************************************** 63 * setup 64 */ 65 66void setup() { 67 /* Initializing */ 68 gameOver = false; 69 hundreds = 0; 70 scoreArr[0] = ""; 71 scoreArr[1] = ""; 72 scoreArr[2] = ""; 73 score = 0; 74 tick = 300; 75 tickCounter = 1; 76 ship = 3; 77 now = millis(); 78 buttonPressed = millis(); 79 randomSeed(analogRead(15)); // better random numbers 80 81 for(int i = 0; i<8; i++) 82 columns[i] = 0; 83 84 // Debugging 85 // Serial.begin(9600); 86 // Serial.print(columns[0]); 87 88 /* 89 The MAX72XX is in power-saving mode on startup, 90 we have to do a wakeup call 91 */ 92 lc.shutdown(0,false); 93 /* Set the brightness to a medium values */ 94 lc.setIntensity(0,1); 95 /* and clear the display */ 96 lc.clearDisplay(0); 97 98 pinMode(pinLeft, INPUT_PULLUP); 99 pinMode(pinRight, INPUT_PULLUP); 100 101 /* attach button press to interrupts */ 102 attachInterrupt(digitalPinToInterrupt(pinLeft), left, FALLING); 103 attachInterrupt(digitalPinToInterrupt(pinRight), right, FALLING); 104 105} 106 107/************************************************************** 108 * handle left button press 109 */ 110void left() 111{ 112 if(millis() - buttonPressed > buttonDelay) // handle switch contact bounce 113 { 114 if(ship != 0) 115 ship--; 116 else 117 ship = 7; 118 119 lc.clearDisplay(0); 120 buttonPressed = millis(); 121 } 122 123 // after game over, button press will restart game 124 if(gameOver == true){ 125 gameOver = false; 126 setup(); 127 } 128} 129 130/************************************************************** 131 * handle right button press 132 */ 133void right() 134{ 135 if(millis() - buttonPressed > buttonDelay) // handle switch contact bounce 136 { 137 if(ship != 7) 138 ship++; 139 else 140 ship = 0; 141 142 lc.clearDisplay(0); 143 buttonPressed = millis(); 144 } 145 146 // after game over, button press will restart game 147 if(gameOver == true){ 148 gameOver = false; 149 setup(); 150 } 151} 152 153 154/************************************************************** 155 * loop 156 */ 157void loop() { 158 159 if(millis() - now > tick){ //do every tick 160 161 // score is: how many ticks you survived 162 score++; 163 164 now = millis(); 165 166 if(tickCounter == 1){ //every 4th tick 167 168 // make game faster over time 169 tick = tick/1.02; 170 171 // randomly choose column 172 randomInt = random(0, 8); 173 174 // if no asteroid exists in column, create one in row 1. 175 if(columns[randomInt] == 0){ 176 columns[randomInt] = 1; 177 } 178 } 179 180 if(tickCounter != 4) 181 tickCounter++; 182 else 183 tickCounter = 1; 184 185 // do for every column 186 for(int i = 0; i<8; i++){ 187 188 if(columns[i] == 10) // delete asteroids when out of display 189 columns[i] = 0; 190 191 if(columns[i] != 0) // make asteroids fall down 192 columns[i]++; 193 } 194 195 lc.clearDisplay(0); 196 } 197 198 199 /* write to display */ 200 201 // ship 202 lc.setLed(0, 7, ship, true); 203 204 // asteroids 205 for(int i = 0; i<8; i++){ 206 if(columns[i] > 0) 207 lc.setLed(0, columns[i]-2, i, true); 208 lc.setLed(0, columns[i]-3, i, true); 209 } 210 211 // detect collision of ship with asteroid 212 if(columns[ship] == 10 or columns[ship] == 9){ 213 lc.clearDisplay(0); 214 215 // animate explosion 216 for(int i = 0; i<4; i++){ 217 lc.setLed(0,7,ship+i,true); 218 lc.setLed(0,7,ship-i,true); 219 lc.setLed(0,7-i,ship+i,true); 220 lc.setLed(0,7-i,ship-i,true); 221 lc.setLed(0,7-1.5*i,ship,true); 222 223 // explosion sound 224 unsigned long time = millis(); 225 int randomSound=1000; 226 while(millis() - time <= 250) { 227 randomSound--; 228 tone(9, random(randomSound, 1000)); // change the parameters of random() for different sound 229 } 230 231 lc.clearDisplay(0); 232 noTone(9); 233 } 234 235 delay(500); 236 237 /* display score */ 238 239 scoreStr = String(score); 240 241 // get single digits from score int 242 scoreArr[0] = scoreStr.charAt(0); 243 scoreArr[1] = scoreStr.charAt(1); 244 scoreArr[2] = scoreStr.charAt(2); 245 246 // matrix can only display up to 2 digits (up to 99) 247 if(score < 100){ 248 for(int i = 0; i<2; i++){ 249 if(scoreArr[i] == "0") 250 draw0(1+i*4); 251 if(scoreArr[i] == "1") 252 draw1(1+i*4); 253 if(scoreArr[i] == "2") 254 draw2(1+i*4); 255 if(scoreArr[i] == "3") 256 draw3(1+i*4); 257 if(scoreArr[i] == "4") 258 draw4(1+i*4); 259 if(scoreArr[i] == "5") 260 draw5(1+i*4); 261 if(scoreArr[i] == "6") 262 draw6(1+i*4); 263 if(scoreArr[i] == "7") 264 draw7(1+i*4); 265 if(scoreArr[i] == "8") 266 draw8(1+i*4); 267 if(scoreArr[i] == "9") 268 draw9(1+i*4); 269 } 270 271 } 272 // if score is > 100, display only last two digits of score 273 else{ 274 275 for(int i = 1; i<3; i++){ 276 if(scoreArr[i] == "0") 277 draw0(1+(i-1)*4); 278 if(scoreArr[i] == "1") 279 draw1(1+(i-1)*4); 280 if(scoreArr[i] == "2") 281 draw2(1+(i-1)*4); 282 if(scoreArr[i] == "3") 283 draw3(1+(i-1)*4); 284 if(scoreArr[i] == "4") 285 draw4(1+(i-1)*4); 286 if(scoreArr[i] == "5") 287 draw5(1+(i-1)*4); 288 if(scoreArr[i] == "6") 289 draw6(1+(i-1)*4); 290 if(scoreArr[i] == "7") 291 draw7(1+(i-1)*4); 292 if(scoreArr[i] == "8") 293 draw8(1+(i-1)*4); 294 if(scoreArr[i] == "9") 295 draw9(1+(i-1)*4); 296 } 297 // first digit of score ("hundreds") is displayed as dots: One dot for every hundred. 298 for(int i = 1; i<10; i++){ 299 if(scoreArr[0] == String(i)) 300 hundreds = i; 301 } 302 for(int i = 1; i <= hundreds; i++){ 303 lc.setLed(0,0,i-1,true); 304 lc.setLed(0,1,i-1,true); 305 delay(200); 306 } 307 308 } 309 gameOver = true; 310 while(gameOver == true){ 311 // do nothing but wait for button press 312 } 313 } 314 315} 316 317///////////////////////////// Draw score digits ///////////////////////////////////////// 318void draw1(int position){ 319 320 lc.setColumn(0,0+position,B00001000); 321 lc.setColumn(0,1+position,B00011111); 322 323} 324 325void draw2(int position){ 326 327 lc.setColumn(0,0+position,B00010111); 328 lc.setColumn(0,1+position,B00010101); 329 lc.setColumn(0,2+position,B00011101); 330} 331void draw3(int position){ 332 333 lc.setColumn(0,0+position,B00010001); 334 lc.setColumn(0,1+position,B00010101); 335 lc.setColumn(0,2+position,B00011111); 336} 337void draw4(int position){ 338 339 lc.setColumn(0,0+position,B00011100); 340 lc.setColumn(0,1+position,B00000100); 341 lc.setColumn(0,2+position,B00011111); 342} 343void draw5(int position){ 344 345 lc.setColumn(0,0+position,B00011101); 346 lc.setColumn(0,1+position,B00010101); 347 lc.setColumn(0,2+position,B00010111); 348} 349void draw6(int position){ 350 351 lc.setColumn(0,0+position,B00011111); 352 lc.setColumn(0,1+position,B00010101); 353 lc.setColumn(0,2+position,B00010111); 354} 355void draw7(int position){ 356 357 lc.setColumn(0,0+position,B00010000); 358 lc.setColumn(0,1+position,B00010011); 359 lc.setColumn(0,2+position,B00011100); 360} 361void draw8(int position){ 362 363 lc.setColumn(0,0+position,B00011111); 364 lc.setColumn(0,1+position,B00010101); 365 lc.setColumn(0,2+position,B00011111); 366} 367void draw9(int position){ 368 369 lc.setColumn(0,0+position,B00011101); 370 lc.setColumn(0,1+position,B00010101); 371 lc.setColumn(0,2+position,B00011111); 372} 373 374void draw0(int position){ 375 376 lc.setColumn(0,0+position,B00011111); 377 lc.setColumn(0,1+position,B00010001); 378 lc.setColumn(0,2+position,B00011111); 379} 380 381
Downloadable files
circuit diagram for the asteroids game
circuit diagram for the asteroids game
circuit diagram for the asteroids game
circuit diagram for the asteroids game
Comments
Only logged in users can leave comments
kreck2003
0 Followers
•0 Projects
Table of contents
Intro
24
0