html5游戏

以前同事做的html5游戏,我稍作修改了一下

<!DOCTYPE html>
<html>
<head>
<title>Orbz</title>

</head>

<body style="font-family:'lucida grande', tahoma, verdana, arial, sans-serif;background-color: #f7f7f7;color: #333;">
<h1>OMG CALL YOUR MUM AND TELL HER YOU JUST PLAYED <del>ORBZ</del> CRAZY CUBEZ IN HTML5</h1>

<img id="spacebg" src="space.jpg" style="display:none" />
<canvas id="canvas" width="360" height="510" style="border:1px dotted;float:left"></canvas>

<b>Click two orbz to swap. Click on a combo to clear. No time limit, just survive as long as possible!</b>
<hr />
<form>
    <p>Adjust paramaters here. They will be updated after your current game finishes.</p>
    NumRows: <input type="text" id="numRows" value="7"/><br/>
    NumColumns: <input type="text" id="numColumns" value="9"/><br/>
    BoxSize: <input type="text" id="boxSize" value="80"/><br/>
    SpawnRate: <input type="text" id="spawnRate" value="0.004"/><br/>
    StepTime: <input type="text" id="stepTime" value="30"/><br/>
    MinComboSize: <input type="text" id="minComboSize" value="3"/><br/>
    ComboBonus: <input type="text" id="comboBonus" value="1.3"/> <i>(Every extra ball in the combo multiplies your score by this amount)</i><br/>
</form>

<script language="javascript">

var cs = document.getElementById("canvas");
var ct = cs.getContext("2d");
var spacebg = document.getElementById("spacebg");

var numRows;
var numColumns;
var boxSize;
var canvasX;
var canvasY;
var footerY = 30;
var spawnRate;
var stepTime;
var minComboSize;

var highScore = 0;
var score;
var selected;
var nuked;
var grid;
var interval;

function init() {
    window.clearInterval(interval);

    numRows = getVal("numRows");
    numColumns = getVal("numColumns");
    boxSize = getVal("boxSize");
    spawnRate = getVal("spawnRate");
    stepTime = getVal("stepTime");
    minComboSize = getVal("minComboSize");
    comboBonus = getVal("comboBonus");
    canvasX = numColumns * boxSize;
    canvasY = numRows * boxSize;
    spawnMultiplier = 1;

    
    cs.width = canvasX;
    cs.height = canvasY + footerY;
    cs.addEventListener("click", doClick, false);

    score = 0;
    selected = [];
    nuked = [];
    grid = new Grid(numColumns, numRows);
    interval = setInterval(step, stepTime);    
}

function getVal(valName) {
    return parseFloat(document.getElementById(valName).value);
}

function Grid(x,y) {
    this.grid = []
    for(var i = 0; i < x; i++) {
	var col = [];
	for(var j = 0; j < y; j++) {
	    col.push(new Cell(i,j));
	}
	this.grid.push(col);
    }
    
    this.get = function(x,y) {
	var col = this.grid[x];
	if(col) {
	    var cell = col[y];
	    if(cell) {return cell;}
	}
	return null;
    }

    this.col = function(x) {
	var col = this.grid[x];
	if(col) {
	    return col;
	}
	return null;
    }
}

function Cell(x,y) {
    var colours = ["#900","#090","#009","#d0d","#dd0"];
    this.draw = function() {
	if(this.hasItem) {
	    // ct.beginPath();
	    // ct.arc((x+0.5) * boxSize, ((y+0.5) * boxSize), (boxSize/2), 0, Math.PI * 2, false);
	    // ct.closePath();
	    ct.fillStyle = this.nuked ? "white" : this.colour;
	    // ct.fill();
	    ct.fillRect((x * boxSize)+1,(y*boxSize)+1,boxSize-2,boxSize-2);
	    if(this.selected) {
		ct.lineWidth = 2;
		ct.strokeStyle = 'rgb(240,240,240)';
		ct.strokeRect(x * boxSize, y * boxSize, boxSize, boxSize);
	    }
	}
	if(this.exploding && this.explosionSize > 0) {
	    ct.beginPath();
	    ct.arc((x+0.5) * boxSize, ((y+0.5) * boxSize), (boxSize/2) + this.explosionSize, 0, Math.PI * 2, false);
	    ct.closePath();

	    ct.lineWidth = 4;
	    ct.strokeStyle = 'rgb(240,240,240)';
	    ct.stroke();
	}
    }
    
    if(Math.random() < (spawnRate * spawnMultiplier)) {
	this.hasItem = true;
	this.colour = colours[Math.floor(Math.random() * colours.length)];
    } else {
	this.hasItem = false;
    }
    this.x = x;
    this.y = y;
    this.exploding = false;
}

