Skip to content

Add local two-player mode with garbage attacks#77

Open
shayanmohd wants to merge 1 commit into
jakesgordon:masterfrom
shayanmohd:feature/two-player-mode
Open

Add local two-player mode with garbage attacks#77
shayanmohd wants to merge 1 commit into
jakesgordon:masterfrom
shayanmohd:feature/two-player-mode

Conversation

@shayanmohd

@shayanmohd shayanmohd commented Jul 4, 2026

Copy link
Copy Markdown

Add local two-player mode with garbage attacks

Reworks the single-player game into a local two-player match on one shared canvas.

Two boards, two players

Player 1 plays on WASD, Player 2 on the arrow keys. Each board has its own state, next-piece preview, score, cleared-row count, and per-player 7-bag piece randomizer.

Garbage attacks

Clearing two or more lines in a single drop sends garbage rows to the opponent (capped at three per drop), queued immediately and inserted with a single random gap. Pending garbage is applied before the player's next piece and can cause a top-out. An "incoming" counter shows each player's queued garbage.

Win condition

The first player to top out loses; the other wins.

Notes

  • The change is contained to index.html. The inline script parses cleanly (node --check); as it's a browser game, a quick manual play-test is worth doing before merge.
  • This turns the project into a competitive variant, so if you'd rather keep the original single-player demo as-is, I'm happy to instead land this as a separate 2player.html / example. Just say the word.

Rework the single-player game into a local two-player match rendered on
one shared canvas:

- Two independent boards, Player 1 on WASD and Player 2 on the arrow
  keys, each with its own state, next-piece preview, score, cleared-row
  count, and per-player 7-bag piece randomizer.

- Garbage attack: clearing two or more lines in a single drop sends
  garbage rows to the opponent (capped at three per drop), queued
  immediately and inserted with a single random gap. Pending garbage is
  applied before the player's next piece and can cause a top-out.

- The first player to top out loses and the other wins.
Copilot AI review requested due to automatic review settings July 4, 2026 20:44

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR converts the existing single-player Tetris implementation into a local two-player versus mode rendered on a shared canvas, adding per-player state and a garbage-attack mechanic.

Changes:

  • Reworks UI/layout to show two player side panels (next preview, score, rows, incoming) around one shared canvas.
  • Refactors game state and loop into per-player objects (separate bags, boards, input queues, scoring/rows).
  • Adds queued garbage insertion and a win condition based on top-out.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread index.html
function randomPiece(p) {
if (p.pieces.length == 0)
p.pieces = [i,i,i,i,j,j,j,j,l,l,l,l,o,o,o,o,s,s,s,s,t,t,t,t,z,z,z,z];
var type = p.pieces.splice(random(0, p.pieces.length-1), 1)[0];
Comment thread index.html
Comment on lines +472 to +474
function garbageForLines(n) {
return Math.max(0, n - 2);
}
Comment thread index.html
Comment on lines +464 to +467
// lines cleared garbage sent
// 2 0
// 3 1
// 4 (tetris) 2
Comment thread index.html
function removeLines() {
function removeLines(p) {
var x, y, complete, n = 0;
for(y = ny ; y > 0 ; --y) {
Comment thread index.html
Comment on lines +56 to +57
<p id="start"><a href="javascript:play();">Press Space to Play.</a></p>
<p id="restart" style="visibility:hidden;"><a href="javascript:play();">Restart</a></p>
Comment thread index.html
setVisualScore(p2);
invalidateAll(p1);
invalidateAll(p2);
html('start', (msg ? msg + ' ' : '') + '<a href="javascript:play();">Press Space to play again.</a>');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants