>  기사  >  웹 프론트엔드  >  JavaScript를 사용하여 추측 게임을 구현하는 방법(자세한 튜토리얼)

JavaScript를 사용하여 추측 게임을 구현하는 방법(자세한 튜토리얼)

亚连
亚连원래의
2018-06-13 17:44:114415검색

이 글에서는 주로 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:&#39;迷你简卡通&#39;;
  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(&#39;../img/readyl.png&#39;);
}
.comp{
  background:url(&#39;../img/readyr.png&#39;);
}
#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(&#39;../img/0.png&#39;);
}
p.guess1{
  background-image:url(&#39;../img/1.png&#39;);
}
p.guess2{
  background-image:url(&#39;../img/2.png&#39;);
}
#game p.text{
  margin-top:20px;
  text-align:center;
  font-size:50px;
  font-weight:bold;
}

js code

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(&#39;text&#39;);
  this.btn = document.getElementById("play");
  this.user = u;
  this.comp = c;
  this.classN =document.getElementsByClassName(&#39;name&#39;);
  this.guess = document.getElementById("guess");
  this.anim = document.getElementsByClassName("anim");
  this.num = 0;
  this.init();
  this.tiemr = null;
}
Game.prototype.Caiquan = function(){
  this.textValue( &#39;请出拳...&#39; );
  this.BtnDisable();
  this.start();
  this.guess.style.display = &#39;block&#39;;
}
//怎么玩
Game.prototype.start = function(){
  var This = this;
  this.timer = setInterval(function(){
    This.anim[0].className = &#39;anim user guess&#39; +( ( This.num ++ ) % 3 );
    This.anim[1].className = &#39;anim comp guess&#39; + ( ( This.num ++ ) % 3 ) ;
  },500)
}
//初始化名字
Game.prototype.init = function(){
  this.classN[0].innerHTML = &#39;我:&#39; + this.user.name;
  this.classN[1].innerHTML = &#39;电脑:&#39; + 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 =&#39;&#39;;
    this.btn.innerHTML = &#39;再来一次&#39;
  }else{
    this.btn.disabled = true;
    this.btn.className =&#39;disabled&#39;;
  }
}
Game.prototype.verdict = function( point ){
  clearInterval(this.timer);
  var userGu = user.guess(point);
  var compGu = comp.guess();
  this.anim[0].className = &#39;anim user guess&#39; + userGu;
  this.anim[1].className = &#39;anim comp guess&#39; + compGu;
  var res = userGu - compGu;
  switch (res){
    case 0:
    this.textValue(&#39;平局!!!&#39;)
      break;
    case 1:
    case -2:
    this.textValue(&#39;lose~~~&#39;);
    break;
    case 2:
    case -1:
    this.textValue(&#39;win!!!&#39;)
      break;
  }
  this.guess.style.display = &#39;none&#39;;
  this.BtnDisable();
}
var user = new User( &#39;锐雯&#39; );
var comp = new Comp( &#39;拉克丝&#39; );
var game = new Game( user , comp );

위는 모두를 위해 편집한 내용입니다. 희망합니다. 앞으로 모든 사람에게 도움이 될 것입니다.

관련 기사:

vue-admin과 백엔드(flask)의 분리 및 조합에 대한 자세한 해석

jquery+css3을 사용하여 Panda TV 탐색을 구현하는 방법

대화 상자의 시간 제한 숨기기를 구현하는 방법 in jQuery

JS/jQuery에서 사라지거나 표시되기 전에 몇 초 동안 DIV 지연을 구현하는 방법

네이티브 js를 사용하여 지방자치단체의 3단계 연결을 달성합니다

위 내용은 JavaScript를 사용하여 추측 게임을 구현하는 방법(자세한 튜토리얼)의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.