Tank. js
//////////////////////////////////////// /Define the color of the Tank Class. // var herocolor = new array ("# ba9658 ", "# fef26e"); var enemycolor = new array ("#00a2b5", "#00 Fefe "); //////////////////////////////////////// /define the color of the Tank Class ////////////////////////////////// //// // create a bullet class //////// //// // function bullet (X, y, direct, speed) {This. X = x; this. y = y; this. direct = direct; // bullets To this. speed = speed; // bullet speed this. timer = NULL; // whether to activate this by refreshing the bullet continuously. islive = true; // This function that constantly modifies coordinates. run = function run () {// when modifying the coordinates of a bullet, first judge whether the bullet has reached the boundary. If (this. x <= 0 | this. x> = 400 | this. Y <= 0 | this. y> = 300) {// The Bullet stops the window. setinterval (this. timer); // This. islive = false;} else {// modify the coordinate switch (this. direct) {Case 0: This. y-= speed; break; Case 1: This. X + = speed; break; Case 2: This. Y + = speed; break; Case 3: This. x-= speed; break;} document. getelementbyid ("AA "). innertext = "x =" + this. X + "Y =" + this. Y ;}};} /////////////////////////////////// ///// Create the bullet class end ////////////////////////////// /// // start the Tank Class Creation //// /////////////// define a tank function tank (X, y, direct, color) {This. X = x; this. y = y; this. speed = 1; this. direct = direct; this. color = color; // encapsulate a mobile tank as a function this. moveup = function () {This. y-= This. speed; this. direct = 0; // up}; this. moveright = function () {This. X + = This. speed; this. direct = 1; // turn right}; this. movedown = Function () {This. Y + = This. speed; this. direct = 2; // go down}; this. moveleft = function () {This. x-= This. speed; this. direct = 3; // turn left };} //////////////////////////////////////// /create a tank class and end it /////////////////////////////////// /// // start with the Hero class creation ///////// //// // function hero (X, y, direct, color) {// impersonate an object to achieve the inheritance effect. This. tank = tank; // run this. tank (X, Y, direct, color); // adds a function to kill enemies. This. shotenemy = function () {// switch (this. direct) {Case 0: herobullet = new bullet (this. X + 9, this. y, this. direct, 1); break; Case 1: herobullet = new bullet (this. X + 30, this. Y + 9, this. direct, 1); break; Case 2: herobullet = new bullet (this. X + 9, this. Y + 30, this. direct, 1); break; Case 3: herobullet = new bullet (this. x, this. Y + 9, this. direct, 1); break;} // put this bullet object into the array herobullets. push (H Erobullet); // call this method to constantly change the coordinates of bullets. This is a high technical difficulty. In the following startup mode, the timer of each bullet is independent. // if you follow the original method, all bullets share a timer window. setinterval ("herobullet. run () ", 50); var timer = Window. setinterval ("herobullets [" + (herobullets. length-1) + "]. run () ", 50); // assign this timer to this bullet (the JS object is passed by reference !) Herobullet. timer = timer ;};} //////////////////////////////////////// /create Hero class end /////////////////////////////////// /// // start with the enemy class ///////// //// // function enemytank (X, y, direct, color) {// impersonate an object to achieve the inheritance effect. This. tank = tank; // run this. tank (X, Y, direct, color );} //////////////////////////////////////// /create an enemy class /////////////////////////////////// //// // draw Bullet start // function drawherobullet () {// draw all bullets for (VAR I = 0; I
<! -- Let the browser point to an HTML webpage --> <! Doctype HTML>