This article mainly introduces the JS game's quick run source code details, provides a more detailed explanation of the game's main processes and principles, along with the complete instance source code, for more information, see the examples in this article to describe the quick run source code of JS games. The details are as follows:
Shows how the game runs:
Some Javascript code is as follows:
/** Fast run * Author: fdipzone * Date: 2012-07-15 * Ver: 1.0 */var gameimg = ['images/start.png ', 'images/start_over.png ', 'images/go.png ', 'images/go_over.png', 'images/running.gif ', 'images/run_start1.gif', 'images/run_start2.gif ', 'images/run_start3.gif']; var speed_obj = new SpeedClass (); window. onload = function () {var callback = function () {speed_obj.init ();} img_preload (gameimg, callback);} // Speed Classfunction SpeedClass () {this. levelset = [8, 5, 8, 12]; // difficulty parameter this. playerlist = null; // contestant list this. player = 0; // The selected player this. level = 2; // difficulty this. lock = 0; // lock this. isstart = 0; // whether to start this. isover = 0; // whether to end} // initSpeedClass. prototype. init = function () {this. reset (); this. create_player (); this. create_event ();} // resetSpeedClass. prototype. reset = function () {this. player = 0; this. level = $ ('level '). value; // level this. playerlist = $ _ tag ('lil', 'playerlist'); for (var I = 0; I
'; Playerlist [I] ='
'+ I +'';} $ ('Runway '). innerHTML = runway. join (''); $ ('playerlist '). innerHTML = playerlist. join (''); runway = null; playerlist = null;} // create eventSpeedClass. prototype. create_event = function () {var self = this; this. playerlist = $ _ tag ('lil', 'playerlist'); for (var I = 0; I
= 745) {clearInterval (et); self. isover = 1; self. gameover (o. id) ;}} step ++;}, 350)} // goSpeedClass. prototype. go = function () {if (this. isstart = 1 & this. isover = 0) {var o = $ ('player' + this. player); var exert = Math. floor (Math. random () * 3) + 2; // 2-5 o. style. marginLeft = parseInt (o. style. marginLeft) + exert + 'px ';} return false;} // gameoverSpeedClass. prototype. gameover = function (id) {id = id. Replace ('player', ''); var self = this; var msg =''; if (id = this. player) {msg = "congratulations, the contestant you supported won \ n";} else {msg = "sorry, the contestant you supported did not win, the winner is "+ id +" No. Select \ n ";} if (confirm (msg + 'do you want to start again? ') = True) {setTimeout (function () {self. init ();}, 1000);} else {return false;}/** common function * // get document. getElementBy (id) function $ (id) {this. id = id; return document. getElementById (id);} // get document. getElementsByTagNamefunction $ _ tag (name, id) {if (typeof (id )! = 'Undefined') {return $ (id ). getElementsByTagName (name);} else {return document. getElementsByTagName (name) ;}/ * p show and hide * @ param id dom id * @ param handle show or hide * @ param classname */function disp (id, handle, classname) {if (handle = 'show') {$ (id ). style. display = 'block';} else {$ (id ). style. display = 'none';} if (typeof (classname )! = 'Undefined') {$ (id ). className = classname;}/* img preload * @ param img the Image array to be loaded * @ param callback the callback method after the image is loaded successfully */function img_preload (img, callback) {var onload_img = 0; var tmp_img = []; for (var I = 0, imgnum = img. length; I
Click here to download the complete instance code.
I believe this article provides some reference value for javascript game design.