JavaScript编写猜拳游戏

 更新时间:2021年11月01日 17:16:31   作者:帝国吾爱  
这篇文章主要为大家详细介绍了JavaScript编写猜拳游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了JavaScript编写猜拳游戏的具体代码,供大家参考,具体内容如下

HTML代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>JS</title>
 
    <script rel="script" src="js1.js"></script>
 
    <style>
        #Div {
            width: 1000px;
            height: 700px;
            position: relative;
            border-style: groove;
            border-width: 2px;
        }
        /*猜拳区*/
        #area {
            width: 300px;
            height: 200px;
            background-color: #011bfd;
            position: absolute;
            top: 20%;
            left: 50%;
            transform: translate(-50%, -50%);
        }
        /*显示区*/
        #results {
            width: 400px;
            height: 50px;
            background-color: #f7f8fd;
            text-align:center;
            font-size:30px;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
        }
        /*卡牌石头*/
        #stone {
            width: 100px;
            height: 150px;
            background-color: #011bfd;
            position: absolute;
            top: 80%;
            left: 30%;
            transform: translate(-50%, -50%);
        }
        /*卡牌剪刀*/
        #scissors {
            width: 100px;
            height: 150px;
            background-color: #011bfd;
            position: absolute;
            top: 80%;
            left: 50%;
            transform: translate(-50%, -50%);
        }
        /*卡牌布*/
        #cloth {
            width: 100px;
            height: 150px;
            background-color: #011bfd;
            position: absolute;
            top: 80%;
            left: 70%;
            transform: translate(-50%, -50%);
        }
    </style>
 
</head>
<body>
 
<div id="Div">
    <div id="area"></div>
 
    <div id="results"></div>
 
    <div id="stone" draggable="true"></div>
    <div id="scissors" draggable="true"></div>
    <div id="cloth" draggable="true"></div>
 
</div>
 
<script rel="script">
    show();
</script>
 
</body>
</html>

JavaScript 代码:

/***
 area 区域
 stone 石头    石头 = 石头  石头 > 剪刀  石头 < 布
 scissors 剪刀 剪刀 < 石头  剪刀 = 剪刀  剪刀 > 布
 cloth 布      布 > 石头  布 < 剪刀  布 = 布
 ***/
 
/***
 查看数据类型:Object.prototype.toString.call(变量)
 刷新局部:window.location.reload('#area');
 ***/
 
 
function Init () {
    //获取并且绑定HTML的ID,返回HTML格式(HTMLDivElement)
    const area = document.querySelector("#area");
    const results = document.querySelector("#results");
    const stone = document.querySelector("#stone");
    const scissors = document.querySelector("#scissors");
    const cloth = document.querySelector("#cloth");
 
    //定义拖拽的卡牌
    let ondragstart_ID = null
    //猜拳类型编写成数组
    const random_Action = ['stone', 'scissors', 'cloth'];
    //随机获取数组中的一个数组的键
    const random_Digital = Math.round(Math.random() * (random_Action.length - 1) + 1);
    //获取数组中的键值,如数组random_Action中的'stone'(random_Action[0])
    const random_Value = random_Action[random_Digital-1];
 
    //编写猜拳类型的方法
    function attribute (parameter) {
        //鼠标移入时(猜拳卡片变大)
        parameter.onmouseover = function () {
            this.style.height = '200px';
            this.style.width = '150px';
        }
        //鼠标移出时(猜拳卡片恢复初始状态)
        parameter.onmouseleave = function () {
            this.style.height = '150px';
            this.style.width = '100px';
        }
        //元素开始拖动时(猜拳卡片变透明)
        parameter.ondragstart = function () {
            this.style.opacity = '0.3';
            ondragstart_ID = parameter.id
        }
    }
    //创建猜拳类型的对象,给猜拳对象的属性赋值
    this.show_attribute = function () {
        attribute(stone)
        attribute(scissors)
        attribute(cloth)
    }
    //编写卡牌拖动事件
    this.overout = function () {
        //当卡牌拖拽到area(猜拳区域)之上
        area.ondragenter = function () {
            //判断随机数random_Digital,不能等于null
           if (random_Digital !== null) {
               //判断拖拽的卡牌
               if (ondragstart_ID === 'stone') {
                   //判断随机数对等于哪一个
                   switch (random_Value) {
                       case stone.id:
                           results.innerHTML = 'stone = stone,平局!';
                           break;
                       case scissors.id:
                           results.innerHTML = 'stone > scissors,你赢了!';
                           break;
                       case cloth.id:
                           results.innerHTML = 'stone < cloth,你输了!';
                           break;
                       default:
                           //刷新
                           window.location.reload();
                   }
                   //元素拖动结束(猜拳卡片恢复初始状态)
                   stone.ondragend = function () {
                       this.style.opacity = '1';
                   }
                   //延迟1秒后刷新
                   setTimeout(function (){
                       window.location.reload();
                   }, 1000);
                   //判断拖拽的卡牌
               }else if (ondragstart_ID === 'scissors') {
                   //判断随机数对等于哪一个
                   switch (random_Value) {
                       case stone.id:
                           results.innerHTML = 'scissors < stone,你输了!';
                           break;
                       case scissors.id:
                           results.innerHTML = 'scissors = scissors,平局!';
                           break;
                       case cloth.id:
                           results.innerHTML = 'scissors > cloth,你赢了!';
                           break;
                       default:
                           //刷新
                           window.location.reload();
                   }
                   //元素拖动结束(猜拳卡片恢复初始状态)
                   scissors.ondragend = function () {
                       this.style.opacity = '1';
                   }
                   //延迟1秒后刷新
                   setTimeout(function (){
                       window.location.reload();
                   }, 1000);
                   //判断拖拽的卡牌
               }else if (ondragstart_ID === 'cloth') {
                   //判断随机数对等于哪一个
                   switch (random_Value) {
                       case stone.id:
                           results.innerHTML = 'cloth > stone,你赢了!';
                           break;
                       case scissors.id:
                           results.innerHTML = 'cloth < scissors,你输了!';
                           break;
                       case cloth.id:
                           results.innerHTML = 'cloth = cloth,平局!';
                           break;
                       default:
                           //刷新
                           window.location.reload();
                   }
                   //元素拖动结束(猜拳卡片恢复初始状态)
                   cloth.ondragend = function () {
                       this.style.opacity = '1';
                   }
                   //延迟1秒后刷新
                   setTimeout(function (){
                       window.location.reload();
                   }, 1000);
               }
           }
        }
    }
}
 
