Juego de bloques luminosos controlados por Arduino y pantalla formada por matriz de 16×8 leds WS2812b, con sonido y manejado con un joystick y un pulsador. Con control de volumen e intensidad de la luz y batería recargable.
Material necesario
Alimentación
El sistema se alimenta con una batería de litio 18650 que administra 3.7V que sebe ser manejada por un módulo de carga y para ello emplearé el cómodo TP4056 con entrada de carga de USB mini.
Este elemento incorpora un led azul de carga completa que indica la finalización de este proceso. Además el uso del módulo permite el funcionamiento del sistema durante el periodo de carga y con el cable de alimentación conectado en el zócalo USB.
Para suministrar los 5 voltios necesarios para alimentar el microprocesador usaremos un conversor DC Step-Up Booster MT3608, pues los 3,7 voltios de la batería de litio son insuficientes.
En el módulo encontraremos un tornillo regulador que giraremos con un destornillador de precisión hasta alcanzar el valor adecuado. Suministra 2Amperios máximo.
Colocaremos un pequeño interruptor entre el módulo cargador de la batería y el Step-Up (parte positiva) para apagar el artefacto.
Módulo reproductor MP3
Será necesario para la música de fondo durante el juego y los sonidos de inicio y finalización del mismo.
Son 3 mp3 que deben grabarse en la tarjeta de memoria en el directorio raiz.
Este módulo se alimenta a 5V y lo controlaremos pro software mediante la librería RedMP3.h que incluimos en el zip de descarga.
Usaremos los pines Rx y Tx para controlarlo y se conectarán a D10 y D11 respectivamente que para actuar como puerto Serial deberemos usar también la librería SoftwareSerial.h del propio IDE de Arduino.
Para acabar se conectará el propio módulo al altavoz correspondiente que en este proyecto hemos empleado uno de pequeñas dimensiones pues no esta necesario una gran potencia de volumen.
El buzzer que colocaremos hará un sonido cada vez que se coloque una pieza del juego o se complete una línea. Este dispositivo se conectará al pin D12 y tierra.
Por otro lado usaremos un potenciómetro de rueda como el de la imagen para variar el nivel de volumen de la música y del buzzer. Así graduará la salida de audio del MP3 y eliminará los servicios del buzzer cuando el volumen esté al mínimo.
Los pies de conexión para este módulo serán 5V, tierra y A3, pues medirá su posición de 0 a 1023 por el pin analógico.
Pantalla
Para la pantalla usaremos dos matrices de 8×8 leds tipo WS2812b que conectaremos en serie y controlaremos con el pin D6 a través de la librería Adafruit_NeoPixel.h que se puede dercargar desde el IDE.
Las matrices de atornillarán a la parte superior de la caja mordiendo a la vez la pieza que actuará de pantalla difusora. Los leds encajan en la cuadrícula destinada para ello.
Por último colocar el joystick conectado al pin A1, A2 y A0 (SW), y el pulsador al D3.
Para controlar la intensidad de los leds se empleará el pulsador interno del joystick incrementando el nivel en cada pulsación.
Piezas STL
El sistema consta de una carcasa de 3 piezas y algunas otras internas de pequeño tamaño.
Alternativa 2
Se ha modificado el modelo de las piezas de la carcaza para que sean más ergonómicas y se ha simplificado el modelo que ahora consta de solo dos partes. Además permite poner el artefacto en posición vertical para exposición en caso de no usarlo.
También se ha añadido un puerto de programación USB hembra para poder programarlo sin necesidad de acceder físicamente al microcontrolador.
En futuras revisiones se añadirá un Bluetooth para manejarlo inalámbricamente desde el móvil para jugar en modo exposición.
Esquema eléctrico
Es posible añadirle un módulo de bluetooth para controlar desde el móvil. Esta actualización no aparece en el esquema siguiente.
Librerías
En el siguiente enlace de descarga puedes obtener las librerías necesarias para el código del juego. Recuerda añadir SoftwareSerial.h del IDE de Arduino.
Código
Comenzamos como siempre incluyendo las librerías que habrás descargado ya.
Primero definimos los pines UART del módulo MP3 que serán D10 y D11.
Seguidamente describimos los paneles luminosos y sus dimensiones que irán al pin D6. La variable definida como BACKWARDS determina el comportamiento de los leds en el espacio es decir, depende de la forma que hayas colocado las 2 pantallas matriciales. En mi caso será el 2, pero puede darse el caso de 0 y 1.
Debajo definimos el tamaño de los bloques (4×4). y el número de tipos de piezas distintos.
Definimos algunas variables más como calibración del joystick, velocidad del juego y algunos pines más de entrada.
Posteriormente definimos las formas de rotación de las piezas. Las hay de varias formas que se asemejan a las letras siguientes: I, L, J, T, S, Z, O.
Luego definimos el color de las piezas en numeración hexagesimal que puedes cambiar si te apetece con ayuda de esta web.
1 2 3 4 5 6 7 8 9 |
const long piece_colors[NUM_PIECE_TYPES] = { 0x009900, // green S 0xFF0000, // red Z 0xFFFFFF, // Blanco L 0x0000FF, // blue J 0xFFFF00, // yellow O 0xFF00FF, // purple T 0x00FFFF, // cyan I }; |
Añadimos un puñado más de variables globales y comenzamos con los métodos.
En este caso se ha colocado el SETUP en la parte final del código. Aquí tienes el código completo y un poco más abajo puedes descargarlo en *.ino.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 |
//------------------------------------------------------------------------------ // 8x16 single color LED Tetris for Arduino UNO and a joystick breakout. // dan@marginallycelver.com 2015-01-22 //------------------------------------------------------------------------------ // Copyright at end of file. // please see http://www.github.com/MarginallyClever/LED8x16tetris for more information. // // The LED grid is 128x WS2811 LEDs in an 'Z' pattern - left to right across the top row, // then right to left on the next. Repeat all the way down. // The Arduino is an UNO. // The joystick is a xinda ps3/xbox style clickable stick on a breakout board. // // The joystick has 5 pins. joystick 5v and GND go to the same pins on arduino. // VRx goes to A0. VRy goes to A1. SW goes to arduino digital pin 2. // // The LED data line goes to Arduino pin 6. The LED ground go to Arduino GND (pin 13?) // IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across // pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input // and minimize distance between Arduino and first pixel. Avoid connecting // on a live circuit...if you must, connect GND first. // see also http://tetris.wikia.com/wiki/ // The SRS and hard drop functions are in. There is no soft-drop or CCW rotation. //-------------------------------------------------------------------------------- // CONSTANTS //-------------------------------------------------------------------------------- #include <Adafruit_NeoPixel.h> #include <SoftwareSerial.h> #include "RedMP3.h" #define MP3_RX 10 //RX of Serial MP3 module connect to D7 of Arduino #define MP3_TX 11 //TX to D8, note that D8 can not be used as RX on Mega2560, you should modify this if you donot use Arduino UNO MP3 mp3(MP3_RX, MP3_TX); int volume = 2; //(30 adjustable level) int oldvolume = 2; // size of the LED grid #define GRID_W (8) #define GRID_H (16) #define STRAND_LENGTH (GRID_W * GRID_H) #define LED_DATA_PIN (6) // did you wire your grid in an 'S' instead of a 'Z'? change this value to 0. #define BACKWARDS (2) // max size of each tetris piece #define PIECE_W (4) #define PIECE_H (4) // how many kinds of pieces #define NUM_PIECE_TYPES (7) // joystick options #define JOYSTICK_DEAD_ZONE (30) #define JOYSTICK_PIN (2) // gravity options #define DROP_MINIMUM (25) // top speed gravity can reach #define DROP_ACCELERATION (20) // how fast gravity increases #define INITIAL_MOVE_DELAY (100) #define INITIAL_DROP_DELAY (500) #define INITIAL_DRAW_DELAY (30) int brillo = 7; #define ejex (2) #define ejey (1) #define pinpot (3) #define buzzpin (12) // 1 color drawings of each piece in each rotation. // Each piece is max 4 wide, 4 tall, and 4 rotations. const char piece_I[] = { 0,0,0,0, 1,1,1,1, 0,0,0,0, 0,0,0,0, 0,0,1,0, 0,0,1,0, 0,0,1,0, 0,0,1,0, 0,0,0,0, 0,0,0,0, 1,1,1,1, 0,0,0,0, 0,1,0,0, 0,1,0,0, 0,1,0,0, 0,1,0,0, }; const char piece_L[] = { 0,0,1,0, 1,1,1,0, 0,0,0,0, 0,0,0,0, 0,1,0,0, 0,1,0,0, 0,1,1,0, 0,0,0,0, 0,0,0,0, 1,1,1,0, 1,0,0,0, 0,0,0,0, 1,1,0,0, 0,1,0,0, 0,1,0,0, 0,0,0,0, }; const char piece_J[] = { 1,0,0,0, 1,1,1,0, 0,0,0,0, 0,0,0,0, 0,1,1,0, 0,1,0,0, 0,1,0,0, 0,0,0,0, 0,0,0,0, 1,1,1,0, 0,0,1,0, 0,0,0,0, 0,1,0,0, 0,1,0,0, 1,1,0,0, 0,0,0,0, }; const char piece_T[] = { 0,1,0,0, 1,1,1,0, 0,0,0,0, 0,0,0,0, 0,1,0,0, 0,1,1,0, 0,1,0,0, 0,0,0,0, 0,0,0,0, 1,1,1,0, 0,1,0,0, 0,0,0,0, 0,1,0,0, 1,1,0,0, 0,1,0,0, 0,0,0,0, }; const char piece_S[] = { 0,1,1,0, 1,1,0,0, 0,0,0,0, 0,0,0,0, 0,1,0,0, 0,1,1,0, 0,0,1,0, 0,0,0,0, 0,0,0,0, 0,1,1,0, 1,1,0,0, 0,0,0,0, 1,0,0,0, 1,1,0,0, 0,1,0,0, 0,0,0,0, }; const char piece_Z[] = { 1,1,0,0, 0,1,1,0, 0,0,0,0, 0,0,0,0, 0,0,1,0, 0,1,1,0, 0,1,0,0, 0,0,0,0, 0,0,0,0, 1,1,0,0, 0,1,1,0, 0,0,0,0, 0,1,0,0, 1,1,0,0, 1,0,0,0, 0,0,0,0, }; const char piece_O[] = { 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,0,0,0, 0,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,0,0,0, 0,0,0,0, }; const long piece_colors[NUM_PIECE_TYPES] = { 0x009900, // green S 0xFF0000, // red Z 0xFFFFFF, // Blanco L 0x0000FF, // blue J 0xFFFF00, // yellow O 0xFF00FF, // purple T 0x00FFFF, // cyan I }; //-------------------------------------------------------------------------------- // GLOBALS //-------------------------------------------------------------------------------- // Parameter 1 = number of pixels in strip // Parameter 2 = Arduino pin number (most are valid) // Parameter 3 = pixel type flags, add together as needed: // NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs) // NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers) // NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products) // NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2) Adafruit_NeoPixel strip = Adafruit_NeoPixel(STRAND_LENGTH, LED_DATA_PIN, NEO_RGB + NEO_KHZ800); // this is how arduino remembers what the button was doing in the past, // so arduino can tell when it changes. int old_button = 0; // so arduino can tell when user moves sideways int old_px = 0; // so arduino can tell when user tries to turn int old_i_want_to_turn = 0; // this is how arduino remembers the falling piece. int piece_id; int piece_rotation; int piece_x; int piece_y; // the bag from which new pieces are grabbed. // the bag is only refilled when all pieces are taken. // this guarantees a maximum of 12 moves between two Is // or four S & Z in a row. char piece_sequence[NUM_PIECE_TYPES]; char sequence_i = NUM_PIECE_TYPES; // this controls how fast the player can move. long last_move; long move_delay; // 100ms = 5 times a second // this controls when the piece automatically falls. long last_drop; long drop_delay; // 500ms = 2 times a second long last_draw; long draw_delay; // 60 fps // this is how arduino remembers where pieces are on the grid. long grid[GRID_W * GRID_H]; //-------------------------------------------------------------------------------- // METHODS //-------------------------------------------------------------------------------- // I want to turn on point P(x,y), which is X from the left and Y from the top. // I might also want to hold it on for us microseconds. void p(int x, int y, long color) { int a = (GRID_H - 1 - y) * GRID_W; if ((y % 2) == BACKWARDS) { // % is modulus. // y%2 is false when y is an even number - rows 0,2,4,6. a += x; } else { // y%2 is true when y is an odd number - rows 1,3,5,7. a += GRID_W - 1 - x; } a %= STRAND_LENGTH; strip.setPixelColor(a, color); } // grid contains the arduino's memory of the game board, including the piece that is falling. void draw_grid() { int x, y; for (y = 0; y < GRID_H; ++y) { for (x = 0; x < GRID_W; ++x) { if (grid[y * GRID_W + x] != 0) { p(x, y, grid[y * GRID_W + x]); } else { p(x, y, 0); } } } strip.show(); } // choose a new piece from the sequence. // the sequence is a random list that contains one of each piece. // that way you're guaranteed an even number of pieces over time, // tho the order is random. void choose_new_piece() { if (sequence_i >= NUM_PIECE_TYPES) { // list exhausted int i, j, k; for (i = 0; i < NUM_PIECE_TYPES; ++i) { do { // pick a random piece j = rand() % NUM_PIECE_TYPES; // make sure it isn't already in the sequence. for (k = 0; k < i; ++k) { if (piece_sequence[k] == j) break; // already in sequence } } while (k < i); // not in sequence. Add it. piece_sequence[i] = j; } // rewind sequence counter sequence_i = 0; } // get the next piece in the sequence. piece_id = piece_sequence[sequence_i++]; // always start the piece top center. piece_y = -4; // -4 squares off the top of the screen. piece_x = 3; // always start in the same orientation. piece_rotation = 0; } void erase_piece_from_grid() { int x, y; const char *piece = pieces[piece_id] + (piece_rotation * PIECE_H * PIECE_W); for (y = 0; y < PIECE_H; ++y) { for (x = 0; x < PIECE_W; ++x) { int nx = piece_x + x; int ny = piece_y + y; if (ny < 0 || ny > GRID_H) continue; if (nx < 0 || nx > GRID_W) continue; if (piece[y * PIECE_W + x] == 1) { grid[ny * GRID_W + nx] = 0; // zero erases the grid location. } } } } void add_piece_to_grid() { int x, y; const char *piece = pieces[piece_id] + (piece_rotation * PIECE_H * PIECE_W); for (y = 0; y < PIECE_H; ++y) { for (x = 0; x < PIECE_W; ++x) { int nx = piece_x + x; int ny = piece_y + y; if (ny < 0 || ny > GRID_H) continue; if (nx < 0 || nx > GRID_W) continue; if (piece[y * PIECE_W + x] == 1) { grid[ny * GRID_W + nx] = piece_colors[piece_id]; // zero erases the grid location. } } } } // Move everything down 1 space, destroying the old row number y in the process. void delete_row(int y) { int x; for (; y > 0; --y) { for (x = 0; x < GRID_W; ++x) { grid[y * GRID_W + x] = grid[(y - 1) * GRID_W + x]; } } // everything moved down 1, so the top row must be empty or the game would be over. for (x = 0; x < GRID_W; ++x) { grid[x] = 0; } } void fall_faster() { if (drop_delay > DROP_MINIMUM) drop_delay -= DROP_ACCELERATION; } void remove_full_rows() { int x, y, c; char row_removed = 0; for (y = 0; y < GRID_H; ++y) { // count the full spaces in this row c = 0; for (x = 0; x < GRID_W; ++x) { if (grid[y * GRID_W + x] > 0) c++; } if (c == GRID_W) { // row full! delete_row(y); fall_faster(); if (volume >= 1) { tone(buzzpin, 1000, 80); delay(100); tone(buzzpin, 2000, 30); delay(30); } } } } void try_to_move_piece_sideways() { // what does the joystick angle say int dx = map(analogRead(ejex), 0, 1023, -512, 512); int new_px = 0; // is the joystick really being pushed? if (dx > JOYSTICK_DEAD_ZONE) { new_px = -1; } if (dx < -JOYSTICK_DEAD_ZONE) { new_px = 1; } if (new_px != old_px && piece_can_fit(piece_x + new_px, piece_y, piece_rotation) == 1) { piece_x += new_px; } old_px = new_px; } void try_to_rotate_piece() { int i_want_to_turn = 0; // what does the joystick button say int new_button = digitalRead(JOYSTICK_PIN); // if the button state has just changed AND it is being let go, if (new_button > 0 && old_button != new_button) { i_want_to_turn = 1; } old_button = new_button; // up on joystick to rotate int dy = map(analogRead(ejey), 0, 1023, -512, 512); if (dy > JOYSTICK_DEAD_ZONE) i_want_to_turn = 1; if (i_want_to_turn == 1 && i_want_to_turn != old_i_want_to_turn) { // figure out what it will look like at that new angle int new_pr = (piece_rotation + 1) % 4; // if it can fit at that new angle (doesn't bump anything) if (piece_can_fit(piece_x, piece_y, new_pr)) { // then make the turn. piece_rotation = new_pr; } else { // wall kick if (piece_can_fit(piece_x - 1, piece_y, new_pr)) { piece_x = piece_x - 1; piece_rotation = new_pr; } else if (piece_can_fit(piece_x + 1, piece_y, new_pr)) { piece_x = piece_x + 1; piece_rotation = new_pr; } } } old_i_want_to_turn = i_want_to_turn; } // can the piece fit in this new location? int piece_can_fit(int px, int py, int pr) { if (piece_off_edge(px, py, pr)) return 0; if (piece_hits_rubble(px, py, pr)) return 0; return 1; } int piece_off_edge(int px, int py, int pr) { int x, y; const char *piece = pieces[piece_id] + (pr * PIECE_H * PIECE_W); for (y = 0; y < PIECE_H; ++y) { for (x = 0; x < PIECE_W; ++x) { int nx = px + x; int ny = py + y; if (ny < 0) continue; // off top, don't care if (piece[y * PIECE_W + x] > 0) { if (nx < 0) return 1; // yes: off left side if (nx >= GRID_W) return 1; // yes: off right side } } } return 0; // inside limits } int piece_hits_rubble(int px, int py, int pr) { int x, y; const char *piece = pieces[piece_id] + (pr * PIECE_H * PIECE_W); for (y = 0; y < PIECE_H; ++y) { int ny = py + y; if (ny < 0) continue; for (x = 0; x < PIECE_W; ++x) { int nx = px + x; if (piece[y * PIECE_W + x] > 0) { if (ny >= GRID_H) return 1; // yes: goes off bottom of grid if (grid[ny * GRID_W + nx] != 0) return 1; // yes: grid already full in this space } } } return 0; // doesn't hit } void game_over() { int x, y; mp3.playWithVolume(4, volume); long over_time = millis(); while (millis() - over_time < 3000) { // click the button? if (digitalRead(1) == 0) { Serial.println("a"); // restart! break; } } setup(); return; } void try_to_drop_piece() { erase_piece_from_grid(); if (piece_can_fit(piece_x, piece_y + 1, piece_rotation)) { piece_y++; // move piece down add_piece_to_grid(); } else { // hit something! // put it back add_piece_to_grid(); remove_full_rows(); if (game_is_over() == 1) { game_over(); } // game isn't over, choose a new piece choose_new_piece(); } } void try_to_drop_faster() { int y = map(analogRead(ejey), 0, 1023, -512, 512); if (y < -JOYSTICK_DEAD_ZONE) { // player is holding joystick down, drop a little faster. try_to_drop_piece(); } } void react_to_player() { erase_piece_from_grid(); try_to_move_piece_sideways(); try_to_rotate_piece(); add_piece_to_grid(); try_to_drop_faster(); } // can the piece fit in this new location int game_is_over() { int x, y; if (volume >= 1) { tone(buzzpin, 1000, 30); delay(30); } const char *piece = pieces[piece_id] + (piece_rotation * PIECE_H * PIECE_W); for (y = 0; y < PIECE_H; ++y) { for (x = 0; x < PIECE_W; ++x) { int ny = piece_y + y; int nx = piece_x + x; if (piece[y * PIECE_W + x] > 0) { if (ny < 0) return 1; // yes: off the top! } } } return 0; // not over yet... } // called once when arduino reboots void setup() { int i; Serial.begin(9600); Serial.println("Saludos, TETRIS Diciembre 2024"); // setup the LEDs strip.begin(); strip.show(); // Initialize all pixels to 'off' strip.setBrightness(brillo); // set up joystick button pinMode(JOYSTICK_PIN, INPUT); //digitalWrite(JOYSTICK_PIN, HIGH); pinMode(pinpot, INPUT); pinMode(buzzpin, OUTPUT); volume = map(analogRead(pinpot), 0, 1023, 0, 20); // make sure arduino knows the grid is empty. for (i = 0; i < GRID_W * GRID_H; ++i) { grid[i] = 0; } // make the game a bit more random - pull a number from space and use it to 'seed' a crop of random numbers. randomSeed(analogRead(1) + analogRead(2) + analogRead(5) + analogRead(6)); // get ready to start the game. choose_new_piece(); move_delay = INITIAL_MOVE_DELAY; drop_delay = INITIAL_DROP_DELAY; draw_delay = INITIAL_DRAW_DELAY; // Música mp3.setVolume(volume); delay(50); mp3.playWithVolume(2, volume); delay(1500); //mp3.playWithVolume(1,volume); mp3.cyclePlay(1); // start the game clock after everything else is ready. last_draw = last_drop = last_move = millis(); } // called over and over after setup() void loop() { long t = millis(); volume = map(analogRead(pinpot), 0, 1023, 0, 20); if (volume != oldvolume) { mp3.setVolume(volume); oldvolume = volume; } // the game plays at one speed, if (t - last_move > move_delay) { last_move = t; react_to_player(); } // ...and drops the falling block at a different speed. if (t - last_drop > drop_delay) { last_drop = t; try_to_drop_piece(); } // when it isn't doing those two things, it's redrawing the grid. if (t - last_draw > draw_delay) { last_draw = t; draw_grid(); } } /** * This file is part of LED8x16tetris. * * LED8x16tetris is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * LED8x16tetris is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with LED8x16tetris. If not, see <http://www.gnu.org/licenses/>. */ |
Basado en el trabajo de GitHub – MarginallyClever/LED8x16tetris: an 8×16 WS2811 LED tetris game
Feliz Navidad