Simple HTML RPG Game - Play Now!
<!DOCTYPE html>
<html>
<head>
<title>Simple HTML RPG Game</title>
<script>
var playerName;
var health;
var weapon;
var enemies;
var enemyHealth;
var level;
<pre><code> function startGame() {
playerName = prompt('What is your character's name?');
health = 100;
weapon = 'fists';
enemies = [];
enemyHealth = 0;
level = 1;
alert('Welcome ' + playerName + '! Let's begin the adventure!');
gameLoop();
}
function gameLoop() {
if (health > 0) {
if (enemies.length == 0) {
alert('You have completed level ' + level + '!');
level++;
spawnEnemies();
} else {
alert('You have ' + health + ' health and are using ' + weapon);
var action = prompt('What would you like to do? (attack, flee)');
if (action == 'attack') {
attack();
} else if (action == 'flee') {
flee();
} else {
alert('Invalid command!');
}
}
} else {
alert('You have died!');
}
}
function spawnEnemies() {
var numEnemies = level*2;
for (var i=0; i<numEnemies; i++) {
enemies[i] = 'Enemy ' + (i+1);
enemyHealth = 100;
}
alert('You are now facing ' + numEnemies + ' enemies!');
}
function attack() {
enemyHealth = enemyHealth - (Math.floor(Math.random() * 10) + 1);
health = health - (Math.floor(Math.random() * 10) + 1);
alert('You attacked and dealt ' + (Math.floor(Math.random() * 10) + 1) + ' damage!');
if (enemyHealth <= 0) {
alert('You defeated an enemy!');
enemies.pop();
}
gameLoop();
}
function flee() {
if (Math.floor(Math.random() * 2) === 0) {
alert('You successfully fled!');
gameLoop();
} else {
alert('You failed to flee!');
health = health - (Math.floor(Math.random() * 10) + 1);
gameLoop();
}
}
</script>
</code></pre>
</head>
<body>
<h1>Simple HTML RPG Game</h1>
<button onclick="startGame()">Start Game</button>
</body>
</html>
原文地址: https://www.cveoy.top/t/topic/lnvD 著作权归作者所有。请勿转载和采集!