//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. iOS微信第三方登录实现

    iOS微信第三方登录实现   一.接入微信第三方登录准备工作.移动应用微信登录是基于OAuth2.0协议标准构建的微信OAuth2.0授权登录系统.在进行微信OAuth2.0授权登录接入之前,在微信开 ...

  2. Atitit.项目修改补丁打包工具 使用说明

    Atitit.项目修改补丁打包工具 使用说明 1.1. 打包工具已经在群里面.打包工具.bat1 1.2. 使用方法:放在项目主目录下,执行即可1 1.3. 打包工具的原理以及要打包的项目列表1 1. ...

  3. WINDOWS系统下MYSQL安装过程中的注意事项

    1.首先MySQL的安装方式有两种:一种是MSI安装方式,很简单就像安装Windows软件一样.另外一种就是ZIP安装方式.这种相对而言比较麻烦.新手推荐MSI安装方式. 安装方式有以下两种: MSI ...

  4. postgresql无法安装pldbgapi的问题

    要对函数进行调试需要安装插件pldbgapi,当初在windows上面的postgresql实例中执行了一下语句就安装上了: create extension pldbgapi; 但是在linux中执 ...

  5. linux之查看系统命令

    cpu信息 1.查看逻辑cpu核数 # cat /proc/cpuinfo| grep "processor"| wc -l 2.查看物理cpu个数 # cat /proc/cpu ...

  6. 前端工程师手中的Sublime Text

    原文地址:http://css-tricks.com/sublime-text-front-end-developers/ 我的Blog:http://cabbit.me/sublime-text-f ...

  7. ABP框架 - 启动配置

    文档目录 本节内容: 配置ABP 替换内置服务 配置模块 为一个模块创建配置 ABP在启动时,提供基础框架和模型来配置和模块化. 置ABP 在预初始化事件中进行配置,示例: kid1412注:XmlL ...

  8. maven repo plugin archiver

    Failure to transfer org.apache.maven:maven-archiver:pom:2.4.1 from http://repo1.maven.org/xxx was ca ...

  9. python描述符理解

    Python中的描述符是一个相对底层的概念 descriptor Any object which defines the methods get(), set(), or delete(). Whe ...

  10. 【Win 10 应用开发】多媒体转码

    上次本来说好,今天咱们来讨论 socket 相关的话题,但,对于 socket ,老周还有一些问题没弄清楚,等弄清楚了,再和大伙伴们一起探讨.故,今天咱们扯一扯多媒体转码的事. 听起来挺复杂的,不过, ...