JavaScript--面向对象--猜拳游戏

//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 );
时间: 2024-08-10 21:17:05

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

JavaScript之猜拳游戏

// 猜拳游戏 // 清屏函数 let clear = () => process.stdout.write(process.platform === 'win32' ? '\x1Bc' : '\x1B[2J\x1B[3J\x1B[H'); let readline = require("readline-sync"); let player = { name : "玩家", // 默认玩家名 victory : 0 // 玩家胜利局数 }; let com

javascript基础-猜拳游戏

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-

C#面向对象编程-猜拳游戏

1.需求 现在要制作一个游戏,玩家与计算机进行猜拳游戏,玩家出拳,计算机出拳,计算机自动判断输赢. 2.需求分析 根据需求,来分析一下对象,可分析出:玩家对象(Player).计算机对象(Computer).裁判对象(Judge). 玩家出拳由用户控制,使用数字代表:1石头.2剪子.3布 计算机出拳由计算机随机产生 裁判根据玩家与计算机的出拳情况进行判断输赢 3.类对象的实现 玩家类示例代码 class Player { string name; public string Name { get

面向对象_猜拳游戏

通过控制台命令方式实现一个猜拳游戏,用户通过输入(1.剪刀,2.石头,3.布)与电脑PK,最后通过积分的多少判定胜负 猜拳游戏实现思路: 1.在控制台输出玩法提示 2.是否开始游戏(接收1表示开始,0表示退出) 3.接收游戏的局数 4.循环接收用户的出拳(1.剪刀 2石头 3.布) 5.系统随机出拳 6.进行比较记录胜者次数 7.公布结果 package com.study.second; import java.util.Random; import java.util.Scanner; /*

JavaScript面向对象及相关知识

最近在学习JavaScript面向对象,写下自己的理解及相关资料,以备查阅. 一.面向对象中涉及的相关几个概念 1.作用域 所谓作用域,就是说属性和函数的可访问范围.在JavaScript中,作用域分为两种.全局作用域和局部作用域. 所有没有var 声明 或 定义于最外层函数和最外层函数外面即为全局作用域.也就是定义可以随意调用. 自定义函数内部用var声明的为局部作用域. var num = 1; //全局作用域 window.onload = function() { //最外层为全局作用域

javascript面向对象之this指针

下午用面向对象的方法写一个幻灯片插件的时候不小心写出了这样的代码: Slider.prototype.auto=function() { setInterval(this.toRun,4000);//注意 } Slider.prototype.toRun=function() { if(this.iNow==this.aA.length - 1) ...... } 在浏览器打开的时候发现幻灯片不能如预期般自动切换,控制台给出了这样的错误提示: this.aA isundefined?然而我已经在

猜拳游戏

//通过控制台命令方式实现一个猜拳游戏,用户通过输入(1.石头,2.剪刀,3.布)与电脑pk,最后通过积分的多少判断胜负 Scanner sc=new Scanner(System.in); for(;;){ System.out.println("欢迎来到猜拳游戏,游戏规则:1.石头,2.剪刀,3.布.赢一次获得1个积分点,输一次扣一个积分点.您共有5次猜拳机会."); System.out.println("请确定是否开始游戏:1.开始  0.退出"); int

javascript面向对象2

原文:javascript面向对象2 首先我们先创建一个对象 var user = Object(); user.name = "张三"; user.age = 20; user.sex = "男"; 上面呢创建了一个对象,当然创建对象的方法有很多种,但是用这样的方法创建对象比较简单直观,也是JavaScript种创建对象最基本的方法. 同时呢也出现了另外一个弊端,当我们想要创建多个对象的时候,我们就要写很多重复的代码,这样一来就增加了代码量,减少了工作的效率, 为

JavaScript面向对象之类的继承

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-