标题:
俄罗斯方块核心代码
[打印本页]
作者:
紫色流星
时间:
2005-11-26 21:48
标题:
俄罗斯方块核心代码
为了方便大家阅读代码,特意对正式版源程序进行了精简优化,整理出基本的核心代码,现发布如下:
说明:
1.Flash最低版本:Flash MX 2004 Profassional(ActionScript 2.0),推荐使用Flash 8 Profassional(ActionScript 2.0)
2.本代码属于完全公开版本,大家可以随意修改使用,并且殷切希望大家能修改完善之。
3.本代码可直接复制到动作内使用,无须修改。
4.特此感谢
工程家园
会员:
风中落叶
对程序的测试和报告BUG。
作者:
紫色流星
时间:
2005-11-26 21:49
Stage.scaleMode = "noScale";
_root.createEmptyMovieClip("rootmc", 0);
_root.createEmptyMovieClip("movemc", 1);
rootmc._x = 10;
rootmc._y = 10;
movemc._x = 10;
movemc._y = 10;
_global.zl = 0;
_global.mcx = 3;
_global.mcy = 0;
_global.zl = 0;
_global.stack = new Array(9);
_global.m = new Array(16);
_global.er = 0;
_global.score = 0;
_global.hard = 500;
_global.cl = new Array(0xFFFFFF, 0xFF0000, 0xFF8000, 0xFFFF00, 0x00FF00, 0x0000FF, 0x8000FF, 0xFF00FF,0xFFFFFF);
//定义底层方块数组。(10*20)
var roots = new Array(10);
for (var i = 0; i<10; i++) {
roots[i] = new Array(20);
}
//定义活动方块数组。(4*4)
var moves = new Array(4);
for (var i = 0; i<4; i++) {
moves[i] = new Array(4);
}
//定义画方块函数1。
squaredraw = function (mc, x, y) {
x = 30*x;
y = 30*y;
var x1 = x+4;
var x2 = x+27;
var y1 = y+4;
var y2 = y+27;
with (mc) {
lineStyle(4, cl[zl+1], 100);
moveTo(x1, y1);
lineTo(x1, y2);
lineTo(x2, y2);
lineTo(x2, y1);
lineTo(x1, y1);
}
};
//定义画方块函数2。
squaredraw2 = function (mc, x, y) {
var m = x;
var n = y;
x = 30*x;
y = 30*y;
var x1 = x+4;
var x2 = x+27;
var y1 = y+4;
var y2 = y+27;
with (mc) {
lineStyle(4, cl[roots[m][n]], 100);
moveTo(x1, y1);
lineTo(x1, y2);
lineTo(x2, y2);
lineTo(x2, y1);
lineTo(x1, y1);
}
};
//定义底层画方块的函数。
rtmcdr = function () {
rootmc.clear();
for (var i = 0; i<10; i++) {
for (var j = 0; j<20; j++) {
if (roots[i][j]>0) {
squaredraw2(rootmc, i, j);
}
}
}
};
//产生随机方块的函数。
movmcrand = function () {
for (var i = 0; i<4; i++) {
for (var j = 0; j<4; j++) {
moves[i][j] = 0;
}
}
zl = random(7);
switch (zl) {
case 0 :
// 棒子
moves[0][1] = 1;
moves[1][1] = 1;
moves[2][1] = 1;
moves[3][1] = 1;
break;
case 1 :
// 左L
moves[0][1] = 2;
moves[1][1] = 2;
moves[2][1] = 2;
moves[0][0] = 2;
break;
case 2 :
// 右L
moves[0][1] = 3;
moves[1][1] = 3;
moves[2][1] = 3;
moves[2][0] = 3;
break;
case 3 :
// 左之
moves[0][0] = 4;
moves[1][0] = 4;
moves[1][1] = 4;
moves[2][1] = 4;
break;
case 4 :
// 右之
moves[0][1] = 5;
moves[1][1] = 5;
moves[1][0] = 5;
moves[2][0] = 5;
break;
case 5 :
// T
moves[0][1] = 6;
moves[1][1] = 6;
moves[2][1] = 6;
moves[1][0] = 6;
break;
case 6 :
// 方块
moves[0][0] = 7;
moves[0][1] = 7;
moves[1][1] = 7;
moves[1][0] = 7;
break;
default :
trace("Error!!!"+" "+zl);
break;
}
if (movebl() == false && er == 4) {
gmo();
}
};
//定义画移动方块的方法。
movmcdr = function () {
movemc.clear();
for (var i = 0; i<4; i++) {
for (var j = 0; j<4; j++) {
if (moves[i][j]>0) {
squaredraw(movemc, i+mcx, j+mcy);
}
}
}
};
//定义下落的函数。
xl = function () {
mcy++;
if (movebl() == false) {
mcy--;
for (var i = 0; i<4; i++) {
for (var j = 0; j<4; j++) {
if (moves[i][j]>0) {
roots[i+mcx][j+mcy] = moves[i][j];
}
}
}
mcclean();
rtmcdr();
mcx = 3;
mcy = 0;
movmcrand();
}
movmcdr();
};
//定义判断方块是否能移动的函数。
movebl = function () {
var ebl = true;
er = 0;
for (var i = 0; i<4; i++) {
for (var j = 0; j<4; j++) {
if (moves[i][j]>0) {
var x = i+mcx;
var y = j+mcy;
if (y>19) {
ebl = false;
er = 8;
}
if (x<0) {
ebl = false;
er = 1;
}
if (x>9) {
ebl = false;
er = 2;
}
if (roots[x][y]>0) {
ebl = false;
er = 4;
}
}
}
}
return (ebl);
};
//定义方块旋转的函数。
movmcrot = function () {
var p = mcx;
var q = mcy;
for (var i = 0; i<4; i++) {
for (var j = 0; j<4; j++) {
m[i*4+j] = moves[i][j];
}
}
if (zl == 0) {
for (var i = 0; i<4; i++) {
for (var j = i; j<4; j++) {
var z = moves[i][j];
moves[i][j] = moves[j][i];
moves[j][i] = z;
}
}
}
if (zl>0 && zl<6) {
for (var i = 0; i<3; i++) {
for (var j = 0; j<3; j++) {
stack.push(moves[j][i]);
}
}
for (var i = 0; i<3; i++) {
for (var j = 2; j>=0; j--) {
moves[i][j] = stack.pop();
}
}
}
while (not (movebl())) {
// trace(er);
switch (er) {
case 1 :
mcx++;
break;
case 2 :
mcx--;
break;
default :
mcx = p;
mcy = q;
for (var i = 0; i<4; i++) {
for (var j = 0; j<4; j++) {
moves[i][j] = m[i*4+j];
}
}
break;
}
}
};
//定义判断消方块的函数。
mcclean = function () {
for (var j = mcy; j<mcy+4; j++) {
var mcf = true;
for (var i = 0; i<10; i++) {
if (not (roots[i][j]>0)) {
mcf = false;
continue;
}
}
// trace(j+" "+mcy+" "+mcf);
if (mcf) {
for (var l = j; l>0; l--) {
for (var k = 0; k<10; k++) {
roots[k][l] = roots[k][l-1];
}
}
}
}
};
//定义判断游戏结束的函数。
gmo = function () {
clearInterval(timer);
zt = -1;
for (var i = 0; i<4; i++) {
for (var j = 0; j<4; j++) {
moves[i][j] = 0;
}
}
my_cm.customItems[2].enabled = true;
// trace("Game Over!");
};
//定义按键函数。
keys = new Object();
keys.onKeyDown = function() {
//trace(Key.getCode())
if (Key.isDown(Key.RIGHT) && zt>=0) {
mcx++;
if (movebl() == false) {
mcx--;
}
movmcdr();
}
if (Key.isDown(Key.LEFT) && zt>=0) {
mcx--;
if (movebl() == false) {
mcx++;
}
movmcdr();
}
if (Key.isDown(Key.DOWN) && zt>=0) {
mcy++;
if (movebl() == false) {
mcy--;
}
movmcdr();
}
if (Key.isDown(Key.SPACE) && zt>=0) {
while (movebl()) {
mcy++;
}
mcy--;
movmcdr();
}
if (Key.isDown(Key.UP) && zt>=0) {
movmcrot();
movmcdr();
}
if (Key.isDown(Key.CONTROL)) {
if (zt>=0) {
clearInterval(timer);
info.txt = "Pause";
zt = -1;
} else {
timer = setInterval(xl, lv[level]);
info.txt = score;
zt = 0;
}
}
};
//产生N行随机方块的函数。
rtran = function (n) {
for (var j = 19; j>=(20-n); j--) {
var m = random(7)+3;
for (var i = 0; i<m; i++) {
do {
k = random(10);
} while (roots[k][j]>0);
roots[k][j] = 8;
}
}
};
//定义右键菜单。
my_cm = new ContextMenu();
my_cm.customItems.push(new ContextMenuItem("作者主站——流星Flash", opensite));
my_cm.customItems.push(new ContextMenuItem("关于作者——紫色流星", aboutme, false));
my_cm.customItems.push(new ContextMenuItem("重新开始", reset, true));
function opensite() {
_root.getURL("http://ilovelassie.tengyi.cn/index.htm");
}
function aboutme() {
_root.getURL("http://ilovelassie.tengyi.cn/about.htm");
}
function reset() {
my_cm.customItems[2].enabled = false;
zl = random(7);
mcx = 3;
mcy = 0;
er = 0;
zt = 0;
for (var i = 0; i<10; i++) {
for (var j = 0; j<20; j++) {
roots[i][j] = 0;
}
}
timer = setInterval(xl, 1000);
rtran(12);
rtmcdr();
movmcrand();
}
my_cm.customItems[2].enabled = false;
my_cm.hideBuiltInItems();
_root.menu = my_cm;
Key.addListener(keys);
timer = setInterval(xl, 1000);
rtran(12);
rtmcdr();
movmcrand();
复制代码
作者:
风中落叶
时间:
2005-11-26 21:59
来祝贺一下先:handshake:handshake
作者:
紫色流星
时间:
2005-11-26 22:03
Originally posted by
风中落叶
at 2005-11-26 21:59:
来祝贺一下先:handshake:handshake
这几天心情乱,编写的代码也乱,所以正式版就懒得再更新了。
连连看的代码也只是编写了一点就停手了,等待我心情好了再恢复工作吧,呵呵
欢迎光临 工程家园 (http://heubbs.com./)
Powered by Discuz! 7.2