乌龟抓鸡

乌龟抓鸡游戏

//html文件
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>乌龟抓鸡游戏</title/>
<link rel = "stylesheet" style = "text/css" href = "game.css"/>
<meta charset="UTF-8">
<script>
	  function $(obj){
	  var wugui = document.getElementById(obj);
	      return wugui;
	  }
	  
	 window.onload = function () {
            document.onkeydown = function (event) {
                event = event || window.event;/*||为或语句,当IE不能识别event时候,就执行window.event 赋值*/
                console.log(event.keyCode);
				$("eat").style.opacity = 0.0;
		        $("suc").style.opacity = 0.0;
		        $("chick").style.opacity = 1.0;
		        $("chick1").style.opacity = 1.0;
                switch (event.keyCode){/*keyCode:字母和数字键的键码值*/
                    /*37、38、39、40分别对应左上右下*/
                    case 37:
                        $("wugui").src = "2.png"; 
			            if($('wugui').style.left =="60px"){
			              //window.alert("已经到头了");
					      break;
			            }
			            var left = $('wugui').style.left;
			               left =parseInt(left.substr(0,left.length - 2));
					       $('wugui').style.left = (left -10) + "px";
			            break;
                    case 38:
					     if($('wugui').style.top == "180px"){
					         //window.alert("已经到头了");
						     break;
					     }
			             var top = $('wugui').style.top;
			             top =parseInt(top.substr(0,top.length - 2));
					     $('wugui').style.top = (top -10) + "px";
                         break;
                    case 39:
                        $("wugui").src = "1.png";
			            if($('wugui').style.left == "1100px"){
					      //window.alert("已经到头了");
						   break;
					    }
					    var right = $('wugui').style.left;
			            right =parseInt(right.substr(0,right.length - 2));
					    $('wugui').style.left = (right +10) + "px";
                        break;
                    case 40:
                        if($('wugui').style.top =="440px"){
			                //window.alert("已经到头了");
					      break;
			            }
			            var down = $('wugui').style.top;
			            down =parseInt(down.substr(0,down.length - 2));
					    $('wugui').style.top = (down +10) + "px";
                         break;
			        case 17:
					    //alert("你好");
					    attack();
						break;
                }
            }
        }
	  var flag = 0;
	  function walk(){
	       $("run").style.opacity = 0.0;
	       var top = $("chick").style.top;
		   var left = $("chick").style.left;
		   
		   if(flag == 0 && $("chick").style.top < "220px"&&$("chick").style.left < "800px"){
		       flag = 1;
			   $("chick").src = "5.png";
		       //window.alert("小鸡到尽头");
		   }else if(flag == 0){
		       flag = 2;
		       top =parseInt(top.substr(0,top.length - 2));
			   $("chick").style.top = (top -10) + "px";
			   left =parseInt(left.substr(0,left.length - 2));
			   $("chick").style.left = (left -25) + "px";
		   }
		   
		   if(flag == 1){
		      if($("chick").style.top > "300px" && $("chick").style.left > "1100px"){
		          flag = 0;
				  $("chick").src = "6.png"; 
		      }else{
			      top=parseInt(top.substr(0,top.length - 2));
			      $("chick").style.top = (top +10) + "px";
		          left =parseInt(left.substr(0,left.length - 2));
			      $("chick").style.left = (left +25) + "px";
		      }
		   }
		   if(flag == 2){
		      flag = 0;
		   }
	  }
	  
	  var flag3 = 0;
	  function walk1(){
	       $("run").style.opacity = 0.0;
	       var top = $("chick1").style.top;
		   var left = $("chick1").style.left;
		   
		   if(flag3 == 0 && $("chick1").style.top < "220px"&&$("chick1").style.left < "800px"){
		       flag3 = 1;
			   $("chick1").src = "5.png"; 
		       //window.alert("小鸡到尽头");
		   }else if(flag3 == 0){
		       flag3 = 2;
		       top =parseInt(top.substr(0,top.length - 2));
			   $("chick1").style.top = (top -5) + "px";
			   left =parseInt(left.substr(0,left.length - 2));
			   $("chick1").style.left = (left -10) + "px";
		   }
		   
		   if(flag3 == 1){
		      if($("chick1").style.top > "300px" && $("chick1").style.left > "1100px"){
		          flag3 = 0;
				  $("chick1").src = "6.png"; 
		      }else{
			      top=parseInt(top.substr(0,top.length - 2));
			      $("chick1").style.top = (top +5) + "px";
		          left =parseInt(left.substr(0,left.length - 2));
			      $("chick1").style.left = (left +10) + "px";
		      }
		   }
		   if(flag3 == 2){
		      flag3 = 0;
		   }
	  }
      setInterval("walk()",1000);
	  setInterval("walk1()",2000);
      function move(num){
	      $("eat").style.opacity = 0.0;
		  $("suc").style.opacity = 0.0;
		  $("chick").style.opacity = 1.0;
		  $("chick1").style.opacity = 1.0;
	      switch(num){
		      case 0:if($('wugui').style.top == "180px"){
					    //window.alert("已经到头了");
						break;
					 }
			         var top = $('wugui').style.top;
			         top =parseInt(top.substr(0,top.length - 2));
					 $('wugui').style.top = (top -10) + "px";
			         break;
			  case 1:$("wugui").src = "1.png";
			         if($('wugui').style.left == "1100px"){
					    //window.alert("已经到头了");
						break;
					 }
					 var right = $('wugui').style.left;
			         right =parseInt(right.substr(0,right.length - 2));
					 $('wugui').style.left = (right +10) + "px";
					 break;
			  case 2:if($('wugui').style.top =="440px"){
			           //window.alert("已经到头了");
					   break;
			         }
			         var down = $('wugui').style.top;
			         down =parseInt(down.substr(0,down.length - 2));
					 $('wugui').style.top = (down +10) + "px";
			         break;
			  case 3:$("wugui").src = "2.png"; 
			         if($('wugui').style.left =="60px"){
			           //window.alert("已经到头了");
					   break;
			         }
			         var left = $('wugui').style.left;
			         left =parseInt(left.substr(0,left.length - 2));
					 $('wugui').style.left = (left -10) + "px";
			         break;
		  }
	  }
	  
	  var flag1 = 0;
	  var count1 = 0;
	  function zidan(){
	    if(flag1 == 1){
		   var top1 = $("chick").style.top;
		   var left1 = $("chick").style.left;
		   top1=parseInt(top1.substr(0,top1.length - 2));
		   left1 =parseInt(left1.substr(0,left1.length - 2));
		   
		   var top = $("zidan").style.top;
		   top=parseInt(top.substr(0,top.length - 2));
		   var left = $("zidan").style.left;
		   left =parseInt(left.substr(0,left.length - 2));
		   
		   var top2 = $("wugui").style.top;
		   var left2 = $("wugui").style.left;
		   top2=parseInt(top2.substr(0,top2.length - 2));
		   left2 =parseInt(left2.substr(0,left2.length - 2));
		   //if($("wugui").src.endsWith("1.png")){
		      if(((top > top1) && (top<(top1 + 60)))&& ((left >left1) && (left <(left1 + 100)))){
		         count1 ++;
		         $("zidan").style.opacity=0.0;
			     flag1 =0;
			     $("run").style.top = (top1 - 60) + "px";
			     $("run").style.left = (left - 15) + "px";
			     $("run").style.opacity = 1.0;
			     // window.alert("大鸡成功");
			     if(count1 == 5){
			       $("suc").style.opacity = 0.0;
			       $("eat").style.top = (top2 - 60) + "px";
			       $("eat").style.left = (left2 + 15) + "px";
			       $("eat").style.opacity = 1.0;
			       $("chick").style.opacity = 0.0;
			       count1 = 0;
			    }else{
			       $("suc").style.top = (top2 - 60) + "px";
			       $("suc").style.left = (left2 + 15) + "px";
			       $("suc").style.opacity = 1.0;
			    }
		   }else{
		     //window.alert($("zidan").style.left);
		     $("zidan").style.left = (left+ 40) + "px";
			// window.alert($("zidan").style.left);
			 if(left > 1100){
			    $("zidan").style.opacity=0.0;
			    flag1 =0;
			 }
		   }
		  /* }else if($("wugui").src.endsWith("2.png")){
		      //window.alert("进");
		      if(((top > top1) && (top<(top1 + 60)))&& ((left >left1) && (left <(left1 + 100)))){
			     //window.alert("入1");
		         count1 ++;
		         $("zidan").style.opacity=0.0;
			     flag1 =0;
			     $("run").style.top = (top1 - 60) + "px";
			     $("run").style.left = (left - 15) + "px";
			     $("run").style.opacity = 1.0;
			     // window.alert("大鸡成功");
			     if(count1 == 5){
			       $("suc").style.opacity = 0.0;
			       $("eat").style.top = (top2 - 60) + "px";
			       $("eat").style.left = (left2 + 15) + "px";
			       $("eat").style.opacity = 1.0;
			       $("chick").style.opacity = 0.0;
			       count1 = 0;
			     }else{
			       $("suc").style.top = (top2 - 60) + "px";
			       $("suc").style.left = (left2 + 15) + "px";
			       $("suc").style.opacity = 1.0;
			    }
		      }else{
		         $("zidan").style.left = (left - 40) + "px";
			     if(left <100){
			         $("zidan").style.opacity=0.0;
			         flag1 =0;
			     }
		      }
		   }*/
		   
	    }
	  }
	  
	  var flag2 = 0;
	  var count2 =0;
	  function zidan1(){
	    //$("suc").style.opacity = 0.0;
		//$("eat").style.opacity = 0.0;
	    if(flag2 == 1){
		   var top1 = $("chick1").style.top;
		   var left1 = $("chick1").style.left;
		   top1=parseInt(top1.substr(0,top1.length - 2));
		   left1 =parseInt(left1.substr(0,left1.length - 2));
		   
		   var top = $("zidan").style.top;
		   top=parseInt(top.substr(0,top.length - 2));
		   var left = $("zidan").style.left;
		   left =parseInt(left.substr(0,left.length - 2));
		   
		   var top2 = $("wugui").style.top;
		   var left2 = $("wugui").style.left;
		   top2=parseInt(top2.substr(0,top2.length - 2));
		   left2 =parseInt(left2.substr(0,left2.length - 2));
		   
		   if(((top > top1) && (top<(top1 + 60)))&& ((left >left1) && (left <(left1 + 100)))){
		      count2 ++;
		      $("zidan").style.opacity=0.0;
			  flag2 =0;
			  $("run").style.top = (top1 - 60) + "px";
			  $("run").style.left = (left - 15) + "px";
			  $("run").style.opacity = 1.0;
			  //window.alert("小鸡成功");
			  if(count2 == 5){
			     $("suc").style.opacity = 0.0;
			     $("eat").style.top = (top2 - 60) + "px";
			     $("eat").style.left = (left2 + 15) + "px";
			     $("eat").style.opacity = 1.0;
				 $("chick1").style.opacity = 0.0;
			     count2 = 0;
			 }else{
			    $("suc").style.top = (top2 - 60) + "px";
			    $("suc").style.left = (left2 + 15) + "px";
			    $("suc").style.opacity = 1.0;
			 }
		   }else{
		     $("zidan").style.left = (left+ 40) + "px";
			 if(left > 1100){
			    $("zidan").style.opacity=0.0;
			    flag2 =0;
			 }
		   }
	    }
	  }
	  
	  function attack(){
	     var top = $("wugui").style.top;
		 var left = $("wugui").style.left;
		 top =parseInt(top.substr(0,top.length - 2));
		 left =parseInt(left.substr(0,left.length - 2));
		 var top1 = 0;
		 var left1 = 0;
		 //if($("wugui").src.endsWith("1.png")){
		   top1 = (top +15) + "px";
		   left1 = (left + 140) + "px";
		/* }else{
		   $("zidan").src = "z1.png";
		   top1 = (top +15) + "px";
		   left1 = (left -40) + "px";
		 }*/
		 $("zidan").style.top = top1;
		 $("zidan").style.left = left1;
		 $("zidan").style.opacity=1.0;
		 flag1 =1;
		 flag2 = 1;
	  }
	  
	  setInterval("zidan()",50);
	  setInterval("zidan1()",50);
	  
