87,910
社区成员
发帖
与我相关
我的任务
分享
//用于画地图的方块类
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");
this.aMapArray[i][j].fillrect(this.backgroundcolor);
this.aMapArray[i][j].issetcolor = false;
}
}
}
//获取地图上的方块
map.prototype.getrect = function (x,y) {
try {
return this.aMapArray[x][y];
}
catch (err) {
return null;
}
}
//俄罗斯方块父类
function tetris(x,y,size,scolor) {
//出生坐标
this.born_x = x;
this.born_y = y;
//方块边长
this.rectsize = size;
//存放方块的数组
this.rectarray = new Array();
//方块的颜色
this.color = scolor;
//方块名称
this.name = "";
//属于哪个地图
this.map = map;
}
tetris.prototype.setbornxy = function (x,y) {
this.born_x = x;
this.born_y = y;
}
//显示方块
tetris.prototype.show = function () {
var iCount = this.rectarray.length;
for (var i = 0; i < iCount; i++) {
this.rectarray[i].fillrect(this.color);
}
}
//清除方块
tetris.prototype.clear = function () {
var iCount = this.rectarray.length;
for (var i = 0; i < iCount; i++) {
//this.rectarray[i].clearrect();
this.rectarray[i].fillrect("Black");
}
}
//方块的旋转
tetris.prototype.trun = function () {
var iCount = this.rectarray.length;
var iTemp_x, iTemp_y, offset_x, offset_y;
/*
以rectarray的第一个方块为中心,
在定义方块时,需要将这个方块设置为中心
由于是按照中心方块来移动,所以中心方块坐标不用变。
*/
if (this.name != "D") {//我实在是找不出哪个字母比D更像“田”了
this.clear();
//取得方块所在矩阵偏移原点(0,0)的偏移量
offset_x = this.rectarray[0].x - 1;
offset_y = this.rectarray[0].y - 1;
for (var i = 1; i < iCount; i++) {
//方块坐标减去偏移量
iTemp_x = this.rectarray[i].x - offset_x;
iTemp_y = this.rectarray[i].y - offset_y;
/*
以原点坐标做矩阵旋转运算 参考公式 array[j][2 - i] = array[i][j]
逆时针旋转
计算完后,再加上偏移量
*/
this.rectarray[i].x = iTemp_y + offset_x;
this.rectarray[i].y = 2 - iTemp_x + offset_y;
}
this.show();
}
}
//方块左移
tetris.prototype.move_left = function () {
/*
左移就是x坐标减1
*/
var iCount = this.rectarray.length;
this.clear();
for (var i = 0; i < iCount; i++) {
this.rectarray[i].x = this.rectarray[i].x - 1;
}
this.show();
}
//方块右移
tetris.prototype.move_right = function (context) {
/*
右移就是x坐标加1
*/
var iCount = this.rectarray.length;
this.clear();
for (var i = 0; i < iCount; i++) {
this.rectarray[i].x = this.rectarray[i].x + 1;
}
this.show();
}
//方块下移
tetris.prototype.move_down = function (context) {
/*
下移就是y坐标加1
*/
var iCount = this.rectarray.length;
this.clear();
for (var i = 0; i < iCount; i++) {
this.rectarray[i].y = this.rectarray[i].y + 1;
}
this.show();
}