利用原生JS实现欢乐水果机小游戏


Posted in Javascript onApril 23, 2020

简介:

玩家点击某个押注物品则在该物品上下注。点击开始则游戏开始,如果没有下注则不能开始游戏。
游戏中的物品有八中,分别为:苹果、西瓜、柠檬、橙子、铃铛、77、双星、BAR。
在放行游戏区左右方为押注区,每种物品下方有加减号按钮,每次点
击加号增加一个筹码注金,反之减号就减少一个筹码注金
开始: 开始键
奖励:GOOD LUCK
由于这个时低配版的,我就没有按照原版的写进去,就是中了GOOD LUCK直接获得15分。
出于好耍,本人想起了小时候玩过的水果机,js也学了一会儿了,就想用它写一个简单的水果机玩玩,废话不多说,直接上代码,只有js有注释,html和css就没有注释了。
这是样式图

利用原生JS实现欢乐水果机小游戏

这里是html代码:

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Fruit</title>
 <link rel="stylesheet" href="Fruit.css" rel="external nofollow" >
 <script src="Fruit.js"></script>
</head>
<body>
<div id="content">
 <h2>欢乐水果机</h2>
 <div class="box">
  <div class="top">
   <div><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="imgs/orange.png" alt=""></a></div>
   <div><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="imgs/ld.png" alt=""></a></div>
   <div><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="imgs/25.png" alt=""></a></div>
   <div><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="imgs/50.png" alt=""></a></div>
   <div><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="imgs/apple.png" alt=""></a></div>
   <div><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="imgs/apple2.png" alt=""></a></div>
  </div>
  <div class="right">
   <div><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="imgs/lemon.png" alt=""></a></div>
   <div><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="imgs/watermelon.png" alt=""></a></div>
   <div><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="imgs/watermelon2.png" alt=""></a></div>
   <div><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="imgs/right.png" alt=""></a></div>
   <div><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="imgs/apple.png" alt=""></a></div>
   <div><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="imgs/orange2.png" alt=""></a></div>
  </div>
  <div class="bottom">
   <div><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="imgs/orange.png" alt=""></a></div>
   <div><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="imgs/ld.png" alt=""></a></div>
   <div><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="imgs/seven2.png" alt=""></a></div>
   <div><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="imgs/seven.png" alt=""></a></div>
   <div><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="imgs/apple.png" alt=""></a></div>
   <div><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="imgs/lemon2.png" alt=""></a></div>
  </div>
  <div class="left">
   <div><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="imgs/lemon.png" alt=""></a></div>
   <div><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="imgs/star.png" alt=""></a></div>
   <div><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="imgs/star2.png" alt=""></a></div>
   <div><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="imgs/left.png" alt=""></a></div>
   <div><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="imgs/apple.png" alt=""></a></div>
   <div><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="imgs/ld2.png" alt=""></a></div>
  </div>
  <button class="begin">开始</button>
  <div class="stake">
   <div class="stake_left">
    <div>
     <img src="imgs/apple.png" alt="">
     <button class="minus">-</button>
     <span>0</span>
     <button class="add">+</button>
    </div>
    <div>
     <img src="imgs/orange.png" alt="">
     <button class="minus">-</button>
     <span>0</span>
     <button class="add">+</button>
    </div>
    <div>
     <img src="imgs/lemon.png" alt="">
     <button class="minus">-</button>
     <span>0</span>
     <button class="add">+</button>
    </div>
    <div>
     <img src="imgs/ld2.png" alt="">
     <button class="minus">-</button>
     <span>0</span>
     <button class="add">+</button>
    </div>
   </div>
   <div class="stake_right">
    <div>
     <img src="imgs/watermelon.png" alt="">
     <button class="minus">-</button>
     <span>0</span>
     <button class="add">+</button>
    </div>
    <div>
     <img src="imgs/star.png" alt="">
     <button class="minus">-</button>
     <span>0</span>
     <button class="add">+</button>
    </div>
    <div>
     <img src="imgs/seven.png" alt="">
     <button class="minus">-</button>
     <span>0</span>
     <button class="add">+</button>
    </div>
    <div>
     <img src="imgs/50.png" alt="">
     <button class="minus">-</button>
     <span>0</span>
     <button class="add">+</button>
    </div>
   </div>
  </div>
  <span id="move"></span>
  <span id="getScore">中将金额<p>0</p></span>
  <span id="allScore">当前余额<p>0</p></span>
  <button id="change">充值¥100</button>
 </div>
</div>
</body>
</html>

加上一点简单的css布局

* {
 margin: 0;
 padding: 0;
 list-style: none;
 text-decoration: none;
}

img {
 display: block;
}

#content {
 width: 1200px;
 height: 1200px;
 margin: 0 auto;
 text-align: center;
}