function draw(grid) {

    ct.fillStyle = "black";
    ct.fillRect(0,0,canvasX,canvasY)
    ct.drawImage(spacebg, 0, 0);
    ct.fillStyle = "#444";
    ct.fillRect(0,canvasY,canvasX,footerY)

    for(var i = 0; i < numColumns; i++) {
	for(var j = 0; j < numRows; j++) {
	    grid.get(i,j).draw();
	}
    }

    ct.fillStyle = "white";
    ct.font = "bold 16px sans-serif";
    ct.fillText("Score:"+score+" High Score:"+highScore+" Spawn Rate:"+(spawnRate*spawnMultiplier),10, canvasY + 20);
    
}

function step() 
{  
    for(var i = 0; i < numColumns; i++) 
    {  
	    var col = grid.col(i);  
	  
	    for(var j = numRows - 1; j >= 0; j--) 
	    {  
	        var cell = col[j];  
	          
	        // (j <= numRows - 2) -> balls on bottom row can't fall down, so don't check if they can  
	        if(j <= numRows -2 && cell.hasItem && !col[j+1].hasItem) 
	        {  
		        col[j+1].hasItem = true;  
		        col[j+1].colour = cell.colour;  
		        cell.hasItem = false;  
		        cell.colour = null;  
	        }  
	  
	        if(cell.exploding) 
	        {  
	        	cell.explosionSize > 20 ? cell.exploding = false : cell.explosionSize += 3;  
	        }  
	    }  
	  
	      
	    if(!col[0].hasItem) 
	    {  
	        col[0] = new Cell(i,0);  
	    } 
	    else 
	    {  
	        if(Math.random() < (spawnRate * spawnMultiplier)) {gameOver();}  
	    }  
    }
    draw(grid);  
} 

function gameOver() {
    alert("Game Over!");
    if(score > highScore) {highScore = score;}
    init();
}

function doClick(e) {
    if (e.pageX != undefined && e.pageY != undefined) {
	x = e.pageX;
	y = e.pageY;
    }
    else {
	x = e.clientX + document.body.scrollLeft +
            document.documentElement.scrollLeft;
	y = e.clientY + document.body.scrollTop +
            document.documentElement.scrollTop;
    }
    
    x -= cs.offsetLeft;
    y -= cs.offsetTop;

    gridX = Math.floor(x / boxSize);
    gridY = Math.floor(y / boxSize);
    select(grid.get(gridX,gridY));

}

function select(cell) {
    //if(!cell.hasItem) {return;}
//debugger;
    switch(selected.length) {
	case 0 : 
	if(nuke(cell)) {
	    return;
	} else {
		if(!cell.hasItem)
		{
			break;
		}
	    cell.selected = true;
	    selected.push(cell);
	}
	break;
	case 1 :
	if(cell == selected[0]) 
	{
		cell.selected=false;
		selected=[];
		return;
	}
	cell.selected = true;
	selected.push(cell);
	swap();
	break;
    }

}

function nuke(cell) {
    nukeColour = cell.colour;
    cell.nuked = true;
    nuked = [];
    nuked.push(cell);
    nukeNeighbours(cell,nukeColour);
    var result;
    if(nuked.length >= minComboSize) {
	score += Math.floor(Math.pow(comboBonus,nuked.length) * 10);
	spawnMultiplier = 1 + (score / 700);

	for(var i = 0; i < nuked.length; i++) {
	    nuked[i].nuked = false;
	    nuked[i].hasItem = false;
	    nuked[i].exploding = true;
	    nuked[i].explosionSize = 0 - (i * 6);
	}
	result = true;
    } else {
	for(var i = 0; i < nuked.length; i++) {
	    nuked[i].nuked = false;
	}
	cell.selected = true;
	result = false;
    }
    nuked = [];
    return result;
}

function nukeNeighbours(cell,nukeColour) {
    var x = cell.x;
    var y = cell.y;

    var n;
    if(n = grid.get(x-1,y)) {nukeChild(n,nukeColour);}
    if(n = grid.get(x+1,y)) {nukeChild(n,nukeColour);}
    if(n = grid.get(x,y-1)) {nukeChild(n,nukeColour);}
    if(n = grid.get(x,y+1)) {nukeChild(n,nukeColour);}

}

function nukeChild(cell,nukeColour) {
    if(!cell || !cell.hasItem || cell.nuked || cell.colour != nukeColour) {return;}
    cell.nuked = true;
    nuked.push(cell);
    nukeNeighbours(cell,nukeColour);
}

function swap() 
{
	if(selected[0].hasItem)
	{
	if(selected[1].hasItem)
	{
		var temp = selected[0].colour;
		selected[0].colour = selected[1].colour;
		selected[1].colour = temp;
	}
    	else
	{
		selected[1].hasItem=true;
		selected[0].hasItem=false;
		selected[1].colour = selected[0].colour;
		selected[0].colour=null;
	}
	}
    selected[0].selected = false;
    selected[1].selected = false;
    selected = [];
}

