index.html 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <title> Disaster Game </title>
  2. <script>
  3. var userHealth = 100;
  4. var enemyHealth = 100;
  5. //Function to damage the players
  6. function damagePlayer(player, amount) {
  7. player.health -= amount;
  8. }
  9. //Function to display the health of the players
  10. function displayPlayerHealth() {
  11. document.getElementById("userHealth").innerHTML = "Your Health: " + userHealth + "<br>";
  12. document.getElementById("enemyHealth").innerHTML = "Enemy Health: " + enemyHealth;
  13. }
  14. //Function to start the game
  15. function startGame() {
  16. document.getElementById("game").style.visibility = "visible";
  17. document.getElementById("start").style.visibility = "hidden";
  18. }
  19. //Function to attack the enemy
  20. function attack() {
  21. var userAttack = Math.floor(Math.random() * 10) + 1;
  22. enemyHealth -= userAttack;
  23. document.getElementById("status").innerHTML = "You hit the enemy for " + userAttack + " damage."
  24. //Display enemy health
  25. displayPlayerHealth()
  26. //Check if enemy health is 0
  27. if (enemyHealth <= 0) {
  28. alert("You win!")
  29. resetGame()
  30. } else {
  31. //Enemy attacks
  32. var enemyAttack = Math.floor(Math.random() * 8) + 1
  33. userHealth -= enemyAttack;
  34. document.getElementById("status").innerHTML += "<br>" + "The enemy hits you for " + enemyAttack + " damage."
  35. //Display user health
  36. displayPlayerHealth()
  37. //Check if user health is 0
  38. if (userHealth <= 0) {
  39. alert("You lose!")
  40. resetGame()
  41. }
  42. }
  43. }
  44. //Function to reset the game
  45. function resetGame() {
  46. userHealth = 100
  47. enemyHealth = 100
  48. document.getElementById("game").style.visibility = "hidden";
  49. document.getElementById("start").style.visibility = "visible";
  50. document.getElementById("status").innerHTML = "";
  51. }
  52. </script>
  53. <h1> Disaster Game </h1>
  54. <div id="start">
  55. <button onclick="startGame()"> Start the Game </button>
  56. </div>
  57. <div id="game" style="visibility:hidden;">
  58. <div id="userHealth"> Your Health: 100 </div>
  59. <div id="enemyHealth"> Enemy Health: 100 </div>
  60. <button onclick="attack()"> Attack! </button>
  61. <div id="status"></div>
  62. </div>