Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!DOCTYPE html>
<html lang="en">
  <head>
    <link rel="stylesheet" href="css/style.css" />
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <div class="game">
        <div class="game__left">
          <div class="game__left__weapons">
            <div id="weapondiv_enemy">
              <div id="Sword">
                <img
                  src="http://pngimg.com/uploads/sword/sword_PNG5526.png"
                  alt=""
                />
              </div>
              <div id="Axe">
                <img
                  src="http://pngimg.com/uploads/viking/viking_PNG37.png"
                  alt=""
                />
              </div>
              <div id="Dagger">
                <img
                  src="http://pngimg.com/uploads/dagger/dagger_PNG78.png"
                  alt=""
                />
              </div>
              <div id="Fireball">
                <img
                  src="https://gamepedia.cursecdn.com/skyrim_gamepedia/e/e2/Spell_Fireball.png"
                  alt=""
                />
              </div>
            </div>
          </div>
          <div class="game__left__potions">
            <div id="potiondiv_enemy">
              <div class="health_item">
                <img
                  src="https://cdn.shopify.com/s/files/1/0763/8841/products/latest_0bb9944b-4477-4ad3-87dc-d5db31c76406_300x300.png?v=1548256132"
                  alt=""
                />
              </div>
              <div class="health_item">
                <img
                  src="https://cdn.shopify.com/s/files/1/0763/8841/products/latest_0bb9944b-4477-4ad3-87dc-d5db31c76406_300x300.png?v=1548256132"
                  alt=""
                />
              </div>
              <div class="health_item">
                <img
                  src="https://cdn.shopify.com/s/files/1/0763/8841/products/latest_0bb9944b-4477-4ad3-87dc-d5db31c76406_300x300.png?v=1548256132"
                  alt=""
                />
              </div>
              <div class="mana_item">
                <img
                  src="https://gamepedia.cursecdn.com/orcsmustdie_gamepedia/thumb/1/1e/Mana_Potion_image.png/400px-Mana_Potion_image.png?version=1fc20294f0781ffafa9cdde1f520e5e8"
                  alt=""
                />
              </div>
              <div class="mana_item">
                <img
                  src="https://gamepedia.cursecdn.com/orcsmustdie_gamepedia/thumb/1/1e/Mana_Potion_image.png/400px-Mana_Potion_image.png?version=1fc20294f0781ffafa9cdde1f520e5e8"
                  alt=""
                />
              </div>
              <div class="mana_item">
                <img
                  src="https://gamepedia.cursecdn.com/orcsmustdie_gamepedia/thumb/1/1e/Mana_Potion_image.png/400px-Mana_Potion_image.png?version=1fc20294f0781ffafa9cdde1f520e5e8"
                  alt=""
                />
              </div>
            </div>
          </div>
        </div>
        <div class="game_middle">
          <div class="game_middle_healthbar_enemy">
            <div class="label">Fighter 500/500</div>
            <div class="healthbar"></div>
          </div>
          <div class="game_middle_manabar_enemy">
            <div class="label">Manapoints100/100</div>
            <div class="manabar"></div>
          </div>
          <div class="game_middle_fight_chat_enemy" id="fight_text">
            Actions Fighter
          </div>
          <div class="game_middle_fight_picture">
            <img
              src="https://www.trzcacak.rs/myfile/detail/326-3261941_dracoespectro-shadow-pictures-art-pictures-akira-shadow-of.png"
              alt="Monster.png"
            />
          </div>
          <div class="game_middle_fight_chat_fighter" id="fight_text_ghost">
            Actions Ghost
          </div>
          <div class="game_middle_healthbar_fighter">
            <div class="label">Ghost 500/500</div>
            <div class="healthbar"></div>
          </div>
        </div>
        <!-- <div class="game__right">
          <div class="game__right__weapons">
            <div id="weapondiv_fighter">
              <div>
                <img
                  src="http://pngimg.com/uploads/sword/sword_PNG5526.png"
                  alt=""
                />
              </div>
              <div>
                <img
                  src="http://pngimg.com/uploads/viking/viking_PNG37.png"
                  alt=""
                />
              </div>
              <div>
                <img
                  src="http://pngimg.com/uploads/dagger/dagger_PNG78.png"
                  alt=""
                />
              </div>
            </div>
          </div>
          <div class="game__right__potions">
            <div id="potiondiv_fighter">
              <div>
                <img
                  src="https://cdn.shopify.com/s/files/1/0763/8841/products/latest_0bb9944b-4477-4ad3-87dc-d5db31c76406_300x300.png?v=1548256132"
                  alt=""
                />
              </div>
              <div>
                <img
                  src="https://cdn.shopify.com/s/files/1/0763/8841/products/latest_0bb9944b-4477-4ad3-87dc-d5db31c76406_300x300.png?v=1548256132"
                  alt=""
                />
              </div>
              <div>
                <img
                  src="https://gamepedia.cursecdn.com/orcsmustdie_gamepedia/thumb/1/1e/Mana_Potion_image.png/400px-Mana_Potion_image.png?version=1fc20294f0781ffafa9cdde1f520e5e8"
                  alt=""
                />
              </div>
              <div>
                <img
                  src="https://gamepedia.cursecdn.com/orcsmustdie_gamepedia/thumb/1/1e/Mana_Potion_image.png/400px-Mana_Potion_image.png?version=1fc20294f0781ffafa9cdde1f520e5e8"
                  alt=""
                />
              </div>
              <div>
                <img
                  src="https://gamepedia.cursecdn.com/orcsmustdie_gamepedia/thumb/1/1e/Mana_Potion_image.png/400px-Mana_Potion_image.png?version=1fc20294f0781ffafa9cdde1f520e5e8"
                  alt=""
                />
              </div>
              <div>
                <img
                  src="https://gamepedia.cursecdn.com/orcsmustdie_gamepedia/thumb/1/1e/Mana_Potion_image.png/400px-Mana_Potion_image.png?version=1fc20294f0781ffafa9cdde1f520e5e8"
                  alt=""
                />
              </div>
            </div>
          </div>
        </div> -->
      </div>
    </div>
  </body>
  <script src="js/script.js"></script>
