WestFighting / 西途决斗 升级 - v1.1.0 版

WestFighting / 西途决斗 升级


这次在原来的基础上升级到v1.1.0版,修正或正加了一些功能如下:

1, 修正上下牛仔走动时脚没有动作的情况;
2, 不支持游戏的浏览器提示修改;
3, 族人的活动和死亡数量的实时更新;

4, 暂停后又继续时记录运行时钟钟变快,修改该问题;
5, 辅助对象名称 sysVariables 修改为 gameSystem 比较直观也好记;
6, 响应键盘的事件 keyPress()函数, keyRelease()函数 和 iniGameUI()函数更改,兼容性更好;
7, 修正牛仔向 左上,左下,右上,右下 四个方向行走时牛仔的面部和行走方向的一致性的处理,即westCowboy.move()方法修改;

8, 增加牛仔的虚弱提示,当生命值小于1000时进入虚弱期;提示几秒钟应该避免被群殴的情况, 建议一对一的单打加搏斗作战方式;



WestFighting / 西途决斗 完整代码如下, v1.1.0


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Fighting - HTML5</title>
<style type="text/css">
body{margin:0px; padding:0; overflow-x:hidden;}
body,td,div,span,input,canvas{font-family:verdana,arial; font-size:12px;}
a:link, a:visited{font-family:verdana,arial; font-size:11px; color:#006699; text-decoration:underline;}
a:hover, a:active{font-family:verdana,arial; font-size:11px; color:#FF0033; text-decoration:none;}
#gameContainer{border:1px solid red;}
#gameCanvas{font-size:12px; font-family:verdana,arial; }
#infoPanel{font-family:verdana,arial; font-size:12px; border:1px solid #D63C06; display:block; padding:5px; background-color:#F9EFE9; overflow:hidden;}
#gameInfo{font-family:verdana,arial; font-size:11px; text-align:right; font-weight:normal; display:block; border-bottom:1px solid #999999;padding-bottom:3px;}
#controlPanel{padding:10px 0;}
.num01{font-family:verdana,arial; font-size:10px; padding:0 2px; font-weight:normal;}
.num02{font-family:verdana,arial; font-size:11px; padding:0 2px; font-weight:normal; color:#990000;}
a.a01:link, a.a01:visited{font-family:verdana,arial; font-size:12px; color:#006699; border:1px solid #999999; padding:2px 4px; margin:0 5px; text-decoration:none}
a.a01:hover, a.a01:active{font-family:verdana,arial; font-size:12px; color:#FFFFCC; border:1px solid #999999; padding:2px 4px; margin:0 5px; text-decoration:none; background-color:#990000}
</style>
</head>

<body οnkeydοwn="javascript:keyPress(event);" οnkeyup="javascript:keyRelease(event);">
<div id="gameContainer">
<canvas id="gameCanvas" width="1024">
<div id="gbTip">请使用支持HTML5的浏览器...</div>
</canvas>
</div>
<!--start: info panel-->
<div id="infoPanel">
<div id="gameInfo"><b>West Fighting[西途决斗]</b>, Version:1.0.0, Current Time:<span class="num02" id="cTime">2010-11-30 10:20:32</span>, Run Time:<span class="num02" id="rTime">0000</span>sec, created by <a target="_blank" title="^_^ welcome..." href="http://hi.csdn.net/shenzhenNBA">shenzhenNBA[CSDN]</a>, <a target="_blank" title="^_^ welcome..." href="http://blog.csdn.net/shenzhennba/">Blog</a> 2011-12-01(商用需授权...)</div>
<div id="controlPanel">
Cowboy Life:<span class="num02" id="cowboyLife">120</span>, Position:[X:<span class="num02" id="cowboyPX">0</span>, Y:<span class="num02" id="cowboyPY">0</span>]    
Tribe man Active:<span class="num02" id="tmActive">0</span>, Death:<span class="num02" id="tmDeath">0</span>    
control key: [ ↑,←, ↓, → ] or [ W,A,S,D ], fast + [ Ctrl ]      
<a class="a01" href="javascript:gameStart();" title="开始">Start</a> <a class="a01" href="javascript:gamePause();" title="暂停">Pause</a> <a class="a01" href="javascript:gameContinue();" title="继续">Continue</a> <a class="a01" href="javascript:gameRestart();" title="结束当前重新开始游戏">Restart</a> <a class="a01" href="javascript:gameEnd();" title="结束游戏">End</a>
      <a class="a01" title="游戏说明" href="javascript:void(0);">How to play</a> <span id="num"></span>
</div>
</div>
<!--end: info panel-->
<script language="javascript" type="text/javascript">
//---------------------start: bloodBottle define---------------------
var bloodBottle = {//no finisth yet
	speed:1,
	x:0,
	y:0,
	bloodValue:30,  //all value
	step:5,  		//each time eat value
	touchTimes:0,	//value is [0~4] =4 then hide bottle
	init: function(){
	
	},
	move: function(){
	
	},
};
//---------------------end: bloodBottle define---------------------


//---------------------start: west cowboy define---------------------
var westCowboy={
	cowboyVersion:"1.0.0",  //about the west Cowboy info and refered author
	cowboyPicSRC : "http://hi.csdn.net/attachment/201112/17/65387_1324102531Z4He.png",  
	cowboyPicObj:"",
	gridSize:50,  //width=height=50
	picSN:0,
	cowboyPicAuthor	: "shenzhenNBA",  
	initLifeValue: 5000,
	lifeValue	: 0,
	killValue	: 1,
	speed		: 1,
	qspeed		: 4,
	x			: 0,
	y			: 0,	
	innerXMin	: 0,
	innerYMin	: 0,
	innerXMax	: 0,	
	innerYMax	: 0,
	r			: 20,     //cowboy size:20*20
	isShowTip	: false,  //[show refered info, true=show, false=hide]
	isWeak		: false,
	i			: 2,
	
	init: function(){
		this.lifeValue=this.initLifeValue;
		this.innerXMin=this.r;
		this.innerYMin=this.r;
		//this.innerXMax=	gameSystem.scrW-this.r;
		//this.innerYMax=	gameSystem.scrH-this.r;
		this.innerXMax=$("gameCanvas").width-this.r;
		this.innerYMax=$("gameCanvas").height-this.r;			
		this.x=Math.floor($("gameCanvas").width/2);
		this.y=Math.floor($("gameCanvas").height/2);
		var img=new Image();
		img.src=this.cowboyPicSRC;
		this.cowboyPicObj=img;
	}
};


westCowboy.getSpeed = function(){  //get move speed flag
	return (inputFlag.quick ? this.qspeed : this.speed);  //attend: this = westCowboy
};


westCowboy.showCowboyTip = function(){  
//function to show cowboy refered information
	if(this.isShowTip){
		var c=$("gameCanvas");
		var cxt=c.getContext("2d");
		//cxt.clearRect(this.left+this.r,this.top+this.r,80,25);  //clear area
		cxt.font="10px Arial";
		cxt.fillStyle="#6600FF";   //FF6600
		cxt.fillText("Life:"+this.lifeValue,this.x+this.r*2,this.y+this.r);		
	}
};

westCowboy.showThew = function(){
	if(this.lifeValue<1000 && this.lifeValue>0){
		if(this.isWeak){
			var c=$("gameCanvas");
			var cxt=c.getContext("2d");
			//cxt.clearRect(this.left+this.r,this.top+this.r,80,25);  //clear area
			cxt.font="10px Arial";
			cxt.fillStyle="#6600FF";   //FF6600
			cxt.fillText(this.lifeValue+", weak, fihging by 1:1 ...",this.x+this.r*2,this.y+this.r*2);		
		}	
	}
};

westCowboy.move=function(){
	if(this.lifeValue>0){
		if (inputFlag.right)westCowboy.x+=westCowboy.getSpeed();
		if (inputFlag.left)	westCowboy.x-=westCowboy.getSpeed();
		if (inputFlag.down)	westCowboy.y+=westCowboy.getSpeed();
		if (inputFlag.up)	westCowboy.y-=westCowboy.getSpeed();
	}
	$("cowboyLife").innerHTML=westCowboy.lifeValue;
	$("cowboyPX").innerHTML=westCowboy.x;
	$("cowboyPY").innerHTML=westCowboy.y;
	
	if (westCowboy.x > westCowboy.innerXMax) westCowboy.x=westCowboy.innerXMax;
	
	//if (westCowboy.x<westCowboy.innerXMin)	westCowboy.x=westCowboy.innerXMin;
	if (westCowboy.x < westCowboy.r) westCowboy.x=westCowboy.innerXMin;
	
	if (westCowboy.y > westCowboy.innerYMax) westCowboy.y=westCowboy.innerYMax;
	
	//if (westCowboy.y<westCowboy.innerYMin)	westCowboy.y=westCowboy.innerYMin;
	if (westCowboy.y < westCowboy.r) westCowboy.y=westCowboy.innerYMin;
	
	var c=$("gameCanvas");
	var cxt=c.getContext("2d");		
	
	//format: drawImage(image, sx, sy, sw, sh, dx, dy, dw, dh)	
	if(this.lifeValue>0){ //not face touch and status is: Move or Stop, include [上下左右,左上,左下,右上,右下]
		if(inputFlag.left)        //move to left
		cxt.drawImage(westCowboy.cowboyPicObj,westCowboy.gridSize*westCowboy.picSN,westCowboy.gridSize*3,westCowboy.gridSize,westCowboy.gridSize,this.x,this.y,westCowboy.gridSize,westCowboy.gridSize);   
		else if(inputFlag.right)  //move to right
		cxt.drawImage(westCowboy.cowboyPicObj,westCowboy.gridSize*westCowboy.picSN,westCowboy.gridSize*4,westCowboy.gridSize,westCowboy.gridSize,this.x,this.y,westCowboy.gridSize,westCowboy.gridSize);   
		else if(inputFlag.up)     //move to up
		cxt.drawImage(westCowboy.cowboyPicObj,westCowboy.gridSize*westCowboy.picSN,westCowboy.gridSize*3,westCowboy.gridSize,westCowboy.gridSize,this.x,this.y,westCowboy.gridSize,westCowboy.gridSize);   
		else if(inputFlag.down)   //move to down
		cxt.drawImage(westCowboy.cowboyPicObj,westCowboy.gridSize*westCowboy.picSN,westCowboy.gridSize*4,westCowboy.gridSize,westCowboy.gridSize,this.x,this.y,westCowboy.gridSize,westCowboy.gridSize);   
		else  //stop move
		cxt.drawImage(westCowboy.cowboyPicObj,westCowboy.gridSize*3,westCowboy.gridSize*3,westCowboy.gridSize,westCowboy.gridSize,this.x,this.y,westCowboy.gridSize,westCowboy.gridSize);   
		
		westCowboy.picSN+=1;
		if(westCowboy.picSN>=4) westCowboy.picSN=0;
	}else{
		cxt.drawImage(westCowboy.cowboyPicObj,westCowboy.gridSize*4,westCowboy.gridSize*3,westCowboy.gridSize,westCowboy.gridSize,this.x,this.y,westCowboy.gridSize,westCowboy.gridSize);   
	}
	
	/* for TEST, ONLY draw a dot */
	/*
	cxt.fillStyle="#666666";  //draw the RED dot   //, FF0000
	cxt.beginPath();
	cxt.arc(westCowboy.x,westCowboy.y,westCowboy.r,0,Math.PI * 2,true);  //draw a dot; syntax: context.arc(centerX, centerY, radius, startingAngle, endingAngle, antiClockwise);
	cxt.closePath();
	cxt.fill();	
	*/
	
	this.showCowboyTip();
	this.showThew();
};

westCowboy.eat= function(){
//function to eat blood when touch blood Bottle
	
	
}
//---------------------end: west cowboy define-----------------------


//--------------------start: keep tribe move time define-------------
var tribeManMoveTimeController={
//function to controll the tribe man keep move in old direction some time and then change the move direction
//设置在上一次方向上持续移动的时间,超过该时间后,动态修正方向[以牛仔为目标]再持续在该方向上移动指定时间在修正方向,一直循环这个过程到死忙
//keepMoveTime 时间越短动态改变方向越快,朝牛仔目标移动越灵敏
	i:0,
	weakTipChangeTime:2,
	weakTipShowTime:2,
	t:0,
	timeControllerID:"",		
	timeOn:function(){		
		//----------------start: about tribe man time control--------------------
		for(this.t=0;this.t<gameSystem.tribeMen.length;this.t++){
			gameSystem.tribeMen[this.t].k+=1;			
			if(gameSystem.tribeMen[this.t].k==gameSystem.tribeMen[this.t].keepMoveTime){			
			gameSystem.tribeMen[this.t].isChangeDirection=true;
			gameSystem.tribeMen[this.t].k=0;
			}else{
			gameSystem.tribeMen[this.t].isChangeDirection=false;
			}								
		}		
		//----------------end: about tribe man time control--------------------
		
		//----------------start: about cowboy weak tip time control--------------------
		this.i++;
		if(this.i > this.weakTipChangeTime){
			if(this.i >= this.weakTipChangeTime + this.weakTipShowTime) this.i = 0;
			westCowboy.isWeak=true;		
		}else
			westCowboy.isWeak=false;					
		//----------------end: about cowboy weak tip time control--------------------
	}
};
//--------------------end: keep tribe move time define---------------


//---------------------start: tribe man define-----------------------
var tribeMan=function(){
	//tribeman properties difine area
	this.tribeManVersion="1.0.0";  //about the Tribe Man info and refered author,
	this.tribeManPicSRC= "http://hi.csdn.net/attachment/201112/17/65387_1324102531Z4He.png";  		   //tribe man pic
	this.tribeManPicAuthor="shenzhenNBA";
	this.gridSize=50;   //width=height=50
	this.tribeManPicObj="";
	this.picSN=0;

	this.lifeValue=800;		//value is [800~4000],default=80
	this.killValue=1;  		//value is [1~3],default=1
	this.x=0;
	this.y=0;
	this.r=20;				//tribe man size:20x20
	this.speed=1;			//value is [1~3],default=1
	this.diffX=0;   		//offset in X axis
	this.diffY=0;			//offset in Y axis
	this.moveAngle=0;		//move angle
	this.isXAddFlag=true;	//
	this.isYAddFlag=true;	//
	this.canvasW=$('gameCanvas').width;
	this.canvasH=$('gameCanvas').height;	
	this.isChangeDirection=true;   //change direction [ true = can change, false= can not change ]
	this.k=0;  					    //temperate variable
	this.keepMoveTime=3;     //keep move in old direction time(unit:s), random 
	this.isShowLifeValue=false;  //show life value [true=show, false=hide(default)]

	//tribeman init area
	this.rangeRandom=function(minValue, maxValue){		
		if(maxValue >= minValue)
		return Math.floor(Math.random()*(maxValue-minValue+1)+minValue);
		else
		return Math.floor(Math.random()*(minValue-maxValue+1)+maxValue);		
	};
	
	this.rangeRandom2=function(minValue, maxValue,maxValuePercent){
		//function to get value, maxValue [percent = maxValuePercent ], minValue [percent = 1-maxValuePercent ]
		var mvPer=0;
		var v=0;
		try{ mvPer=parseFloat(maxValuePercent); }catch(e){ mvPer=0.8; }
		if(Math.random()>=mvPer) 
		v=maxValue;
		else
		v=minValue;
		return v;
	};
	
	this.init=function(){			
		//----------------------?????----------------------------------------
		var img=new Image();
		img.src=this.tribeManPicSRC;
		this.tribeManPicObj=img;
		this.keepMoveTime=this.rangeRandom(2,5);  //keep move in old direction time(unit:s), random 

		this.lifeValue=this.rangeRandom(80,100);  //attend: this = tribe
		this.killValue=this.rangeRandom(1,3);
		this.speed=this.rangeRandom2(1,3,0.75);
		
		this.moveAngle=Math.floor(Math.random()*91);  	//angle value is [0~91]
		var maxR=Math.floor(Math.sqrt(this.canvasW*this.canvasW+this.canvasH*this.canvasH));	  	//用勾股定律求画布canvas的对角线的长度
		var randomR=this.rangeRandom(maxR*0.5,maxR);	//value is [maxR/2 ~ maxR]
		
		//get X,Y axis position here //以对角线为最大值随机取值,以90为最大角度随机取角度,分别用三角函数求出XY值
		this.x=randomR*Math.sin(this.moveAngle);		//x value is [0~maxR]		
		this.y=randomR*Math.cos(this.moveAngle);		//x value is [0~maxR]
		if(this.x>this.canvasW) this.x=this.canvasW;  //限定在 canvas 内部
		if(this.x<0) this.x=0;
		if(this.y>this.canvasH) this.y=this.canvasH;
		if(this.y<0) this.y=0;
		
		//判断所在像限:左上[+X,+Y],右上[-X,+Y],左下[+X,-Y],右下[-X,-Y],确定增量的方向,
		if(this.x<this.canvasW/2 && this.y<=this.canvasH/2){  //左上,含Y中间左轴线
			this.isXAddFlag=true;
			this.isYAddFlag=true;
		}else if(this.x>this.canvasW/2 && this.y<=this.canvasH/2){  //右上,含Y中间右轴线 
			this.isXAddFlag=false;
			this.isYAddFlag=true;
		}else if(this.x<=this.canvasW/2 && this.y>this.canvasH/2){  //左下,含X中间下轴线
			this.isXAddFlag=true;
			this.isYAddFlag=false;
		}else{ //右下
			this.isXAddFlag=false;
			this.isYAddFlag=false;
		}
		
		//设置 XY 两个方向的增量方式:增或减,以及位移量
		this.diffX=Math.sin(Math.PI/180*this.moveAngle)*this.speed;
		this.diffX=this.isXAddFlag ? this.diffX : 0-this.diffX ;
		this.diffY=Math.cos(Math.PI/180*this.moveAngle)*this.speed;
		this.diffY=this.isYAddFlag ? this.diffY : 0-this.diffY;	
		if(this.diffX < 0.001) this.diffX=Math.random();  //diffX,diffY > 0.001, //set  the min diffX,diffY
		if(this.diffY < 0.001) this.diffY=Math.random();
		//alert('tribe init...');   //for TEST
	};
	
	//tribeman Action area			
	this.move=function(){
	//function to move tribemen, if death then can not move more.		
		var c=$('gameCanvas');
		var cxt=c.getContext("2d");		
		
		if(this.lifeValue>0){ //active lefe
			var ftFlag=this.faceTouch();			
			if(ftFlag){  //face touch	
				//face touch then fighting
				cxt.drawImage(this.tribeManPicObj,this.gridSize*this.picSN,this.gridSize*2,this.gridSize,this.gridSize,this.x,this.y,this.gridSize,this.gridSize);   
				if(westCowboy.lifeValue>0)
				cxt.drawImage(westCowboy.cowboyPicObj,westCowboy.gridSize*westCowboy.picSN,westCowboy.gridSize*5,westCowboy.gridSize,westCowboy.gridSize,westCowboy.x,westCowboy.y,westCowboy.gridSize,westCowboy.gridSize);   
				else
				cxt.drawImage(westCowboy.cowboyPicObj,westCowboy.gridSize*4,westCowboy.gridSize*3,westCowboy.gridSize,westCowboy.gridSize,westCowboy.x,westCowboy.y,westCowboy.gridSize,westCowboy.gridSize);   
			}else{ //no face touch, move continue

				if(westCowboy.lifeValue<=0){ //cowboy death then tribe man walk free
					//-----start: walk free-----
					if(this.x<0) this.diffX = Math.abs(this.diffX);
					if(this.x>this.canvasW) this.diffX = -Math.abs(this.diffX);
					if(this.y<0) this.diffY = Math.abs(this.diffY);
					if(this.y>this.canvasH) this.diffY = -Math.abs(this.diffY);
					//-----end: walk free-----
				}else{				
					//-----start: walk to cowboy-----
					if(this.isChangeDirection){  
						if(westCowboy.x >= this.x) 
						this.diffX = Math.abs(this.diffX);
						else
						this.diffX = -Math.abs(this.diffX);				
						if(westCowboy.y >= this.y) 
						this.diffY = Math.abs(this.diffY);
						else
						this.diffY = -Math.abs(this.diffY);
					}						
					//-----end: walk to cowboy-------
				}
				this.x=this.x+this.diffX;   //attend: this = tribe
				this.y=this.y+this.diffY;		
				
				//format: drawImage(image, sx, sy, sw, sh, dx, dy, dw, dh)	
				if(westCowboy.x >= this.x)  //move to right
				cxt.drawImage(this.tribeManPicObj,this.gridSize*this.picSN,this.gridSize*0,this.gridSize,this.gridSize,this.x,this.y,this.gridSize,this.gridSize);   
				else  //move to left
				cxt.drawImage(this.tribeManPicObj,this.gridSize*this.picSN,this.gridSize*1,this.gridSize,this.gridSize,this.x,this.y,this.gridSize,this.gridSize);   
			}		
			this.picSN+=1;
			if(this.picSN>=4) this.picSN=0;
		}else{  //death
			cxt.drawImage(this.tribeManPicObj,this.gridSize*4,0,this.gridSize,this.gridSize,this.x,this.y,this.gridSize,this.gridSize);   
		}
		
		
		/*
		//only for TEST		
		cxt.fillStyle="#DDDDDD";  //, 0000FF
		cxt.beginPath();
		cxt.arc(this.x,this.y,this.r,0,Math.PI*2,true);			
		cxt.closePath();
		cxt.fill();			
		*/
		
		if(this.isShowLifeValue){
			cxt.font="10px Arial";
			cxt.fillStyle="#006600";
			cxt.fillText("Life: "+this.lifeValue,this.x+this.r*2,this.y+this.r);	
		}
	};

	this.faceTouch=function(){
	//function to jugme tribe man face touch cowboy or else,[true=face touch, false=no touch]		
		var dx = Math.abs(westCowboy.x-this.x);
		var dy = Math.abs(westCowboy.y-this.y);
			
		var dl=dx*dx+dy*dy;		
		var tl=(this.r*this.r+this.r*this.r)*2;   //attend the Size of tribe man and cowboy should be the same

		if(dl<tl){  
			return true;
		}else{			
			return false;		
		}		
	};
	

	this.showTribeTip=function(){
	//function to show the tribe man information
		var c=$('gameCanvas');
		var cxt=c.getContext("2d");		
		cxt.font="normal normal 11px verdana";
		//show tribe man lefe value
		cxt.fillStyle="#0000FF";
		cxt.fillText("Life:"+this.lifeValue,this.x+this.r*2,this.y);	//+this.r
		//show cowboy lefe value
		cxt.fillStyle="#FF0000";
		//cxt.clearRect(westCowboy.x+westCowboy.r,westCowboy.y-11,10*6,11);  //format:context.clearRect(x,y,width,height);  //clear the block first
		cxt.fillText("Life:"+westCowboy.lifeValue,westCowboy.x+westCowboy.r*2,westCowboy.y);		//westCowboy.r
	};
	

	this.showFightTip=function(){
	//function to show the tribe man information
		var c=$('gameCanvas');
		var cxt=c.getContext("2d");		
		cxt.font="normal normal 11px verdana";
		//show tribe man fighting Words
		cxt.fillStyle="#0000FF";
		if(this.lifeValue>0) 
		cxt.fillText("kill... kill...",this.x+this.r*2,this.y+this.r/2);	
		else
		cxt.fillText("death...",this.x+this.r*2,this.y+this.r/2);	
		
		//show cowboy fighting Words
		cxt.fillStyle="#FF0000";
		cxt.font="normal normal 11px verdana";
		//cxt.clearRect(westCowboy.x+westCowboy.r,westCowboy.y-11,10*6,11);  //format:context.clearRect(x,y,width,height);  //clear the block first
		if(westCowboy.lifeValue>0)
		cxt.fillText("fighting...fighting...",westCowboy.x+westCowboy.r*2,westCowboy.y+this.r/2);		
		else
		cxt.fillText("death...",westCowboy.x+westCowboy.r*2,westCowboy.y+this.r/2);		
	};

	this.fighting=function(){
	//functionto fighting with cowboy here if face touch
		var ft=this.faceTouch();		
		if(ft){ //face touch here					
			if(this.lifeValue>0){
				if(westCowboy.lifeValue>0) westCowboy.lifeValue = westCowboy.lifeValue - this.killValue;			
				if(westCowboy.lifeValue<0) westCowboy.lifeValue=0;
			}
			
			if(westCowboy.lifeValue>0){
				if(this.lifeValue>0) this.lifeValue = this.lifeValue - westCowboy.killValue;
				if(this.lifeValue<0) this.lifeValue=0;
			}
			
			if(westCowboy.lifeValue>0){
				this.showTribeTip();
				this.showFightTip();
			}
		}else{  //face touch here			
			//no fighting....
		}
	};
};
//---------------------end: tribe man define-------------------------




//--------------------start: global function define------------------
var $ = function(id){
	return "string"==typeof(id) ? document.getElementById(id) : id ;
}


//定义键盘值
var KEY = { D: 68, W: 87, A: 65, S:83, DOWN:40, RIGHT:39, UP:38, LEFT:37, QUICK:17, C:67, Z:90,X:88};

//定义输入标志对象
var inputFlag = {
	right	: false,
	up		: false,
	left	: false,
	down	: false,
	quick	: false
};

//按键按下相应动作修改
var keyPress = function(event){
	var e = event || window.event;
	var code = e.keyCode | e.which | e.charCode;	
	switch(code) {
		case KEY.RIGHT:
		case KEY.D: inputFlag.right = true; break;
		case KEY.UP:
		case KEY.W: inputFlag.up = true; break;
		case KEY.LEFT:
		case KEY.A: inputFlag.left = true; break;
		case KEY.DOWN:
		case KEY.S: inputFlag.down = true; break;
		case KEY.QUICK: inputFlag.quick = true; break;
		case KEY.C: westCowboy.isShowTip=true; break;
		case KEY.X: gameSystem.showTribeMenLife(); break;		
	}
};

//按键放开相应动作修改
var keyRelease = function(event) {	
	var e = event || window.event;
	var code = e.keyCode | e.which | e.charCode;	
	switch(code) {
		case KEY.RIGHT:
		case KEY.D: inputFlag.right = false; break;
		
		case KEY.UP:
		case KEY.W: inputFlag.up = false; break;
		
		case KEY.LEFT:
		case KEY.A: inputFlag.left = false; break;
		
		case KEY.DOWN:
		case KEY.S: inputFlag.down = false; break;
		
		case KEY.QUICK: inputFlag.quick = false; break;
		
		case KEY.C: 
		westCowboy.isShowTip=false; break;
		
		case KEY.Z: 
		if(westCowboy.lifeValue >= westCowboy.initLifeValue)
		westCowboy.lifeValue = westCowboy.initLifeValue;
		else		
		westCowboy.lifeValue+=2; 		
		break;
		
		case KEY.X:
		gameSystem.hideTribeMenLife();
		break;
	}
};


var gameSystem = {   
//function to get game system variables 
	gameName : "West Fighting......",   //about game information 
	gameNameCN: "西途决斗......",
	gameVersion : "1.1.0",     			//first: v1.0.0
	gameLastUpdate : "2011-12-23",
	gameAuthor : "shenzhenNBA",
	gameAuthorIM: "",
	gameAuthorEmail: "qyhzqw@163.com",
	
	//scrW : screen.width,
	//scrH : screen.height,		
	scrW : (document.compatMode == "CSS1Compat" ? document.documentElement.clientWidth : document.body.clientWidth) || screen.availWidth,
	scrH : (document.compatMode == "CSS1Compat" ? document.documentElement.clientHeight : document.body.clientHeight) || screen.availHeight,	
	
	tribeManNum:30,
	tribeMen:[],	
	runTime: 0,
	runTimerID : "",
	gameFrameSpeed: 20,  //PFS, refresh speed
	gamePlayerID: "",
	i:0,
	activeTManNum:0,
	ua: navigator.userAgent,
	
	isSupportHTML5: function(){
		var bn,bv,v;
		try{
			bn=this.getBrowserName().toLowerCase();
			bv=parseFloat(this.getBrowserVersion());			
		}catch(e){}
		//first Support HTML5: IE v=9,Firefox v=3.5, Chrome v=3.5,Opera v=10.5, Safari v=3.0 //找的,不知是否对
		if(bn=="ms ie" && bv>=9){
			v=true;
		}else if(bn=="firefox" && bv>=3.5){
			v=true;
		}else if(bn=="chrome" && bv>=3.5){
			v=true;
		}else if(bn=="opera" && bv>=3.5){
			v=true;
		}else if(bn=="safari" && bv>=3.0){
			v=true;
		}else{
			v=false;
		}
		return v;
	},		
	getBrowserName: function(){
		var uai=this.ua.toLowerCase();   	
		var bn='';	
		if(uai.indexOf('msie')>=0) bn="MS IE";
		if(uai.indexOf('firefox')>=0) bn="FireFox";
		if(uai.indexOf('chrome')>=0) bn="Chrome";
		if(uai.indexOf('opera')>=0) bn="Opera";
		if(uai.indexOf('safari')>=0) bn="Safari";
		if(bn=="") bn="Unknow browser";
		return bn;
	},	
	getBrowserVersion:function(){
		var v='';
		var uai=this.ua.toLowerCase();   
		if(s = uai.match(/msie ([\d.]+)/))  v = RegExp.$1 ; 
		if(s = uai.match(/firefox\/([\d.]+)/))  v = RegExp.$1 ;
		if(s = uai.match(/chrome\/([\d.]+)/))  v = RegExp.$1 ;
		if(s = uai.match(/opera.([\d.]+)/))  v = RegExp.$1 ;
		if(s = uai.match(/version\/([\d.]+).*safari/))  v = RegExp.$1; 		
		if(v=="") v='unknow version';
		return v;
	},
	showTribeMenLife: function(){
		var n=0;
		for(n=0;n<gameSystem.tribeMen.length;n++){
		gameSystem.tribeMen[n].isShowLifeValue=true;
		}
	},
	hideTribeMenLife: function(){
		var n=0;
		for(n=0;n<gameSystem.tribeMen.length;n++){
		gameSystem.tribeMen[n].isShowLifeValue=false;
		}
	}
};


var gameUIFace=function(objCanvas){  
//function to create game UI face 
	var oGC=objCanvas;
	//var oGC=$('gameCanvas');     //get object directly for Test
	var cxt=oGC.getContext("2d");
	var cx,cy,r1,r2;
	r1=30;	
	try{ cx=oGC.width/2; }catch(e){ cx=screen.width/2; };
	try{ cy=oGC.height/2; }catch(e2){ cy=screen.height/2; };
	try{ r2=oGC.height/2; }catch(e3){ r2=screen.height/2; };
	/*  */
	//----start: draw radial/gradient circle ----
	var rg=cxt.createRadialGradient(cx,cy,r1,cx,cy,r2);  //create object type
	rg.addColorStop(0,'#FF6600');  //set the start position color
	rg.addColorStop(1,'#FAFAFA');  //set the end position color
	cxt.fillStyle=rg;  			   //set the pen style 
	//cxt.strokeStyle=rg;		   //会使用单色黑色绘制,应该使用 fillStyle 
	cxt.beginPath();
	cxt.arc(cx,cy,r2,0,Math.PI*2,true);
	cxt.closePath();
	cxt.fill();	
	//----end: draw radial/gradient circle ----
	
	//----start: draw cowboy image ----
	var img=new Image();
	img.src="http://hi.csdn.net/attachment/201112/17/65387_13241032476Ybf.png";  	
	cxt.globalAlpha=0.5;    //value is [0~1],0=hide[disappear], 1 = show [clear all]
	cxt.drawImage(img,cx/6,-20);
	cxt.globalAlpha=1;
	//----end: draw cowboy image ----	
	
	//----start: draw gradient line ----
	var i=0,n=100,step=0;
	try{ step=Math.floor(oGC.width/n); }catch(e4){ step=Math.floor(screen.width/n);}	
	for(i=0;i<=n;i++){
		var lg=cxt.createLinearGradient((cx-n/2+i),cy,i*step,cy+300);
		lg.addColorStop(0,'#FF3300');
		lg.addColorStop(1,'#FFFFFF');   //0099CC
		//cxt.fillStyle=lg;		//单色黑色绘制
		cxt.strokeStyle=lg;		//多种颜色绘制
		cxt.beginPath();
		cxt.moveTo((cx-n*2+i*4),cy+n/4);   //set the start position
		//cxt.moveTo((cx-n/2+i),cy);
		//cxt.moveTo(cx,cy);
		
		//cxt.lineCap = 'round' ;
		if((i % 5)==0) 
		cxt.lineWidth=2;   //set the line size
		//cxt.lineJoin ="miter";
		else
		cxt.lineWidth=1;
		cxt.lineTo(i*step,cy+300);
		cxt.stroke();
		//cxt.fill();
		cxt.closePath();		
	}
	//----end: draw gradient line ------
	
	//----start: draw game Title ------	
	var moveTXTPos=60;  //about game EN Name
	cxt.font="italic bold 45px Arial Black";   //Verdana ,format: context.font="style variant weight size line-height family"
	cxt.fillStyle="#0000FF";
	cxt.shadowColor ="#FFFF99";  //"rgb(255,0,0)"   //99CCFF
	cxt.shadowOffsetX=4;
	cxt.shadowOffsetY=3;		
	cxt.shadowBlur=3;
	cxt.fillText(gameSystem.gameName,cx-moveTXTPos*4,cy-40); 
	
	cxt.font="italic bold 24px 黑体";   //Verdana,Arial Black,,华文新魏  //about game CN Name
	cxt.shadowColor="#FFFF99"; 
	cxt.shadowOffsetX=2;
	cxt.shadowOffsetY=2;
	cxt.shadowBlur=2;
	cxt.fillStyle="#660066";
	cxt.fillText(gameSystem.gameNameCN,cx-moveTXTPos*1.5,cy);	
	
	cxt.font="italic normal 11px verdana";   //about game version and author
	cxt.shadowOffsetX=0;
	cxt.shadowOffsetY=0;
	cxt.shadowBlur=1;
	cxt.fillStyle="#FF0000";
	cxt.fillText("Version: "+gameSystem.gameVersion+'  Author: '+gameSystem.gameAuthor,cx+moveTXTPos/4,cy+20);	
	//----end: draw game Title ------	
	
}


var initGameUI=function(){
//function to initialize  Game UI here
	//var scrW=screen.width;
	//var scrH=screen.height;	
	
	//var canvasW=scrW-8;	
	//var canvasH=scrH-infoPanelH-xBarH;	
	
	var infoPanelH=80;	
	//var xBarH=120;    //height of x Bars	
	
	var canvasW=gameSystem.scrW-6; 
	var canvasH=gameSystem.scrH-infoPanelH-20;
	
	var paddingLR=5;  //padding of left and right in infoPanel
	
	var bn='',bv='';
	bn=gameSystem.getBrowserName().toLowerCase();
	bv=parseFloat(gameSystem.getBrowserVersion());
	var oGC='';
	//first Support HTML5: IE v=9,Firefox v=3.5, Chrome v=3.5,Opera v=10.5, Safari v=3.0 //找的,不知是否对
	if((bn=="ms ie" && bv<9) || (bn=="firefox" && bv<3.5) || (bn=="chrome" && bv<3.5) || (bn=="opera" && bv<10.5) || (bn=="safari" && bv<3.0)){  //support HTML5 go here
		oGC=$('gameContainer');
		var tempHTML='';
		tempHTML="<br><p><b> Error:</b></p> 您的浏览器是 "+bn+" ,版本是 "+bv +" ,还不支持 HTML5; ";
		tempHTML+=" 请使用下列(或更高版本)支持 HTML5 的的浏览器:<br>";
		tempHTML+="<ul><li>MS IE, version = 9 或更高版本</li>";
		tempHTML+="<li>Firefox, version = 3.5 或更高版本</li>";
		tempHTML+="<li>Chrome, version = 3.5  或更高版本</li>";
		tempHTML+="<li>Opera, version = 10.5 或更高版本</li>";
		tempHTML+="<li>Safari, version = 3.0 或更高版本</li></ul>";
		oGC.innerHTML=tempHTML;
		oGC.style.width=canvasW+"px";
		oGC.style.height=canvasH+"px";	
		
	}else{  //support HTML5 go here
		oGC=$('gameCanvas');
		//oGC.style.width=canvasW+"px";
		//oGC.style.height=canvasH+"px";	
		oGC.width=canvasW;
		oGC.height=canvasH;	
		gameUIFace(oGC);		
	}
	
	var oIP=$('infoPanel');
	oIP.style.left="0";	
	oIP.style.top=canvasH+2;		
	oIP.style.position="fixed";			
	oIP.style.zIndex=8000;
	oIP.style.width=(canvasW-paddingLR*2)+"px";	 //canvasW+"px";	
	oIP.style.height=infoPanelH+"px";		
};

var curTime=function(){ //function to display current system time
	var dt=new Date()
	var strHTML=dt.getFullYear()+"-"+(dt.getMonth()+1<10 ? ('0'+(dt.getMonth()+1)): dt.getMonth()+1);
	strHTML+="-"+(dt.getDate()<10 ? '0'+dt.getDate() : dt.getDate());
	strHTML+=" "+(dt.getHours()<10 ? '0'+dt.getHours() : dt.getHours());
	strHTML+=":"+(dt.getMinutes()<10 ? '0'+dt.getMinutes(): dt.getMinutes());
	strHTML+=":"+(dt.getSeconds()<10 ? '0'+dt.getSeconds() : dt.getSeconds());
	$('cTime').innerHTML=strHTML;
};


var TimeOn=function(){
	gameSystem.runTime++;	
	if(gameSystem.runTime<10) $('rTime').innerHTML='000'+gameSystem.runTime;
	if(gameSystem.runTime>=10 && gameSystem.runTime<100) $('rTime').innerHTML='00'+gameSystem.runTime;
	if(gameSystem.runTime>=100 && gameSystem.runTime<1000) $('rTime').innerHTML='0'+gameSystem.runTime;
	if(gameSystem.runTime>=1000) $('rTime').innerHTML=gameSystem.runTime;
};


var runTimeStart=function(){
	gameSystem.runTimerID=setInterval(function(){TimeOn();},1000);
};


var runTimePause=function(){
	clearInterval(gameSystem.runTimerID);
	gameSystem.runTimerID="";
};

var runTimeStop=function(){
	clearInterval(gameSystem.runTimerID);
	gameSystem.runTimerID="";
}

var runTimeReset=function(){
	gameSystem.runTime=0;
	clearInterval(gameSystem.runTimerID);
	gameSystem.runTimerID="";
	$('rTime').innerHTML='0000';
};


var gameFrameUpdate=function(){
//function to play next Game Frame
	var c=$("gameCanvas");
	//var cxt=c.getContext("2d");
	c.width=c.width;  //clear and redraw it 	
	westCowboy.move();
	gameSystem.activeTManNum=0;
	for(gameSystem.i=0; gameSystem.i<gameSystem.tribeMen.length; gameSystem.i++){		
		gameSystem.tribeMen[gameSystem.i].move();		
		gameSystem.tribeMen[gameSystem.i].fighting();
		if(gameSystem.tribeMen[gameSystem.i].lifeValue>0) gameSystem.activeTManNum++ ;		
	}
	$("tmActive").innerHTML = gameSystem.activeTManNum ;
	$("tmDeath").innerHTML = gameSystem.tribeMen.length - gameSystem.activeTManNum ;
};



var initLoad=function(){
//function to initialize when loading 
	initGameUI(); 
	var isSupportHTML5=gameSystem.isSupportHTML5();	
	if(isSupportHTML5){		
		setInterval('curTime()',1000);
		westCowboy.init();		
		var i=0;
		for(i=0;i<gameSystem.tribeManNum;i++){  
			var tMan=new tribeMan();
			tMan.init();
			gameSystem.tribeMen[i]=tMan;
		}
	}	
}


window.οnlοad=function(){
initLoad();	
};


var gameStart=function(){	
	if(gameSystem.isSupportHTML5()){
		runTimeStart();
		if(!gameSystem.gamePlayerID){
			initLoad();
			gameSystem.gamePlayerID=setInterval(function(){gameFrameUpdate()},gameSystem.gameFrameSpeed);
			tribeManMoveTimeController.timeControllerID=setInterval(function(){tribeManMoveTimeController.timeOn();},1000);		
		}else
			alert('Game is running...');
	}else
		initGameUI();
}


var gamePause=function(){
	if(gameSystem.isSupportHTML5()){
		runTimePause();		
		clearInterval(gameSystem.gamePlayerID);	
		gameSystem.gamePlayerID="";
		clearInterval(tribeManMoveTimeController.timeControllerID);
		tribeManMoveTimeController.timeControllerID="";
	}else
		initGameUI();
}


var gameContinue=function(){
	if(gameSystem.isSupportHTML5()){
		if(!gameSystem.runTimerID) runTimeStart();
		if(!gameSystem.gamePlayerID) gameSystem.gamePlayerID=setInterval(function(){gameFrameUpdate()},gameSystem.gameFrameSpeed);
		if(!tribeManMoveTimeController.timeControllerID) tribeManMoveTimeController.timeControllerID = setInterval(function(){tribeManMoveTimeController.timeOn();},1000);
	}else
		initGameUI();
}


var gameRestart=function(){
	if(gameSystem.isSupportHTML5()){
		runTimeReset();
		clearInterval(gameSystem.gamePlayerID);
		gameSystem.gamePlayerID="";
		clearInterval(tribeManMoveTimeController.timeControllerID);
		tribeManMoveTimeController.timeControllerID="";
		initLoad();
		gameStart();	
	}else
		initGameUI();
}


var gameEnd=function(){
	if(gameSystem.isSupportHTML5()){
		runTimeStop();
		westCowboy.lifeValue=0;	
		var h=0;
		for(h=0;gameSystem.tribeMen.length;h++){
			gameSystem.tribeMen[h].lifeValue=0;
		}
		gameFrameUpdate();
		clearInterval(gameSystem.gamePlayerID);
		gameSystem.gamePlayerID="";
		clearInterval(tribeManMoveTimeController.timeControllerID);
		tribeManMoveTimeController.timeControllerID="";
	}else
		initGameUI();
}
//--------------------end: global function define--------------------
</script>
</body>
</html>

有些地方可能还有点小问题,或想不到的地方,希望大家指正。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值