HTML5塔防(一)


现在我们就开始做一个HTML5塔防游戏,首先我们需要建工程好吧!接下来我们直接开始html文件和js了。

首先index.html
<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<title>我的TD</title>
		<style type="text/css">
		    html,body{
		    	margin: 0px;
		    	padding: 0px;
		    }
			#game {
				margin: 10px;
				width: 600px;
				height: 500px;
				border: 1px solid #589300;
			}
			
			#map {
				width: 500px;
				height: 500px;
				/*border: 1px solid #000000;*/
				/*position: absolute;*/
				left: 0;
				top: 0;
				z-index: 99;
			}
		</style>
	</head>

	<body>
		<div id="game">
			<canvas id="map" width="500" height="500"></canvas>
		</div>
		<div style="float:left;width:500px;margin-left:50px;">
				<br /> 选择地图
				<select id="select_map">
					<option value="1">地图一</option>
					<option value="2">地图二</option>
				</select>,然后按
				<input type="button" value="开始游戏" onclick="startGame()" />
			</p>

		</div>
	</body>
	<script type="text/javascript" src="Td-game.js"></script>
	<script type="text/javascript">
		function startGame() {
			Td_Game.start();
		}
	</script>

</html>

然后呢,开始写Td_Game.js
首先我们写的是控制器,如下:
//游戏控制类
var Td_Game = {
	FPS: 20, //帧
	imgList: {}, //图片信息列表
	canvasList: {}, //画布信息列表
	//计时器ID
	timer: 0,
	//初始化
	init: function() {
	},
	//初始化图片
	initImg: function() {
	},
	//初始化画布
	initCanvas: function() {
	},
	//开始游戏
	start: function() {
	},
	//循环体
	loop: function() {
	}
};
这个就是我们接下来的主体框架,接下来就是根据自己的想法去完善里面的方法;
初始化:
	//初始化
	init: function() {
		this.initImg();
		this.initCanvas();
	},
我们需要初始化下图片和画布,好吧其实光写地图的话不需要这个,但是我们写了备用你懂得。
初始画布:
	//初始化画布
	initCanvas: function() {
		this.canvasList = {
			map: document.getElementById("map").getContext("2d")
		}
	},
