write a html page that plays a snake but the graphics is all in letters, the game is a space rocket that flyes through space and fire lasers at asteroids on its way, destroyed asteroids give score, the key moment is the game is in pseudo 3d space and uses ascii graphics to render
Assistant
Sure! Below is an HTML page that implements a simple pseudo-3D space shooter game using ASCII graphics. The game features a space rocket that flies through space, fires lasers at approaching asteroids, and gains score by destroying them.
Here's the code:
html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ASCII Space Shooter</title>
<style>
body {
background-color: black;
color: lime;
font-family: monospace;
margin: 0;
overflow: hidden;
}
#game {
white-space: pre;
font-size: 16px;
line-height: 16px;
}
#score {
position: absolute;
top: 0;
left: 0;
padding: 10px;
font-size: 16px;
}
</style>
</head>
<body>
<div id="score">Score: 0</div>
<pre id="game"></pre>
<script>
// Game settings
const COLS = 40; // Width of the game area
const ROWS = 20; // Height of the game area
const EMPTY = ' '; // Empty space character
const SHIP = '^'; // Player's ship character
const ASTEROID = 'O'; // Asteroid character
const LASER = '|'; // Laser character
const ASTEROID_CHANCE = 0.1; // Chance of asteroid spawning each frame
let score = 0; // Player's score
// Initialize the game area as a 2D array filled with EMPTY spaces
const gameArea = [];
for (let y = 0; y < ROWS; y++) {
gameArea[y] = [];
for (let x = 0; x < COLS; x++) {
gameArea[y][x] = EMPTY;
}
}
// Player's ship position
let shipX = Math.floor(COLS / 2);
gameArea[ROWS - 1][shipX] = SHIP;
// Arrays to hold asteroids and lasers
let asteroids = [];
let lasers = [];
// Game update function
function update() {
// Clear previous positions (except the ship)
for (let y = 0; y < ROWS; y++) {
for (let x = 0; x < COLS; x++) {
if (gameArea[y][x] !== SHIP) {
gameArea[y][x] = EMPTY;
}
}
}
// Move asteroids down the screen
for (let i = asteroids.length - 1; i >= 0; i--) {
let asteroid = asteroids[i];
asteroid.y += 1;
if (asteroid.y >= ROWS) {
asteroids.splice(i, 1); // Remove asteroid if it goes off screen
} else {
gameArea[asteroid.y][asteroid.x] = ASTEROID;
}
}
// Move lasers up the screen
for (let i = lasers.length - 1; i >= 0; i--) {
let laser = lasers[i];
laser.y -= 1;
if (laser.y < 0) {
lasers.splice(i, 1); // Remove laser if it goes off screen
continue;
}
// Check collision with asteroids
let hit = false;
for (let j = asteroids.length - 1; j >= 0; j--) {
let asteroid = asteroids[j];
if (asteroid.x === laser.x && asteroid.y === laser.y) {
// Destroy asteroid and laser
asteroids.splice(j, 1);
lasers.splice(i, 1);
score += 100;
document.getElementById('score').textContent = 'Score: ' + score;
hit = true;
break;
}
}
if (!hit) {
gameArea[laser.y][laser.x] = LASER;
}
}
// Randomly add new asteroids at the top
if (Math.random() < ASTEROID_CHANCE) {
let x = Math.floor(Math.random() * COLS);
asteroids.push({x: x, y: 0});
}
// Redraw the player's ship
gameArea[ROWS - 1][shipX] = SHIP;
// Render the game area to the screen
let output = '';
for (let y = 0; y < ROWS; y++) {
output += gameArea[y].join('') + '\n';
}
document.getElementById('game').textContent = output;
// Check for collision between asteroids and the ship
for (let asteroid of asteroids) {
if (asteroid.x === shipX && asteroid.y === ROWS - 1) {
alert('Game Over! Final Score: ' + score);
window.location.reload();
}
}
}
// Handle keyboard input for ship movement and firing lasers
document.addEventListener('keydown', function(e) {
if (e.key === 'ArrowLeft') {
if (shipX > 0) {
gameArea[ROWS - 1][shipX] = EMPTY;
shipX -= 1;
gameArea[ROWS - 1][shipX] = SHIP;
}
} else if (e.key === 'ArrowRight') {
if (shipX < COLS - 1) {
gameArea[ROWS - 1][shipX] = EMPTY;
shipX += 1;
gameArea[ROWS - 1][shipX] = SHIP;
}
} else if (e.key === ' ') {
// Fire a laser
lasers.push({x: shipX, y: ROWS - 2});
}
});
// Start the game loop with updates every 200 milliseconds
setInterval(update, 200);
</script>
</body>
</html>
This code creates a simple ASCII space shooter game. The player controls a ship represented by ^ at the bottom of the screen and can move left and right using the arrow keys. Press the spacebar to fire lasers upward (|). Asteroids (O) appear at the top and move downward. Destroying asteroids increases your score by 100 points. If an asteroid reaches the bottom row and collides with your ship, the game ends.
To run the game:
Copy the code above into a text editor and save it as space_shooter.html.
Open the space_shooter.html file in a modern web browser (e.g., Chrome, Firefox, Edge).
Play the game! The game will start automatically.
Controls:
Left Arrow: Move ship left
Right Arrow: Move ship right
Spacebar: Fire laser
Enjoy the game! Feel free to modify and expand upon it to add more features or improve the graphics.