Make an HTML mobile friendly game like vampire survivors I can play in the browser.
- User
Make an HTML mobile friendly game like vampire survivors I can play in the browser.
- Assistant
Make an HTML mobile friendly game like vampire survivors I can play in the browser.
Vampire Survivors is an action-packed game where you navigate through hordes of enemies, surviving as long as possible. While replicating the entire game is complex, you can create a simplified version using HTML5 and JavaScript that runs in a mobile browser.
Below is a step-by-step guide to building a basic survival game:
Create an index.html file with the following content:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Survivor Game</title>
<style>
body { margin: 0; overflow: hidden; }
canvas { display: block; background: #000; }
</style>
</head>
<body>
<canvas id="gameCanvas"></canvas>
<script src="game.js"></script>
</body>
</html>
In a game.js file, initialize the canvas and its context:
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// Resize canvas to fit the window
function resizeCanvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
resizeCanvas();
window.addEventListener('resize', resizeCanvas);
Define the player's properties and drawing function:
const player = {
x: canvas.width / 2,
y: canvas.height / 2,
size: 20,
color: 'white',
speed: 5,
dx: 0,
dy: 0
};
function drawPlayer() {
ctx.fillStyle = player.color;
ctx.beginPath();
ctx.arc(player.x, player.y, player.size, 0, Math.PI * 2);
ctx.fill();
}
Implement touch controls for mobile devices:
let touchX = null;
let touchY = null;
canvas.addEventListener('touchstart', handleTouchStart);
canvas.addEventListener('touchmove', handleTouchMove);
canvas.addEventListener('touchend', handleTouchEnd);
function handleTouchStart(event) {
const touch = event.touches[0];
touchX = touch.clientX;
touchY = touch.clientY;
}
function handleTouchMove(event) {
const touch = event.touches[0];
const deltaX = touch.clientX - touchX;
const deltaY = touch.clientY - touchY;
touchX = touch.clientX;
touchY = touch.clientY;
player.x += deltaX;
player.y += deltaY;
}
function handleTouchEnd() {
touchX = null;
touchY = null;
}
Define enemy properties and behaviors:
const enemies = [];
function createEnemy() {
const size = 15;
const speed = 2;
let x, y;
// Spawn enemies from the edges
if (Math.random() < 0.5) {
x = Math.random() * canvas.width;
y = Math.random() < 0.5 ? -size : canvas.height + size;
} else {
x = Math.random() < 0.5 ? -size : canvas.width + size;
y = Math.random() * canvas.height;
}
const angle = Math.atan2(player.y - y, player.x - x);
enemies.push({
x,
y,
size,
speed,
dx: Math.cos(angle),
dy: Math.sin(angle),
color: 'red'
});
}
function drawEnemies() {
enemies.forEach(enemy => {
ctx.fillStyle = enemy.color;
ctx.beginPath();
ctx.arc(enemy.x, enemy.y, enemy.size, 0, Math.PI * 2);
ctx.fill();
});
}
function updateEnemies() {
enemies.forEach(enemy => {
enemy.x += enemy.dx * enemy.speed;
enemy.y += enemy.dy * enemy.speed;
});
}
Check for collisions between the player and enemies:
function detectCollisions() {
enemies.forEach((enemy, index) => {
const dist = Math.hypot(player.x - enemy.x, player.y - enemy.y);
if (dist < player.size + enemy.size) {
// Collision detected
gameOver();
}
// Remove enemies that move out of bounds
if (
enemy.x < -enemy.size || enemy.x > canvas.width + enemy.size ||
enemy.y < -enemy.size || enemy.y > canvas.height + enemy.size
) {
enemies.splice(index, 1);
}
});
}
Create the main game loop to update and render the game:
let gameRunning = true;
let enemySpawnInterval = 1000; // Spawn an enemy every second
let lastEnemySpawn = Date.now();
function gameLoop() {
if (!gameRunning) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawPlayer();
updateEnemies();
drawEnemies();
detectCollisions();
// Spawn enemies over time
if (Date.now() - lastEnemySpawn > enemySpawnInterval) {
createEnemy();
lastEnemySpawn = Date.now();
}
requestAnimationFrame(gameLoop);
}
// Start the game
gameLoop();
Define what happens when the game ends:
function gameOver() {
gameRunning = false;
ctx.fillStyle = 'white';
ctx.font = '48px sans-serif';
ctx.textAlign = 'center';
ctx.fillText('Game Over', canvas.width / 2, canvas.height / 2);
// Restart after a delay
setTimeout(() => {
resetGame();
gameRunning = true;
gameLoop();
}, 3000);
}
function resetGame() {
player.x = canvas.width / 2;
player.y = canvas.height / 2;
enemies.length = 0;
lastEnemySpawn = Date.now();
}
Ensure the game works well on mobile devices:
requestAnimationFrame for smooth animations.Open index.html on a mobile device to test the game. For further enhancement:
game.js CodeFor convenience, here's the full game.js file:
Save the Files:
index.html and game.js inside it.Test Locally:
For local testing, you might need to run a local server (due to browser security policies). You can use simple servers like:
python -m http.server in the terminal within the folder.npm install -g http-server and run http-server.Open in Mobile Browser:
http://localhost:8000) from your mobile device. Ensure your device is on the same network.You've now created a basic, mobile-friendly browser game reminiscent of Vampire Survivors. This foundation allows you to:
Feel free to ask if you need further clarification or assistance with additional features!
Not yet included in a published release.
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// Resize canvas to fit the window
function resizeCanvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
resizeCanvas();
window.addEventListener('resize', resizeCanvas);
// Player setup
const player = {
x: canvas.width / 2,
y: canvas.height / 2,
size: 20,
color: 'white',
speed: 5,
dx: 0,
dy: 0
};
function drawPlayer() {
ctx.fillStyle = player.color;
ctx.beginPath();
ctx.arc(player.x, player.y, player.size, 0, Math.PI * 2);
ctx.fill();
}
// Handle touch movement
let touchX = null;
let touchY = null;
canvas.addEventListener('touchstart', handleTouchStart);
canvas.addEventListener('touchmove', handleTouchMove);
canvas.addEventListener('touchend', handleTouchEnd);
function handleTouchStart(event) {
const touch = event.touches[0];
touchX = touch.clientX;
touchY = touch.clientY;
}
function handleTouchMove(event) {
const touch = event.touches[0];
const deltaX = touch.clientX - touchX;
const deltaY = touch.clientY - touchY;
touchX = touch.clientX;
touchY = touch.clientY;
player.x += deltaX;
player.y += deltaY;
// Keep player within bounds
player.x = Math.max(player.size, Math.min(canvas.width - player.size, player.x));
player.y = Math.max(player.size, Math.min(canvas.height - player.size, player.y));
}
function handleTouchEnd() {
touchX = null;
touchY = null;
}
// Enemies setup
const enemies = [];
function createEnemy() {
const size = 15;
const speed = 2;
let x, y;
// Spawn enemies from the edges
if (Math.random() < 0.5) {
x = Math.random() * canvas.width;
y = Math.random() < 0.5 ? -size : canvas.height + size;
} else {
x = Math.random() < 0.5 ? -size : canvas.width + size;
y = Math.random() * canvas.height;
}
const angle = Math.atan2(player.y - y, player.x - x);
enemies.push({
x,
y,
size,
speed,
dx: Math.cos(angle),
dy: Math.sin(angle),
color: 'red'
});
}
function drawEnemies() {
enemies.forEach(enemy => {
ctx.fillStyle = enemy.color;
ctx.beginPath();
ctx.arc(enemy.x, enemy.y, enemy.size, 0, Math.PI * 2);
ctx.fill();
});
}
function updateEnemies() {
enemies.forEach(enemy => {
enemy.x += enemy.dx * enemy.speed;
enemy.y += enemy.dy * enemy.speed;
});
}
// Collision detection
function detectCollisions() {
enemies.forEach((enemy, index) => {
const dist = Math.hypot(player.x - enemy.x, player.y - enemy.y);
if (dist < player.size + enemy.size) {
// Collision detected
gameOver();
}
// Remove enemies that move out of bounds
if (
enemy.x < -enemy.size || enemy.x > canvas.width + enemy.size ||
enemy.y < -enemy.size || enemy.y > canvas.height + enemy.size
) {
enemies.splice(index, 1);
}
});
}
// Game control
let gameRunning = true;
let enemySpawnInterval = 1000; // Spawn an enemy every second
let lastEnemySpawn = Date.now();
function gameLoop() {
if (!gameRunning) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawPlayer();
updateEnemies();
drawEnemies();
detectCollisions();
// Spawn enemies over time
if (Date.now() - lastEnemySpawn > enemySpawnInterval) {
createEnemy();
lastEnemySpawn = Date.now();
}
requestAnimationFrame(gameLoop);
}
function gameOver() {
gameRunning = false;
ctx.fillStyle = 'white';
ctx.font = '48px sans-serif';
ctx.textAlign = 'center';
ctx.fillText('Game Over', canvas.width / 2, canvas.height / 2);
// Restart after a delay
setTimeout(() => {
resetGame();
gameRunning = true;
gameLoop();
}, 3000);
}
function resetGame() {
player.x = canvas.width / 2;
player.y = canvas.height / 2;
enemies.length = 0;
lastEnemySpawn = Date.now();
}
// Start the game
gameLoop();