//html代码
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>猜拳游戏</title>
<link rel="stylesheet" href="css/game.css"></link>
</head>
<body>
<div id="game">
<ul class="panel">
<li>
<p class="name">我:name</p>
<div class="anim user"></div>
</li>
<li>
<p class="name">电脑:name</p>
<div class="anim comp"></div>
</li>
</ul>
<div class="op">
<button id="play" onclick = "game.Caiquan();">开始</button>
</div>
<div id="text" class="text">请开始游戏...</div>
<ul id="guess" class="guess">
<li>
<div class="guess0" onclick="game.verdict(0)">石头</div>
</li>
<li>
<div class="guess1" onclick="game.verdict(1)">剪刀</div>
</li>
<li>
<div class="guess2" onclick="game.verdict(2)">布</div>
</li>
</ul>
</div>
<script type="text/javascript" src="js/game.js"></script> </body>
</html>
//css样式(字体:迷你简卡通)
*{
margin:0px;
padding:0px;
font-family:'迷你简卡通';
font-size:28px;
}
html,body{
width:100%;
height:100%;
background:rgba(244, 184, 202, 1);
}
ul{
list-style:none;
}
#game{
width:800px;
height:600px;
margin:auto;
top:20%;
}
#game ul{
width:100%;
height:415px;
}
#game ul li{
width:50%;
height:100%;
float:left;
text-align:center;
}
#game ul li .anim{
width:223px;
height:337px;
border:10px solid #ff6699;
border-radius:50%;
margin:20px auto 0;
background-position:center;
background-repeat:no-repeat;
}
.user{
background:url('../img/readyl.png');
}
.comp{
background:url('../img/readyr.png');
}
#game .op{
width:100%;
text-align:center;
}
#game .op button{
width:200px;
height:60px;
border:10px solid #ff6699;
background:rgb(253, 217, 227);
border-radius:50%;
outline:none;
cursor:pointer;
font-weight:bold;
}
#game .op button:hover{
border-color:#ff0000;
background-color:#ff0000;
font-size:36px;
color:rgb(253, 217, 227);
}
#game .op button.disabled{
border-color:#bbb;
color:#bbb;
background-color:#ccc;
font-size:28px;
cursor:default;
}
#game .guess{
width:220px;
height:100%;
position:fixed;
top:0px;
left:0px;
display:none;
}
#game ul.guess li{
width:100%;
height:32%;
}
#game ul.guess li div{
width:100%;
height:90%;
border:10px solid #ff6699;
border-radius:50%;
background-position:center;
background-repeat:no-repeat;
cursor:pointer;
background-color:rgba(244, 184, 202, 1);
}
#game ul.guess li div:hover{
background-color:#ff6699;
color:#fff;
}
div.guess0{
background-image:url('../img/0.png');
}
div.guess1{
background-image:url('../img/1.png');
}
div.guess2{
background-image:url('../img/2.png');
}
#game div.text{
margin-top:20px;
text-align:center;
font-size:50px;
font-weight:bold;
}
//js代码
Function.prototype.extend = function( fn ){
for( var attr in fn.prototype ){
this.prototype[attr] = fn.prototype[attr];
}
} //父级构造函数用于继承,共有属性
function Caiquan( name ){
this.name = name;
this.point = 0;
}
//用于继承下面衍生,共有方法
Caiquan.prototype.guess = function(){} //继承父,玩家的构造函数
function User( name ){
Caiquan.call(this,name);
}
User.extend( Caiquan );
User.prototype.guess = function( point ){
return this.point = point;
}
//电脑的构造函数
function Comp( name ){
Caiquan.call(this,name);
}
Comp.extend( Caiquan ) ;
//电脑的猜拳方法,随机
Comp.prototype.guess = function(){
return this.point = Math.floor( Math.random()*3 );
}
//裁判构造函数
function Game( u , c ){
this.text = document.getElementById('text');
this.btn = document.getElementById("play");
this.user = u;
this.comp = c;
this.classN =document.getElementsByClassName('name');
this.guess = document.getElementById("guess");
this.anim = document.getElementsByClassName("anim");
this.num = 0;
this.init();
this.tiemr = null;
}
Game.prototype.Caiquan = function(){
this.textValue( '请出拳...' );
this.BtnDisable();
this.start();
this.guess.style.display = 'block'; }
//怎么玩
Game.prototype.start = function(){
var This = this;
this.timer = setInterval(function(){
This.anim[0].className = 'anim user guess' +( ( This.num ++ ) % 3 );
This.anim[1].className = 'anim comp guess' + ( ( This.num ++ ) % 3 ) ;
},500) }
//初始化名字
Game.prototype.init = function(){
this.classN[0].innerHTML = '我:' + this.user.name;
this.classN[1].innerHTML = '电脑:' + this.comp.name; }
//提示面板区域的修改
Game.prototype.textValue = function( val ){
this.text.innerHTML = val;
}
//按钮失效
Game.prototype.BtnDisable = function(){
if( this.btn.disabled ){
this.btn.disabled = false;
this.btn.className ='';
this.btn.innerHTML = '再来一次'
}else{
this.btn.disabled = true;
this.btn.className ='disabled';
} }
Game.prototype.verdict = function( point ){
clearInterval(this.timer);
var userGu = user.guess(point);
var compGu = comp.guess();
this.anim[0].className = 'anim user guess' + userGu;
this.anim[1].className = 'anim comp guess' + compGu;
var res = userGu - compGu;
switch (res){
case 0:
this.textValue('平局!!!')
break;
case 1:
case -2:
this.textValue('lose~~~');
break;
case 2:
case -1:
this.textValue('win!!!')
break;
}
this.guess.style.display = 'none';
this.BtnDisable(); }
var user = new User( '锐雯' );
var comp = new Comp( '拉克丝' );
var game = new Game( user , comp );

