This article mainly introduces the details of the game chess dark chess source code of JS games, analyzes the game source code in more detail, and attaches the complete instance code for your reference, you can refer to the examples in this article to describe the chess and dark chess source code of JS games. The details are as follows:
Shows how the game runs:
Javascript section:
/** Chinese chess * Author: fdipzone * Date: 2012-06-24 * Ver: 1.0 */var gameimg = ['images/a1.gif ', 'images/a2.gif ', 'images/a3.gif ', 'images/a4.gif', 'images/a5.gif ', 'images/a6.gif', 'images/a7.gif ', 'images/b1.gif ', 'images/b2.gif ', 'images/b3.gif', 'images/b4.gif ', 'images/b5.gif', 'images/b6.gif ', 'images/b7.gif ', 'images/bg.gif ', 'images/bg_over.gif', 'images/bg_sel.gif ']; var chess_obj = new ChessClass (); window. onload = function () {$ ('init _ btn '). onclick = function () {chess_obj.init ();} var callback = function () {chess_obj.init ();} img_preload (gameimg, callback);} // chess class function ChessClass () {this. chess = []; this. boardrows = 4; this. boardcols = 8; this. area = 82; this. player = 1; // 1: red 2: green this. selected = null; // selected chess this. chesstype = ['', 'A', 'B']; this. isover = 0;} // init ChessClass. prototype. init = function () {this. reset_grade (); this. create_board (); this. create_chess (); this. create_event (); this. player = 1; this. selected = null; this. isover = 0; disp ('init _ P', 'hide ');} // create board ChessClass. prototype. create_board = function () {var board = ''; for (var I = 0; I
';}}$ ('Board '). innerHTML = board; $ ('board '). style. width = this. boardcols * (this. area + 2) + 'px '; $ ('board '). style. height = this. boardrows * (this. area + 2) + 'px ';} // create random chess ChessClass. prototype. create_chess = function () {// 32 chesses var chesses = ['a1', 'b7 ', 'a2', 'b7 ', 'a2', 'b7 ', 'a3 ', 'b7', 'a3 ', 'b7', 'a4 ', 'b6', 'a4 ', 'b6', 'a5 ', 'b5 ', 'a5 ', 'b5', 'a6 ', 'b4', 'a6 ', 'b4', 'a7 ', 'b3', 'a7 ', 'b3 ', 'a7 ', 'b2', 'a7', 'b2', 'a7 ', 'b1']; this. chess = []; while (chesses. length> 0) {var rnd = Math. floor (Math. random () * chesses. length); var tmpchess = chesses. splice (rnd, 1 ). toString (); this. chess. push ({'chess ': tmpchess, 'type': tmpchess. substr (0, 1), 'val ': tmpchess. substr (1, 1), 'status': 0}) ;}// create event ChessClass. prototype. create_event = function () {var self = this; var chess_area = $ _ tag ('P', 'board'); for (var I = 0; I
Num2) {// redsquare wins $ ('grade _ res2 '). innerHTML = 'loss'; $ ('grade _ res2 '). className = 'loss'; $ ('grade _ res1 '). innerHTML = 'win'; $ ('grade _ res1 '). className = 'win';} else if (num1
Click here to download the complete instance code.
I believe this article provides some reference for you to learn javascript game design.