JavaScript 编写猜拳游戏

HTML代码:
1<!DOCTYPE html> 2<html lang="en"> 3<head> 4 <meta charset="UTF-8"> 5 <title>JS</title> 6 7 <script rel="script" src="js1.js"></script> 8 9 <style> 10 #Div { 11 width: 1000px; 12 height: 700px; 13 position: relative; 14 border-style: groove; 15 border-width: 2px; 16 } 17 /*猜拳区*/ 18 #area { 19 width: 300px; 20 height: 200px; 21 background-color: #011bfd; 22 position: absolute; 23 top: 20%; 24 left: 50%; 25 transform: translate(-50%, -50%); 26 } 27 /*显示区*/ 28 #results { 29 width: 400px; 30 height: 50px; 31 background-color: #f7f8fd; 32 text-align:center; 33 font-size:30px; 34 position: absolute; 35 top: 50%; 36 left: 50%; 37 transform: translate(-50%, -50%); 38 } 39 /*卡牌石头*/ 40 #stone { 41 width: 100px; 42 height: 150px; 43 background-color: #011bfd; 44 position: absolute; 45 top: 80%; 46 left: 30%; 47 transform: translate(-50%, -50%); 48 } 49 /*卡牌剪刀*/ 50 #scissors { 51 width: 100px; 52 height: 150px; 53 background-color: #011bfd; 54 position: absolute; 55 top: 80%; 56 left: 50%; 57 transform: translate(-50%, -50%); 58 } 59 /*卡牌布*/ 60 #cloth { 61 width: 100px; 62 height: 150px; 63 background-color: #011bfd; 64 position: absolute; 65 top: 80%; 66 left: 70%; 67 transform: translate(-50%, -50%); 68 } 69 </style> 70 71</head> 72<body> 73 74<div id="Div"> 75 <div id="area"></div> 76 77 <div id="results"></div> 78 79 <div id="stone" draggable="true"></div> 80 <div id="scissors" draggable="true"></div> 81 <div id="cloth" draggable="true"></div> 82 83</div> 84 85<script rel="script"> 86 show(); 87</script> 88 89</body> 90</html>
JavaScript 代码:
1/*** 2 area 区域 3 stone 石头 石头 = 石头 石头 > 剪刀 石头 < 布 4 scissors 剪刀 剪刀 < 石头 剪刀 = 剪刀 剪刀 > 布 5 cloth 布 布 > 石头 布 < 剪刀 布 = 布 6 ***/ 7 8/*** 9 查看数据类型:Object.prototype.toString.call(变量) 10 刷新局部:window.location.reload('#area'); 11 ***/ 12 13 14function Init () { 15 //获取并且绑定HTML的ID,返回HTML格式(HTMLDivElement) 16 const area = document.querySelector("#area"); 17 const results = document.querySelector("#results"); 18 const stone = document.querySelector("#stone"); 19 const scissors = document.querySelector("#scissors"); 20 const cloth = document.querySelector("#cloth"); 21 22 //定义拖拽的卡牌 23 let ondragstart_ID = null 24 //猜拳类型编写成数组 25 const random_Action = ['stone', 'scissors', 'cloth']; 26 //随机获取数组中的一个数组的键 27 const random_Digital = Math.round(Math.random() * (random_Action.length - 1) + 1); 28 //获取数组中的键值,如数组random_Action中的'stone'(random_Action[0]) 29 const random_Value = random_Action[random_Digital-1]; 30 31 //编写猜拳类型的方法 32 function attribute (parameter) { 33 //鼠标移入时(猜拳卡片变大) 34 parameter.onmouseover = function () { 35 this.style.height = '200px'; 36 this.style.width = '150px'; 37 } 38 //鼠标移出时(猜拳卡片恢复初始状态) 39 parameter.onmouseleave = function () { 40 this.style.height = '150px'; 41 this.style.width = '100px'; 42 } 43 //元素开始拖动时(猜拳卡片变透明) 44 parameter.ondragstart = function () { 45 this.style.opacity = '0.3'; 46 ondragstart_ID = parameter.id 47 } 48 } 49 //创建猜拳类型的对象,给猜拳对象的属性赋值 50 this.show_attribute = function () { 51 attribute(stone) 52 attribute(scissors) 53 attribute(cloth) 54 } 55 //编写卡牌拖动事件 56 this.overout = function () { 57 //当卡牌拖拽到area(猜拳区域)之上 58 area.ondragenter = function () { 59 //判断随机数random_Digital,不能等于null 60 if (random_Digital !== null) { 61 //判断拖拽的卡牌 62 if (ondragstart_ID === 'stone') { 63 //判断随机数对等于哪一个 64 switch (random_Value) { 65 case stone.id: 66 results.innerHTML = 'stone = stone,平局!'; 67 break; 68 case scissors.id: 69 results.innerHTML = 'stone > scissors,你赢了!'; 70 break; 71 case cloth.id: 72 results.innerHTML = 'stone < cloth,你输了!'; 73 break; 74 default: 75 //刷新 76 window.location.reload(); 77 } 78 //元素拖动结束(猜拳卡片恢复初始状态) 79 stone.ondragend = function () { 80 this.style.opacity = '1'; 81 } 82 //延迟1秒后刷新 83 setTimeout(function (){ 84 window.location.reload(); 85 }, 1000); 86 //判断拖拽的卡牌 87 }else if (ondragstart_ID === 'scissors') { 88 //判断随机数对等于哪一个 89 switch (random_Value) { 90 case stone.id: 91 results.innerHTML = 'scissors < stone,你输了!'; 92 break; 93 case scissors.id: 94 results.innerHTML = 'scissors = scissors,平局!'; 95 break; 96 case cloth.id: 97 results.innerHTML = 'scissors > cloth,你赢了!'; 98 break; 99 default: 100 //刷新 101 window.location.reload(); 102 } 103 //元素拖动结束(猜拳卡片恢复初始状态) 104 scissors.ondragend = function () { 105 this.style.opacity = '1'; 106 } 107 //延迟1秒后刷新 108 setTimeout(function (){ 109 window.location.reload(); 110 }, 1000); 111 //判断拖拽的卡牌 112 }else if (ondragstart_ID === 'cloth') { 113 //判断随机数对等于哪一个 114 switch (random_Value) { 115 case stone.id: 116 results.innerHTML = 'cloth > stone,你赢了!'; 117 break; 118 case scissors.id: 119 results.innerHTML = 'cloth < scissors,你输了!'; 120 break; 121 case cloth.id: 122 results.innerHTML = 'cloth = cloth,平局!'; 123 break; 124 default: 125 //刷新 126 window.location.reload(); 127 } 128 //元素拖动结束(猜拳卡片恢复初始状态) 129 cloth.ondragend = function () { 130 this.style.opacity = '1'; 131 } 132 //延迟1秒后刷新 133 setTimeout(function (){ 134 window.location.reload(); 135 }, 1000); 136 } 137 } 138 } 139 } 140} 141 142//调用函数 143function show() { 144 const show_html = new Init(); 145 show_html.show_attribute() 146 show_html.overout() 147}
点赞
收藏

评论区

加载中...

相关推荐

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

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

一篇文章带你了解JavaScript日期

日期对象允许您使用日期(年、月、日、小时、分钟、秒和毫秒)。一、JavaScript的日期格式一个JavaScript日期可以写为一个字符串:ThuFeb02201909:59:51GMT0800(中国标准时间)或者是一个数字:1486000791164写数字的日期,指定的毫秒数自1970年1月1日00:00:00到现在。1\.显示日期使用

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )