JIT
Tetris Game HTML CSS Javascript
create a complete tetris game using html javascript and css in one file

Okay, here is a complete Tetris game in a single HTML file.

Save this code as an HTML file (e.g., tetris.html) and open it in your browser to play. Use the arrow keys to move and rotate the pieces, and 'P' to pause/unpause.

Updated files
v1
add automatic movement feature

The game already includes automatic downward movement for the Tetris pieces. The gameLoop function continuously checks the time elapsed since the last drop (delta = now - dropStart). When this time exceeds the dropInterval (which gets shorter as the level increases), the dropPiece() function is called, moving the piece down one step automatically.

Here's the complete code again, with a comment added in the gameLoop to highlight this:

The automatic downward movement is fundamental to Tetris and is already implemented. The speed increases automatically as you clear lines and level up.

Updated files
v2
pieces don't downward automatically

You are right! My apologies. There was an issue in how the game loop and the pause functionality interacted, potentially stopping the requestAnimationFrame loop unintentionally.

I've reviewed and corrected the gameLoop, togglePause, resetGame, and dropPiece functions to ensure the animation loop continues reliably when the game is not paused or over.

Here is the updated code with the fix for the automatic downward movement:

The key changes involve:

  1. Centralized Loop Control: Using startGameLoop() and stopGameLoop() functions to manage requestAnimationFrame.
  2. gameLoop Refinement: Ensures drawing happens correctly and the next frame is requested only if !gameOver.
  3. togglePause Correction: Explicitly stops the loop on pause and restarts it (via startGameLoop) on unpause.
  4. resetGame Correction: Ensures any old loop is stopped before starting the new one.
  5. dropPiece and showGameOver: Ensure that when game over occurs, the loop is definitively stopped via cancelAnimationFrame.

This should restore the automatic downward movement.

Updated files
v3
doesn't work, sorry

You're absolutely right to push back! My apologies again. It seems the interaction between the game loop, pausing, and the timing mechanism was still flawed.

Let's try a more robust approach using the timestamp provided by requestAnimationFrame for calculating the drop interval and simplifying the pause/resume logic.

Here is the corrected code. I've focused on ensuring the requestAnimationFrame loop continues reliably and that the timing for the automatic drop (elapsedSinceDrop > dropInterval) is calculated correctly within the loop.

Key changes in this version:

  1. Loop Structure: requestAnimationFrame is called at the start of the gameLoop (if not game over). This ensures the loop continues unless explicitly stopped.
  2. Pause Handling: When paused, the loop continues running via requestAnimationFrame but returns early, skipping game logic and drawing only the pause message. When unpaused, it simply stops returning early, and the existing loop picks up the game logic. Timers (dropStart, lastFrameTime) are reset upon unpausing.
  3. Timing: Uses the timestamp from requestAnimationFrame consistently for calculating elapsedSinceDrop and resetting dropStart. performance.now() is used in resetGame and togglePause to get accurate current times when initializing/resuming.
  4. dropPieceAndCheckState: A helper function now handles the drop logic, including locking, clearing, getting the next piece, checking for game over, and resetting dropStart. This cleans up the main loop.
  5. State Checks: Added more checks for gameOver and currentPiece existence before attempting actions like drawing or resetting timers, aiming for more robustness.

This structure should hopefully provide the reliable automatic downward movement. Please try this version.

Updated files
v4
ok, it works now. can i have the grid bigger?

Okay, let's make the grid taller. We'll increase the number of rows from 20 to 22 while keeping the columns at 10. This requires updating the ROWS constant in the JavaScript and the height attribute of the main game canvas in the HTML.

Now the game board is 10 blocks wide and 22 blocks tall.

Updated files
v5
i want 30 rows and 30 columns

Okay, changing the grid to 30x30 will make the blocks quite small if we keep the overall canvas size reasonable. Let's adjust the canvas dimensions and block size accordingly.

We'll set the canvas width to 300px. With 30 columns, this makes each block 10px wide. The height will also be 300px for 30 rows. The "Next Piece" preview will also be scaled down.

The game board is now 30x30 blocks, displayed within a 300x300 pixel canvas. Be aware that playing Tetris on such a wide board significantly changes the strategy compared to the standard 10-column width.

Updated files
v6