</html>

              
            
!

CSS

              
                *,
*::before,
*::after {
  margin: 0;
  padding: 0;
  box-sizing: inherit;
}

html {
  box-sizing: border-box;
  font-size: 62.5%; // 10px/16px = 62.5% -> 1rem = 10px
  font-family: "Lato", sans-serif;
}

body {
  font-weight: 300;
  line-height: 1.6;
}

// utility classes

$background-primary: #24303e;
$background-secondary: lighten($background-primary, 40%);
$background-tertiary: darken($background-primary, 10%);

@mixin potions {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 0.5rem;
  padding: 0.5rem 0.5rem;
  justify-content: space-around;
}

@mixin weapons {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 2fr 2fr;
  gap: 0.5rem;
  padding: 0.5rem 0.5rem;
  justify-content: space-around;
}

@mixin item {
  cursor: pointer;
  border-radius: 5px;
  border: 1px solid #fff;
  background: $background-secondary;
}

@mixin bar {
  // display: flex;
  // flex-direction: column;
  // justify-content: left;
  // align-items: center;
  margin-left: 0.5rem;
}

@mixin sidebar {
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  padding-bottom: 3rem;
}

a {
  border-radius: 3px;
  text-decoration: none;
  min-width: 20rem;
  padding: 0.5rem 1.5rem;
  color: #fff;
  margin-top: 1rem;
}

.healthbar {
  background: darkgreen;
  border: 1.5px solid black;
  width: 99%;
  height: 3rem;
}

.manabar {
  background: blue;
  border: 1.5px solid black;
  width: 100%;
  height: 3rem;
}

.label {
  color: white;
  text-align: left;
  font-size: 1.4rem;
  text-shadow: -1px 0 black, 0 1px black, 1px 0 black, 0 -1px black;
  width: 100%;
}

