FCC小游戏——老虎机

FCC学习过程中遇到的一个小游戏,记录一下

关于

  • 这是一个简陋的js游戏(甚至都没有做动态效果)
  • 代码取自FCC学习过程
  • 略微优化代码

游戏截图

老虎机.png

演示地址

https://codepen.io/websyn/full/EzyeMm

代码

<!doctype html>
<html lang="en">

<head>
  <title>老虎机</title>
  <!-- Required meta tags -->
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <style>
    h1 {
      margin-top: 1em;
      text-align: center;
    }

    .slot>img {
      margin: 0 !important;
      height: 71px;
      width: 50px;
    }

    .container {
      background-color: #4a2b0f;
      height: 420px;
      width: 260px;
      margin: auto;
      padding: 20px;
      border-radius: 4px;
    }

    .header {
      border: 2px solid #fff;
      border-radius: 4px;
      height: 60px;
      margin: 14px auto;
      background-color: #457f86
    }

    .header h2 {
      height: 30px;
      margin: auto;
    }

    .header h2 {
      font-size: 14px;
      margin: 0 0;
      padding: 0;
      color: #fff;
      text-align: center;
    }

    .slots {
      display: flex;
      background-color: #457f86;
      border-radius: 6px;
      border: 2px solid #fff;
    }

    .slot {
      flex: 1 0 auto;
      background: white;
      height: 75px;
      width: 50px;
      margin: 8px;
      border: 2px solid #215f1e;
      border-radius: 4px;
      text-align: center;
    }

    .go {
      width: 100%;
      color: #fff;
      background-color: #457f86;
      border: 2px solid #fff;
      border-radius: 2px;
      box-sizing: none;
      outline: none !important;
    }

    .foot {
      height: 150px;
      background-color: 457f86;
      border: 2px solid #fff;
    }

    .logger {
      color: white;
      margin: 10px;
    }

    .outset {
      -webkit-box-shadow: 0px 0px 15px -2px rgba(0, 0, 0, 0.75);
      -moz-box-shadow: 0px 0px 15px -2px rgba(0, 0, 0, 0.75);
      box-shadow: 0px 0px 15px -2px rgba(0, 0, 0, 0.75);
    }

    .inset {
      -webkit-box-shadow: inset 0px 0px 15px -2px rgba(0, 0, 0, 0.75);
      -moz-box-shadow: inset 0px 0px 15px -2px rgba(0, 0, 0, 0.75);
      box-shadow: inset 0px 0px 15px -2px rgba(0, 0, 0, 0.75);
    }
  </style>
</head>

<body>
  <div>
    <h1>FCC模拟老虎机</h1>
    <div class='container inset'>
      <div class='header inset'>
        <img src='https://www.freecodecamp.cn/images/freecodecamp_logo.svg'
          alt='learn to code JavaScript at Free Code Camp logo' class='img-responsive nav-logo'>
        <h2>FCC Slot Machine</h2>
      </div>
      <div class='slots inset'>
        <div class='slot inset'>

        </div>
        <div class='slot inset'>

        </div>
        <div class='slot inset'>

        </div>
      </div>
      <br />
      <div class='outset'>
        <button class='go inset'>
          开始
        </button>
      </div>
      <br />
      <div class='foot inset'>
        <span class='logger'></span>
      </div>
    </div>
  </div>

  <!-- Optional JavaScript -->
  <!-- jQuery first, then Popper.js, then Bootstrap JS -->
  <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
    integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous">
  </script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"
    integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous">
  </script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
    integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous">
  </script>
  <script>
    function runSlots() {
      var slotOne;
      var slotTwo;
      var slotThree;

      var images = ["https://s2.ax1x.com/2019/05/12/E4imGQ.png", "https://s2.ax1x.com/2019/05/12/E4iMMn.png",
        "https://s2.ax1x.com/2019/05/12/E4i3ZV.png"
      ];

      slotOne = Math.floor(Math.random() * (3 - 1 + 1)) + 1;
      slotTwo = Math.floor(Math.random() * (3 - 1 + 1)) + 1;
      slotThree = Math.floor(Math.random() * (3 - 1 + 1)) + 1;

      $($('.slot')[0]).html('<img src = "' + images[slotOne - 1] + '">');
      $($('.slot')[1]).html('<img src = "' + images[slotTwo - 1] + '">');
      $($('.slot')[2]).html('<img src = "' + images[slotThree - 1] + '">');

      if (slotOne === slotTwo && slotTwo === slotThree) {
        $('.logger').html(" 你赢了!");
        return null;
      }

      if (slotOne !== undefined && slotTwo !== undefined && slotThree !== undefined) {
        $(".logger").html(slotOne + " " + slotTwo + " " + slotThree);
      }

      $('.logger').append(" 再接再厉 ");

      return [slotOne, slotTwo, slotThree];
    }

    $(document).ready(function () {
      $('.go').click(function () {
        runSlots();
      });
    });
  </script>
</body>

</html>
最后修改:2019 年 09 月 19 日 12 : 15 AM
如果觉得我的文章对你有用,请随意赞赏

1 条评论

  1. 罗木、

    厉害了!

发表评论