JavaScript--面向对象--猜拳游戏的更多相关文章

  1. 用javascript编写猜拳游戏(函数)

    const readline = require('readline-sync')//引用readline-sync console.log('欢迎进入猜拳游戏'); //电脑随机出拳 let fn ...

  2. C#面向对象编程实例-猜拳游戏

    1.需求 现在要制作一个游戏,玩家与计算机进行猜拳游戏,玩家出拳,计算机出拳,计算机自动判断输赢. 2.需求分析 根据需求,来分析一下对象,可分析出:玩家对象(Player).计算机对象(Comput ...

  3. JavaScript 面向对象(二) —— 案例篇

    看案例前可以先看看基础篇:JavaScript 面向对象(一) —— 基础篇 案例——面向对象的选项卡:把面向过程的程序一步步改成面向对象的形式,使其能够更加的通用(但是通用的东西,一般会比较臃肿). ...

  4. ajax简单手写了一个猜拳游戏

    使用ajax简单写一个猜拳游戏 HTML代码 <!DOCTYPE HTML> <html lang="en-US"> <head> <me ...

  5. JavaScript学习笔记(三)——this、原型、javascript面向对象

    一.this 在JavaScript中this表示:谁调用它,this就是谁. JavaScript是由对象组成的,一切皆为对象,万物皆为对象.this是一个动态的对象,根据调用的对象不同而发生变化, ...

  6. JavaScript学习总结(三)——this、原型、javascript面向对象

    一.this 在JavaScript中this表示:谁调用它,this就是谁. JavaScript是由对象组成的,一切皆为对象,万物皆为对象.this是一个动态的对象,根据调用的对象不同而发生变化, ...

  7. Javascript面向对象特性实现封装、继承、接口详细案例——进级高手篇

    Javascript面向对象特性实现(封装.继承.接口) Javascript作为弱类型语言,和Java.php等服务端脚本语言相比,拥有极强的灵活性.对于小型的web需求,在编写javascript ...

  8. javascript面向对象系列第四篇——选项卡的实现

    前面的话 面向对象的应用并非只是读几本书那么容易,需要有大量的工程实践做基础才能真正理解并学会使用它.本文将用面向对象的技术来制作一个简单的选项卡 图示说明 由图示结果看到,这是一个非常简单的选项卡. ...

  9. javascript面向对象系列第三篇——实现继承的3种形式

    × 目录 [1]原型继承 [2]伪类继承 [3]组合继承 前面的话 学习如何创建对象是理解面向对象编程的第一步,第二步是理解继承.本文是javascript面向对象系列第三篇——实现继承的3种形式 [ ...

随机推荐

  1. 自建git node pm2 (不赘述,就说遇见的问题)

    //======================[git]部分 主题部分还是按照网上的办法进行安装. 安装的话  分为两个办法(一个是yum (contos办法)  或者sudo(ubuntu办法) ...

  2. MongoDB学习笔记~对集合属性的操作

    回到目录 $unset清除元素 请注意在单个数组元素上使用$unset的结果可能与你设想的不一样.其结果只是将元素的值设置为null,而非删除整个元素.要想彻底删除某个数组元素,可以用$pull 和$ ...

  3. 跟着老男孩教育学Python开发【第三篇】:Python函数

    set 无序,不重复,可嵌套. 函数 创建函数: 1.def关键字,创建函数 2.函数名 3.() 4.函数体 5.返回值 发邮件函数 def sendmail():     import smtpl ...

  4. 【每日一linux命令1】linux命令路径

    一.路径: 执行命令前必须要考虑的一步是命令的路径,若是路径错误或是没有正确的指定,可能导致错误 的执行或是找不到该命令.要知道设置的路径,可执行以下命令: echo $PATH 显示结果: 这时我们 ...

  5. python-time 模块

    1.时间戳是以秒为单位的浮点小数,时间戳以自1970年1月1日午夜到现在经过了的时间来表示 2.时间模块引入方式:import time 3.返回时间戳 time.time() 4.返回时间元组:ti ...

  6. U3D DrawCall优化手记

    在最近,使用U3D开发的游戏核心部分功能即将完成,中间由于各种历史原因,导致项目存在比较大的问题,这些问题在最后,恐怕只能通过一次彻底的重构来解决 现在的游戏跑起来会有接近130-170个左右的Dra ...

  7. 用Maven部署war包到远程Tomcat服务器

    过去我们发布一个Java Web程序通常的做法就是把它打成一个war包,然后用SSH这样的工具把它上传到服务器,并放到相应的目录里,让Tomcat自动去解包,完成部署. 很显然,这样做不够方便,且我们 ...

  8. 将自己打代码添加到cocoapods

    1,Github 上创建新站点 2, 从gitHub上 clone 一份,将源码拷贝到该目录下提交3,开源库发布之后,需要打上tag git tag 0.0.1 git push --tags git ...

  9. 正则表达式和文本挖掘(Text Mining)

    在进行文本挖掘时,TSQL中的通配符(Wildchar)显得功能不足,这时,使用“CLR+正则表达式”是非常不错的选择,正则表达式看似非常复杂,但,万变不离其宗,熟练掌握正则表达式的元数据,就能熟练和 ...

  10. K-Means聚类算法原理

    K-Means算法是无监督的聚类算法,它实现起来比较简单,聚类效果也不错,因此应用很广泛.K-Means算法有大量的变体,本文就从最传统的K-Means算法讲起,在其基础上讲述K-Means的优化变体 ...