JSFiddle - React, Tailwind, and code Playground
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Resonance Flux - Prototype</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<canvas id="gameCanvas"></canvas>
<div id="ui">
Score: <span id="score">0</span>
</div>
<div id="gameOverScreen" style="display: none;">
<h1>GAME OVER</h1>
<p>Final Score: <span id="finalScore">0</span></p>
<button id="restartButton">Restart</button>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
CSS
body {
margin: 0;
background-color: #111; /* Dark background */
color: #eee;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
overflow: hidden; /* Prevent scrollbars */
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
.container {
position: relative; /* Needed for absolute positioning of UI elements */
}
canvas {
display: block; /* Removes potential space below the canvas */
background-color: #222; /* Slightly lighter than body for contrast */
border: 1px solid #555;
}
#ui {
position: absolute;
top: 10px;
left: 10px;
font-size: 1.5em;
color: #fff;
text-shadow: 1px 1px 2px black;
}
#gameOverScreen {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: rgba(0, 0, 0, 0.8);
padding: 30px;
border-radius: 10px;
text-align: center;
color: #fff;
border: 1px solid #888;
}
#gameOverScreen h1 {
color: #ff4d4d; /* Reddish color for game over */
margin-bottom: 10px;
}
#gameOverScreen button {
padding: 10px 20px;
font-size: 1em;
cursor: pointer;
background-color: #4CAF50; /* Green */
color: white;
border: none;
border-radius: 5px;
margin-top: 20px;
}
#gameOverScreen button:hover {
background-color: #45a049;
}
JavaScript
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score');
const gameOverScreen = document.getElementById('gameOverScreen');
const finalScoreElement = document.getElementById('finalScore');
const restartButton = document.getElementById('restartButton');
// --- Config ---
const PLAYER_RADIUS = 15;
const BULLET_RADIUS = 5;
const BULLET_SPEED = 2;
const SPAWN_RATE = 15; // Lower = more bullets (every X frames)
// Use distinct, bright colors
const COLOR_A = 'cyan'; // Player starts as this
const COLOR_B = 'orange'; // Player switches to this
let canvasWidth = window.innerWidth * 0.8;
let canvasHeight = window.innerHeight * 0.8;
canvas.width = canvasWidth;
canvas.height = canvasHeight;
// --- Game State ---
let player;
let bullets = [];
let score = 0;
let frameCount = 0;
let isGameOver = false;
let animationFrameId;
// --- Player ---
class Player {
constructor(x, y, radius, color) {
this.x = x;
this.y = y;
this.radius = radius;
this.color = color;
}
draw() {
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
ctx.fillStyle = this.color;
ctx.fill();
// Add a subtle outline to make it pop
ctx.strokeStyle = 'white';
ctx.lineWidth = 1;
ctx.stroke();
ctx.closePath();
}
switchColor() {
this.color = (this.color === COLOR_A) ? COLOR_B : COLOR_A;
}
updatePosition(mouseX, mouseY) {
this.x = mouseX;
this.y = mouseY;
// Optional: Clamp position to canvas bounds
this.x = Math.max(this.radius, Math.min(canvasWidth - this.radius, this.x));
this.y = Math.max(this.radius, Math.min(canvasHeight - this.radius, this.y));
}
}
// --- Bullet ---
class Bullet {
constructor(x, y, radius, color, dx, dy) {
this.x = x;
this.y = y;
this.radius = radius;
this.color...