// The game
.game {
  background: $background-primary;
  text-align: center;
  font-size: 2rem;
  display: grid;
  row-gap: 0.5rem;
  column-gap: 0.5rem;
  grid-template-columns: 1fr 3fr;
  height: 120rem;
  width: 160rem;

  &__left {
    background: $background-primary;
    border-right: 2px solid white;
    @include sidebar;
  }

  &_middle_fight_picture {
    background: white !important;

    & > p {
      color: red;
      font-weight: bold;
    }

    & img {
      width: 35rem;

      &::after {
        content: "bla";
      }
    }
  }

  &__right {
    background: $background-primary;
    border-left: 2px solid white;
    @include sidebar;
  }
}

// Left bar
#weapondiv_enemy {
  @include weapons;

  & > div {
    @include item;

    & > img {
      width: 5rem;
    }
  }
}

#potiondiv_enemy {
  @include potions;

  & > div {
    @include item;

    & > img {
      width: 5rem;
    }
  }
}

// Right bar

// Left bar
#weapondiv_fighter {
  @include weapons;

  & > div {
    @include item;

    & > img {
      width: 5rem;
    }
  }
}

#potiondiv_fighter {
  @include potions;

  & > div {
    @include item;

    & > img {
      width: 5rem;
    }
  }
}

// Middle bar
.game_middle {
  display: grid;
  padding: 1rem 2rem;
  gap: 0.5rem;
  grid-template-areas:
    "healthbar_1 healthbar_1 healthbar_1"
    "manabar_1    manabar_1    manabar_1"
    "box1        picture       box2"
    "box1        picture       box2"
    "box1        picture       box2"
    "box1        picture       box2"
    "box1        picture       box2"
    "box1        picture       box2"
    "box1        picture       box2"
    "box1        picture       box2"
    "box1        picture       box2"
    "box1        picture       box2"
    "box1        picture       box2"
    "box1        picture       box2"
    "box1        picture       box2"
    "box1        picture       box2"
    "box1        picture       box2"
    "box1        picture       box2"
    "box1        picture       box2"
    "box1        picture       box2"
    "box1        picture       box2"
    "healthbar_2 healthbar_2 healthbar_2"
    "manabar_2    manabar_2    manabar_2"
    "buttons    buttons    buttons";

  &_healthbar_enemy {
    grid-area: healthbar_1;
    @include bar;
  }

  &_manabar_enemy {
    grid-area: manabar_1;
    @include bar;
  }

  &_fight_chat_enemy {
    grid-area: box1;
    background: $background-secondary;

    & > p {
      font-size: 1.7rem;
    }
  }

  &_fight_picture {
    grid-area: picture;
    color: white;
    background: $background-tertiary;
  }

  &_fight_chat_fighter {
    grid-area: box2;
    background: $background-secondary;

    & > p {
      font-size: 1.7rem;
    }
  }

  &_healthbar_fighter {
    grid-area: healthbar_2;
    @include bar;
  }

  &_manabar_fighter {
    grid-area: manabar_2;
    @include bar;
  }

  &_buttons {
    grid-area: buttons;
    background: $background-tertiary;
    border: 1px solid white;
    border-radius: 5px;

    &_start {
      display: flex;
      justify-content: space-around;

      & a {
        background: orangered;
      }
    }

    &_fights {
      display: flex;
      justify-content: space-around;

      & a {
        background: yellowgreen;
      }
    }
  }
}

              
            
!

JS

              
                function getRandomFloat(min, max) {
  return Math.random() * (max - min) + min;
}

class Player {
  constructor(health, mana, weapon, healthpotion = [], manapotion = []) {
    this.health = health;
    this.mana = mana;
    this.weapon = weapon;
    this.healthpotion = healthpotion;
    this.manapotion = manapotion;
  }

