Javascript Mathematical Operations
Popcorn hack 2
How can we implement this into our game?
We can use this to interact with npcs in our game, such as a monster that you are attacking. Here is an example where the main character attack the npc: ```javascript let mainCharacterHealth = 100; let npcHealth = 80; // Main character attacks NPC let attackDamage = 15; npcHealth -= attackDamage; // Subtract damage from NPC’s health console.log(“NPC Health: “ + npcHealth); // Output: NPC Health: 65 // NPC counter-attacks the main character let npcAttackDamage = 12; mainCharacterHealth -= npcAttackDamage; // Subtract damage from main character’s health console.log(“Main Character Health: “ + mainCharacterHealth); // Output: Main Character Health: 88
Popcorn Hack Two
## Problem: Monster Attack Button When the button is clicked, a monster is attacked. However, the current code doesn’t track the monster’s health or reduce its health when attacked. Your task is to fill in the missing lines to give the monster 100 health initially and deal 10 damage to the monster with each button click.
Instructions:
- Set the monster’s health to 100 when the page loads.
- Each time the button is clicked, reduce the monster’s health by 10.
- Display the monster’s remaining health in the console after each click.
Hints:
- Look for the comment that says
// Set "monsterHealth" to 100and initialize the monster’s health there. - Find the placeholder
XXXXin the code and replace it with the correct operator to reduce the monster’s health. You will need to use an operator that subtracts 10 from the current health value.
```python
from IPython.display import display, HTML
html_code = “””
”””
Display the HTML and JavaScript in the notebook
display(HTML(html_code))