console.log("testing 1 2 3");const canvas = document.getElementById("gameCanvas");const ctx = canvas.getContext("2d");- console.log("testing 1 2 3");
const gridSize = 20;const tileCount = canvas.width / gridSize;let snake = [{ x: 10, y: 10 }];let direction = { x: 1, y: 0 };let nextDirection = { x: 1, y: 0 };let food = spawnFood();let score = 0;function spawnFood() {let newFood;while (true) {newFood = {x: Math.floor(Math.random() * tileCount),y: Math.floor(Math.random() * tileCount)};if (!snake.some(part => part.x === newFood.x && part.y === newFood.y)) {return newFood;}}}function draw() {ctx.clearRect(0, 0, canvas.width, canvas.height);ctx.fillStyle = "red";ctx.fillRect(food.x * gridSize, food.y * gridSize, gridSize, gridSize);// Draw snakesnake.forEach((part, index) => {ctx.fillStyle = index === 0 ? "lime" : "green";ctx.fillRect(part.x * gridSize, part.y * gridSize, gridSize, gridSize);});}function update() {direction = nextDirection;const head = {x: snake[0].x + direction.x,y: snake[0].y + direction.y};// Wall collisionif (head.x < 0 || head.x >= tileCount ||head.y < 0 || head.y >= tileCount) {resetGame();return;}// Self collisionif (snake.some(part => part.x === head.x && part.y === head.y)) {resetGame();return;}snake.unshift(head);// Eat foodif (head.x === food.x && head.y === food.y) {score++;document.getElementById("score").textContent = score;food = spawnFood();} else {snake.pop();}draw();}function resetGame() {snake = [{ x: 10, y: 10 }];direction = { x: 1, y: 0 };nextDirection = { x: 1, y: 0 };food = spawnFood();score = 0;document.getElementById("score").textContent = score;}draw();setInterval(update, 120);
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
const gridSize = 20;
const tileCount = canvas.width / gridSize;
let snake = [{ x: 10, y: 10 }];
let direction = { x: 1, y: 0 };
let nextDirection = { x: 1, y: 0 };
let food = spawnFood();
let score = 0;
function spawnFood() {
let newFood;
while (true) {
newFood = {
x: Math.floor(Math.random() * tileCount),
y: Math.floor(Math.random() * tileCount)
};
if (!snake.some(part => part.x === newFood.x && part.y === newFood.y)) {
return newFood;
}
}
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "red";
ctx.fillRect(food.x * gridSize, food.y * gridSize, gridSize, gridSize);
// Draw snake
snake.forEach((part, index) => {
ctx.fillStyle = index === 0 ? "lime" : "green";
ctx.fillRect(part.x * gridSize, part.y * gridSize, gridSize, gridSize);
});
}
function update() {
direction = nextDirection;
const head = {
x: snake[0].x + direction.x,
y: snake[0].y + direction.y
};
// Wall collision
if (
head.x < 0 || head.x >= tileCount ||
head.y < 0 || head.y >= tileCount
) {
resetGame();
return;
}
// Self collision
if (snake.some(part => part.x === head.x && part.y === head.y)) {
resetGame();
return;
}
snake.unshift(head);
// Eat food
if (head.x === food.x && head.y === food.y) {
score++;
document.getElementById("score").textContent = score;
food = spawnFood();
} else {
snake.pop();
}
draw();
}
function resetGame() {
snake = [{ x: 10, y: 10 }];
direction = { x: 1, y: 0 };
nextDirection = { x: 1, y: 0 };
food = spawnFood();
score = 0;
document.getElementById("score").textContent = score;
}
draw();
setInterval(update, 120);// Since Node 10, we're using Mocha.
// You can use `chai` for assertions.
const chai = require("chai");
const assert = chai.assert;
// Uncomment the following line to disable truncating failure messages for deep equals, do:
// chai.config.truncateThreshold = 0;
// Since Node 12, we no longer include assertions from our deprecated custom test framework by default.
// Uncomment the following to use the old assertions:
// const Test = require("@codewars/test-compat");
describe("Solution", function() {
it("should test for something", function() {
// Test.assertEquals(1 + 1, 2);
// assert.strictEqual(1 + 1, 2);
});
});