  attack(weapon) {
    if (
      weapon != "Fireball" &&
      weapon != "Sword" &&
      weapon != "Axe" &&
      weapon != "Dagger"
    ) {
      return;
    }

    document.querySelector(".game_middle_healthbar_fighter").remove();
    document.querySelector(".game_middle_manabar_enemy").remove();

    let value;
    let damage;
    if (weapon === "Sword") {
      value = getRandomFloat(1.25, 0.7);
    } else if (weapon === "Axe") {
      value = getRandomFloat(1.1, 0.95);
    } else if (weapon === "Dagger") {
      value = getRandomFloat(1.7, 0.4);
    } else {
      value = getRandomFloat(2.4, 0.75);
      this.mana = this.mana - 50;
    }

    damage = Math.floor(27 * value);
    ghost.health = ghost.health - damage;

    if (this.mana < 50) {
      document.querySelector("#Fireball").style.pointerEvents = "none";
      document.querySelector("#Fireball").style.opacity = 0.4;
    }

    if (this.mana <= 0) {
      this.mana = 0;
    } else {
    }

    if (ghost.health < 0) {
      ghost.health = 0;
      document.querySelector(".game__left__weapons").remove();
      document.querySelector(".game__left__potions").remove();

      let won = document.createElement("p");
      won.innerHTML = "You have defeated me. Princess Peach is awating you.";
      document.querySelector(".game_middle_fight_picture").appendChild(won);
    }

    let barcolor;
    if (ghost.health >= 400) {
      barcolor = "#006400";
    } else if (ghost.health <= 400 && ghost.health > 300) {
      barcolor = "#6de55a";
    } else if (ghost.health <= 300 && ghost.health > 200) {
      barcolor = "#f2bd59";
    } else if (ghost.health <= 200 && ghost.health > 100) {
      barcolor = "#f05252";
    } else if (ghost.health <= 100 && ghost.health > 0) {
      barcolor = "#bd0000";
    } else {
    }

    let div = document.createElement("div");
    div.className = "game_middle_healthbar_fighter";
    div.innerHTML = `<div class="label">Ghost ${ghost.health}/500</div>
    <div class="healthbar" style='width: ${ghost.health /
      5}%; background: ${barcolor}'></div>`;
    document.querySelector(".game_middle").appendChild(div);

    let div_mana = document.createElement("div");
    div_mana.className = "game_middle_manabar_enemy";
    div_mana.innerHTML = `<div class="label">Manapoints: ${this.mana}/100</div>
    <div class="healthbar" style='width: ${this.mana}%; background: blue;'></div>`;

    document.querySelector(".game_middle").appendChild(div_mana);

    // Add Text
    let action = document.createElement("p");
    action.innerHTML = `Attack with ${weapon} for ${damage}`;
    document.querySelector("#fight_text").appendChild(action);
  }

  take_potion(potion) {
    document.querySelector(".game_middle_healthbar_enemy").remove();
    document.querySelector(".game_middle_manabar_enemy").remove();
    document.querySelector("#potiondiv_enemy").innerHTML = "";

    if (potion === "health_item") {
      this.healthpotion.pop();
      this.health = this.health + 75;
      if (this.health > 500) {
        this.health = 500; // Health can´t be greater than 500
      }
    } else {
      this.manapotion.pop();
      this.mana = this.mana + 50;

      if (this.mana >= 50) {
        document.querySelector("#Fireball").style.pointerEvents = "auto";
        document.querySelector("#Fireball").style.opacity = 1;
      } else {
        document.querySelector("#Fireball").style.pointerEvents = "none";
        document.querySelector("#Fireball").style.opacity = 0.4;
      }

      if (this.mana > 100) {
        this.mana = 100; // Mana can´t be greater than 100
      }
    }

    this.healthpotion.forEach(function() {
      let div = document.createElement("div");
      div.className = "health_item";
      div.innerHTML = `<img 
                        src="https://cdn.shopify.com/s/files/1/0763/8841/products/latest_0bb9944b-4477-4ad3-87dc-d5db31c76406_300x300.png?v=1548256132"
                        alt=""
                        />`;
      document.querySelector("#potiondiv_enemy").appendChild(div);
    });

    this.manapotion.forEach(function() {
      let div = document.createElement("div");
      div.className = "mana_item";
      div.innerHTML = `<img
                          src="https://gamepedia.cursecdn.com/orcsmustdie_gamepedia/thumb/1/1e/Mana_Potion_image.png/400px-Mana_Potion_image.png?version=1fc20294f0781ffafa9cdde1f520e5e8"
                          alt=""
                          />`;
      document.querySelector("#potiondiv_enemy").appendChild(div);
    });

    let barcolor;
    if (this.health >= 400) {
      barcolor = "#006400";
    } else if (this.health <= 400 && this.health > 300) {
      barcolor = "#6de55a";
    } else if (this.health <= 300 && this.health > 200) {
      barcolor = "#f2bd59";
    } else if (this.health <= 200 && this.health > 100) {
      barcolor = "#f05252";
    } else if (this.health <= 100 && this.health > 0) {
      barcolor = "#bd0000";
    } else {
    }

    let div = document.createElement("div");
    div.className = "game_middle_healthbar_enemy";
    div.innerHTML = `<div class="label">Fighter ${this.health}/500</div>
    <div class="healthbar" style='width: ${this.health /
      5}%; background: ${barcolor}'></div>`;

    document.querySelector(".game_middle").appendChild(div);

    let div_mana = document.createElement("div");
    div_mana.className = "game_middle_manabar_enemy";
    div_mana.innerHTML = `<div class="label">Manapoints: ${this.mana}/100</div>
    <div class="healthbar" style='width: ${this.mana}%; background: blue;'></div>`;

    document.querySelector(".game_middle").appendChild(div_mana);

    // Add Text
    let add;
    if (potion === "health_item") {
      add = "Health Potion";
    } else {
      add = "Mana Potion";
    }
    let action = document.createElement("p");
    action.innerHTML = `Took a ${add}`;
    document.querySelector("#fight_text").appendChild(action);
  }
}