然后该开始游戏了,大家都知道开始游戏要加载地图对吧,但是我们没有地图阿怎么办?
构造地图:
var Map = {
		//画出地图
		draw: function(map) {
			var i, j;
			for (i = 0; i < 10; i++) {
				for (j = 0; j < 10; j++) {
					//画背景地图
					if (this.data[i][j] == 0) Canvas.drawRect(map, i * 50, j * 50, 50, 50, '#589300');
					//画可以走的路
					else Canvas.fillRect(map, i * 50, j * 50, 50, 50, 'antiquewhite');
				}
			}
		},
		data: [],
		data_1: [
			[1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
			[1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
			[0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
			[0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
			[0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
			[0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
			[0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
			[0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
			[0, 1, 1, 1, 1, 1, 1, 1, 1, 1],
			[0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
		]
};
构造地图的过程中我们用到了canvas这个类,大家别想太多哦,其实这个类也是我们自己定义的;
构造画布:
var Canvas = {
	//清除画布
	clear: function(cxt, x, y) {
		cxt.clearRect(0, 0, x, y);
	},
	clearRect: function(cxt, x, y, width, height) {
		cxt.clearRect(x, y, width, height);
	},
	//画图
	drawImg: function(cxt, img, x, y, sw, sh, dx, dy, dw, dh) {

		if (!sw) cxt.drawImage(img, x, y);
		else cxt.drawImage(img, x, y, sw, sh, dx, dy, dw, dh);
	},
	//画文字
	drawText: function(cxt, string, x, y, color) {

		cxt.fillStyle = color;
		cxt.font = 'bold 12px sans-serif';
		cxt.fillText(string, x, y);
	},
	//画填充的方
	fillRect: function(cxt, x, y, width, height, color) {

		cxt.fillStyle = color;
		cxt.fillRect(x, y, width, height);
	},
	//画边框的方
	drawRect: function(cxt, x, y, width, height, color) {

		cxt.strokeStyle = color;
		cxt.lineWidth = 1;
		cxt.strokeRect(x, y, width, height);
	},
	//画圆
	//ctx:context2d对象,x:圆心x坐标,y:圆心y坐标,radius:半径,color:颜色
	fillArc: function(cxt, x, y, radius, color) {
		cxt.fillStyle = color;
		cxt.beginPath();
		cxt.arc(x, y, radius, 0, Math.PI * 2, true);
		cxt.closePath();
		cxt.fill();
	}
}

接下来我们开始画土地把:
//开始游戏
	start: function() {
        Map.data=Map.data_1;
		Map.draw(this.canvasList.map);
		this.timer = setInterval(Td_Game.loop, 1000 / this.FPS);
	},
这样我们就完成了,绘制地图.

完整JS:
//游戏控制类
var Td_Game = {
	FPS: 20, //帧
	imgList: {}, //图片信息列表
	canvasList: {}, //画布信息列表
	//计时器ID
	timer: 0,
	//初始化
	init: function() {
		this.initImg();
		this.initCanvas();
	},
	//初始化图片
	initImg: function() {

	},
	//初始化画布
	initCanvas: function() {
		this.canvasList = {
			map: document.getElementById("map").getContext("2d")
		}
	},
	//开始游戏
	start: function() {
        Map.data=Map.data_1;
		Map.draw(this.canvasList.map);
		this.timer = setInterval(Td_Game.loop, 1000 / this.FPS);
	},
	//循环体
	loop: function() {

	}
};
//地图绘制类
var Map = {
		//画出地图
		draw: function(map) {
			var i, j;
			for (i = 0; i < 10; i++) {
				for (j = 0; j < 10; j++) {
					//画背景地图
					if (this.data[i][j] == 0) Canvas.drawRect(map, i * 50, j * 50, 50, 50, '#589300');
					//画可以走的路
					else Canvas.fillRect(map, i * 50, j * 50, 50, 50, 'antiquewhite');
				}
			}
		},
		data: [],
		data_1: [
			[1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
			[1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
			[0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
			[0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
			[0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
			[0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
			[0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
			[0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
			[0, 1, 1, 1, 1, 1, 1, 1, 1, 1],
			[0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
		]
};
	//画布类
var Canvas = {
	//清除画布
	clear: function(cxt, x, y) {
		cxt.clearRect(0, 0, x, y);
	},
	clearRect: function(cxt, x, y, width, height) {
		cxt.clearRect(x, y, width, height);
	},
	//画图
	drawImg: function(cxt, img, x, y, sw, sh, dx, dy, dw, dh) {

		if (!sw) cxt.drawImage(img, x, y);
		else cxt.drawImage(img, x, y, sw, sh, dx, dy, dw, dh);
	},
	//画文字
	drawText: function(cxt, string, x, y, color) {

		cxt.fillStyle = color;
		cxt.font = 'bold 12px sans-serif';
		cxt.fillText(string, x, y);
	},
	//画填充的方
	fillRect: function(cxt, x, y, width, height, color) {

		cxt.fillStyle = color;
		cxt.fillRect(x, y, width, height);
	},
	//画边框的方
	drawRect: function(cxt, x, y, width, height, color) {

		cxt.strokeStyle = color;
		cxt.lineWidth = 1;
		cxt.strokeRect(x, y, width, height);
	},
	//画圆
	//ctx:context2d对象,x:圆心x坐标,y:圆心y坐标,radius:半径,color:颜色
	fillArc: function(cxt, x, y, radius, color) {
		cxt.fillStyle = color;
		cxt.beginPath();
		cxt.arc(x, y, radius, 0, Math.PI * 2, true);
		cxt.closePath();
		cxt.fill();
	}
}
Td_Game.init();

看看我做的效果吧:


格子是放塔的位子,黄色的是跑道。谢谢大家!




内容索引:脚本资源,HTML,网页游戏,魔塔   运行方法:进入 src 或 build 目录,用浏览器(如IE9)打开 td.html 即可运行。   请注意,IE8不能运行本游戏,你必需按以上要求运行。   相关的技术说明:   1.本游戏完全使用 HTML5 / JavaScript / CSS 实现,没有用到 Flash、SilverLight 等技术。   2.这个版本没有用到图片,游戏中的所有物品都是使用 HTML5 画出来的。   3.这个版本部分地方为 IE9 做了专门的优化,可正常运行在 IE9 下。   脚本及资源目录说明:   /build 压缩后的可发布的文件   /screenshorts 屏幕截图   /src 源码   /css 样式表   /js JavaScripts 源文件   /tools 小工具、脚本      作弊方法:为方便测试,本游戏内置了几个作弊方法,如下:   1.增加 100 万金钱:[removed]_TD.cheat="money+";void(0);   2.难度增倍:[removed]_TD.cheat="difficulty+";void(0);   3.难度减半:[removed]_TD.cheat="difficulty-";void(0);   4.生命值恢复:[removed]_TD.cheat="life+";void(0);   5.生命值降为最低:[removed]_TD.cheat="life-";void(0);   在浏览器地址栏输入上面的“[removed]...;”并回车,即可实现作弊。   注意,以上作弊方法主要是为测试设计,正常游戏过程中请酌情使用,否则可能会降低游戏乐趣。   更新历史:   2010-12-29 根据网友建议,增加生命自动恢复功能(每隔 5 波生命恢复 5 点,每隔 10 波生命恢复 10 点)。调整参数,减小了激光枪的射程,增强了重机枪的威力。(v0.1.12)。   2010-12-18 添加新武器“激光枪”(v0.1.8.0)。   2010-12-12 暂停图片资源版本分支的开发,继续优化、开发圈圈版(v0.1.7.0)。   2010-11-28 第个图片资源版本(v0.2.1.3267)。   2010-11-23 发布 圈圈版(v0.1.6.2970)。   2010-11-14 线上发布第个版本。   2010-11-11 开始编写这个游戏
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值