JavaScript로 게임 만들기 - 11

Fantazindy·2022년 4월 15일
1

JSFightGame

목록 보기
12/12

마지막으로 디자인을 적용하자.
개인의 취향에 따라 html, css들만 간단히 바꿔주면 되겠다.

폰트는 google font에서 press start라는 폰트를 쓰기로 했다.

<head>
  <link rel="preconnect" href="https://fonts.googleapis.com" />
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
  <link
    href="https://fonts.googleapis.com/css2?family=Press+Start+2P&family=Rubik+Beastly&display=swap"
    rel="stylesheet"
  />
  <style>
    * {
      box-sizing: border-box;
      font-family: "Press Start 2P", cursive;
    }
  </style>
</head>

체력바 부분도 css를 좀 수정해주고

      <!-- player health -->
      <div
        style="
          position: relative;
          width: 100%;
          display: flex;
          justify-content: flex-end;
          border-top: 4px solid white;
          border-left: 4px solid white;
          border-bottom: 4px solid white;
          border-top-left-radius: 7px;
          border-bottom-left-radius: 7px;
        "
      >
        <div style="background-color: red; height: 30; width: 100%"></div>
        <div
          id="playerHealth"
          style="
            position: absolute;
            background: #818cf8;
            top: 0;
            right: 0;
            bottom: 0;
            width: 100%;
          "
        ></div>
      </div>
      <!-- enemy health -->
      <div
        style="
          position: relative;
          width: 100%;
          border-top: 4px solid white;
          border-right: 4px solid white;
          border-bottom: 4px solid white;
          border-top-right-radius: 7px;
          border-bottom-right-radius: 7px;
        "
      >
        <div style="background-color: red; height: 30"></div>
        <div
          id="enemyHealth"
          style="
            position: absolute;
            background: #818cf8;
            top: 0;
            right: 0;
            bottom: 0;
            left: 0;
          "
        ></div>

이 과정에서 현재체력과 최대체력이 구분되어있어 이런 문제가 발생하는데

health의 상위 div인 container에 있는 height값을 지워주면 해결된다.

이어서 타이머 부분도 수정했다.

      <div
        id="timer"
        style="
          background-color: black;
          width: 100px;
          height: 50px;
          flex-shrink: 0;
          display: flex;
          align-items: center;
          justify-content: center;
          color: white;
          border: 4px solid white;
          border-radius: 7px;
        "
      >
        10
      </div>

배경도 조금 수정이 필요해 보이는데 너무 선명해서 캐릭터가 잘 안보인다.

function animate() {
  window.requestAnimationFrame(animate);
  c.fillStyle = "black";
  c.fillRect(0, 0, canvas.width, canvas.height);
  background.update();
  shop.update();
  c.fillStyle = "rgba(255, 255, 255, 0.1)";
  c.fillRect(0, 0, canvas.width, canvas.height);
  player.update();
  enemy.update();

캐릭터를 그려주기 이전에 투명하게 배경을 살짝 깔아서 캐릭터를 좀 더 잘보이게 해주자

마지막으로 체력바가 감소할 떄 애니메이션을 주려고 하는데 gsap이라는 것을 사용하면 적용할 수 있다.
https://cdnjs.com/libraries/gsap/3.10.3
이곳에서 받을 수 있으며 버전에 따라 주소가 달라질 수 있다.

    <canvas></canvas>
  </div>

  <script
    src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.10.3/gsap.min.js"
    integrity="sha512-6zTDRWNxo8vI6JZYDCwhrJpg5icK3P4HNnW3czsO5Scb3lAoPDam+/wF3eog4hxcl0h44d0XlIcFkuoSaWHQ2g=="
    crossorigin="anonymous"
    referrerpolicy="no-referrer"
  ></script>
  <script src="js/classes.js"></script>
  <script src="js/utils.js"></script>
  <script src="index.js"></script>
</body>

제일 아래 스크립트에 추가하고 체력을 감소시키는 코드인

  //detect collision & player get hit
  if (
    rectangularCollision({ rectangle1: enemy, rectangle2: player }) &&
    enemy.isAttacking &&
    enemy.framesCurrent === 2
  ) {
    player.takeHit();
    enemy.isAttacking = false;
    // document.querySelector("#playerHealth").style.width = player.health + "%";
    console.log("enemy attack");
    gsap.to("#playerHealth", { width: player.health + "%" });
  }

이 부분에서 쿼리셀렉터 대신에 gsap을 사용해주면 된다.
위 코드는 플레이어 피격시의 코드이므로 반대의 경우인 enemy피격시에도 동일하게 적용해주면 된다.

profile
풀스택 개발자를 목표로 성장중입니다!

0개의 댓글