</script>
</head>
<body>
<div class = "bg">
<span class = "gameName">       乌龟抓鸡  </span><a href = "rule.html" target = "_blank">游戏规则说明</a><br/>
<img src = "4.jpg" class = "img1"/>
<img id = "wugui" src = "1.png" class = "img2" style = "left:60px; top:440px;"/>
<img id = "chick" src = "6.png" class = "img3" style = "opacity:1.0;left:1100px;top:300px;"/>
<img id = "chick1" src = "6.png" class = "img3" style = "opacity:1.0;left:900px;top:400px;"/>
<img src = "8.png" class = "img7"/>
<img src = "7.png" class = "img4"/>
<img id = "zidan" src = "z2.png" class = "img5" style = "left:200px; top:420px; opacity:0.0;"/>
</div>
<div class = "controlcenterdiv">
<table class = "style1">
<tr><td></td><td><input class = "input0" type = "button" value ="" onclick = "move(0)"/></td><td></td></tr>
<tr><td><input class = "input3" type = "button" value = "" onclick = "move(3)"/></td><td><input class = "input" type = "button" value = "乌龟攻击" onclick = "attack()"/></td><td><input class = "input1" type = "button" value = "" onclick = "move(1)"/></td></tr>
<tr><td></td><td><input class = "input2" type = "button" value = "" onclick = "move(2)"/></td><td></td></tr>
</table>
</div>
<img id = "eat" src = "bg3.png" class = "img8" style = "left:200px;top:100px;opacity:0.0;"/>
<img id = "run" src = "bg4.png" class = "img6" style = "opacity:0.0;left:300px;top:100px;"/>
<img id = "suc" src = "bg2.png" class = "img6" style = "opacity:0.0;left:400px;top:100px;"/>
</body>
</html>
//css文件
.gameName{
   font-family:"华文新魏";
   font-size:60px;
   font-weight:30px;
   //color:orange;
   left:30px;
}