window.onload=init;
</script>

</body>
</html>

游戏的背景图片
http://blog.youkuaiyun.com/xiaoxiao108/article/details/8913616 html5 挺火,写个html5游戏玩玩吧,想起cocos2d 貌似各个平台都有,网上找了找,下载了个Cocos2d-html5引擎包。 貌似另一个开源引擎lufylegend.js也很好,下次用用lufylegend.js试试。 开发环境 chrome Cocos2d-html5 游戏地址:http://hehe108.sinaapp.com/cocos2d/ (adsw回车) 实现方法如下 1.创建好 LayerGradient的子类 (里面放坦克子弹) 2.重写 onEnter 方法添加一些基本按钮 跟一些初始坦克,子弹 3.通过schedule方法 控制 坦克 子弹的重画 4.根据键盘按键(ASWD)确定出坦克的方向,根据坦克的方向修改坦克的X,Y轴坐标,来实现坦克的移动 5.通过cc.rectIntersectsRect函数来进行碰撞检测,实现子弹打击坦克 具体代码 1.在项目里面添加方向 var Direction = { L:0, U:1, D:2, R:3, STOP:4 }; 2.添加子弹类的相关属性 SPEED:10, WIDTH:15, HEIGHT:15, x:null, y:null, dir:null, live:true, tankClient:null, //LayerGradient子类TankClient 的引用 good:null, 3.子弹初始化,重画 ctor:function (x,y,good,dir,tankClient) { cc.associateWithNative( this, cc.Sprite ); this.x=x; this.y=y; this.dir=dir; this.tankClient=tankClient; this.good=good; this.initWithFile(s_missile); this.setPosition( cc.p(this.x, this.y) ); this.tankClient.addChild(this); }, Draw:function(){ if(!this.live){ this.tankClient.removeChild(this, true); return; } this.setPosition( cc.p(this.x, this.y) ); this.Move(); }, 4.添加子弹打击坦克的方法 HitTank:function(t){ if (cc.rectIntersectsRect(this.GetRectangle(), t.GetRectangle()) && t.live && this.live && this.good != t.good){ t.live = false; this.live = false; return true; } return false; }, 5.添加坦克类相关属性 SPEED:5, WIDTH:58, HEIGHT:58, x:0, y:0, l:false, u:false, r:false, d:false, dir:Direction["STOP"], ptDir:Direction["D"], tankClient:null, good:null, step:0, live:true, 6.在tank类中 坦克初始化,重画 ctor:function (x,y,good,tankClient) { cc.associateWithNative( this, cc.Sprite ); this.x=x; this.y=y; this.tankClient=tankClient; this.good=good; if(good){ this.initWithFile(s_tank); }else{ this.initWithFile(s_enemy); } this.setPosition( cc.p(this.x, this.y) ); this.tankClient.addChild(this); }, Draw:function(){ if (!this.live){ if (!this.good){ this.tankClient.removeChild(this, true); } this.tankClient.removeChild(this, true); return; } this.setPosition( cc.p(this.x, this.y) ); switch (this.ptDir) { case Direction["D"]: this.setRotation(0); //旋转精灵控制 炮筒方向 break; case Direction["U"]: this.setRotation(180); break; case Direction["L"]: this.setRotation(270); break; case Direction["R"]: this.setRotation(90); break; } this.Move(); }, 7.tank发子弹的方法 Fire:function() { if(!this.live) return null; for(var i=0;i<this.tankClient.missiles.length;i++){ var m = this.tankClient.missiles[i]; if (m.live == false){ m.x=this.x; m.y=this.y; m.live=true; m.dir=this.ptDir; m.good=this.good; this.tankClient.addChild(m); return m; } } var missile=new Missile(this.x,this.y,this.good,this.ptDir,this.tankClient); this.tankClient.missiles.push(missile); return missile; }, 8.LayerGradient加入坦克 this.tanks = []; this.myTank = new Tank(60,20, true, this); for (var i = 0; i < 10; i++){ this.tanks.push(new Tank(50 + 70 * (i + 1), 420, false, this)); } 9.LayerGradient中调用子弹打击坦克的方法 for(var i=0;i<this.missiles.length;i++){ var m = this.missiles[i]; m.HitTank(this.myTank); m.HitTanks(this.tanks); m.Draw(); } 10.控制坦克移动射击的部分代码 onKeyUp:function(key) { this.myTank.KeyReleased(key); }, onKeyDown:function(key) { this.myTank.KeyPressed(key); } 11.用Ant和compiler合并压缩js后发布到sae 如果你发现有什么不合理的,需要改进的地方,请留言。或者可以通过 328452421@qq.com 联系我,非常感谢。 http://blog.youkuaiyun.com/xiaoxiao108/article/details/8913616
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值