JavaScript 编写猜拳游戏

陈占占
• 阅读 1402
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()
}
点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Karen110 Karen110
2年前
一篇文章带你了解JavaScript日期
日期对象允许您使用日期(年、月、日、小时、分钟、秒和毫秒)。一、JavaScript的日期格式一个JavaScript日期可以写为一个字符串:ThuFeb02201909:59:51GMT0800(中国标准时间)或者是一个数字:1486000791164写数字的日期,指定的毫秒数自1970年1月1日00:00:00到现在。1\.显示日期使用
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Stella981 Stella981
2年前
Android So动态加载 优雅实现与原理分析
背景:漫品Android客户端集成适配转换功能(基于目标识别(So库35M)和人脸识别库(5M)),导致apk体积50M左右,为优化客户端体验,决定实现So文件动态加载.!(https://oscimg.oschina.net/oscnet/00d1ff90e4b34869664fef59e3ec3fdd20b.png)点击上方“蓝字”关注我
Stella981 Stella981
2年前
IE7、IE8、IE9对min
问题:    IE7、IE8、IE9对minheight不识别,其他无问题解决:   box{width:100px;height:35px;}   htmlbodybox{width:auto;height:auto;width:100px;minheight:35px;} 实例:
Stella981 Stella981
2年前
35. Search Insert Position
Category:BinarySearch&ArrayGivenasortedarrayandatargetvalue,returntheindexifthetargetisfound.Ifnot,returntheindexwhereit
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Wesley13 Wesley13
2年前
35岁是技术人的天花板吗?
35岁是技术人的天花板吗?我非常不认同“35岁现象”,人类没有那么脆弱,人类的智力不会说是35岁之后就停止发展,更不是说35岁之后就没有机会了。马云35岁还在教书,任正非35岁还在工厂上班。为什么技术人员到35岁就应该退役了呢?所以35岁根本就不是一个问题,我今年已经37岁了,我发现我才刚刚找到自己的节奏,刚刚上路。
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这