<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Space Archery</title>
<style>
body {
margin: 0;
padding: 0;
background: #000;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
font-family: Arial, sans-serif;
}
canvas {
border: 1px solid #333;
background: linear-gradient(45deg, #001122, #000033);
}
.ui {
position: absolute;
top: 10px;
left: 10px;
color: white;
font-size: 18px;
}
.controls {
position: absolute;
bottom: 10px;
left: 50%;
transform: translateX(-50%);
color: white;
text-align: center;
}
</style>
</head>
<body>
<canvas id="gameCanvas" width="1000" height="700"></canvas>
<div class="ui">
<div>Player 1 Health: <span id="p1Health">100</span></div>
<div>Player 2 Health: <span id="p2Health">100</span></div>
<div>Current Turn: <span id="currentTurn">Player 1</span></div>
</div>
<div class="controls">
<div>Mouse to aim, Click to shoot</div>
<div>Space to restart game</div>
</div>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// Game state
let gameState = {
planets: [],
player1: { x: 0, y: 0, health: 100, planetIndex: 0 },
player2: { x: 0, y: 0, health: 100, planetIndex: 0 },
currentPlayer: 1,
arrows: [],
gameOver: false,
winner: null
};
// Mouse state
let mouse = { x: 0, y: 0, down: false };
// Planet class
class Planet {
constructor(x, y, radius, mass, color) {
this.x = x;
this.y = y;
this.radius = radius;
this.mass = mass;
this.color = color;
}
draw() {
ctx.fillStyle = this.color;
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
ctx.fill();
// Add some shading
const gradient = ctx.createRadialGradient(
this.x - this.radius/3, this.y - this.radius/3, 0,
this.x, this.y, this.radius
);
gradient.addColorStop(0, 'rgba(255,255,255,0.3)');
gradient.addColorStop(1, 'rgba(0,0,0,0.3)');
ctx.fillStyle = gradient;
ctx.fill();
}
getGravityForce(x, y) {
const dx = this.x - x;
const dy = this.y - y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance < this.radius) return { x: 0, y: 0 };
const force = (this.mass * 0.1) / (distance * distance);
const angle = Math.atan2(dy, dx);
return {
x: Math.cos(angle) * force,
y: Math.sin(angle) * force
};
}
}
// Arrow class
class Arrow {
constructor(x, y, vx, vy) {
this.x = x;
this.y = y;
this.vx = vx;
this.vy = vy;
this.trail = [];
this.active = true;
}
update() {
if (!this.active) return;
// Apply gravity from all planets
let totalForceX = 0;
let totalForceY = 0;
gameState.planets.forEach(planet => {
const force = planet.getGravityForce(this.x, this.y);
totalForceX += force.x;
totalForceY += force.y;
});
this.vx += totalForceX;
this.vy += totalForceY;
// Update position
this.x += this.vx;
this.y += this.vy;
// Add to trail
this.trail.push({ x: this.x, y: this.y });
if (this.trail.length > 20) {
this.trail.shift();
}
// Check bounds
if (this.x < 0 || this.x > canvas.width || this.y < 0 || this.y > canvas.height) {
this.active = false;
}
// Check planet collisions
gameState.planets.forEach(planet => {
const dx = this.x - planet.x;
const dy = this.y - planet.y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance < planet.radius) {
this.active = false;
}
});
// Check player collisions
const checkPlayerHit = (player) => {
const dx = this.x - player.x;
const dy = this.y - player.y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 15) {
player.health -= 25;
this.active = false;
if (player.health <= 0) {
gameState.gameOver = true;
gameState.winner = player === gameState.player1 ? 2 : 1;
}
updateUI();
return true;
}
return false;
};
checkPlayerHit(gameState.player1);
checkPlayerHit(gameState.player2);
}
draw() {
if (!this.active) return;
// Draw trail
ctx.strokeStyle = 'rgba(255, 255, 0, 0.5)';
ctx.lineWidth = 2;
ctx.beginPath();
for (let i = 1; i < this.trail.length; i++) {
const alpha = i / this.trail.length;
ctx.globalAlpha = alpha * 0.5;
ctx.moveTo(this.trail[i-1].x, this.trail[i-1].y);
ctx.lineTo(this.trail[i].x, this.trail[i].y);
}
ctx.stroke();
ctx.globalAlpha = 1;
// Draw arrow
ctx.fillStyle = '#ffff00';
ctx.beginPath();
ctx.arc(this.x, this.y, 3, 0, Math.PI * 2);
ctx.fill();
}
}
function generatePlanets() {
const planets = [];
const colors = ['#ff6b6b', '#4ecdc4', '#45b7d1', '#96ceb4', '#ffeaa7', '#dda0dd', '#98d8c8'];
// Generate 5-8 random planets
const numPlanets = 5 + Math.floor(Math.random() * 4);
for (let i = 0; i < numPlanets; i++) {
let x, y, radius;
let validPosition = false;
let attempts = 0;
while (!validPosition && attempts < 50) {
radius = 30 + Math.random() * 60;
x = radius + Math.random() * (canvas.width - 2 * radius);
y = radius + Math.random() * (canvas.height - 2 * radius);
validPosition = true;
// Check distance from other planets
for (let j = 0; j < planets.length; j++) {
const dx = x - planets[j].x;
const dy = y - planets[j].y;
const distance = Math.sqrt(dx * dx + dy * dy);
const minDistance = radius + planets[j].radius + 50;
if (distance < minDistance) {
validPosition = false;
break;
}
}
attempts++;
}
if (validPosition) {
const mass = radius * 2;
const color = colors[Math.floor(Math.random() * colors.length)];
planets.push(new Planet(x, y, radius, mass, color));
}
}
return planets;
}
function initGame() {
gameState.planets = generatePlanets();
// Place players on different planets
if (gameState.planets.length >= 2) {
const planet1 = gameState.planets[0];
const planet2 = gameState.planets[gameState.planets.length - 1];
gameState.player1.x = planet1.x;
gameState.player1.y = planet1.y - planet1.radius - 10;
gameState.player1.health = 100;
gameState.player1.planetIndex = 0;
gameState.player2.x = planet2.x;
gameState.player2.y = planet2.y - planet2.radius - 10;
gameState.player2.health = 100;
gameState.player2.planetIndex = gameState.planets.length - 1;
}
gameState.currentPlayer = 1;
gameState.arrows = [];
gameState.gameOver = false;
gameState.winner = null;
updateUI();
}
function drawPlayer(player, color) {
ctx.fillStyle = color;
ctx.beginPath();
ctx.arc(player.x, player.y, 8, 0, Math.PI * 2);
ctx.fill();
// Draw health bar
const barWidth = 30;
const barHeight = 4;
const healthPercent = player.health / 100;
ctx.fillStyle = 'rgba(255, 0, 0, 0.7)';
ctx.fillRect(player.x - barWidth/2, player.y - 20, barWidth, barHeight);
ctx.fillStyle = 'rgba(0, 255, 0, 0.7)';
ctx.fillRect(player.x - barWidth/2, player.y - 20, barWidth * healthPercent, barHeight);
}
function drawAimLine() {
if (gameState.gameOver) return;
const currentPlayerObj = gameState.currentPlayer === 1 ? gameState.player1 : gameState.player2;
const dx = mouse.x - currentPlayerObj.x;
const dy = mouse.y - currentPlayerObj.y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance > 0) {
const maxDistance = 100;
const clampedDistance = Math.min(distance, maxDistance);
const power = clampedDistance / maxDistance;
ctx.strokeStyle = `rgba(255, 255, 255, ${0.3 + power * 0.4})`;
ctx.lineWidth = 2;
ctx.setLineDash([5, 5]);
ctx.beginPath();
ctx.moveTo(currentPlayerObj.x, currentPlayerObj.y);
ctx.lineTo(
currentPlayerObj.x + (dx / distance) * clampedDistance,
currentPlayerObj.y + (dy / distance) * clampedDistance
);
ctx.stroke();
ctx.setLineDash([]);
}
}
function shoot() {
if (gameState.gameOver) return;
const currentPlayerObj = gameState.currentPlayer === 1 ? gameState.player1 : gameState.player2;
const dx = mouse.x - currentPlayerObj.x;
const dy = mouse.y - currentPlayerObj.y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance > 0) {
const power = Math.min(distance / 100, 1) * 8;
const vx = (dx / distance) * power;
const vy = (dy / distance) * power;
gameState.arrows.push(new Arrow(currentPlayerObj.x, currentPlayerObj.y, vx, vy));
// Switch turns
gameState.currentPlayer = gameState.currentPlayer === 1 ? 2 : 1;
updateUI();
}
}
function updateUI() {
document.getElementById('p1Health').textContent = Math.max(0, gameState.player1.health);
document.getElementById('p2Health').textContent = Math.max(0, gameState.player2.health);
if (gameState.gameOver) {
document.getElementById('currentTurn').textContent = `Player ${gameState.winner} Wins!`;
} else {
document.getElementById('currentTurn').textContent = `Player ${gameState.currentPlayer}`;
}
}
function gameLoop() {
// Clear canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw stars
ctx.fillStyle = 'rgba(255, 255, 255, 0.8)';
for (let i = 0; i < 100; i++) {
const x = (i * 37) % canvas.width;
const y = (i * 73) % canvas.height;
ctx.fillRect(x, y, 1, 1);
}
// Draw planets
gameState.planets.forEach(planet => planet.draw());
// Draw players
drawPlayer(gameState.player1, '#00ff00');
drawPlayer(gameState.player2, '#ff0000');
// Draw arrows
gameState.arrows.forEach(arrow => {
arrow.update();
arrow.draw();
});
// Remove inactive arrows
gameState.arrows = gameState.arrows.filter(arrow => arrow.active);
// Draw aim line
drawAimLine();
requestAnimationFrame(gameLoop);
}
// Event listeners
canvas.addEventListener('mousemove', (e) => {
const rect = canvas.getBoundingClientRect();
mouse.x = e.clientX - rect.left;
mouse.y = e.clientY - rect.top;
});
canvas.addEventListener('click', (e) => {
shoot();
});
document.addEventListener('keydown', (e) => {
if (e.code === 'Space') {
e.preventDefault();
initGame();
}
});
// Initialize and start game
initGame();
gameLoop();
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Space Archery</title>
<style>
body {
margin: 0;
padding: 0;
background: #000;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
font-family: Arial, sans-serif;
}
canvas {
border: 1px solid #333;
background: linear-gradient(45deg, #001122, #000033);
}
.ui {
position: absolute;
top: 10px;
left: 10px;
color: white;
font-size: 18px;
}
.controls {
position: absolute;
bottom: 10px;
left: 50%;
transform: translateX(-50%);
color: white;
text-align: center;
}
</style>
</head>
<body>
<canvas id="gameCanvas" width="1000" height="700"></canvas>
<div class="ui">
<div>Player 1 Health: <span id="p1Health">100</span></div>
<div>Player 2 Health: <span id="p2Health">100</span></div>
<div>Current Turn: <span id="currentTurn">Player 1</span></div>
</div>
<div class="controls">
<div>Mouse to aim, Click to shoot</div>
<div>Space to restart game</div>
</div>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// Game state
let gameState = {
planets: [],
player1: { x: 0, y: 0, health: 100, planetIndex: 0 },
player2: { x: 0, y: 0, health: 100, planetIndex: 0 },
currentPlayer: 1,
arrows: [],
gameOver: false,
winner: null,
canShoot: true
};
// Mouse state
let mouse = { x: 0, y: 0, down: false };
// Planet class
class Planet {
constructor(x, y, radius, mass, color) {
this.x = x;
this.y = y;
this.radius = radius;
this.mass = mass;
this.color = color;
}
draw() {
ctx.fillStyle = this.color;
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
ctx.fill();
// Add some shading
const gradient = ctx.createRadialGradient(
this.x - this.radius/3, this.y - this.radius/3, 0,
this.x, this.y, this.radius
);
gradient.addColorStop(0, 'rgba(255,255,255,0.3)');
gradient.addColorStop(1, 'rgba(0,0,0,0.3)');
ctx.fillStyle = gradient;
ctx.fill();
}
getGravityForce(x, y) {
const dx = this.x - x;
const dy = this.y - y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance < this.radius + 5) return { x: 0, y: 0 };
const force = (this.mass * 0.02) / (distance * distance);
const angle = Math.atan2(dy, dx);
return {
x: Math.cos(angle) * force,
y: Math.sin(angle) * force
};
}
}
// Arrow class
class Arrow {
constructor(x, y, vx, vy, shooter) {
this.x = x;
this.y = y;
this.vx = vx;
this.vy = vy;
this.trail = [];
this.active = true;
this.shooter = shooter; // 1 or 2
this.age = 0;
}
update() {
if (!this.active) return;
this.age++;
// Apply gravity from all planets
let totalForceX = 0;
let totalForceY = 0;
gameState.planets.forEach(planet => {
const force = planet.getGravityForce(this.x, this.y);
totalForceX += force.x;
totalForceY += force.y;
});
this.vx += totalForceX;
this.vy += totalForceY;
// Update position (slower movement)
this.x += this.vx * 0.8;
this.y += this.vy * 0.8;
// Add to trail
this.trail.push({ x: this.x, y: this.y });
if (this.trail.length > 15) {
this.trail.shift();
}
// Check bounds
if (this.x < -50 || this.x > canvas.width + 50 || this.y < -50 || this.y > canvas.height + 50) {
this.active = false;
this.checkTurnEnd();
}
// Check planet collisions
for (let planet of gameState.planets) {
const dx = this.x - planet.x;
const dy = this.y - planet.y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance < planet.radius - 5) {
this.active = false;
this.checkTurnEnd();
break;
}
}
// Check player collisions (only after arrow has traveled a bit)
if (this.age > 10) {
this.checkPlayerCollisions();
}
// Auto-deactivate after too long
if (this.age > 1000) {
this.active = false;
this.checkTurnEnd();
}
}
checkPlayerCollisions() {
// Check collision with player 1 (only if shot by player 2)
if (this.shooter === 2) {
const dx1 = this.x - gameState.player1.x;
const dy1 = this.y - gameState.player1.y;
const distance1 = Math.sqrt(dx1 * dx1 + dy1 * dy1);
if (distance1 < 12) {
gameState.player1.health -= 25;
this.active = false;
this.checkGameEnd();
this.checkTurnEnd();
return;
}
}
// Check collision with player 2 (only if shot by player 1)
if (this.shooter === 1) {
const dx2 = this.x - gameState.player2.x;
const dy2 = this.y - gameState.player2.y;
const distance2 = Math.sqrt(dx2 * dx2 + dy2 * dy2);
if (distance2 < 12) {
gameState.player2.health -= 25;
this.active = false;
this.checkGameEnd();
this.checkTurnEnd();
return;
}
}
}
checkGameEnd() {
if (gameState.player1.health <= 0) {
gameState.gameOver = true;
gameState.winner = 2;
} else if (gameState.player2.health <= 0) {
gameState.gameOver = true;
gameState.winner = 1;
}
updateUI();
}
checkTurnEnd() {
// Check if this was the last active arrow
setTimeout(() => {
const activeArrows = gameState.arrows.filter(arrow => arrow.active);
if (activeArrows.length === 0 && !gameState.gameOver) {
gameState.canShoot = true;
}
}, 100);
}
draw() {
if (!this.active) return;
// Draw trail
if (this.trail.length > 1) {
ctx.strokeStyle = 'rgba(255, 255, 100, 0.6)';
ctx.lineWidth = 2;
ctx.beginPath();
for (let i = 1; i < this.trail.length; i++) {
const alpha = (i / this.trail.length) * 0.6;
ctx.globalAlpha = alpha;
if (i === 1) {
ctx.moveTo(this.trail[i-1].x, this.trail[i-1].y);
}
ctx.lineTo(this.trail[i].x, this.trail[i].y);
}
ctx.stroke();
ctx.globalAlpha = 1;
}
// Draw arrow
ctx.fillStyle = '#ffff00';
ctx.strokeStyle = '#ffaa00';
ctx.lineWidth = 1;
ctx.beginPath();
ctx.arc(this.x, this.y, 4, 0, Math.PI * 2);
ctx.fill();
ctx.stroke();
}
}
function generatePlanets() {
const planets = [];
const colors = ['#ff6b6b', '#4ecdc4', '#45b7d1', '#96ceb4', '#ffeaa7', '#dda0dd', '#98d8c8'];
// Generate 5-8 random planets
const numPlanets = 5 + Math.floor(Math.random() * 4);
for (let i = 0; i < numPlanets; i++) {
let x, y, radius;
let validPosition = false;
let attempts = 0;
while (!validPosition && attempts < 50) {
radius = 30 + Math.random() * 60;
x = radius + Math.random() * (canvas.width - 2 * radius);
y = radius + Math.random() * (canvas.height - 2 * radius);
validPosition = true;
// Check distance from other planets
for (let j = 0; j < planets.length; j++) {
const dx = x - planets[j].x;
const dy = y - planets[j].y;
const distance = Math.sqrt(dx * dx + dy * dy);
const minDistance = radius + planets[j].radius + 80;
if (distance < minDistance) {
validPosition = false;
break;
}
}
attempts++;
}
if (validPosition) {
const mass = radius * 3;
const color = colors[Math.floor(Math.random() * colors.length)];
planets.push(new Planet(x, y, radius, mass, color));
}
}
return planets;
}
function initGame() {
gameState.planets = generatePlanets();
// Place players on different planets
if (gameState.planets.length >= 2) {
const planet1 = gameState.planets[0];
const planet2 = gameState.planets[gameState.planets.length - 1];
gameState.player1.x = planet1.x;
gameState.player1.y = planet1.y - planet1.radius - 15;
gameState.player1.health = 100;
gameState.player1.planetIndex = 0;
gameState.player2.x = planet2.x;
gameState.player2.y = planet2.y - planet2.radius - 15;
gameState.player2.health = 100;
gameState.player2.planetIndex = gameState.planets.length - 1;
}
gameState.currentPlayer = 1;
gameState.arrows = [];
gameState.gameOver = false;
gameState.winner = null;
gameState.canShoot = true;
updateUI();
}
function drawPlayer(player, color, isCurrentPlayer) {
// Draw player with glow effect if it's their turn
if (isCurrentPlayer && !gameState.gameOver) {
ctx.shadowColor = color;
ctx.shadowBlur = 15;
}
ctx.fillStyle = color;
ctx.beginPath();
ctx.arc(player.x, player.y, 10, 0, Math.PI * 2);
ctx.fill();
ctx.shadowBlur = 0;
// Draw health bar
const barWidth = 40;
const barHeight = 6;
const healthPercent = Math.max(0, player.health) / 100;
ctx.fillStyle = 'rgba(255, 0, 0, 0.8)';
ctx.fillRect(player.x - barWidth/2, player.y - 25, barWidth, barHeight);
ctx.fillStyle = 'rgba(0, 255, 0, 0.8)';
ctx.fillRect(player.x - barWidth/2, player.y - 25, barWidth * healthPercent, barHeight);
// Draw border around health bar
ctx.strokeStyle = 'white';
ctx.lineWidth = 1;
ctx.strokeRect(player.x - barWidth/2, player.y - 25, barWidth, barHeight);
}
function drawAimLine() {
if (gameState.gameOver || !gameState.canShoot) return;
const currentPlayerObj = gameState.currentPlayer === 1 ? gameState.player1 : gameState.player2;
const dx = mouse.x - currentPlayerObj.x;
const dy = mouse.y - currentPlayerObj.y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance > 0) {
const maxDistance = 120;
const clampedDistance = Math.min(distance, maxDistance);
const power = clampedDistance / maxDistance;
ctx.strokeStyle = `rgba(255, 255, 255, ${0.4 + power * 0.4})`;
ctx.lineWidth = 3;
ctx.setLineDash([8, 4]);
ctx.beginPath();
ctx.moveTo(currentPlayerObj.x, currentPlayerObj.y);
ctx.lineTo(
currentPlayerObj.x + (dx / distance) * clampedDistance,
currentPlayerObj.y + (dy / distance) * clampedDistance
);
ctx.stroke();
ctx.setLineDash([]);
}
}
function shoot() {
if (gameState.gameOver || !gameState.canShoot) return;
const currentPlayerObj = gameState.currentPlayer === 1 ? gameState.player1 : gameState.player2;
const dx = mouse.x - currentPlayerObj.x;
const dy = mouse.y - currentPlayerObj.y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance > 10) { // Minimum distance to shoot
const power = Math.min(distance / 120, 1) * 4; // Reduced power
const vx = (dx