js创建的石头剪刀布在线游戏源码

所属分类: 网页特效-抽奖&游戏    2024-03-03 10:52:06

js创建的石头剪刀布在线游戏源码 ie兼容6
反馈问题  查看演示  登录后下载 温馨提示
登录会员即可享受免费下载
 我要建站

js创建的石头剪刀布在线游戏源码(共6个文件)

    • script.js
    • index.html
    • style.css

使用方法

// Get  to DOM elements
const gameContainer = document.querySelector(".container"),
  userResult = document.querySelector(".user_result img"),
  cpuResult = document.querySelector(".cpu_result img"),
  result = document.querySelector(".result"),
  optionImages = document.querySelectorAll(".option_image");

// Loop through each option image element
optionImages.forEach((image, index) => {
  image.addEventListener("click", (e) => {
    image.classList.add("active");

    userResult.src = cpuResult.src = "images/rock.png";
    result.textContent = "稍候...";

    // Loop through each option image again
    optionImages.forEach((image2, index2) => {
      // If the current index doesn't match the clicked index
      // Remove the "active" class from the other option images
      index !== index2 && image2.classList.remove("active");
    });

    gameContainer.classList.add("start");

    // Set a timeout to delay the result calculation
    let time = setTimeout(() => {
      gameContainer.classList.remove("start");

      // Get the source of the clicked option image
      let imageSrc = e.target.querySelector("img").src;
      // Set the user image to the clicked option image
      userResult.src = imageSrc;

      // Generate a random number between 0 and 2
      let randomNumber = Math.floor(Math.random() * 3);
      // Create an array of CPU image options
      let cpuImages = ["images/rock.png", "images/paper.png", "images/scissors.png"];
      // Set the CPU image to a random option from the array
      cpuResult.src = cpuImages[randomNumber];

      // Assign a letter value to the CPU option (R for rock, P for paper, S for scissors)
      let cpuValue = ["R", "P", "S"][randomNumber];
      // Assign a letter value to the clicked option (based on index)
      let userValue = ["R", "P", "S"][index];

      // Create an object with all possible outcomes
      let outcomes = {
        RR: "Draw",
        RP: "电脑",
        RS: "你",
        PP: "Draw",
        PR: "你",
        PS: "电脑",
        SS: "Draw",
        SR: "电脑",
        SP: "你",
      };

      // Look up the outcome value based on user and CPU options
      let outComeValue = outcomes[userValue + cpuValue];

      // Display the result
      result.textContent = userValue === cpuValue ? "不分胜负" : `${outComeValue} 赢了!!`;
    }, 2500);
  });
});

站长提示:
1. 平台上所有素材资源,需注册登录会员方能正常下载。
2. 会员用户积极反馈网站、素材资源BUG或错误问题,每次奖励20K币
3. PHP源码类素材,如需协助安装调试,或你有二次开发需求,可联系苦力吧客服。
4. 付费素材资源,需充值后方能下载,如有任何疑问可直接联系苦力吧客服
相关资源 / 抽奖&游戏

html5版连连看游戏源码

带积分等级记录,还可以刷新等功能,有能力的大佬可以做二次开发。
  抽奖&游戏
 173  

HTML5俄罗斯方块网页小游戏源码

一款响应式的俄罗斯方块游戏代码,可自定义设置自动播放、控制游戏速度、区域宽度等。
  抽奖&游戏
 56  

javascript弹珠打砖块2D网页小游戏源码

一款弹珠击打砖块小游戏源码,使用了砖块、圆球和球拍渲染画布,球从墙壁、砖块和球拍上反弹,目的是打碎砖块。可以使用键盘或鼠标控制移动球拍,带得分统计和生命值。
  抽奖&游戏
 361  

jquery大转盘抽奖特效zepto代码

大转盘抽奖分为三种状态:活动未开始(no-start)、活动进行中(start)、活动结束(completed),可通过切换class进行切换状态,js会根据这3个class进行匹配状态
  抽奖&游戏
 153  

评论数(0) 回复有机会获得K币 用户协议

^_^ 还没有人评论,快来抢个沙发!
😀
  • 😀
  • 😊
  • 😂
  • 😍
  • 😑
  • 😷
  • 😵
  • 😛
  • 😣
  • 😱
  • 😋
  • 😎
  • 😵
  • 😕
  • 😶
  • 😚
  • 😜
  • 😭
发表评论