Certainly! Below is the code for a simple Star Wars-themed game that you can save as a single HTML file. The game allows you to control an X-wing starfighter and shoot down incoming TIE fighters.
To play the game, copy the code below into a text editor and save it with a .html extension (e.g., star_wars_game.html). Then, open the file in a web browser.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Star Wars Mini Game</title>
<style>
body {
margin: 0;
overflow: hidden;
background-color: black;
}
#gameCanvas {
display: block;
margin: 0 auto;
background-color: #000;
}
</style>
</head>
<body>
<canvas id="gameCanvas" width="480" height="640"></canvas>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// Game variables
let rightPressed = false;
let leftPressed = false;
let spacePressed = false;
const shipWidth = 40;
const shipHeight = 20;
let shipX = (canvas.width - shipWidth) / 2;
const shipY = canvas.height - shipHeight - 10;
const bullets = [];
const enemies = [];
const enemyWidth = 40;
const enemyHeight = 20;
const enemyInterval = 2000; // Enemies spawn every 2 seconds
let score = 0;
let isGameOver = false;
// Event listeners
document.addEventListener('keydown', keyDownHandler);
document.addEventListener('keyup', keyUpHandler);
function keyDownHandler(e) {
if(e.key === 'Right' || e.key === 'ArrowRight') {
rightPressed = true;
}
else if(e.key === 'Left' || e.key === 'ArrowLeft') {
leftPressed = true;
}
else if(e.key === ' ' || e.key === 'Spacebar') {
spacePressed = true;
}
}
function keyUpHandler(e) {
if(e.key === 'Right' || e.key === 'ArrowRight') {
rightPressed = false;
}
else if(e.key === 'Left' || e.key === 'ArrowLeft') {
leftPressed = false;
}
else if(e.key === ' ' || e.key === 'Spacebar') {
spacePressed = false;
}
}
// Functions to draw ship, bullets, enemies
function drawShip() {
ctx.fillStyle = '#00FF00'; // Green color for the ship
ctx.beginPath();
ctx.moveTo(shipX, shipY + shipHeight);
ctx.lineTo(shipX + shipWidth / 2, shipY);
ctx.lineTo(shipX + shipWidth, shipY + shipHeight);
ctx.closePath();
ctx.fill();
}
function drawBullets() {
ctx.fillStyle = '#FFFF00'; // Yellow color for bullets
bullets.forEach(function(bullet) {
ctx.fillRect(bullet.x, bullet.y, 2, 10);
});
}
function drawEnemies() {
ctx.fillStyle = '#FF0000'; // Red color for enemies
enemies.forEach(function(enemy) {
ctx.fillRect(enemy.x, enemy.y, enemyWidth, enemyHeight);
});
}
function moveShip() {
if(rightPressed && shipX < canvas.width - shipWidth) {
shipX += 5;
}
else if(leftPressed && shipX > 0) {
shipX -= 5;
}
}
function updateBullets() {
bullets.forEach(function(bullet, index) {
bullet.y -= 7;
if(bullet.y < 0) {
bullets.splice(index, 1);
}
});
}
function updateEnemies() {
enemies.forEach(function(enemy, index) {
enemy.y += 2;
if(enemy.y > canvas.height) {
// Remove enemy if it passes the bottom
enemies.splice(index, 1);
// End the game if an enemy reaches the bottom
isGameOver = true;
}
});
}
function spawnEnemy() {
const enemyX = Math.random() * (canvas.width - enemyWidth);
enemies.push({ x: enemyX, y: -enemyHeight });
}
function fireBullet() {
bullets.push({ x: shipX + shipWidth / 2 - 1, y: shipY });
}
function detectCollisions() {
bullets.forEach(function(bullet, bIndex) {
enemies.forEach(function(enemy, eIndex) {
if(bullet.x > enemy.x && bullet.x < enemy.x + enemyWidth && bullet.y < enemy.y + enemyHeight && bullet.y > enemy.y) {
bullets.splice(bIndex, 1);
enemies.splice(eIndex, 1);
score += 10;
}
});
});
}
function drawScore() {
ctx.fillStyle = '#FFFFFF';
ctx.font = '16px Arial';
ctx.fillText('Score: ' + score, 8, 20);
}
function drawGameOver() {
ctx.fillStyle = '#FFFFFF';
ctx.font = '48px Arial';
ctx.fillText('Game Over', canvas.width / 2 - 120, canvas.height / 2);
ctx.font = '24px Arial';
ctx.fillText('Press F5 to Restart', canvas.width / 2 - 110, canvas.height / 2 + 40);
}
// Main draw function
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
if(isGameOver) {
drawGameOver();
return;
}
drawShip();
drawBullets();
drawEnemies();
moveShip();
if(spacePressed) {
fireBullet();
spacePressed = false; // Prevent continuous firing
}
updateBullets();
updateEnemies();
detectCollisions();
drawScore();
requestAnimationFrame(draw);
}
// Start the game
setInterval(spawnEnemy, enemyInterval);
draw();
</script>
</body>
</html>
Instructions:
-
Save the Code:
- Copy the entire code above.
- Paste it into a text editor (e.g., Notepad, TextEdit).
- Save the file with a name like
star_wars_game.html.
-
Open the Game:
- Open the saved HTML file in a modern web browser (e.g., Chrome, Firefox, Edge).
-
How to Play:
- Move Left/Right: Use the left and right arrow keys to move your ship (the green triangle) horizontally across the bottom of the screen.
- Fire: Press the spacebar to shoot bullets (yellow rectangles) upwards to destroy enemies.
- Enemies: Red rectangles will appear from the top of the screen and move downwards. They represent the TIE fighters.
- Score: Destroying an enemy increases your score by 10 points.
- Game Over: The game ends if an enemy reaches the bottom of the screen. A "Game Over" message will be displayed.
-
Restarting the Game:
- To restart after a game over, refresh the browser page (press F5 or the refresh button).
Notes:
- This simple game uses basic shapes to represent the X-wing (your ship) and TIE fighters (enemies).
- The game keeps running until an enemy reaches the bottom of the screen.
- The objective is to score as many points as possible by shooting down enemies.
- You can modify the code to adjust the game's difficulty, appearance, or behavior.
Customization Ideas:
- Change Ship Speed: Modify the value
5 in the moveShip() function's shipX += 5; and shipX -= 5; lines.
- Adjust Enemy Speed: Change
enemy.y += 2; in the updateEnemies() function to make enemies move faster or slower.
- Increase Enemy Frequency: Reduce the
enemyInterval value to make enemies spawn more frequently.
- Add More Enemy Types: Introduce different enemies with varying speeds or sizes.
Example of Changing Enemy Speed:
To make the enemies move faster, you can change the line:
enemy.y += 2;
to:
enemy.y += 4; // Enemies move twice as fast
Feel free to explore and enhance the game as you like!
Let me know if you need further assistance or additional features added to the game.