gpt-3.5-turbo-0613· openai
Suzhen/CodeChat
Give me the js code for the Snake Game
- User
Give me the js code for the Snake Game
- Assistant
Give me the js code for the Snake Game
Sure! Here's an example code for a simple version of the Snake game using JavaScript:
// Set up the canvas
const canvas = document.getElementById("snakeCanvas");
const context = canvas.getContext("2d");
// Scale for the game
const scale = 20;
const rows = canvas.height / scale;
const columns = canvas.width / scale;
// Initialize the snake
let snake = [];
snake[0] = {
x: Math.floor(columns / 2) * scale,
y: Math.floor(rows / 2) * scale
};
// Initialize the fruit
let fruit = {
x: Math.floor(Math.random() * columns) * scale,
y: Math.floor(Math.random() * rows) * scale
};
// Direction of the snake
let direction;
// Function to draw the snake and fruit
function draw() {
// Clear the canvas
context.clearRect(0, 0, canvas.width, canvas.height);
// Draw the snake
for (let i = 0; i < snake.length; i++) {
context.fillStyle = (i === 0) ? "green" : "white";
context.fillRect(snake[i].x, snake[i].y, scale, scale);
context.strokeStyle = "black";
context.strokeRect(snake[i].x, snake[i].y, scale, scale);
}
// Draw the fruit
context.fillStyle = "red";
context.fillRect(fruit.x, fruit.y, scale, scale);
context.strokeStyle = "black";
context.strokeRect(fruit.x, fruit.y, scale, scale);
}
// Function to update the game
function update() {
// Move the snake
if (direction === "left") snake[0].x -= scale;
if (direction === "right") snake[0].x += scale;
if (direction === "up") snake[0].y -= scale;
if (direction === "down") snake[0].y += scale;
// Check for collision with the fruit
if (snake[0].x === fruit.x && snake[0].y === fruit.y) {
// Increase the snake length
snake.unshift({});
// Generate new fruit
fruit = {
x: Math.floor(Math.random() * columns) * scale,
y: Math.floor(Math.random() * rows) * scale
};
} else {
// Remove the tail segment of the snake
snake.pop();
}
// Check for collision with the walls or itself
for (let i = 1; i < snake.length; i++) {
if (snake[0].x === snake[i].x && snake[0].y === snake[i].y) {
clearInterval(game);
alert("Game over!");
}
}
// Check for collision with the walls
if (snake[0].x < 0 || snake[0].x >= canvas.width ||
snake[0].y < 0 || snake[0].y >= canvas.height) {
clearInterval(game);
alert("Game over!");
}
// Call the draw function
draw();
}
// Function to handle keydown event
function keyPressed(event) {
if (event.keyCode === 37 && direction !== "right") direction = "left";
if (event.keyCode === 38 && direction !== "down") direction = "up";
if (event.keyCode === 39 && direction !== "left") direction = "right";
if (event.keyCode === 40 && direction !== "up") direction = "down";
}
// Event listener for keydown event
document.addEventListener("keydown", keyPressed);
// Set up the game interval
const game = setInterval(update, 100);
To use this code, you'll need an HTML file with a canvas element with the ID "snakeCanvas" like this:
<!DOCTYPE html>
<html>
<head>
<title>Snake Game</title>
<style>
body {
margin: 0;
padding: 0;
}
canvas {
background: #ddd;
display: block;
margin: 20px auto;
}
</style>
</head>
<body>
<canvas id="snakeCanvas" width="400" height="400"></canvas>
<script src="snake.js"></script>
</body>
</html>
Save the JavaScript code in a file named "snake.js" in the same directory as the HTML file. Open the HTML file in a web browser, and you should see the Snake game running in the canvas!