let fighter = new Player(
  500,
  100,
  "Sword",
  ["Health", "Health", "Health"],
  ["Mana", "Mana", "Mana"]
);

let ghost = new Player(
  500,
  100,
  "Sword",
  ["Health", "Health", "Health"],
  ["Mana", "Mana", "Mana"]
);

document
  .querySelector("#potiondiv_enemy")
  .addEventListener("click", function(e) {
    var elem;
    if (e.target.tagName === "IMG") {
      elem = e.target.parentNode.className;
    } else {
      elem = e.target.className;
    }
    fighter.take_potion(elem);
    ghostattack();
  });

document
  .querySelector("#weapondiv_enemy")
  .addEventListener("click", function(e) {
    var elem;
    if (e.target.tagName === "IMG") {
      elem = e.target.parentNode.id;
    } else {
      elem = e.target.id;
    }
    fighter.attack(elem);
    ghostattack();
  });

function ghostattack() {
  document.querySelector(".game_middle_healthbar_enemy").remove();

  let value = getRandomFloat(1.5, 0.75);
  let damage = Math.floor(30 * value);
  fighter.health = fighter.health - damage;

  if (fighter.health < 0) {
    fighter.health = 0;
  }

  let barcolor;
  if (fighter.health >= 400) {
    barcolor = "#006400";
  } else if (fighter.health <= 400 && fighter.health > 300) {
    barcolor = "#6de55a";
  } else if (fighter.health <= 300 && fighter.health > 200) {
    barcolor = "#f2bd59";
  } else if (fighter.health <= 200 && fighter.health > 100) {
    barcolor = "#f05252";
  } else if (fighter.health <= 100 && fighter.health > 0) {
    barcolor = "#bd0000";
  } else {
  }

  let div = document.createElement("div");
  div.className = "game_middle_healthbar_enemy";
  div.innerHTML = `<div class="label">Fighter ${fighter.health}/500</div>
    <div class="healthbar" style='width: ${fighter.health /
      5}%; background: ${barcolor}'></div>`;

  document.querySelector(".game_middle").appendChild(div);

  let action = document.createElement("p");
  action.innerHTML = `Ghost made ${damage} damage`;
  document.querySelector("#fight_text_ghost").appendChild(action);

  if (fighter.health === 0) {
    document.querySelector(".game__left__weapons").remove();
    document.querySelector(".game__left__potions").remove();

    let won = document.createElement("p");
    won.innerHTML = "You lost the fight. Princess Peach is forever lost";
    document.querySelector(".game_middle_fight_picture").appendChild(won);
  }
}

              
            
!
999px

Console