Components and supplies
AA Batteries
Male/Female Jumper Wires
Rotary potentiometer (generic)
Arduino UNO
4xAA battery holder
MaxMatrix2719 8x8
Tools and machines
Computer
Apps and platforms
Arduino IDE
Project description
Code
Pong
c_cpp
You will need to download the Timer and LEDControl Libraries
1#include "LedControl.h" 2#include "Timer.h" 3 4#define POTPIN 5 A5 // Potentiometer 6#define PADSIZE 3 7#define BALL_DELAY 200 8#define GAME_DELAY 9 10 10#define BOUNCE_VERTICAL 1 11#define BOUNCE_HORIZONTAL -1 12#define NEW_GAME_ANIMATION_SPEED 13 50 14#define HIT_NONE 0 15#define HIT_CENTER 1 16#define HIT_LEFT 2 17#define 18 HIT_RIGHT 3 19 20//#define DEBUG 1 21 22byte sad[] = { 23B00000000, 24B01000100, 25B00010000, 26B00010000, 27B00000000, 28B00111000, 29B01000100, 30B00000000 31}; 32 33 34byte smile[] = { 35B00000000, 36B01000100, 37B00010000, 38B00010000, 39B00010000, 40B01000100, 41B00111000, 42B00000000 43}; 44 45 46Timer timer; 47 48LedControl lc = LedControl(12,11,10,1); 49 50byte direction; 51 // Wind rose, 0 is north 52int xball; 53int yball; 54int yball_prev; 55byte 56 xpad; 57int ball_timer; 58 59void setSprite(byte *sprite){ 60 for(int r 61 = 0; r < 8; r++){ 62 lc.setRow(0, r, sprite[r]); 63 } 64} 65 66void 67 newGame() { 68 lc.clearDisplay(0); 69 // initial position 70 xball = 71 random(1, 7); 72 yball = 1; 73 direction = random(3, 6); // Go south 74 75 for(int r = 0; r < 8; r++){ 76 for(int c = 0; c < 8; c++){ 77 lc.setLed(0, 78 r, c, HIGH); 79 delay(NEW_GAME_ANIMATION_SPEED); 80 } 81 } 82 83 setSprite(smile); 84 delay(1500); 85 lc.clearDisplay(0); 86} 87 88void 89 setPad() { 90 xpad = map(analogRead(POTPIN), 0, 1020, 8 - PADSIZE, 0); 91} 92 93 94void debug(const char* desc){ 95#ifdef DEBUG 96 Serial.print(desc); 97 98 Serial.print(" XY: "); 99 Serial.print(xball); 100 Serial.print(", 101 "); 102 Serial.print(yball); 103 Serial.print(" XPAD: "); 104 Serial.print(xpad); 105 106 Serial.print(" DIR: "); 107 Serial.println(direction); 108#endif 109} 110 111 112int checkBounce() { 113 if(!xball || !yball || xball == 7 || yball == 6){ 114 115 int bounce = (yball == 0 || yball == 6) ? BOUNCE_HORIZONTAL : BOUNCE_VERTICAL; 116#ifdef 117 DEBUG 118 debug(bounce == BOUNCE_HORIZONTAL ? "HORIZONTAL" : "VERTICAL"); 119#endif 120 121 return bounce; 122 } 123 return 0; 124} 125 126int getHit() { 127 128 if(yball != 6 || xball < xpad || xball > xpad + PADSIZE){ 129 return 130 HIT_NONE; 131 } 132 if(xball == xpad + PADSIZE / 2){ 133 return HIT_CENTER; 134 135 } 136 return xball < xpad + PADSIZE / 2 ? HIT_LEFT : HIT_RIGHT; 137} 138 139 140bool checkLoose() { 141 return yball == 6 && getHit() == HIT_NONE; 142} 143 144 145void moveBall() { 146 debug("MOVE"); 147 int bounce = checkBounce(); 148 149 if(bounce) { 150 switch(direction){ 151 case 0: 152 direction 153 = 4; 154 break; 155 case 1: 156 direction = 157 (bounce == BOUNCE_VERTICAL) ? 7 : 3; 158 break; 159 case 2: 160 161 direction = 6; 162 break; 163 case 6: 164 165 direction = 2; 166 break; 167 case 7: 168 169 direction = (bounce == BOUNCE_VERTICAL) ? 1 : 5; 170 break; 171 172 case 5: 173 direction = (bounce == BOUNCE_VERTICAL) ? 174 3 : 7; 175 break; 176 case 3: 177 direction 178 = (bounce == BOUNCE_VERTICAL) ? 5 : 1; 179 break; 180 case 181 4: 182 direction = 0; 183 break; 184 } 185 debug("->"); 186 187 } 188 189 // Check hit: modify direction is left or right 190 switch(getHit()){ 191 192 case HIT_LEFT: 193 if(direction == 0){ 194 direction 195 = 7; 196 } else if (direction == 1){ 197 direction = 198 0; 199 } 200 break; 201 case HIT_RIGHT: 202 if(direction 203 == 0){ 204 direction = 1; 205 } else if(direction == 7){ 206 207 direction = 0; 208 } 209 break; 210 } 211 212 213 // Check orthogonal directions and borders ... 214 if((direction == 0 && 215 xball == 0) || (direction == 4 && xball == 7)){ 216 direction++; 217 } 218 219 if(direction == 0 && xball == 7){ 220 direction = 7; 221 } 222 if(direction 223 == 4 && xball == 0){ 224 direction = 3; 225 } 226 if(direction == 2 227 && yball == 0){ 228 direction = 3; 229 } 230 if(direction == 2 && yball 231 == 6){ 232 direction = 1; 233 } 234 if(direction == 6 && yball == 0){ 235 236 direction = 5; 237 } 238 if(direction == 6 && yball == 6){ 239 direction 240 = 7; 241 } 242 243 // "Corner" case 244 if(xball == 0 && yball == 245 0){ 246 direction = 3; 247 } 248 if(xball == 0 && yball == 6){ 249 250 direction = 1; 251 } 252 if(xball == 7 && yball == 6){ 253 direction 254 = 7; 255 } 256 if(xball == 7 && yball == 0){ 257 direction = 5; 258 259 } 260 261 yball_prev = yball; 262 if(2 < direction && direction < 6) 263 { 264 yball++; 265 } else if(direction != 6 && direction != 2) { 266 yball--; 267 268 } 269 if(0 < direction && direction < 4) { 270 xball++; 271 } else 272 if(direction != 0 && direction != 4) { 273 xball--; 274 } 275 xball 276 = max(0, min(7, xball)); 277 yball = max(0, min(6, yball)); 278 debug("AFTER 279 MOVE"); 280} 281 282void gameOver() { 283 setSprite(sad); 284 delay(1500); 285 286 lc.clearDisplay(0); 287} 288 289void drawGame() { 290 if(yball_prev != yball){ 291 292 lc.setRow(0, yball_prev, 0); 293 } 294 lc.setRow(0, yball, byte(1 295 << (xball))); 296 byte padmap = byte(0xFF >> (8 - PADSIZE) << xpad) ; 297#ifdef 298 DEBUG 299 //Serial.println(padmap, BIN); 300#endif 301 lc.setRow(0, 7, padmap); 302} 303 304 305void setup() { 306 // The MAX72XX is in power-saving mode on startup, 307 // 308 we have to do a wakeup call 309 pinMode(POTPIN, INPUT); 310 311 lc.shutdown(0,false); 312 313 // Set the brightness to a medium values 314 lc.setIntensity(0, 8); 315 // and 316 clear the display 317 lc.clearDisplay(0); 318 randomSeed(analogRead(0)); 319#ifdef 320 DEBUG 321 Serial.begin(9600); 322 Serial.println("Pong"); 323#endif 324 newGame(); 325 326 ball_timer = timer.every(BALL_DELAY, moveBall); 327} 328 329void loop() { 330 331 timer.update(); 332 // Move pad 333 setPad(); 334#ifdef DEBUG 335 Serial.println(xpad); 336#endif 337 338 // Update screen 339 drawGame(); 340 if(checkLoose()) { 341 debug("LOOSE"); 342 343 gameOver(); 344 newGame(); 345 } 346 delay(GAME_DELAY); 347}
Downloadable files
Pong
All done!
Pong
Pong
All done!
Pong
Comments
Only logged in users can leave comments
wbowl4
0 Followers
•0 Projects
Table of contents
Intro
1
0