面向对象,猜拳游戏

作者: 韦德国际1946国际网址  发布:2019-06-02

正文实例讲述了JavaScript基于面向对象落成的猜拳游戏。分享给我们供大家参照他事他说加以考察,具体如下:

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>
  <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样式(字体:Mini简卡通)

*{
  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相关内容感兴趣的读者可查看本站专项论题:《javascript面向对象入门教程》、《JavaScript错误与调整技艺总计》、《JavaScript数据结构与算法技能总计》、《JavaScript遍历算法与手艺计算》及《JavaScript数学生运动算用法计算》

期望本文所述对大家JavaScript程序设计具备援救。

本文实例讲述了JavaScript基于面向对象完成的猜拳游戏。分享给我们供我们参谋,具体如...

Form1.cs

//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 );

html代码:

图片 1图片 2View Code

 

<!doctype html>
<html>
 <head>
 <meta charset="UTF-8">
 <title>猜拳游戏</title>
 <link rel="stylesheet" href="css/game.css" rel="external nofollow" ></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>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 猜拳游戏
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnJianDao_Click(object sender, EventArgs e)
        {
            Player player = new Player();
            Computer computer = new Computer();
            string pcChooseStr=Computer.PCchoose();
            //lblDianNao.Text = pcChooseStr; 文字展示
            string jiandaoUrl = Computer.BackPic(pcChooseStr);
            picMy.Load(@"d:我的文档Visual Studio 2010Projects随机点名猜拳游戏Img剪刀.jpg");
            picPC.Load(jiandaoUrl);
            lblResult.Text = Player.OutJianDao(pcChooseStr);


        }

        private void btnShiTou_Click(object sender, EventArgs e)
        {
            Player player = new Player();
            Computer computer = new Computer();
            string pcChooseStr = Computer.PCchoose();
            //lblDianNao.Text = pcChooseStr;
            string shitouUrl = Computer.BackPic(pcChooseStr);
            picMy.Load(@"d:我的文档Visual Studio 2010Projects随机点名猜拳游戏Img石头.jpg");
            picPC.Load(shitouUrl);
            lblResult.Text = Player.OutShiTou(pcChooseStr);
        }

        private void btnBu_Click(object sender, EventArgs e)
        {
            Player player = new Player();
            Computer computer = new Computer();
            string pcChooseStr = Computer.PCchoose();
            //lblDianNao.Text = pcChooseStr;
            string buUrl = Computer.BackPic(pcChooseStr);
            picMy.Load(@"d:我的文档Visual Studio 2010Projects随机点名猜拳游戏Img布.jpg");
            picPC.Load(buUrl);
            lblResult.Text = Player.OutBu(pcChooseStr);
        }
    }
}

本文由韦德国际1946发布于韦德国际1946国际网址,转载请注明出处:面向对象,猜拳游戏

关键词: 韦德国际 .NET学习笔记 软件设计 面向对象