.bg{
    margin-top:20px;
    margin-left:50px;
	width:1200px;
	height:500px;
	//border:1px solid red;
}

.img1{
    width:1200px;
	height:430px;
	//border:1px solid blue;
}

.controlcenterdiv{
	width:205px;
	margin-left:500px;
	//border:1px solid black;
}

.style1{
	// border:1px solid green;
     width:200px;
	 height:120px;
	 text-align:center;
}

.input{
    font-family:"华文新魏";
    font-size:14px;
	width:65px;
	height:28px;
	
}
.input0{
    background-image:url(a0.png);
	width:65px;
	height:28px;
	background-size:40% 100%
}
.input1{
    background-image:url(a1.png);
	width:65px;
	height:28px;
	background-size:80% 50%
}
.input2{
    background-image:url(a2.png);
	width:65px;
	height:28px;
	background-size:40% 100%
}
.input3{
    background-image:url(a3.png);
	width:65px;
	height:28px;
	background-size:100% 50%
}

.img2{
    width:150px;
	height:80px;
	position:absolute;
}

.img3{
    width:100px;
	height:60px;
	position:absolute;
	//border:1px solid black;
}

.img4{
    width:150px;
	height:80px;
	position:absolute;
	left:1010px;
	top:200px;
}

.img5{
    width:30px;
	height:8px;
	position:absolute;
	//opacity:1.0;
	//border:1px solid black;
}

.img7{
    width:80px;
	height:50px;
	position:absolute;
	left:1180px;
	top:220px;
}

.img8{
   position:absolute;
   width:200px;
   height:200px;
}

.img6{
   position:absolute;
   width:200px;
   height:130px;
}

a:link{
   color:red;
   font-size:24px;
   font-family:"华文新魏";    /*注意与html文件中的区别,<font face = "华文新魏">...</font>*/
   text-decoration:none;
}
a:hover{
    text-decoration:underline;
	font-size:40px;
	font-family:"华文新魏";
}

运行结果:

在这里插入图片描述
在这里插入图片描述

(纯属练习,存在不足。)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值