java游戏下载象棋暗棋_JS小游戏之象棋暗棋源码详解

本文实例讲述了JS小游戏的象棋暗棋源码,分享给大家供大家参考。具体如下:

游戏运行后如下图所示:

c4e35541ad87a3b1380504dc4f7d2841.gif

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

for(var j=0; j

board = board + '

chessbg.gif
';

}

}

$('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[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']){ // 與selected不是同一位置

if(this.chess[index]['status'] == 0){ // 未打开的棋子

this.show(index);

}else if(this.chess[index]['status'] == -1){ // 點空白位置

this.move(index);

}else{ // 點其他棋子

if(this.chess[index]['type']==this.chesstype[this.player]){

this.select(index);

}else{

this.kill(index);

}

}

}

}

}

// show chess

ChessClass.prototype.show = function(index){

$(this.getid(index)).innerHTML = ''%20+%20this.chess%5Bindex%5D%5B'chess'%5D%20+%20'.gif';

this.chess[index]['status'] = 1; // opened

if(this.selected!=null){ // 清空選中

$(this.getid(this.selected.index)).className = '';

this.selected = null;

}

this.change_player();

this.gameover();

}

// select chess

ChessClass.prototype.select = function(index){

if(this.selected!=null){

$(this.getid(this.selected['index'])).className = '';

}

this.selected = {'index':index, 'chess':this.chess[index]};

$(this.getid(index)).className = 'onsel';

}

// move chess

ChessClass.prototype.move = function(index){

if(this.beside(index)){

this.chess[index] = {'chess':this.selected['chess']['chess'], 'type':this.selected['chess']['type'], 'val':this.selected['chess']['val'], 'status':this.selected['chess']['status']};

this.remove(this.selected['index']);

this.show(index);

}

}

// kill chess

ChessClass.prototype.kill = function(index){

if(this.beside(index)==true && this.can_kill(index)==true){

this.chess[index] = {'chess':this.selected['chess']['chess'], 'type':this.selected['chess']['type'], 'val':this.selected['chess']['val'], 'status':this.selected['chess']['status']};

this.remove(this.selected['index']);

var killed = this.player==1? 2 : 1;

$('grade_num' + killed).innerHTML = parseInt($('grade_num' + killed).innerHTML)-1;

this.show(index);

}

}

// remove chess

ChessClass.prototype.remove = function(index){

this.chess[index]['status'] = -1; // empty

$(this.getid(index)).innerHTML = '';

$(this.getid(index)).className = '';

}

/* check is beside

* @param index 目標棋子index

* @param selindex 执行的棋子index,可为空, 为空则读取选中的棋子

*/

ChessClass.prototype.beside = function(index,selindex){

if(typeof(selindex)=='undefined'){

if(this.selected!=null){

selindex = this.selected['index'];

}else{

return false;

}

}

if(typeof(this.chess[index])=='undefined'){

return false;

}

var from_info = this.getid(selindex).split('_');

var to_info = this.getid(index).split('_');

var fw = parseInt(from_info[0]);

var fc = parseInt(from_info[1]);

var tw = parseInt(to_info[0]);

var tc = parseInt(to_info[1]);

if(fw==tw && Math.abs(fc-tc)==1 || fc==tc && Math.abs(fw-tw)==1){ // row or colunm is same and interval=1

return true;

}else{

return false;

}

}

/* check can kill

* @param index 被消灭的棋子index

* @param selindex 执行消灭的棋子index,可为空, 为空则读取选中的棋子

*/

ChessClass.prototype.can_kill = function(index,selindex){

if(typeof(selindex)=='undefined'){ // 没有指定执行消灭的棋子

if(this.selected!=null){ // 有选中的棋子

selindex = this.selected['index'];

}else{

return false;

}

}

if(this.chess[index]['type']!=this.chesstype[this.player]){

if(parseInt(this.chess[selindex]['val'])==7 && parseInt(this.chess[index]['val'])==1){ // 7 can kill 1

return true;

}else if(parseInt(this.chess[selindex]['val'])==1 && parseInt(this.chess[index]['val'])==7){ // 1 can't kill 7

return false;

}else if(parseInt(this.chess[selindex]['val']) <= parseInt(this.chess[index]['val'])){ // small kill big

return true;

}

}

return false;

}

// change player

ChessClass.prototype.change_player = function(){

if(this.player == 1){

this.player = 2; // to green

$('grade_img2').className = 'img_on';

$('grade_img1').className = 'img';

}else{

this.player = 1; // to red

$('grade_img1').className = 'img_on';

$('grade_img2').className = 'img';

}

}

