Skip to content
This repository has been archived by the owner on Nov 2, 2024. It is now read-only.

Apply fixes to snake game. #5

Merged
merged 2 commits into from
Sep 26, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 29 additions & 23 deletions assets/Views/status.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
var context = canvas.getContext("2d");
var grid = 16;
var count = 0;
var changing_direction = false;

var snake = {
x: 160,
Expand All @@ -77,6 +78,7 @@
return;
}
count = 0;
changing_direction = false;
context.clearRect(0, 0, canvas.width, canvas.height);
snake.x += snake.dx;
snake.y += snake.dy;
Expand Down Expand Up @@ -106,7 +108,29 @@
context.fillRect(apple.x, apple.y, grid - 1, grid - 1);

context.fillStyle = "green";
snake.cells.forEach(function(cell, index) {

for (var i = 4; i < snake.cells.length; i++) {

if (snake.cells[i].x === snake.cells[0].x && snake.cells[i].y === snake.cells[0].y) {
document.getElementById("game").className = "snakeDeath";

snake.x = 160;
snake.y = 160;
snake.cells = [];
snake.maxCells = 4;
snake.dx = grid;
snake.dy = 0;
apple.x = getRandomInt(0, 25) * grid;
apple.y = getRandomInt(0, 25) * grid;

document.getElementById("score").innerHTML = `${snake.maxCells - 4}`;
setTimeout(() => {
document.getElementById("game").className = "snakeNormal";
}, 350);
}
}

snake.cells.forEach(function(cell) {

context.fillRect(cell.x, cell.y, grid - 1, grid - 1);
if (cell.x === apple.x && cell.y === apple.y) {
Expand Down Expand Up @@ -135,30 +159,12 @@
document.getElementById("game").className = "snakeNormal";
}, 350);
}

for (var i = index + 1; i < snake.cells.length; i++) {

if (cell.x === snake.cells[i].x && cell.y === snake.cells[i].y) {
document.getElementById("game").className = "snakeDeath";

snake.x = 160;
snake.y = 160;
snake.cells = [];
snake.maxCells = 4;
snake.dx = grid;
snake.dy = 0;
apple.x = getRandomInt(0, 25) * grid;
apple.y = getRandomInt(0, 25) * grid;

document.getElementById("score").innerHTML = `${snake.maxCells - 4}`;
setTimeout(() => {
document.getElementById("game").className = "snakeNormal";
}, 350);
}
}
});
}

document.addEventListener("keydown", function(e) {
if (changing_direction) return;
changing_direction = true;
// Go up
// Arrow - 38, W - 87
if ((e.which === 38 || e.which === 87) && snake.dy === 0) {
Expand Down Expand Up @@ -260,4 +266,4 @@
<% } %>
<script><% if (type === "Error") { %>document.getElementById("premidPageInfo").innerText="<%= __("premid.error", status)%>";<% } else if (type === "Success") { %>document.getElementById("premidPageInfo").innerText="<%= __("premid.success", status)%>";<% } else if (type === "Information") { %>document.getElementById("premidPageInfo").innerText="<%= __("premid.information", status)%>";<% } %></script>
</div>
<%- include("./partials/footer.ejs") %>
<%- include("./partials/footer.ejs") %>