.box {
 width: 1000px;
 height: 100%;
 margin: 0 auto;
 background-color: blue;
 position: relative;
}
.top{
 width: 600px;
 height: 100px;
 display: flex;
 position: absolute;
 top: 50px;
 left: 150px;
}
.right{
 width: 100px;
 height: 600px;
 position: absolute;
 top: 50px;
 right: 150px;
}
.bottom{
 width: 600px;
 height: 100px;
 display: flex;
 flex-direction: row-reverse;
 position: absolute;
 top: 650px;
 right: 150px;
}
.left{
 width: 100px;
 height: 600px;
 display: flex;
 flex-flow: column-reverse;
 position: absolute;
 top: 150px;
 left: 150px;
}
.stake>div>div{
 width: 100px;
 height: 150px;
 font-size: 20px;
}
.stake>div>div>img{
 border: 1px solid black;
}
.stake>div>div>button{
 width: 30px;
 height: 30px;
 font-size: 25px;
 background-color: #fff3fc;
 border: 1px solid black;
}
.stake_left{
 position: absolute;
 top: 100px;
 left: 25px;
}
.stake_right{
 position: absolute;
 top: 100px;
 right: 25px;
}
#move{
 display: block;
 width: 98px;
 height: 98px;
 border: 2px solid purple;
 position: absolute;
 top: 50px;
 left: 150px;
}
#getScore{
 display: block;
 width: 200px;
 height: 50px;
 font-size: 35px;
 position: absolute;
 top: 200px;
 left: 290px;
}
#getScore>p{
 display: block;
 width: 200px;
 height: 50px;
 border: 2px solid yellow;
}
#allScore{
 display: block;
 width: 200px;
 height: 50px;
 font-size: 35px;
 position: absolute;
 top: 200px;
 right: 290px;
}
#allScore>p{
 display: block;
 width: 200px;
 height: 50px;
 border: 2px solid yellow;
}
.begin{
 width: 120px;
 height: 30px;
 position: absolute;
 top: 700px;
 right: 15px;
 background-color: pink;
}
#change{
 width: 120px;
 height: 30px;
 position: absolute;
 top: 25px;
 right: 15px;
}

最后是js部分

