怎么用JavaScript制作一个猜拳游戏???

发布网友 发布时间:2022-04-23 09:46

我来回答

3个回答

懂视网 时间:2022-05-15 00:55

这篇文章主要介绍了JavaScript基于面向对象实现的猜拳游戏,结合完整实例形式分析了javascript基于面向对象实现猜拳游戏的具体页面布局、样式及功能相关操作技巧,需要的朋友可以参考下

本文实例讲述了JavaScript基于面向对象实现的猜拳游戏。分享给大家供大家参考,具体如下:

html代码:

<!doctype html>
<html>
 <head>
 <meta charset="UTF-8">
 <title>猜拳游戏</title>
 <link rel="stylesheet" href="css/game.css" rel="external nofollow" ></link>
 </head>
 <body>
 <p id="game">
 <ul class="panel">
 <li>
 <p class="name">我:name</p>
 <p class="anim user"></p>
 </li>
 <li>
 <p class="name">电脑:name</p>
 <p class="anim comp"></p>
 </li>
 </ul>
 <p class="op">
 <button id="play" onclick = "game.Caiquan();">开始</button>
 </p>
 <p id="text" class="text">请开始游戏...</p>
 <ul id="guess" class="guess">
 <li>
 <p class="guess0" onclick="game.verdict(0)">石头</p>
 </li>
 <li>
 <p class="guess1" onclick="game.verdict(1)">剪刀</p>
 </li>
 <li>
 <p class="guess2" onclick="game.verdict(2)">布</p>
 </li>
 </ul>
 </p>
 <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 p{
 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 p:hover{
 background-color:#ff6699;
 color:#fff;
}
p.guess0{
 background-image:url('../img/0.png');
}
p.guess1{
 background-image:url('../img/1.png');
}
p.guess2{
 background-image:url('../img/2.png');
}
#game p.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 );

上面是我整理给大家的,希望今后会对大家有帮助。

相关文章:

详细解读vue-admin和后端(flask)分离结合

使用jquery+css3如何实现熊猫tv导航

在jQuery中如何实现定时隐藏对话框

在JS/jQuery中如何实现DIV延时几秒后消失或显示

使用原生js实现省市区三级联动

热心网友 时间:2022-05-14 22:03

<!DOCTYPE html>
<html>
<head>

<style type="text/css">
    #main{
        text-align:center;
        margin-left:25%;
        width:50%;
        border:1px solid black;
    }
    #player,#result,#cpu{
        height:100%;
        float:left;
    }
    #player{
        background:#dde;
    }
    #cpu{
        background:#ecc;
    }
    #player,#cpu{
        width:25%;
    }
    #result{
        width:50%;
    }
    #viewBox{
        width:80%;
        height:45%;
    }
    #choiceBox .item{
        float:left;
        width:33.33%;
        height:100%;
        line-height:100%;
        background:#bbe;
        cursor:pointer;
    }
    #choiceBox{
        height:25%;
        width:60%;
        margin-left:20%;
    }
</style>
</head>
<body>
<div id="main">
    <div id="viewBox">
        <div id="player">玩家</div>
        <div id="result"></div>
        <div id="cpu">电脑</div>
    </div>
    <div id="choiceBox">
        <span class="item">剪刀</span>
        <span class="item">石头</span>
        <span class="item">布</span>
    </div>
</div>
<script type="text/javascript">
    var _main = document.getElementById("main");
    var _viewBox = document.getElementById("viewBox");
    var _choiceBox = document.getElementById("choiceBox");
    var _player = document.getElementById("player");
    var _result = document.getElementById("result");
    var _cpu = document.getElementById("cpu");
    _choiceBox.childNodes[1].onmouseover =
    _choiceBox.childNodes[3].onmouseover =
    _choiceBox.childNodes[5].onmouseover = function(){this.style["background"] = "#aae"};
    _choiceBox.childNodes[1].onmouseout =
    _choiceBox.childNodes[3].onmouseout =
    _choiceBox.childNodes[5].onmouseout = function(){this.style["background"] = "#bbe"};
    function resize(){
        var width = window.innerWidth;
        var height = window.innerHeight;
        var smaller = width > height ? height : width;
        _main.style["height"] = height * 0.5 + "px";
        _main.style["margin-top"] = height * 0.2 + "px";
        _main.style["font-size"] = smaller * 0.08 + "px";
        _player.style["line-height"] = height * 0.225 + "px";
        _result.style["line-height"] = height * 0.225 + "px";
        _cpu.style["line-height"] = height * 0.225 + "px";
        _viewBox.style["padding"] = height * 0.05 + "px " + width * 0.05 + "px";
        _choiceBox.childNodes[1].style["line-height"] = height * 0.125 + "px";
        _choiceBox.childNodes[3].style["line-height"] = height * 0.125 + "px";
        _choiceBox.childNodes[5].style["line-height"] = height * 0.125 + "px";
        _choiceBox.style["font-size"] = smaller * 0.04 + "px";
    };
    resize();
    window.onresize = resize;
    //以下是猜拳逻辑部分

    var choiceItems = [{name:"剪刀"},{name:"石头"},{name:"布"}];
    choiceItems[0]["next"] = choiceItems[1];
    choiceItems[0]["pre"] = choiceItems[2];
    choiceItems[1]["next"] = choiceItems[2];
    choiceItems[1]["pre"] = choiceItems[0];
    choiceItems[2]["next"] = choiceItems[0];
    choiceItems[2]["pre"] = choiceItems[1];
    _choiceBox.childNodes[1].onclick = function(){cal(choiceItems[0]);};
    _choiceBox.childNodes[3].onclick = function(){cal(choiceItems[1]);};
    _choiceBox.childNodes[5].onclick = function(){cal(choiceItems[2]);};
    function cal(player_choice){
        var cpu_choice = choiceItems[parseInt(Math.random() * 3)];
        _player.innerHTML = player_choice["name"];
        _cpu.innerHTML = cpu_choice["name"];
        if(cpu_choice == player_choice){
            _result.innerHTML = "-";
        }else if(cpu_choice == player_choice["pre"]){
            _result.innerHTML = "win";
        }else if(cpu_choice == player_choice["next"]){
            _result.innerHTML = "loss";
        }
    }
</script>
</body>
</html>

热心网友 时间:2022-05-14 23:21

和java逻辑判断一样

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com