//调用函数
function show() {
    const show_html = new Init();
    show_html.show_attribute()
    show_html.overout()
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • elementui更改el-dialog关闭按钮的图标d的示例代码

    elementui更改el-dialog关闭按钮的图标d的示例代码

    这篇文章主要介绍了elementui更改el-dialog关闭按钮的图标,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-08-08
  • 微信小程序的动画效果详解

    微信小程序的动画效果详解

    本文主要介绍了微信小程序动画效果的实现方法与原理解析。具有一定的参考价值,下面跟着小编一起来看下吧
    2017-01-01
  • 原生js实现验证码功能

    原生js实现验证码功能

    本文主要介绍了原生js实现验证码功能的实例,具有很好的参考价值。下面跟着小编一起来看下吧
    2017-03-03
  • 基于javascript处理二进制图片流过程详解

    基于javascript处理二进制图片流过程详解

    这篇文章主要介绍了基于javascript处理二进制图片流过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-06-06
  • 点击button获取text内容并改变样式的js实现

    点击button获取text内容并改变样式的js实现

    这篇文章主要介绍了点击button获取text内容并改变样式的js实现,经测试非常实用,需要的朋友可以参考下
    2014-09-09
  • JS清空多文本框、文本域示例代码

    JS清空多文本框、文本域示例代码

    点击按钮清空页面上所有的文本框、文本域,下面有个不错的示例,大家可以参考下其具体实现
    2014-02-02
  • ES6代码转ES5详细教程(babel安装使用教程)

    ES6代码转ES5详细教程(babel安装使用教程)

    Babel 是一个广泛使用的 ES6 转码器,可以将 ES6 代码转为 ES5 代码,从而在老版本的浏览器执行,这意味着,你可以用 ES6 的方式编写程序,又不用担心现有环境是否支持,这篇文章主要介绍了ES6代码转ES5教程(babel安装使用教程),需要的朋友可以参考下
    2023-01-01
  • JS循环遍历ul中li的点击事件给选中li添加css样式(示例代码)

    JS循环遍历ul中li的点击事件给选中li添加css样式(示例代码)

    对于一个ul中固定的li个数,当点击其中一个li时,改变选中li的颜色;同时改变对应的另一个ul中li的颜色,怎么实现这个功能呢,下面小编给大家带来了js循环遍历ul中li的点击事件,给给选中li添加css样式,感兴趣的朋友一起看看吧
    2023-07-07
  • JS操作input标签属性checkbox全选的实现代码

    JS操作input标签属性checkbox全选的实现代码

    这篇文章主要介绍了JS操作input标签属性checkbox全选的代码,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2017-03-03
  • 详解JavaScript中Promise类的使用方法

    详解JavaScript中Promise类的使用方法

    这篇文章主要为大家详细介绍了JavaScript中Promise类的使用方法,文中的示例代码简洁易懂,对我们学习JavaScript有一定的帮助,需要的可以参考一下
    2023-05-05

最新评论