This article is a mirror article of machine translation, please click here to jump to the original article.

View: 17480|Reply: 8

[JavaScript] A little game made by JavaScript

[Copy link]
Posted on 11/7/2014 10:58:39 PM | | | |
Statement: The technology is a bit scum, the god doesn't hack me, I didn't do the button processing, I can only refresh the page when I hang it
       Here is the code: You can also download the attachment directly
  1. <html>
  2. <head>
  3.     <title></title>
  4.     <script type ="text/javascript">

  5.         //***********封装一个Tank类***
  6.         //当创建一个实例时在页面生成一个Tank
  7.         function Tank(x, y) {
  8.             this.x = x;
  9.             this.y = y;
  10.             this.shape = "~^~";
  11.             //****创建div到页面****
  12.             this.div = document.createElement("div");
  13.             this.div.style.position = "absolute";
  14.             this.div.style.width = "40px";
  15.             this.div.style.height = "40px";
  16.             this.div.style.lineHeight = "20px";
  17.             this.div.style.textAlign = "center";
  18.             this.div.style.backgroundColor = "red";
  19.             this.div.style.left = this.x + "px";
  20.             this.div.style.top = this.y + "px";
  21.             this.div.innerHTML = this.shape;
  22.             document.body.appendChild(this.div);
  23.             //*************************
  24.             this.move = function (left, top) {
  25.                 var h = window.innerHeight || document.documentElement.clientHeight;  // 浏览器内容窗口的高
  26.                 var w = document.documentElement.clientWidth;  // 宽
  27.                 this.x += left;
  28.                 this.y += top;
  29.                 if (this.x + 40 > w) {
  30.                     this.x = w - 40;
  31.                 }
  32.                 if (this.x < 0) {
  33.                     this.x = 0;
  34.                 }
  35.                 if (this.y + 40 > h) {
  36.                     this.y = h - 40;
  37.                 }
  38.                 if (this.y < 0) {
  39.                     this.y = 0;
  40.                 }
  41.                 this.div.style.left = this.x + "px";
  42.                 this.div.style.top = this.y + "px";
  43.             };
  44.             //消失的方法
  45.             this.die = function () {
  46.                 document.body.removeChild(this.div);
  47.             };
  48.         }

  49.         //****************创建*****
  50.         var arr = new Array();
  51.         var i = 0;
  52.         function createTank() {
  53.             var x = document.getElementById("xp").value;
  54.             var y = document.getElementById("yp").value;
  55.             arr[i] = new Tank(parseInt(x), parseInt(y));
  56.             startMove(i);   
  57.             i++;
  58.         }

  59.         var siArr = new Array();//记录下所有回调
  60.         function startMove(num) {
  61.                 //速度与方向**************************
  62.                 var xLen = Math.random() * 20;
  63.                 xLen = parseInt(xLen);  //5
  64.                 var yLen = Math.random() * 20;
  65.                 yLen = parseInt(yLen);  //5
  66.                 //************************
  67.                 siArr[num] = setInterval(function () {
  68.                     arr[num].move(xLen, yLen);
  69.                     if (mouseX >= arr[num].x && mouseX <= arr[num].x + 40 &&
  70.                         mouseY >= arr[num].y && mouseY <= arr[num].y + 40
  71.                     ) {

  72.                         for (var j = 0; j < siArr.length; j++) {
  73.                             clearInterval(siArr[j]);
  74.                         }
  75.                         clearInterval(si);
  76.                         alert("game over!");

  77.                     }

  78.                     var h = window.innerHeight || document.documentElement.clientHeight;  // 浏览器内容窗口的高
  79.                     var w = document.documentElement.clientWidth;  // 宽
  80.                     if (arr[num].x >= w - 40) {
  81.                         xLen = Math.abs(xLen) * (-1);
  82.                     }
  83.                     if (arr[num].x <= 0) {
  84.                         xLen = Math.abs(xLen);
  85.                     }
  86.                     if (arr[num].y >= h - 40) {
  87.                         yLen = Math.abs(yLen) * (-1);
  88.                     }
  89.                     if (arr[num].y <= 0) {
  90.                         yLen = Math.abs(yLen);
  91.                     }
  92.                 }, 10);
  93.         }


  94.         var mouseX = 0;
  95.         var mouseY = 0;
  96.         document.onmousemove = function (e) {
  97.             var ev = e || event;
  98.             mouseX =parseInt( ev.clientX);
  99.             mouseY =parseInt( ev.clientY);
  100.             document.title = "x:" + mouseX + " y:" + mouseY;
  101.         }

  102.         //*********************开始游戏
  103.         var si = null;
  104.         function startGame(v) {
  105.             i = 0;
  106.             v.disabled = true;
  107.             var time = 0;
  108.             si = setInterval(function () {
  109.                 time++;
  110.                 v.value = "时间:" + time + "秒";
  111.             }, 1000);
  112.             var n=document.getElementById("lev").value;
  113.             n=parseInt(n);
  114.             for ( ; i < n; i++) {
  115.                 arr[i] = new Tank(200, 200);
  116.                 startMove(i);
  117.             }
  118.         }
  119.     </script>
  120. </head>
  121. <body >
  122. <center>
  123. <br />看你能玩多久!!
  124. 难度:<input  type ="text" value ="10" id="lev" />
  125. <input type ="button" value="开始" onclick="startGame(this)" />
Copy code




Tank.zip

1.48 KB, Download times: 13, Download credits: -1 prestige, -1 contribution





Previous:C# calls RunDll32.exe clean up browser cache and junk
Next:Send a few Thunder members!
Posted on 11/7/2014 11:17:26 PM |
I still download the attachment and forget it, the game is quite difficult to play, I suggest you adjust the difficulty to 1  
 Landlord| Posted on 11/7/2014 11:20:14 PM |
admin posted on 2014-11-7 23:17
I still download the attachment and forget it, the game is quite difficult to play, I suggest you adjust the difficulty to 1

Change the source code and adjust the time
Posted on 11/13/2014 8:09:28 PM |


Guess I put my mouse there?
Posted on 11/13/2014 9:15:56 PM |
What game? How to play Gameover from the beginning
 Landlord| Posted on 11/14/2014 10:21:24 PM |
Beicheng weaving temperature 22 degrees Posted on 2014-11-13 21:15
What game? How to play Gameover from the beginning

The mouse cannot be hit by a moving tank and hangs as soon as it is hit
 Landlord| Posted on 11/14/2014 10:22:10 PM |
admin posted on 2014-11-13 20:09
Guess I put my mouse there?

What a powerful look
Posted on 11/14/2014 10:22:51 PM |

Actually, I used censorship elements
 Landlord| Posted on 11/14/2014 10:23:50 PM |
admin posted on 2014-11-14 22:22
Actually, I used censorship elements

The all-purpose F12  
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com