work 18. Feb 2020
This commit is contained in:
@ -1,8 +1,8 @@
|
|||||||
// @ts-check
|
// @ts-check
|
||||||
|
|
||||||
/* Initialize variables */
|
/* Initialize variables */
|
||||||
const pixelSize = 20;
|
const pixelSize = 20; //Cannot be odd
|
||||||
const pixelAmount = 40;
|
const pixelAmount = 20;
|
||||||
|
|
||||||
/* Register HTML DOM elements by variables */
|
/* Register HTML DOM elements by variables */
|
||||||
const canvas = document.getElementById('game');
|
const canvas = document.getElementById('game');
|
||||||
@ -13,52 +13,46 @@ const startButton = document.getElementById('start');
|
|||||||
document.addEventListener('keydown', updateDirection, false);
|
document.addEventListener('keydown', updateDirection, false);
|
||||||
startButton.addEventListener('click', gameLoop, false);
|
startButton.addEventListener('click', gameLoop, false);
|
||||||
|
|
||||||
/* Initialize HTML with functions */
|
/* Initialize HTML */
|
||||||
canvas.style.display = 'block';
|
canvas.style.display = 'block';
|
||||||
canvas.style.width = canvas.style.height = `${pixelSize * pixelAmount}px`;
|
game.canvas.width = game.canvas.height = pixelSize * pixelAmount;
|
||||||
canvas.style.backgroundColor = '#ffffff';
|
canvas.style.backgroundColor = '#ffffff';
|
||||||
|
|
||||||
/* Snake class */
|
/* Snake object */
|
||||||
class snake {
|
const snake = {
|
||||||
constructor() {
|
speed: 5, //in FPS
|
||||||
this.x = 0;
|
direction: 'right',
|
||||||
this.y = 0;
|
color: '#00ff00',
|
||||||
this.speed = 5;
|
tail: [
|
||||||
this.direction = 'left';
|
[3, 0],
|
||||||
this.tail = [
|
[2, 0],
|
||||||
[3, 0],
|
[1, 0],
|
||||||
[2, 0],
|
[0, 0],
|
||||||
[1, 0],
|
],
|
||||||
[0, 0],
|
|
||||||
];
|
|
||||||
this.color = '#00ff00';
|
|
||||||
}
|
|
||||||
|
|
||||||
|
/* Draw the snake by the tail array */
|
||||||
draw() {
|
draw() {
|
||||||
game.fillStyle = this.color;
|
game.fillStyle = this.color;
|
||||||
|
|
||||||
for (let tailPoint = 0; tailPoint < this.tail.length; tailPoint++) {
|
for (let tailPoint = 0; tailPoint < this.tail.length; tailPoint++) {
|
||||||
/* Correct tailpoint x and y to actual size */
|
/* Correct tailpoint x and y to actual size */
|
||||||
const pixelPosition = this.tail[tailPoint].map(
|
const realPosition = this.tail[tailPoint].map(point => point * pixelSize);
|
||||||
point => point * pixelSize
|
|
||||||
);
|
|
||||||
game.fillRect(
|
|
||||||
pixelPosition[0],
|
|
||||||
pixelPosition[1],
|
|
||||||
pixelPosition[0] + pixelSize,
|
|
||||||
pixelPosition[1] + pixelSize
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
game.fillRect(realPosition[0], realPosition[1], pixelSize, pixelSize);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
/* Generate the next tail array */
|
||||||
updateTail() {
|
updateTail() {
|
||||||
this.tail.pop();
|
const realPosition = this.tail.pop().map(point => point * pixelSize);
|
||||||
|
game.clearRect(realPosition[0], realPosition[1], pixelSize, pixelSize);
|
||||||
|
|
||||||
switch (this.direction) {
|
switch (this.direction) {
|
||||||
case 'left':
|
case 'left':
|
||||||
this.tail.unshift([this.tail[0][0] + 1, this.tail[0][1]]);
|
this.tail.unshift([this.tail[0][0] - 1, this.tail[0][1]]);
|
||||||
break;
|
break;
|
||||||
case 'right':
|
case 'right':
|
||||||
this.tail.unshift([this.tail[0][0] - 1, this.tail[0][1]]);
|
this.tail.unshift([this.tail[0][0] + 1, this.tail[0][1]]);
|
||||||
break;
|
break;
|
||||||
case 'up':
|
case 'up':
|
||||||
this.tail.unshift([this.tail[0][0], this.tail[0][1] - 1]);
|
this.tail.unshift([this.tail[0][0], this.tail[0][1] - 1]);
|
||||||
@ -67,57 +61,77 @@ class snake {
|
|||||||
this.tail.unshift([this.tail[0][0], this.tail[0][1] + 1]);
|
this.tail.unshift([this.tail[0][0], this.tail[0][1] + 1]);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
}
|
};
|
||||||
|
|
||||||
/* Apple class */
|
/* Apple object */
|
||||||
class apple {
|
const apple = {
|
||||||
constructor() {
|
position: [20, 20],
|
||||||
this.x = 0;
|
color: 'red',
|
||||||
this.y = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const Snake = new snake();
|
/* Generate new coordinates */
|
||||||
|
generate() {
|
||||||
|
this.position.map(() => Math.floor(Math.random() * pixelAmount));
|
||||||
|
},
|
||||||
|
|
||||||
|
/* Draw the apple */
|
||||||
|
draw() {
|
||||||
|
const realPosition = this.position.map(point => point * pixelSize);
|
||||||
|
|
||||||
|
game.fillStyle = this.color;
|
||||||
|
game.fillRect(realPosition[0], realPosition[1], pixelSize, pixelSize);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
/* Wrapper function for gameloop */
|
/* Wrapper function for gameloop */
|
||||||
function gameLoop() {
|
function gameLoop() {
|
||||||
/* Slow down loop */
|
/* Slow down loop by snake speed*/
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
window.requestAnimationFrame(gameLoop);
|
window.requestAnimationFrame(gameLoop);
|
||||||
gameUpdate();
|
gameUpdate();
|
||||||
}, 1000 / Snake.speed);
|
}, 1000 / snake.speed);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Function that is being run each tick */
|
/* Function that is being run each tick */
|
||||||
function gameUpdate() {
|
function gameUpdate() {
|
||||||
/* Draw snake */
|
/* Draw snake */
|
||||||
Snake.draw();
|
snake.draw();
|
||||||
Snake.updateTail();
|
snake.updateTail();
|
||||||
|
|
||||||
|
if (snake.tail[0] === apple.position) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Update direction for snake */
|
/* Update direction for snake */
|
||||||
function updateDirection(evt) {
|
function updateDirection(evt) {
|
||||||
const key = evt.code;
|
const key = evt.code;
|
||||||
|
|
||||||
console.log(key);
|
console.debug(`Pressed %c${key}`, 'color: red');
|
||||||
|
|
||||||
switch (key) {
|
switch (key) {
|
||||||
case 'ArrowLeft':
|
case 'ArrowLeft':
|
||||||
evt.preventDefault();
|
evt.preventDefault();
|
||||||
Snake.direction = 'left';
|
if (snake.direction !== 'right') {
|
||||||
|
snake.direction = 'left';
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case 'ArrowRight':
|
case 'ArrowRight':
|
||||||
evt.preventDefault();
|
evt.preventDefault();
|
||||||
Snake.direction = 'right';
|
if (snake.direction !== 'left') {
|
||||||
|
snake.direction = 'right';
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case 'ArrowUp':
|
case 'ArrowUp':
|
||||||
evt.preventDefault();
|
evt.preventDefault();
|
||||||
Snake.direction = 'up';
|
if (snake.direction !== 'down') {
|
||||||
|
snake.direction = 'up';
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case 'ArrowDown':
|
case 'ArrowDown':
|
||||||
evt.preventDefault();
|
evt.preventDefault();
|
||||||
Snake.direction = 'down';
|
if (snake.direction !== 'up') {
|
||||||
|
snake.direction = 'down';
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user