@@ -3,6 +3,7 @@ import { ScoreManager } from "./score.js";
33const canvas = document . getElementById ( 'board' ) ;
44const ctx = canvas . getContext ( '2d' ) ;
55const size = 20 ;
6+ let gameMode = "normal" ;
67
78// ------------------ INITIALIZE SNAKES ------------------ //
89let snakeA = [ { x : 3 , y : 3 } ] ;
@@ -28,6 +29,7 @@ function reset() {
2829 gameRunning = true ;
2930 gamePaused = false ;
3031 updateButtonText ( ) ;
32+ gameMode = document . getElementById ( "mode" ) . value ;
3133 draw ( ) ;
3234}
3335
@@ -41,8 +43,8 @@ function spawnFood() {
4143function tick ( ) {
4244 if ( ! gameRunning || gamePaused ) return ;
4345
44- moveSnake ( snakeA , dirA , "A" ) ;
45- moveSnake ( snakeB , dirB , "B" ) ;
46+ if ( ! moveSnake ( snakeA , dirA , "A" ) ) return ;
47+ if ( ! moveSnake ( snakeB , dirB , "B" ) ) return ;
4648
4749 if ( checkCollision ( snakeA ) || checkCollision ( snakeB ) || checkSnakeCollision ( snakeA , snakeB ) ) {
4850 let { scoreA, scoreB } = ScoreManager . getScores ( ) ;
@@ -57,8 +59,30 @@ function tick() {
5759function moveSnake ( snake , dir , player ) {
5860 const head = { x : snake [ 0 ] . x + dir . x , y : snake [ 0 ] . y + dir . y } ;
5961
60- head . x = ( head . x + canvas . width / size ) % ( canvas . width / size ) ;
61- head . y = ( head . y + canvas . height / size ) % ( canvas . height / size ) ;
62+ // head.x = (head.x + canvas.width / size) % (canvas.width / size);
63+ // head.y = (head.y + canvas.height / size) % (canvas.height / size);
64+ if ( gameMode === "normal" ) {
65+
66+ head . x = ( head . x + canvas . width / size ) % ( canvas . width / size ) ;
67+ head . y = ( head . y + canvas . height / size ) % ( canvas . height / size ) ;
68+ } else {
69+
70+ if (
71+ head . x < 0 ||
72+ head . x >= canvas . width / size ||
73+ head . y < 0 ||
74+ head . y >= canvas . height / size
75+ ) {
76+ const winner = player === "A" ? "Player B" : "Player A" ;
77+ stop ( ) ;
78+ gameRunning = false ;
79+ gamePaused = false ;
80+ updateButtonText ( ) ;
81+ gameOver ( winner ) ;
82+ return false ;
83+ }
84+ }
85+
6286
6387 snake . unshift ( head ) ;
6488
@@ -68,6 +92,8 @@ function moveSnake(snake, dir, player) {
6892 } else {
6993 snake . pop ( ) ;
7094 }
95+
96+ return true ;
7197}
7298
7399function checkCollision ( snake ) {
@@ -112,6 +138,12 @@ function draw() {
112138 for ( let y = 0 ; y < canvas . height ; y += size ) {
113139 ctx . beginPath ( ) ; ctx . moveTo ( 0 , y ) ; ctx . lineTo ( canvas . width , y ) ; ctx . stroke ( ) ;
114140 }
141+ if ( gameMode === "boundary" ) {
142+ ctx . strokeStyle = "#ff0000" ;
143+ ctx . lineWidth = 3 ;
144+ ctx . strokeRect ( 1 , 1 , canvas . width - 2 , canvas . height - 2 ) ;
145+ }
146+
115147
116148 // Pause overlay
117149 if ( gamePaused ) {
@@ -182,7 +214,9 @@ document.addEventListener('keydown', (e) => {
182214} ) ;
183215
184216// ------------------ EVENT LISTENERS ------------------ //
185-
217+ document . getElementById ( "mode" ) . addEventListener ( "change" , function ( ) {
218+ gameMode = this . value ;
219+ } ) ;
186220document . getElementById ( 'startBtn' ) . addEventListener ( 'click' , handleButtonClick ) ;
187221document . getElementById ( 'speed' ) . addEventListener ( 'input' , function ( ) {
188222 if ( gameRunning && ! gamePaused ) {
0 commit comments