Components and supplies
Rotary potentiometer (generic)
8x8 led matrix
Arduino 101
Buzzer
Apps and platforms
Arduino IDE
Project description
Code
Untitled file
c_cpp
Copy and paste this code to do this project
1int row[] = {2, 7, A5, 5, 13, A4, 12, A2}; 2int col[] = {6, 11, 10, 3 3, A3, 4, 8, 9}; 4int joystick = A0; //es un simple potenciometro 5int lastUpdate; 6int 7 puntos = 0; 8int vidas = 10; 9int speakerPin = A1; 10int speed = 16; 11bool 12 updatedAsteroid = false, updatedPlayer = true; 13 14typedef struct coordenada 15 { 16 int x; //cordenada de la columna 17 int y; //cordenada de la fila 18}COORD; 19 20COORD 21 asteroid, jugador; 22 23void setup() { 24 // put your setup code here, to run 25 once: 26 for (int i = 0; i < 8; i++) 27 { 28 pinMode(row[i], OUTPUT); 29 pinMode(col[i], 30 OUTPUT); 31 digitalWrite(col[i], HIGH); 32 digitalWrite(row[i], LOW); 33 } 34 35 pinMode(speakerPin, OUTPUT); 36 initGame(); 37 Serial.begin(9600); 38} 39 40void 41 loop() { 42 // put your main code here, to run repeatedly: 43 for(int i = 0; 44 i < 2; i++) 45 { 46 clearScreen(); //limpia la matriz de leds 47 if(lastUpdate 48 == 1) { 49 movePlayer(); 50 } 51 if(lastUpdate == 0) { 52 asteroidFall(); 53 54 //Serial.print("El asteroide cambia de posicion!!\ 55"); 56 } 57 lastUpdate 58 = Update(); //actualiza la matriz de leds segun movePlayer() y asteroidFall() 59 60 delay(speed); 61 } 62 puntuacion(); //calcula la puntuacion y aumenta una 63 vida por punto, cuando el jugador se queda sin vidas se pasa a gameover() para reiniciar 64 la partida 65 if (asteroid.y == 8 || asteroid.y > 8) { 66 initasteroid(); 67 //inizializa un nuevo asteroid cuando el anterior ya ha caido 68 } 69} 70 71void 72 initGame() { 73 vidas = 10; 74 puntos = 0; 75 speed = 16; 76 initplayer(); 77 78 initasteroid(); 79 return; 80} 81 82void initplayer() { 83 jugador.x = 84 3; 85 jugador.y = 7; 86 digitalWrite(col[jugador.y], HIGH); 87 digitalWrite(row[jugador.x], 88 LOW); 89 return; 90} 91 92void initasteroid() { 93 asteroid.x = random(0, 94 7); 95 asteroid.y = -1; //para que asteroidFall() comienze en 0 nada mas ejecutarse 96 digitalWrite(col[asteroid.x], 97 LOW); 98 digitalWrite(row[asteroid.y], HIGH); 99 return; 100} 101 102void movePlayer() 103 { 104 int value = analogRead(joystick); 105 value = map(value, 0, 1023, 0, 7); 106 value 107 = constrain(value, 0, 7); 108 //Serial.print("joystickX: "); 109 //Serial.print(jugador.x); 110 111 //Serial.print("\ 112"); 113 if(value >= 0 && value <= 7) { 114 jugador.x 115 = value; 116 } 117 return; 118} 119 120void asteroidFall() { 121 asteroid.y++; 122 123 //Serial.print("AsteroidY: "); 124 //Serial.print(asteroid.y); 125 //Serial.print("\ 126"); 127 return; 128} 129 130void 131 clearScreen() { 132 for (int led = 0; led < 8; led++) 133 { 134 digitalWrite(col[led], 135 HIGH); 136 digitalWrite(row[led], LOW); 137 } 138 return; 139} 140 141int Update() 142 { 143 if(updatedAsteroid == true && updatedPlayer == false) { 144 digitalWrite(col[jugador.x], 145 LOW); 146 digitalWrite(row[jugador.y], HIGH); 147 updatedAsteroid = false; 148 149 updatedPlayer = true; 150 //Serial.print("Jugador Actualizado!!\ 151"); 152 153 return 0; //retorna que se ha actualizado el jugador 154 } else { 155 digitalWrite(col[asteroid.x], 156 LOW); 157 digitalWrite(row[asteroid.y], HIGH); 158 updatedAsteroid = true; 159 160 updatedPlayer = false; 161 //Serial.print("Asteroid Actualizado!!\ 162"); 163 164 return 1; //retorna que se ha actualizado el asteroide 165 } 166} 167 168void 169 puntuacion() { 170 if(asteroid.y == jugador.y && asteroid.x == jugador.x) { 171 172 digitalWrite(speakerPin, HIGH); 173 puntos++; 174 delay(100); 175 176 digitalWrite(speakerPin, LOW); 177 vidas += 2; 178 if(speed > 9 179 && puntos >= 4) { 180 speed--; //aumenta la velocidad del juego (nivel nuevo) 181 182 puntos = 0; //reiniciamos la puntucion al pasar al siguiente nivel 183 } 184 185 Serial.print("Puntos: "); 186 Serial.print(puntos); 187 Serial.print("\ 188"); 189 190 } 191 if(asteroid.y == jugador.y && asteroid.x != jugador.x) { 192 vidas--; 193 194 Serial.print("Vidas: "); 195 Serial.print(vidas); 196 Serial.print("\ 197"); 198 199 if(vidas == 0) { 200 gameover(); 201 } 202 } 203 return; 204} 205 206void 207 gameover() { 208 //animacion cuando pierdes la partida 209 for(int y = 0; y < 210 5; y++) 211 { 212 //Serial.print("Animacion: "); 213 //Serial.print(y); 214 215 //Serial.print("\ 216"); 217 //bucle que apaga todos los leds de la matriz 218 219 digitalWrite(speakerPin, HIGH); 220 for(int i = 0; i < 8; i++) 221 { 222 223 digitalWrite(col[i], HIGH); 224 digitalWrite(row[i], LOW); 225 } 226 227 delay(500); 228 //bucle que enciende todos los leds de la matriz 229 for(int 230 i = 0; i < 8; i++) 231 { 232 digitalWrite(col[i], LOW); 233 digitalWrite(row[i], 234 HIGH); 235 } 236 digitalWrite(speakerPin, LOW); 237 delay(500); 238 } 239 240 delay(1000); 241 initGame(); //inicializa una nueva partida 242 return; 243} 244
Untitled file
c_cpp
Copy and paste this code to do this project
1int row[] = {2, 7, A5, 5, 13, A4, 12, A2}; 2int col[] = {6, 11, 10, 3, A3, 4, 8, 9}; 3int joystick = A0; //es un simple potenciometro 4int lastUpdate; 5int puntos = 0; 6int vidas = 10; 7int speakerPin = A1; 8int speed = 16; 9bool updatedAsteroid = false, updatedPlayer = true; 10 11typedef struct coordenada { 12 int x; //cordenada de la columna 13 int y; //cordenada de la fila 14}COORD; 15 16COORD asteroid, jugador; 17 18void setup() { 19 // put your setup code here, to run once: 20 for (int i = 0; i < 8; i++) 21 { 22 pinMode(row[i], OUTPUT); 23 pinMode(col[i], OUTPUT); 24 digitalWrite(col[i], HIGH); 25 digitalWrite(row[i], LOW); 26 } 27 pinMode(speakerPin, OUTPUT); 28 initGame(); 29 Serial.begin(9600); 30} 31 32void loop() { 33 // put your main code here, to run repeatedly: 34 for(int i = 0; i < 2; i++) 35 { 36 clearScreen(); //limpia la matriz de leds 37 if(lastUpdate == 1) { 38 movePlayer(); 39 } 40 if(lastUpdate == 0) { 41 asteroidFall(); 42 //Serial.print("El asteroide cambia de posicion!!\ 43"); 44 } 45 lastUpdate = Update(); //actualiza la matriz de leds segun movePlayer() y asteroidFall() 46 delay(speed); 47 } 48 puntuacion(); //calcula la puntuacion y aumenta una vida por punto, cuando el jugador se queda sin vidas se pasa a gameover() para reiniciar la partida 49 if (asteroid.y == 8 || asteroid.y > 8) { 50 initasteroid(); //inizializa un nuevo asteroid cuando el anterior ya ha caido 51 } 52} 53 54void initGame() { 55 vidas = 10; 56 puntos = 0; 57 speed = 16; 58 initplayer(); 59 initasteroid(); 60 return; 61} 62 63void initplayer() { 64 jugador.x = 3; 65 jugador.y = 7; 66 digitalWrite(col[jugador.y], HIGH); 67 digitalWrite(row[jugador.x], LOW); 68 return; 69} 70 71void initasteroid() { 72 asteroid.x = random(0, 7); 73 asteroid.y = -1; //para que asteroidFall() comienze en 0 nada mas ejecutarse 74 digitalWrite(col[asteroid.x], LOW); 75 digitalWrite(row[asteroid.y], HIGH); 76 return; 77} 78 79void movePlayer() { 80 int value = analogRead(joystick); 81 value = map(value, 0, 1023, 0, 7); 82 value = constrain(value, 0, 7); 83 //Serial.print("joystickX: "); 84 //Serial.print(jugador.x); 85 //Serial.print("\ 86"); 87 if(value >= 0 && value <= 7) { 88 jugador.x = value; 89 } 90 return; 91} 92 93void asteroidFall() { 94 asteroid.y++; 95 //Serial.print("AsteroidY: "); 96 //Serial.print(asteroid.y); 97 //Serial.print("\ 98"); 99 return; 100} 101 102void clearScreen() { 103 for (int led = 0; led < 8; led++) 104 { 105 digitalWrite(col[led], HIGH); 106 digitalWrite(row[led], LOW); 107 } 108 return; 109} 110 111int Update() { 112 if(updatedAsteroid == true && updatedPlayer == false) { 113 digitalWrite(col[jugador.x], LOW); 114 digitalWrite(row[jugador.y], HIGH); 115 updatedAsteroid = false; 116 updatedPlayer = true; 117 //Serial.print("Jugador Actualizado!!\ 118"); 119 return 0; //retorna que se ha actualizado el jugador 120 } else { 121 digitalWrite(col[asteroid.x], LOW); 122 digitalWrite(row[asteroid.y], HIGH); 123 updatedAsteroid = true; 124 updatedPlayer = false; 125 //Serial.print("Asteroid Actualizado!!\ 126"); 127 return 1; //retorna que se ha actualizado el asteroide 128 } 129} 130 131void puntuacion() { 132 if(asteroid.y == jugador.y && asteroid.x == jugador.x) { 133 digitalWrite(speakerPin, HIGH); 134 puntos++; 135 delay(100); 136 digitalWrite(speakerPin, LOW); 137 vidas += 2; 138 if(speed > 9 && puntos >= 4) { 139 speed--; //aumenta la velocidad del juego (nivel nuevo) 140 puntos = 0; //reiniciamos la puntucion al pasar al siguiente nivel 141 } 142 Serial.print("Puntos: "); 143 Serial.print(puntos); 144 Serial.print("\ 145"); 146 } 147 if(asteroid.y == jugador.y && asteroid.x != jugador.x) { 148 vidas--; 149 Serial.print("Vidas: "); 150 Serial.print(vidas); 151 Serial.print("\ 152"); 153 if(vidas == 0) { 154 gameover(); 155 } 156 } 157 return; 158} 159 160void gameover() { 161 //animacion cuando pierdes la partida 162 for(int y = 0; y < 5; y++) 163 { 164 //Serial.print("Animacion: "); 165 //Serial.print(y); 166 //Serial.print("\ 167"); 168 //bucle que apaga todos los leds de la matriz 169 digitalWrite(speakerPin, HIGH); 170 for(int i = 0; i < 8; i++) 171 { 172 digitalWrite(col[i], HIGH); 173 digitalWrite(row[i], LOW); 174 } 175 delay(500); 176 //bucle que enciende todos los leds de la matriz 177 for(int i = 0; i < 8; i++) 178 { 179 digitalWrite(col[i], LOW); 180 digitalWrite(row[i], HIGH); 181 } 182 digitalWrite(speakerPin, LOW); 183 delay(500); 184 } 185 delay(1000); 186 initGame(); //inicializa una nueva partida 187 return; 188} 189
Downloadable files
Arduino CTB Scheme
Arduino CTB Scheme
Comments
Only logged in users can leave comments
MagonxESP
0 Followers
•0 Projects
Table of contents
Intro
1
0