Add local two-player mode with garbage attacks#77
Open
shayanmohd wants to merge 1 commit into
Open
Conversation
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.
There was a problem hiding this comment.
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.
| 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 on lines
+472
to
+474
| function garbageForLines(n) { | ||
| return Math.max(0, n - 2); | ||
| } |
Comment on lines
+464
to
+467
| // lines cleared garbage sent | ||
| // 2 0 | ||
| // 3 1 | ||
| // 4 (tetris) 2 |
| function removeLines() { | ||
| function removeLines(p) { | ||
| var x, y, complete, n = 0; | ||
| for(y = ny ; y > 0 ; --y) { |
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> |
| setVisualScore(p2); | ||
| invalidateAll(p1); | ||
| invalidateAll(p2); | ||
| html('start', (msg ? msg + ' ' : '') + '<a href="javascript:play();">Press Space to play again.</a>'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
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.2player.html/ example. Just say the word.