window.addEventListener('load', function (ev) {
 //获取使用元素
 var begin = document.querySelector('.begin');
 var move = document.querySelector('#move');
 var getScore = document.querySelector('#getScore>p');
 var allScore = document.querySelector('#allScore>p');
 var change = document.querySelector('#change');
 var pours = document.querySelectorAll('.stake span');
 var add = document.querySelectorAll('.stake .add');
 var minus = document.querySelectorAll('.stake .minus');
 var timer = null;  //定义两个空对象null
 var time = null;
 var flag = null;  //定义一个flag,以此来判断是否下注
 var Score = 0;   //Score为当前余额
 //开始按钮点击事件
 begin.addEventListener('click', function (evt) {
  //每次点击开始的时候都从第一个开始
  move.style.left = 150 + 'px';
  move.style.top = 50 + 'px';
  clickMove(Score); //调用开始按钮的点击事件函数
 });

 //开始按钮的点击事件函数
 function clickMove(score) {
  //判断flag的值,for循环判断每一个水果是否有下注,如果都为下注,flag为false;
  //反之有任意一个水果有下注,flag都为true;
  for (var i = 0; i < pours.length; i++) {
   //先判断flag的值,再调用函数判断每个水果是否下注
   if (flag) {
    break;
   }
   decide_pour(pours[i]);
  }
  //点击开始按钮的条件
  if (score === 0) {//先判断是否有余额
   alert('您的余额已不足,请充值后再开始!');
  } else if (!flag) {//再判断flag的值,flag为false执行当前代码。
   alert('您未下注,请下注后再开始!');
  } else {//如果上诉都满足了,则可以执行开始按钮
   clearInterval(timer); //定时器先清后设。
   var randNum = Math.ceil(Math.random() * 200) * 24; //定义一个0-4800的随机整数
   var scoreNum = Math.floor(randNum / 20 % 24 + 1); //利用上随机数来获得移动过后得到的水果的索引号
   timer = setInterval(function () {  //定时器使得可以让move盒子移动起来
    var moveLeft = move.offsetLeft;
    var moveTop = move.offsetTop;
    if (moveTop === 50 && moveLeft <= 650) {
     move.style.left = moveLeft + 100 + 'px';
    } else if (moveLeft >= 650 && moveTop >= 50 && moveTop <= 550) {
     move.style.top = moveTop + 100 + 'px';
    } else if (moveTop >= 650 && moveLeft <= 750 && moveLeft >= 250) {
     move.style.left = moveLeft - 100 + 'px';
    } else if (moveLeft <= 150 && moveTop <= 750 && moveTop > 50) {
     move.style.top = moveTop - 100 + 'px';
    }
   }, 20);
   begin.disabled = true;      //move盒子停止移动后才能再次点击开始按钮
   for (var i = 0; i < add.length; i++) {  //利用for循环使得每个下注按钮和取消下注按钮开始之后就不能被点击
    add[i].disabled = true;     // move盒子停止移动后才能再次点击下注和取消下注按钮
    minus[i].disabled = true;
   }
   time = setTimeout(function () {  //结束的判断
    clearInterval(timer);     //当到达指定的随机数时间后,清除定时器timer,move盒子停止运动
    setTimeout(function () {
     switch (scoreNum) {
      case 5:
      case 11:
      case 17:
      case 23:
       score = reward(1) * 5;
       getScore.innerText = score;
       sum(score);
       break;
      case 6:
       score = reward(1) * 10;
       getScore.innerText = score;
       sum(score);
       break;
      case 1:
      case 13:
       score = reward(2) * 10;
       getScore.innerText = score;
       sum(score);
       break;
      case 12:
       score = reward(2) * 20;
       getScore.innerText = score;
       sum(score);
       break;
      case 7:
      case 19:
       score = reward(3) * 10;
       getScore.innerText = score;
       sum(score);
       break;
      case 18:
       score = reward(3) * 20;
       getScore.innerText = score;
       sum(score);
       break;
      case 2:
      case 14:
       score = reward(4) * 10;
       getScore.innerText = score;
       sum(score);
       break;
      case 24:
       score = reward(4) * 20;
       getScore.innerText = score;
       sum(score);
       break;
      case 8:
       score = reward(5) * 20;
       getScore.innerText = score;
       sum(score);
       break;
      case 9:
       score = reward(5) * 40;
       getScore.innerText = score;
       sum(score);
       break;
      case 20:
       score = reward(6) * 20;
       getScore.innerText = score;
       sum(score);
       break;
      case 21:
       score = reward(6) * 40;
       getScore.innerText = score;
       sum(score);
       break;
      case 16:
       score = reward(7) * 20;
       getScore.innerText = score;
       sum(score);
       break;
      case 15:
       score = reward(7) * 40;
       getScore.innerText = score;
       sum(score);
       break;
      case 10:
      case 22:
       score = 15;
       sum(score);
       break;
      case 3:
       score = reward(8) * 25;
       getScore.innerText = score;
       sum(score);
       break;
      case 4:
       score = reward(8) * 50;
       getScore.innerText = score;
       sum(score);
       break;
     }    //当停止运动后,算出当前获得的分数,并加在当前余额上
     for (var i = 0; i < add.length; i++) { //利用for循环对上一次下注的清零,然后下注和取消下注停止禁用
      add[i].previousElementSibling.innerText = 0;
      add[i].disabled = false;
      minus[i].disabled = false;
     }
     begin.disabled = false;    //开始按钮停止禁用
     score = 0;       //获得的分数清零
    }, 500);       //当move盒子停止运动后,0.5s后执行定时器time里的定时器
    flag = false;        //当move盒子停止运动后,flag的值重新变为false
   }, randNum);         //利用上面定义的随机数来控制盒子的随机运动

  }
 }

 //充值按钮,每点击一次,当前余额增加100
 change.addEventListener('click', function (evt) {
  allScore.innerText = Score += 100;
  //console.log(Score);
  Score = allScore.innerText - 0;  //string强制转换为number
 });
 //下注按钮
 for (var i = 0; i < add.length; i++) {
  decide_minus();   //调用函数判断取消下注是否可以点击,每点击一次下注和取消下注都要判断取消下注按钮是否可以点击
  //给每个下注按钮绑定点击事件
  add[i].addEventListener('click', function (evt) {
   //判断有无余额
   if (Score <= 0) {
    alert('您的余额已不足,请充值后再下注');
   } else {
    this.previousElementSibling.innerText++;
    decide_minus();
    Score--;
    allScore.innerText = Score;
   }

  });
  minus[i].addEventListener('click', function (evt) {
   this.nextElementSibling.innerText--;
   decide_minus();
   Score++;
   allScore.innerText = Score;
  });
 }

 //判断是否下注函数
 function decide_pour(obj) {
  var nums = obj.innerText - 0;
  nums !== 0 ? flag = true : flag = false;
 }

 //判断是否可以点击取消下注按钮,当未下注时,不能点击取消下注按钮
 function decide_minus() {
  for (var i = 0; i < add.length; i++) {
   pours[i].innerText - 0 <= 0 ? pours[i].previousElementSibling.disabled = true : pours[i].previousElementSibling.disabled = false
  }
 }

 //返回当前move盒子停在的水果的下注次数
 function reward(number) {
  return pours[number-1].innerText - 0;
 }

 //获得的分数加在当前余额上
 function sum(score) {
  allScore.innerText = Score += score;
 }
});

