Creating a simple HTML5 game inspired by the classic Atari game Super Cobra (often misspelled as "Duper Cobra") involves building a side-scrolling shooter where a helicopter flies through caverns, avoiding obstacles and shooting enemies.
Here’s a basic implementation using HTML5, CSS, and JavaScript (Canvas API). This is a simplified version and lays the foundation for enhancements.
🚁 Super Cobra-Inspired Game (Simplified)
✅ Features:
- Continuous side-scrolling
- Helicopter with gravity and lift
- Collision with terrain
- Basic shooting mechanism
📁 Folder Structure:
super-cobra/
│
├── index.html
├── style.css
└── game.js
🔹 index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Super Cobra Clone</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<canvas id="gameCanvas" width="800" height="400"></canvas>
<script src="game.js"></script>
</body>
</html>
🔹 style.css
body {
margin: 0;
background-color: #000;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
canvas {
border: 2px solid #fff;
background-color: #111;
}
🔹 game.js
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
const keys = {};
document.addEventListener("keydown", (e) => keys[e.code] = true);
document.addEventListener("keyup", (e) => keys[e.code] = false);
const game = {
width: canvas.width,
height: canvas.height,
speed: 2,
};
class Helicopter {
constructor() {
this.x = 80;
this.y = 200;
this.width = 40;
this.height = 20;
this.color = "lime";
this.dy = 0;
this.gravity = 0.5;
this.lift = -0.8;
this.maxSpeed = 5;
}
update() {
if (keys["Space"]) this.dy += this.lift;
this.dy += this.gravity;
this.dy = Math.max(-this.maxSpeed, Math.min(this.maxSpeed, this.dy));
this.y += this.dy;
// Boundaries
if (this.y < 0) this.y = 0;
if (this.y + this.height > game.height) this.y = game.height - this.height;
}
draw() {
ctx.fillStyle = this.color;
ctx.fillRect(this.x, this.y, this.width, this.height);
}
}
class Terrain {
constructor() {
this.segments = [];
this.segmentWidth = 10;
this.maxHeight = game.height - 100;
for (let i = 0; i < game.width / this.segmentWidth + 2; i++) {
this.segments.push(this.generateHeight());
}
}
generateHeight() {
return Math.random() * this.maxHeight + 50;
}
update() {
this.segments.shift();
this.segments.push(this.generateHeight());
}
draw() {
ctx.fillStyle = "green";
for (let i = 0; i < this.segments.length; i++) {
const h = this.segments[i];
ctx.fillRect(i * this.segmentWidth, game.height - h, this.segmentWidth, h);
}
}
checkCollision(heli) {
const idx = Math.floor((heli.x + heli.width) / this.segmentWidth);
const terrainHeight = this.segments[idx];
if (heli.y + heli.height > game.height - terrainHeight) {
return true;
}
return false;
}
}
class Bullet {
constructor(x, y) {
this.x = x;
this.y = y;
this.speed = 5;
this.width = 5;
this.height = 2;
this.color = "red";
}
update() {
this.x += this.speed;
}
draw() {
ctx.fillStyle = this.color;
ctx.fillRect(this.x, this.y, this.width, this.height);
}
}
const helicopter = new Helicopter();
const terrain = new Terrain();
const bullets = [];
function shoot() {
bullets.push(new Bullet(helicopter.x + helicopter.width, helicopter.y + helicopter.height / 2));
}
let lastShot = 0;
function gameLoop(timestamp) {
ctx.clearRect(0, 0, game.width, game.height);
helicopter.update();
helicopter.draw();
terrain.update();
terrain.draw();
// Bullets
if (keys["KeyX"] && timestamp - lastShot > 300) {
shoot();
lastShot = timestamp;
}
for (let i = bullets.length - 1; i >= 0; i--) {
bullets[i].update();
bullets[i].draw();
if (bullets[i].x > game.width) {
bullets.splice(i, 1);
}
}
if (terrain.checkCollision(helicopter)) {
alert("Game Over!");
document.location.reload();
}
requestAnimationFrame(gameLoop);
}
requestAnimationFrame(gameLoop);
▶️ Controls:
- Spacebar: Fly upward (helicopter lift)
- X: Shoot
🧠 Suggestions for Future Enhancements:
- Add enemies
- Add levels and scoring
- Add background & parallax scrolling
- Add explosion animations
- Add sound effects and intro/start menu
Would you like me to zip all the files together or deploy it to something like CodePen or JSFiddle for live testing?