Skip to content

Commit

Permalink
finished snake moving logic; added game over checking;
Browse files Browse the repository at this point in the history
  • Loading branch information
FairlyTales committed Apr 26, 2021
1 parent fc736f1 commit 290e808
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 17 deletions.
31 changes: 25 additions & 6 deletions js/main.js
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);

}
35 changes: 24 additions & 11 deletions js/matrix.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
export class Matrix {
constructor(elem) {
export default class Matrix {

constructor(elem, boardDimensions) {
this.elem = elem;
this.cells = [];
this.boardDimensions = boardDimensions;
}

// creates a game board with given parameters
create(boardDimensions) {
create() {
// calculate cells and board dimensions using given parameters
const cellsTotal = boardDimensions.x * boardDimensions.y;
const cellWidth = boardDimensions.cellSize + 'px';
const boardWidth = boardDimensions.x * boardDimensions.cellSize + 'px';
const boardHeight = boardDimensions.y * boardDimensions.cellSize + 'px';
const cellsTotal = this.boardDimensions.x * this.boardDimensions.y;
const cellSize = this.boardDimensions.cellSize + 'px';
const boardWidth = this.boardDimensions.x * this.boardDimensions.cellSize + 'px';
const boardHeight = this.boardDimensions.y * this.boardDimensions.cellSize + 'px';

// assign widht and height to the board
this.elem.style.width = boardWidth;
Expand All @@ -19,17 +21,28 @@ export class Matrix {
// create cells and set their dimensions
for(let i = 0; i < cellsTotal; i++) {
let div = document.createElement('div');
div.style.width = cellWidth;
div.style.height = cellWidth;
div.style.width = cellSize;
div.style.height = cellSize;
this.elem.appendChild(div);
this.cells[i] = '';
}
}

// return cell
getCell(x, y) {

let num = this._calcNum(x, y);
return this.cells[num];
}

// set cell value
setCell(x, y, val) {
let num = this._calcNum(x, y, this.boardDimensions);
this.cells[num] = val;
this.elem.children[num].setAttribute('data-game', val);
}

// colculate cell number based on x and y coordinates
_calcNum(x, y) {
return ( y - 1 ) * this.boardDimensions.x + ( x - 1 );
}
}
98 changes: 98 additions & 0 deletions js/snake.js
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;
}
}
8 changes: 8 additions & 0 deletions style.css
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,12 @@ html {
float: left;

box-sizing: border-box;
}

[data-game="fruit"] {
background-color: red;
}

[data-game="snake"] {
background-color: green;
}

0 comments on commit 290e808

Please sign in to comment.