到此这篇关于利用原生JS实现欢乐水果机小游戏的文章就介绍到这了,更多相关js 水果机小游戏内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Javascript 相关文章推荐
JavaScript高级程序设计 阅读笔记(二十) js错误处理
Aug 14 Javascript
jquery的ajax()函数传值中文乱码解决方法介绍
Nov 08 Javascript
jquery中的事件处理详细介绍
Jun 24 Javascript
如何使用Javascript获取距今n天前的日期
Jul 08 Javascript
原生js做的手风琴效果的导航菜单
Nov 08 Javascript
基于JavaScript操作DOM常用的API小结
Dec 01 Javascript
vue双向数据绑定原理探究(附demo)
Jan 17 Javascript
js的三种继承方式详解
Jan 21 Javascript
JS使用JSON.parse(),JSON.stringify()实现对对象的深拷贝功能分析
Mar 06 Javascript
使用Node.js实现一个多人游戏服务器引擎
Mar 13 Javascript
浅谈express.js框架中间件(middleware)
Apr 07 Javascript
jquery实现简易验证插件封装
Sep 13 jQuery
JS eval代码快速解密实例解析
Apr 23 #Javascript
浅谈vue权限管理实现及流程
Apr 23 #Javascript
js实现简单的贪吃蛇游戏
Apr 23 #Javascript
Vue Cli3 打包配置并自动忽略console.log语句的方法
Apr 23 #Javascript
vue项目打包之开发环境和部署环境的实现
Apr 23 #Javascript
javascript设计模式 ? 模板方法模式原理与用法实例分析
Apr 23 #Javascript
谈谈我在vue-cli3中用预渲染遇到的坑
Apr 22 #Javascript
You might like
IIS下PHP连接数据库提示mysql undefined function mysql_connect()
2010/06/04 PHP
MongoDB在PHP中的常用操作小结
2014/02/20 PHP
详解WordPress中过滤链接与过滤SQL语句的方法
2015/12/18 PHP
twig里使用js变量的方法
2016/02/05 PHP
CI框架数据库查询缓存优化的方法
2016/11/21 PHP
zend框架实现支持sql server的操作方法
2016/12/08 PHP
YII2框架使用控制台命令的方法分析
2020/03/18 PHP
jQuery中的.bind()、.live()和.delegate()之间区别分析
2011/06/08 Javascript
使用npm发布Node.JS程序包教程
2015/03/02 Javascript
jQuery简单实现禁用右键菜单
2015/03/10 Javascript
JavaScript 七大技巧(二)
2015/12/13 Javascript
JS实现的验证身份证及获取地区功能示例
2017/01/16 Javascript
浅谈jQuery的bind和unbind事件(绑定和解绑事件)
2017/03/02 Javascript
JavaScript表单验证完美代码
2017/03/02 Javascript
ztree实现权限横向显示功能
2017/05/20 Javascript
swiper自定义分页器使用方法详解
2020/09/14 Javascript
node.js部署之启动后台运行forever的方法
2018/05/23 Javascript
vee-validate vue 2.0自定义表单验证的实例
2018/08/28 Javascript
傻瓜式vuex语法糖kiss-vuex整理
2018/12/21 Javascript
详解几十行代码实现一个vue的状态管理
2019/01/28 Javascript
微信小程序wx.request拦截器使用详解
2019/07/09 Javascript
解决VUE项目localhost端口服务器拒绝连接,只能用127.0.0.1的问题
2020/08/14 Javascript
解决vue安装less报错Failed to compile with 1 errors的问题
2020/10/22 Javascript
微信小程序用户登录和登录态维护的实现
2020/12/10 Javascript
原生JavaScript实现留言板
2021/01/10 Javascript
Python 找出英文单词列表(list)中最长单词链
2020/12/14 Python
利用CSS3的transition属性实现滑动效果
2015/08/05 HTML / CSS
值得收藏的HTML5资源(学习html5的朋友可以收藏下)
2010/07/20 HTML / CSS
韩国家庭购物网上商店:Nsmall
2017/05/07 全球购物
年度考核自我评价
2014/01/25 职场文书
教学改革实施方案
2014/03/31 职场文书
消防安全责任书
2014/04/14 职场文书
2015员工年度考核评语
2015/03/25 职场文书
2015年药店工作总结
2015/04/20 职场文书
2016年圣诞节义工活动总结
2016/04/01 职场文书
【海涛dota解说】一房久违的影魔魂守二连发
2022/04/01 DOTA