// reset grade

ChessClass.prototype.reset_grade = function(){

$('grade_img1').className = 'img_on';

$('grade_img2').className = 'img';

$('grade_num1').innerHTML = $('grade_num2').innerHTML = 16;

$('grade_res1').className = $('grade_res2').className = 'none';

$('grade_res1').innerHTML = $('grade_res2').innerHTML = '';

}

// game over

ChessClass.prototype.gameover = function(){

if($('grade_num1').innerHTML==0 || $('grade_num2').innerHTML==0){ // 任一方棋子为0

this.isover = 1;

this.show_grade();

disp('init_div','show');

}else{

if(this.can_action()==false){

this.isover = 1;

this.show_grade();

disp('init_div','show');

}

}

}

// show grade

ChessClass.prototype.show_grade = function(){

var num1 = parseInt($('grade_num1').innerHTML);

var num2 = parseInt($('grade_num2').innerHTML);

if(num1>num2){ // 红方胜

$('grade_res2').innerHTML = 'LOSS';

$('grade_res2').className = 'loss';

$('grade_res1').innerHTML = 'WIN';

$('grade_res1').className = 'win';

}else if(num1

$('grade_res1').innerHTML = 'LOSS';

$('grade_res1').className = 'loss';

$('grade_res2').innerHTML = 'WIN';

$('grade_res2').className = 'win';

}else{ // 平局

$('grade_res1').innerHTML = $('grade_res2').innerHTML = 'DRAW';

$('grade_res1').className = $('grade_res2').className = 'draw';

}

}

// check chess can action

ChessClass.prototype.can_action = function(){

var chess = this.chess;

for(var i=0,max=chess.length; i

if(chess[i].status==0){ // 有未翻开的棋子

return true;

}else{

if(chess[i].status==1 && chess[i].type==this.chesstype[this.player]){ // 己方已翻开的棋子

if(this.beside(i-this.boardcols, i) && (chess[i-this.boardcols].status==-1 || this.can_kill(i-this.boardcols,i) )){ // 上

return true;

}

if(this.beside(i+this.boardcols, i) && (chess[i+this.boardcols].status==-1 || this.can_kill(i+this.boardcols,i) )){ // 下

return true;

}

if(this.beside(i-1, i) && (chess[i-1].status==-1 || this.can_kill(i-1,i) )){ // 左

return true;

}

if(this.beside(i+1, i) && (chess[i+1].status==-1 || this.can_kill(i+1,i) )){ // 右

return true;

}

}

}

}

return false;

}

/** common function */

// get document.getElementBy(id)

function $(id){

this.id = id;

return document.getElementById(id);

}

// get document.getElementsByTagName

function $_tag(name, id){

if(typeof(id)!='undefined'){

return $(id).getElementsByTagName(name);

}else{

return document.getElementsByTagName(name);

}

}

/* div show and hide

* @param id dom id

* @param handle show or hide

*/

function disp(id, handle){

if(handle=='show'){

$(id).style.display = 'block';

}else{

$(id).style.display = 'none';

}

}

/* img preload

* @param img 要加载的图片数组

* @param callback 图片加载成功后回调方法

*/

function img_preload(img, callback){

var onload_img = 0;

var tmp_img = [];

for(var i=0,imgnum=img.length; i

tmp_img[i] = new Image();

tmp_img[i].src = img[i];

if(tmp_img[i].complete){

onload_img ++;

}else{

tmp_img[i].onload = function(){

onload_img ++;

}

}

}

var et = setInterval(

function(){

if(onload_img==img.length){ // 定时器,判断图片完全加载后调用callback

clearInterval(et);

callback();

}

},200);

}

完整实例代码点击此处本站下载。

