write a html page that plays a snake but the graphics is all in letters, the game takes place on a white blank canvas text where snake and food are letters that can be copied
Assistant
Here is an HTML page that plays a simple snake game using letters. The game takes place on a white background, and both the snake and the food are represented by letters that you can select and copy.
html
<!DOCTYPE html>
<html>
<head>
<title>Text Snake Game</title>
<style>
/* Set monospace font for proper alignment */
#game {
font-family: monospace;
font-size: 16px;
line-height: 16px;
white-space: pre;
user-select: text; /* Allow text selection */
}
body {
background-color: white;
}
</style>
</head>
<body>
<div id="game"></div>
<script>
var COLS = 30;
var ROWS = 20;
var snake = [{x: Math.floor(COLS/2), y: Math.floor(ROWS/2)}];
var direction = {x: 0, y: 0}; // Initial direction (stationary)
var food = null;
var gameOver = false;
var gameLoop;
function init() {
placeFood();
document.addEventListener('keydown', changeDirection);
gameLoop = setInterval(update, 200); // Update every 200ms
draw();
}
function placeFood() {
var validPosition = false;
while (!validPosition) {
var fx = Math.floor(Math.random() * COLS);
var fy = Math.floor(Math.random() * ROWS);
// Ensure food is not placed on the snake
validPosition = !snake.some(segment => segment.x === fx && segment.y === fy);
if (validPosition) {
food = {x: fx, y: fy};
}
}
}
function changeDirection(e) {
var key = e.keyCode;
// Up arrow or 'W'
if ((key === 38 || key === 87) && direction.y !== 1) {
direction = {x: 0, y: -1};
e.preventDefault();
}
// Down arrow or 'S'
else if ((key === 40 || key === 83) && direction.y !== -1) {
direction = {x: 0, y: 1};
e.preventDefault();
}
// Left arrow or 'A'
else if ((key === 37 || key === 65) && direction.x !== 1) {
direction = {x: -1, y: 0};
e.preventDefault();
}
// Right arrow or 'D'
else if ((key === 39 || key === 68) && direction.x !== -1) {
direction = {x: 1, y: 0};
e.preventDefault();
}
}
function update() {
if (gameOver) {
clearInterval(gameLoop);
alert('Game Over! Refresh the page to restart.');
return;
}
// If the snake hasn't started moving yet
if (direction.x === 0 && direction.y === 0) return;
// Current head position
var head = snake[0];
// New head position based on the current direction
var newHead = {x: head.x + direction.x, y: head.y + direction.y};
// Check for collisions with walls
if (newHead.x < 0 || newHead.x >= COLS || newHead.y < 0 || newHead.y >= ROWS) {
gameOver = true;
draw();
return;
}
// Check for collisions with itself
if (snake.some(segment => segment.x === newHead.x && segment.y === newHead.y)) {
gameOver = true;
draw();
return;
}
// Add new head to the snake
snake.unshift(newHead);
// Check if food is eaten
if (newHead.x === food.x && newHead.y === food.y) {
placeFood(); // Place new food
} else {
snake.pop(); // Remove the tail segment
}
draw();
}
function draw() {
var board = '';
for (var y = 0; y < ROWS; y++) {
var row = '';
for (var x = 0; x < COLS; x++) {
if (gameOver) {
row += 'X';
} else if (x === food.x && y === food.y) {
row += 'F'; // Represent food with 'F'
} else {
var snakePart = snake.find(segment => segment.x === x && segment.y === y);
if (snakePart) {
row += 'S'; // Represent snake with 'S'
} else {
row += ' '; // Empty space
}
}
}
board += row + '\n';
}
document.getElementById('game').textContent = board;
}
init();
</script>
</body>
</html>
Instructions:
Open the HTML file in your web browser.
Use the arrow keys or WASD keys to control the snake.
Try to eat the food represented by F to grow longer.
Avoid colliding with the walls or the snake's own body.
The game will display X characters when it's over.
You can select and copy the text from the game area if needed.
Note: Since the game uses plain text, ensure that the font is set to monospace to keep the alignment correct. The white-space: pre; CSS property preserves the spaces and line breaks, which is essential for the game's layout.
Feel free to modify the COLS and ROWS variables to adjust the size of the game board.