本文實例講述了JS小游戲的象棋暗棋源碼,分享給大家供大家參考。具體如下:
游戲運行后如下圖所示:

Javascript 部分:
/** 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_div','hide'); } // create board ChessClass.prototype.create_board = function(){ var board = ''; for(var i=0; i<this.boardrows; i++){ for(var j=0; j<this.boardcols; j++){ board = board + '<div id="' + i + '_' + j + '"><img src="images/chessbg.gif" /></div>'; } } $('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('div', 'board'); for(var i=0; i<chess_area.length; i++){ chess_area[i].onmouseover = function(){ // mouseover if(this.className!='onsel'){ this.className = 'on'; } } chess_area[i].onmouseout = function(){ // mouseout if(this.className!='onsel'){ this.className = ''; } } chess_area[i].onclick = function(){ // onclick self.action(this); } } } // id change index ChessClass.prototype.getindex = function(id){ var tid = id.split('_'); return parseInt(tid[0])*this.boardcols + parseInt(tid[1]); } // index change id ChessClass.prototype.getid = function(index){ return parseInt(index/this.boardcols) + '_' + parseInt(index%this.boardcols); } // action ChessClass.prototype.action = function(o){ if(this.isover==1){ // game over return false; } var index = this.getindex(o.id); if(this.selected == null){ // 未選過棋子 if(this.chess[index]['status'] == 0){ // not opened this.show(index); }else if(this.chess[index]['status'] == 1){ // opened if(this.chess[index]['type'] == this.chesstype[this.player]){ this.select(index); } } }else{ // 已選過棋子 if(index != this.selected['index']){ //
主站蜘蛛池模板:
三穗县|
名山县|
翼城县|
彝良县|
乃东县|
同江市|
九江市|
康马县|
界首市|
渭南市|
绥中县|
永川市|
沙河市|
定兴县|
阆中市|
汽车|
棋牌|
托里县|
安泽县|
增城市|
凤城市|
朝阳县|
四会市|
巫溪县|
临洮县|
宁化县|
县级市|
高要市|
蓬溪县|
环江|
苍山县|
五峰|
天台县|
内丘县|
县级市|
外汇|
吐鲁番市|
阿城市|
西吉县|
苏州市|
陇西县|