-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
finished snake moving logic; added game over checking;
- Loading branch information
1 parent
fc736f1
commit 290e808
Showing
4 changed files
with
155 additions
and
17 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,33 @@ | ||
import { Matrix } from './matrix.js' | ||
import Matrix from './matrix.js'; | ||
import Snake from './snake.js'; | ||
|
||
window.onload = function (e) { | ||
let field = document.querySelector('.fields'); | ||
let matrix = new Matrix(field); | ||
|
||
// set board dimensions and cell size | ||
let boardDimensions = { | ||
x: 20, | ||
y: 13, | ||
cellSize: 30 } | ||
y: 10, | ||
cellSize: 40 | ||
} | ||
|
||
matrix.create(boardDimensions); | ||
} | ||
// create gameboard(matrix) and snake objects | ||
let matrix = new Matrix(field, boardDimensions); | ||
let snake = new Snake(matrix, 5, 1, 'right'); | ||
|
||
// create gameboard and snake on the page | ||
matrix.create(); | ||
snake.show(); | ||
snake.initInputListener(); | ||
|
||
// game loop | ||
let gameLoop = setInterval(() => { | ||
if(!snake.snekDead) { | ||
snake.move(snake.direction); | ||
} else { | ||
console.log('snek is dead'); | ||
clearInterval(gameLoop); | ||
} | ||
}, 500); | ||
|
||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
export default class Snake { | ||
|
||
constructor(matrix, x, y, direction) { | ||
this.matrix = matrix; | ||
this.x = x; | ||
this.y = y; | ||
this.direction = direction; | ||
|
||
this.snekDead = false; | ||
} | ||
|
||
show() { | ||
this.matrix.setCell(this.x, this.y, 'snake'); | ||
} | ||
|
||
move(direction) { | ||
// save current snake position | ||
let lastCoords = { | ||
x: this.x, | ||
y: this.y | ||
} | ||
|
||
switch(direction) { | ||
case 'right': | ||
this.x++; | ||
break; | ||
|
||
case 'left': | ||
this.x--; | ||
break; | ||
|
||
case 'up': | ||
this.y--; | ||
break; | ||
|
||
case 'down': | ||
this.y++; | ||
break; | ||
} | ||
|
||
if (this._snekDeadCheck()) { | ||
this.snekDead = true; | ||
return; | ||
} | ||
|
||
// remove snake from current cell | ||
this.matrix.setCell(lastCoords.x, lastCoords.y, ''); | ||
|
||
// "move" (assign) snake to the next cell | ||
this.matrix.setCell(this.x, this.y, 'snake'); | ||
|
||
|
||
} | ||
|
||
initInputListener() { | ||
let lastDirection = null; | ||
|
||
window.addEventListener('keydown', (e) => { | ||
switch(e.key) { | ||
case 'w': | ||
case 'W': | ||
case 'ArrowUp': | ||
if (lastDirection === 'down') break; | ||
this.direction = 'up'; | ||
lastDirection = 'up'; | ||
break; | ||
|
||
case 'a': | ||
case 'A': | ||
case 'ArrowLeft': | ||
if (lastDirection === 'right') break; | ||
this.direction = 'left'; | ||
lastDirection = 'left'; | ||
break; | ||
|
||
case 's': | ||
case 'S': | ||
case 'ArrowDown': | ||
if (lastDirection === 'up') break; | ||
this.direction = 'down'; | ||
lastDirection = 'down'; | ||
break; | ||
|
||
case 'd': | ||
case 'D': | ||
case 'ArrowRight': | ||
if (lastDirection === 'left') break; | ||
this.direction = 'right'; | ||
lastDirection = 'right'; | ||
break; | ||
} | ||
}) | ||
} | ||
|
||
_snekDeadCheck() { | ||
return this.x < 1 || this.x > this.matrix.boardDimensions.x || this.y < 1 || this.y > this.matrix.boardDimensions.y; | ||
} | ||
} |
This file contains 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