利用原生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 相关文章推荐
JQuery实现简单验证码提示解决方案
Dec 20 Javascript
简单漂亮的js弹窗可自由拖拽且兼容大部分浏览器
Oct 22 Javascript
js中substr,substring,indexOf,lastIndexOf的用法小结
Dec 27 Javascript
window.print打印指定div指定网页指定区域的方法
Aug 04 Javascript
Javascript日期格式化format函数的使用方法
Aug 30 Javascript
微信开发 微信授权详解
Oct 21 Javascript
vue-router:嵌套路由的使用方法
Feb 21 Javascript
解决vue attr取不到属性值的问题
Sep 18 Javascript
微信小程序实现点赞、取消点赞功能
Nov 02 Javascript
js实现拖拽元素选择和删除
Aug 25 Javascript
vue 导出文件,携带请求头token操作
Sep 10 Javascript
vite+vue3.0+ts+element-plus快速搭建项目的实现
Jun 24 Vue.js
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
PHP中for循环语句的几种变型
2007/03/16 PHP
php debug 安装技巧
2011/04/30 PHP
ThinkPHP入口文件设置及相关注意事项分析
2014/12/05 PHP
PHP实现的购物车类实例
2015/06/17 PHP
PHP正则之正向预查与反向预查讲解与实例
2020/04/06 PHP
用js计算页面执行时间的函数
2006/12/07 Javascript
javascript hashtable实现代码
2009/10/13 Javascript
关于COOKIE个数与大小的问题
2011/01/17 Javascript
jquery.validate分组验证代码
2011/03/17 Javascript
JQuery中判断一个元素下面是否有内容或者有某个标签的判断代码
2012/02/02 Javascript
jquery实现的一个导航滚动效果具体代码
2013/05/27 Javascript
JavaScript设计模式之工厂方法模式介绍
2014/12/28 Javascript
深入理解Node.js的HTTP模块
2016/10/12 Javascript
利用JQuery阻止事件冒泡
2016/12/01 Javascript
NodeJS实现微信公众号关注后自动回复功能
2017/05/31 NodeJs
Vue 全局loading组件实例详解
2018/05/29 Javascript
使用vue-infinite-scroll实现无限滚动效果
2018/06/22 Javascript
微信小程序登录态和检验注册过没的app.js写法
2019/05/22 Javascript
JS实现放烟花效果
2020/03/10 Javascript
vue实现导航菜单和编辑文本的示例代码
2020/07/04 Javascript
python使用PythonMagick将jpg图片转换成ico图片的方法
2015/03/26 Python
Python3 读、写Excel文件的操作方法
2018/10/20 Python
8段用于数据清洗Python代码(小结)
2019/10/31 Python
详解pandas绘制矩阵散点图(scatter_matrix)的方法
2020/04/23 Python
Python用类实现扑克牌发牌的示例代码
2020/06/01 Python
python PyAUtoGUI库实现自动化控制鼠标键盘
2020/09/09 Python
python 三种方法实现对Excel表格的读写
2020/11/19 Python
html5 canvas绘制矩形和圆形的实例代码
2016/06/16 HTML / CSS
canvas 基础之图像处理的使用
2020/04/10 HTML / CSS
Stefania Mode英国:奢华设计师和时尚服装
2017/10/23 全球购物
大学生毕业自我鉴定范文
2013/09/19 职场文书
优秀体育委员自荐书
2014/01/31 职场文书
市级三好学生事迹材料
2014/08/27 职场文书
2015年党员个人剖析材料
2014/12/18 职场文书
2015年语言文字工作总结
2015/07/23 职场文书
关于mysql中时间日期类型和字符串类型的选择
2021/11/27 MySQL