日期:2014-05-16 浏览次数:20503 次
//用于画地图的方块类
function Rect(x,y,size,context) {
this.x = x;
this.y = y;
this.size = size;
//是否着色的标志,由游戏控制类来改写
this.issetcolor = false;
this.color = "";
this.context = context;
}
//画边框,用于画地图
Rect.prototype.strokerect = function (sColor) {
this.context.strokeStyle = sColor;
this.context.strokeRect(this.x * this.size,this.y * this.size,this.size,this.size);
}
//填充颜色
Rect.prototype.fillrect = function (sColor) {
this.context.fillStyle = sColor;
this.context.fillRect(this.x * this.size + 1,this.y * this.size + 1,this.size - 2, this.size - 2);
this.color = sColor;
}
//清除颜色
Rect.prototype.clearrect = function () {
this.context.clearRect(this.x * this.size + 1,this.y * this.size + 1,this.size - 2,this.size - 2);
}
//地图类
function map(RectSize,color,canvas) {
this.width = canvas.width;
this.height = canvas.height;
this.RectSize = RectSize; //方块边长
this.backgroundcolor = color;
this.aMapArray = null;
this.ctx = canvas.getContext("2d");
this.born();
}
//地图由若干只有边框的方块组成
map.prototype.born = function () {
var i; var j;
var xCount;
var yCount;
xCount = this.width / this.RectSize;
yCount = this.height / this.RectSize;
this.aMapArray = new Array();
for (i = 0; i < xCount; i++) {
this.aMapArray[i] = new Array();
for (j = 0; j < yCount; j++) {
this.aMapArray[i][j] = new Rect(i,j,this.RectSize,this.ctx);
this.aMapArray[i][j].strokerect(this.backgroundcolor);
this.aMapArray[i][j].fillrect(this.backgroundcolor);
}
}
}
//地图刷新
map.prototype.refresh = function () {
var xCount;
var yCount;
xCount = this.width / this.RectSize;
yCount = this.height / this.RectSize;
for (var i = 0; i < xCount; i++) {
for (var j = 0; j < yCount; j++) {
this.aMapArray[i][j].fillrect("White");
t