相信本文所述对大家javascript游戏设计的学习有一定的借鉴价值。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; import java.io.*; //主类 public class Chess{ public static void main(String args[]){ new ChessMainFrame("中国象棋:观棋不语真君子,棋死无悔大丈夫"); } } //主框架类 class ChessMainFrame extends JFrame implements ActionListener,MouseListener,Runnable{ //玩家 JLabel play[] = new JLabel[32]; //棋盘 JLabel image; //窗格 Container con; //工具栏 JToolBar jmain; //重新开始 JButton anew; //悔棋 JButton repent; //退出 JButton exit; //当前信息 JLabel text; //保存当前操作 Vector Var; //规则类对象(使于调用方法) ChessRule rule; /** ** 单击棋子 ** chessManClick = true 闪烁棋子 并给线程响应 ** chessManClick = false 吃棋子 停止闪烁 并给线程响应 */ boolean chessManClick; /** ** 控制玩家走棋 ** chessPlayClick=1 黑棋走棋 ** chessPlayClick=2 红棋走棋 默认红棋 ** chessPlayClick=3 双方都不能走棋 */ int chessPlayClick=2; //控制棋子闪烁的线程 Thread tmain; //把第一次的单击棋子给线程响应 static int Man,i; ChessMainFrame(){ new ChessMainFrame("中国象棋"); } /** ** 构造函数 ** 初始化图形用户界面 */ ChessMainFrame(String Title){ //获行客格引用 con = this.getContentPane(); con.setLayout(null); //实例化规则类 rule = new ChessRule(); Var = new Vector(); //创建工具栏 jmain = new JToolBar(); text = new JLabel("欢迎使用象棋对弈系统"); //当鼠标放上显示信息 text.setToolTipText("信息提示"); anew = new JButton(" 新 游 戏 "); anew.setToolTipText("重新开始新的一局"); exit = new JButton(" 退 出 "); exit.setToolTipText("退出象棋程序程序"); repent = new JButton(" 悔 棋 "); repent.setToolTipText("返回到上次走棋的位置"); //把组件添加到工具栏 jmain.setLayout(new GridLayout(0,4)); jmain.add(anew); jmain.add(repent); jmain.add(exit); jmain.add(text); jmain.setBounds(0,0,558,30); con.add(jmain); //添加棋子标签 drawChessMan(); //注册按扭监听 anew.addActionListener(this); repent.addActionListener(this); exit.addActionListener(this); //注册棋子移动监听 for (int i=0;i<32;i++){ con.add(play[i]); play[i].addMouseListener(this); } //添加棋盘标签 con.add(image = new JLabel(new ImageIcon("image\\Main.GIF"))); image.setBounds(0,30,558,620); image.addMouseListener(this); .................... ....................
目录 摘 要 (中文) I (英文) II 第一章 概述 1 1.1系统开发背景 1 1.2 WEB游戏的优势与特性 2 1.3 系统设计的目标 4 1.4 系统开发与测试环境 4 1.5可行性分析 5 第二章 相关技术与开发工具 6 2.1 J2EE WEB应用基础 6 2.2 AJAX概述 8 2.3 DIV+CSS 10 2.4开发工具 11 第三章 模块需求分析与总体设计 13 3.1 需求分析说明 13 3.2 整体系统用例图 15 3.3整体系统业务流图 15 3.4 系统总体设计框架 16 第四章 系统设计和系统实现 17 4.1 数据库设计 17 4.2 详细模块设计 19 4.3 页面设计 34 第五章 系统实施 37 5.1 系统运行结果 37 5.2 模块测试与评价 40 5.3难点及解决方法 40 结束语 43 参考文献 44 4.1.2 数据库表结构 Chessuser,用户信息表,存储用户的基本信息,其表结构如表4-1: 表4-1用户信息表结构 列名 数据类型 长度 允许空 说明 id int 4 编号,主键 name varchar 50 用户名 password varchar 50 用户密码 Status Varchar 50 用户状态 losecount int 4 输局数 wincount int 4 赢局数 Allcount Int 4 总局数 point int 4 积分 leve varchar 50 等级 Email varchar 50 V Email IdCard Varchar 50 V 身份证号 Chessstatus,棋室表,存储棋室状态信息,其表结构如表4-2: 表4-2棋室表结构 列名 数据类型 长度 空 说明 id Int 4 编号,主键 Status Varchar 50 状态 Redpalyer Varchar 50 V 红方玩家 Blackpalyer Varchar 50 V 黑方玩家 Course Varchar 500 V 棋步 Watcher Varchar 50 观棋者 Whogo Varchar 50 走子方 Boardstatus Varchar 500 棋子信息 Win Varchar 50 胜利方标志 Course,棋局表,存储已经结束的棋局信息,以备以后查询和分析,其表结构如表4-3: 表4-3棋局信息表结构 列名 数据类型 长度 空 说明 Id Int 4 编号,主键 Redplayer varchar 50 红方玩家 blackplayer Varchar 50 黑方玩家 Winner Varchar 50 V 胜利者 Status Varchar 50 结束状态 Playtime Varchar 50 V 游戏时间 Course Varchar 50 V 棋步 Chessid Varchar 50 棋室号

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值