Untitled

 avatar
unknown
plain_text
23 days ago
3.1 kB
3
Indexable
// Variables globales
let tiles = [];           // Arreglo para almacenar los bloques que caen
let score = 0;            // Puntuación del jugador
let spawnTimer = 30;      // Cuadros hasta que aparezca el próximo bloque (0.5s a 60fps)
let speed = 2;            // Velocidad de los bloques en píxeles por cuadro
let tileWidth = 100;      // Ancho del bloque (coincide con el ancho de la columna)
let tileHeight = 50;      // Altura del bloque
let hitLine = 550;        // Posición Y de la línea de impacto
let gameOver = false;     // Estado del juego

function setup() {
  createCanvas(400, 600); // Configura el lienzo: 400 de ancho, 600 de alto
}

function draw() {
  if (gameOver) {
    background(0);          // Fondo negro
    fill(255);              // Texto blanco
    textSize(32);           // Texto más grande
    text("Fin del Juego", 100, 300);  // Mostrar "Fin del Juego"
    text("Puntuación: " + score, 100, 350); // Mostrar puntuación final
    return;
  }

  // Juego activo
  background(220);        // Fondo gris claro
  // Dibujar la línea de impacto
  stroke(255, 0, 0);      // Color rojo
  line(0, hitLine, width, hitLine); // Línea horizontal en y=550
  stroke(0);              // Restablecer trazo a negro

  // Actualizar y dibujar bloques
  for (let i = tiles.length - 1; i >= 0; i--) {
    let tile = tiles[i];
    tile.y += speed;      // Mover el bloque hacia abajo
    fill(0);              // Relleno negro para el bloque
    rect(tile.column * tileWidth, tile.y, tileWidth, tileHeight); // Dibujar bloque

    // Verificar si el bloque pasa la línea de impacto
    if (tile.y >= hitLine) {
      tiles.splice(i, 1); // Eliminar bloque
      gameOver = true;    // Terminar el juego al fallar
    }
  }

  // Generar nuevos bloques
  spawnTimer--;
  if (spawnTimer <= 0) {
    let column = floor(random(4)); // Columna aleatoria (0-3)
    tiles.push({ column: column, y: 0 }); // Nuevo bloque en la parte superior
    spawnTimer = 30;      // Reiniciar temporizador
  }

  // Mostrar puntuación
  fill(0);                // Texto negro
  textSize(20);           // Tamaño del texto
  text("Puntuación: " + score, 10, 30); // Mostrar puntuación
}

function keyPressed() {
  if (gameOver) return;   // Ignorar entrada si el juego terminó

  // Mapear teclas a columnas
  let column;
  if (key === 'A' || key === 'a') column = 0;
  else if (key === 'S' || key === 's') column = 1;
  else if (key === 'D' || key === 'd') column = 2;
  else if (key === 'F' || key === 'f') column = 3;
  else return;            // Ignorar otras teclas

  // Verificar si hay un bloque que se pueda golpear
  for (let i = 0; i < tiles.length; i--) {
    let tile = tiles[i];
    // Condición modificada: se puede golpear si está por encima de la línea
    if (tile.column === column && tile.y < hitLine) {
      tiles.splice(i, 1); // Eliminar bloque
      score++;            // Incrementar puntuación
      return;             // Salir tras el golpe
    }
  }
  console.log("Presión fallida"); // Retroalimentación opcional
}
Editor is loading...
Leave a Comment