[java] Harry Potter

Viewer

copydownloadembedprintName: Harry Potter
  1. // Harry Potter Video Game
  2.  
  3. // Define variables for player character
  4. let playerCharacter = {
  5.   name: "Harry Potter",
  6.   house: "Gryffindor",
  7.   health: 100,
  8.   magic: 100,
  9.   spells: ["Expelliarmus""Stupefy""Expecto Patronum"],
  10.   wand: {
  11.     wood: "Holly",
  12.     core: "Phoenix Feather",
  13.     length: 11.5
  14.   }
  15. };
  16.  
  17. // Define variables for enemy character
  18. let enemyCharacter = {
  19.   name: "Voldemort",
  20.   health: 100,
  21.   magic: 100,
  22.   spells: ["Avada Kedavra""Crucio""Imperio"]
  23. };
  24.  
  25. // Define variables for teachers
  26. let teachers = [
  27.   {
  28.     name: "Albus Dumbledore",
  29.     house: "Gryffindor",
  30.     health: 150,
  31.     magic: 200,
  32.     spells: ["Fawkes""Petrificus Totalus""Expecto Patronum"],
  33.     wand: {
  34.       wood: "Elder",
  35.       core: "Thestral Tail Hair",
  36.       length: 15
  37.     }
  38.   },
  39.   {
  40.     name: "Severus Snape",
  41.     house: "Slytherin",
  42.     health: 150,
  43.     magic: 200,
  44.     spells: ["Sectumsempra""Expulso""Morsmordre"],
  45.     wand: {
  46.       wood: "Yew",
  47.       core: "Dragon Heartstring",
  48.       length: 13
  49.     }
  50.   }
  51. ];
  52.  
  53. // Define variables for students
  54. let students = [
  55.   {
  56.     name: "Hermione Granger",
  57.     house: "Gryffindor",
  58.     health: 120,
  59.     magic: 150,
  60.     spells: ["Lumos""Wingardium Leviosa""Alohomora"],
  61.     wand: {
  62.       wood: "Vine",
  63.       core: "Dragon Heartstring",
  64.       length: 10
  65.     }
  66.   },
  67.   {
  68.     name: "Draco Malfoy",
  69.     house: "Slytherin",
  70.     health: 120,
  71.     magic: 150,
  72.     spells: ["Expelliarmus""Crucio""Imperio"],
  73.     wand: {
  74.       wood: "Hawthorn",
  75.       core: "Unicorn Hair",
  76.       length: 12
  77.     }
  78.   }
  79. ];
  80.  
  81. // Define function for player attack
  82. function playerAttack(spell) {
  83.   let damage = Math.floor(Math.random() * 10) + 1;
  84.   if (spell === "Expelliarmus") {
  85.     damage *= 2;
  86.     enemyCharacter.health -= damage;
  87.     console.log(`${playerCharacter.name} used ${spell} and dealt ${damage} damage!`);
  88.   } else if (spell === "Stupefy") {
  89.     enemyCharacter.health -= damage;
  90.     console.log(`${playerCharacter.name} used ${spell} and dealt ${damage} damage!`);
  91.   } else if (spell === "Expecto Patronum") {
  92.     playerCharacter.magic -= 10;
  93.     console.log(`${playerCharacter.name} used ${spell} and gained 10 magic points!`);
  94.   } else {
  95.     console.log("Invalid spell! Try again.");
  96.   }
  97. }
  98.  
  99. // Define function for enemy attack
  100. function enemyAttack() {
  101.   let spell = enemyCharacter.spells[Math.floor(Math.random() * enemyCharacter.spells.length)];
  102.   let damage = Math.floor(Math.random() * 10) + 1;
  103.   if (spell === "Avada Kedavra") {
  104.     damage *= 2;
  105.     playerCharacter.health -= damage;
  106.     console.log(`${enemyCharacter.name} used ${spell} and dealt ${damage} damage!`);
  107.   } else if (spell === "Crucio") {
  108.     playerCharacter.health -= damage;
  109.     console.log(`${enemyCharacter.name} used ${spell} and dealt ${damage} damage!`);
  110.   } else if (spell === "Imperio") {
  111.     playerCharacter.magic -= 10;
  112.     console.log(`${enemyCharacter.name} used ${spell} and gained 10 magic points!`);
  113.   } else {
  114.     console.log("Invalid spell! Try again.");
  115.   }
  116. }
  117.  
  118. // Define function for game loop
  119. function gameLoop() {
  120.   console.log(`${playerCharacter.name} (${playerCharacter.house}) vs ${enemyCharacter.name}`);
  121.   while (playerCharacter.health > 0 && enemyCharacter.health > 0) {
  122.     let playerSpell = playerCharacter.spells[Math.floor(Math.random() * playerCharacter.spells.length)];
  123.     playerAttack(playerSpell);
  124.     enemyAttack();
  125.     console.log(`${playerCharacter.name} health: ${playerCharacter.health}, magic: ${playerCharacter.magic}`);
  126.     console.log(`${enemyCharacter.name} health: ${enemyCharacter.health}, magic: ${enemyCharacter.magic}`);
  127.   }
  128.   if (playerCharacter.health <= 0) {
  129.     console.log(`${enemyCharacter.name} wins!`);
  130.   } else {
  131.     console.log(`${playerCharacter.name} wins!`);
  132.   }
  133. }
  134.  
  135. // Call game loop function
  136. gameLoop();

Editor

You can edit this paste and save as new:


File Description
  • Harry Potter
  • Paste Code
  • 07 May-2024
  • 3.82 Kb
You can Share it: