整理以前的硬盘资料,翻出来一个N年前的俄罗斯方块applet程序,很简陋,仅做个留存
-
import java.applet.*;
-
import java.awt.*;
-
-
public class mtetris extends Applet
-
{
-
boolean tetrisMap=true;
-
Graphics background=null;
-
Color bg=new Color(0x33,0x99,0xcc);
-
Color fg=Color.blue;
-
mtetrisThread tetris;
-
public void init()
-
{
-
this.setLayout(new FlowLayout(FlowLayout.RIGHT,5,40));
-
//Button button2=new Button("Reset");
-
Button button1= new Button("Start");
-
//this.add(button2);
-
//this.setLayout(new FlowLayout(FlowLayout.RIGHT,5,60));
-
this.add(button1);
-
setBackground(bg);
-
setForeground(fg);
-
}
-
-
public void start()
-
{
-
tetris=new mtetrisThread(this,10,20,bg,fg);
-
}
-
/*public void paint(Graphics g)
-
*{
-
*
-
*}
-
*/
-
public void update(Graphics g)
-
{
-
g.drawLine(200,0,200,400);
-
g.setColor(bg);
-
g.fillRect(205,80,30,30);
-
g.setColor(fg);
-
g.drawString(tetris.retScore(),210,90);
-
}
-
-
private boolean CommandAction(Object commandName)
-
{
-
if("Start".equals(commandName))
-
tetris.start();
-
return true;
-
}
-
-
public synchronized boolean handleEvent(Event event)
-
{
-
if(event.id==403||event.id==401)
-
{
-
tetris.keyAnswer(event.key);
-
}
-
if(event.id==1001)
-
return CommandAction(event.arg);
-
return true;
-
}
-
public synchronized void refresh()
-
{
-
repaint();
-
}
-
-
}
-
-
class mtetrisThread extends Thread
-
{
-
private Graphics curGraphic;
-
private int i=0;
-
private mtetris curTetris;
-
private int width;
-
private int height;
-
private boolean mContinue; //is the game over
-
private boolean mRun; //is running
-
private boolean mKey; //is calling keyAnswer()
-
private boolean isgetBlock; //should get new Block
-
private int paintpoint[][];
-
private int rootMap[][];
-
//public int activeMap[][];
-
-
private int baseX; //方块基准X
-
private int baseY; //方块基准Y
-
-
private Color bg;
-
private Color fg;
-
-
private static int Rotate=1004;
-
private static int Left=1006;
-
private static int Right=1007;
-
private static int Down=1005;
-
-
private int activeBlock[][]; //current active block
-
private int curBmode; //blockmode
-
private int curPmode; //paintmode
-
private int curCmode; //changemode
-
-
private int score;
-
-
public mtetrisThread(mtetris curApp,int width,int height,Color bg,Color fg)
-
{
-
curTetris=curApp;
-
this.width=width;
-
this.height=height;
-
this.bg=bg;
-
this.fg=fg;
-
//[0][],[1][]分别保存行坐标和纵坐标,和数组的存储是相反的
-
paintpoint=new int[2][width*height+1]; //[0][0]保存有效数据长度
-
rootMap=new int[height][width];
-
//activeBlock=new int[2][6]; //[0][0] the length
-
-
curBmode=0; //blockmode
-
curPmode=0; //paintmode
-
curCmode=0; //changemode
-
score=0;
-
-
isgetBlock=true;
-
mContinue=true;
-
mRun=true;
-
mKey=false;
-
-
for(int i=0;i<height;i++)
-
for(int j=0;j<width;j++)
-
rootMap[i][j]=0;
-
}
-
-
public void keyAnswer(int key)
-
{
-
if(mContinue && !mRun) //is Game over?
-
{
-
mKey=true;
-
switch(key)
-
{
-
case 1004:
-
if(Layout(Rotate))
-
{
-
paintBlock(baseX,baseY,curBmode,curCmode,2);
-
curCmode=(curCmode+1)%4;
-
paintBlock(baseX,baseY,curBmode,curCmode,curPmode);
-
}
-
break;
-
case 1007:
-
if(Layout(Right))
-
{
-
paintBlock(baseX,baseY,curBmode,curCmode,2);
-
baseX++;
-
paintBlock(baseX,baseY,curBmode,curCmode,curPmode);
-
}
-
break;
-
case 1006:
-
if(Layout(Left))
-
{
-
paintBlock(baseX,baseY,curBmode,curCmode,2);
-
baseX--;
-
paintBlock(baseX,baseY,curBmode,curCmode,curPmode);
-
}
-
break;
-
case 1005:
-
if(Layout(Down))
-
{
-
paintBlock(baseX,baseY,curBmode,curCmode,2);
-
baseY++;
-
paintBlock(baseX,baseY,curBmode,curCmode,curPmode);
-
}
-
break;
-
}
-
}
-
mKey=false;
-
return;
-
}
-
-
public String retScore()
-
{
-
return new Integer(score).toString();
-
}
-
-
private void getBlock() //get new Block
-
{
-
curBmode=(int)(Math.random()*9571)%7;
-
-
if(curBmode==0||curBmode==1)
-
{
-
baseX=3;
-
baseY=1;
-
}
-
else
-
{
-
baseX=3;
-
baseY=0;
-
}
-
paintBlock(baseX,baseY,curBmode,curCmode,-1);
-
-
//activeBlock[0][0]=paintpoint[0][0];
-
for(int i=1;i<=paintpoint[0][0];i++)
-
if(rootMap[paintpoint[1][i]][paintpoint[0][i]]!=0)
-
{
-
GameStop();
-
return;
-
}
-
Block(curPmode);
-
}
-
-
private void GameStop()
-
{
-
mContinue=false;
-
//curGraphic=curTetris.getGraphics();
-
//curGraphic.drawString("GameStop",200,20);
-
//curTetris.refresh();
-
}
-
-
public void run()
-
{
-
while(true)
-
{
-
try
-
{
-
sleep(300);
-
}
-
catch(InterruptedException e)
-
{
-
}
-
if(mContinue && !mKey)
-
{
-
mRun=true;
-
if(isgetBlock) //get new barrier
-
{
-
getBlock();
-
isgetBlock=false;
-
continue;
-
//GameStop();
-
}
-
if(Layout(Down))
-
{
-
paintBlock(baseX,baseY,curBmode,curCmode,2);
-
paintBlock(baseX,baseY+1,curBmode,curCmode,0);
-
baseY++;
-
/*activeBlock[0][0]=paintpoint[0][0];
-
for(int i=1;i<=paintpoint[0][0];i++)
-
{
-
activeBlock[0][i]=paintpoint[0][i];
-
activeBlock[1][i]=paintpoint[1][i];
-
}*/
-
}
-
else
-
{
-
paintBlock(baseX,baseY,curBmode,curCmode,1);
-
isgetBlock=true;
-
//renew the changemode
-
curCmode=0;
-
for(int j=1;j<=paintpoint[0][0];j++)
-
rootMap[paintpoint[1][j]][paintpoint[0][j]]=1;
-
updateMap();
-
}
-
-
//curGraphic.fill3DRect(100,(100+20*i++)%400,20,20,true);
-
}
-
mRun=false;
-
}
-
}
-
-
private void updateMap()
-
{
-
boolean fullLine=true;
-
paintpoint[0][0]=0;
-
int count=0;
-
int numline=0;
-
for(int i=height-1;i>=0;i--)
-
{
-
for(int j=0;j<width;j++)
-
if(rootMap[i][j]==0) fullLine=false;
-
-
if(!fullLine)
-
{
-
for(int k=0;k<width;k++)
-
if(rootMap[i][k]!=0)
-
{
-
count++;
-
paintpoint[1][count]=i+numline;
-
paintpoint[0][count]=k;
-
}
-
}
-
else
-
{
-
numline++;
-
score++;
-
}
-
fullLine=true;
-
}
-
paintpoint[0][0]=count;
-
-
if(numline==0)
-
return;
-
else
-
{
-
for(int i=0;i<height;i++) //refresh rootMap
-
for(int j=0;j<width;j++)
-
{
-
rootMap[i][j]=0;
-
}
-
for(int i=1;i<=paintpoint[0][0];i++)
-
rootMap[paintpoint[1][i]][paintpoint[0][i]]=1;
-
-
curGraphic=curTetris.getGraphics();
-
curGraphic.setColor(bg);
-
curGraphic.fillRect(0,0,200,400);
-
curGraphic.setColor(fg);
-
curTetris.refresh();
-
Block(1);
-
return;
-
}
-
}
-
-
private boolean Layout(int supposeMove)
-
{
-
boolean Feasibility=true;
-
switch(supposeMove)
-
{
-
case 1004:
-
paintBlock(baseX,baseY,curBmode,(curCmode+1)%4,-1);
-
for(int i=1;i<=paintpoint[0][0];i++)
-
if(paintpoint[1][i]>=0 && paintpoint[1][i]<height && paintpoint[0][i]>=0 && paintpoint[0][i]<width)
-
{
-
if(rootMap[paintpoint[1][i]][paintpoint[0][i]]!=0)
-
Feasibility=false;
-
}
-
else
-
{
-
return false;
-
}
-
break;
-
case 1006:
-
paintBlock(baseX-1,baseY,curBmode,curCmode,-1);
-
for(int i=1;i<=paintpoint[0][0];i++)
-
if(paintpoint[1][i]>=0 && paintpoint[1][i]<height && paintpoint[0][i]>=0 && paintpoint[0][i]<width)
-
{
-
if(rootMap[paintpoint[1][i]][paintpoint[0][i]]!=0)
-
Feasibility=false;
-
}
-
else
-
{
-
return false;
-
}
-
break;
-
case 1007:
-
paintBlock(baseX+1,baseY,curBmode,curCmode,-1);
-
for(int i=1;i<=paintpoint[0][0];i++)
-
if(paintpoint[1][i]>=0 && paintpoint[1][i]<height && paintpoint[0][i]>=0 && paintpoint[0][i]<width)
-
{
-
if(rootMap[paintpoint[1][i]][paintpoint[0][i]]!=0)
-
Feasibility=false;
-
}
-
else
-
{
-
return false;
-
}
-
break;
-
case 1005:
-
paintBlock(baseX,baseY+1,curBmode,curCmode,-1);
-
for(int i=1;i<=paintpoint[0][0];i++)
-
if(paintpoint[1][i]>=0 && paintpoint[1][i]<height && paintpoint[0][i]>=0 && paintpoint[0][i]<width)
-
{
-
if(rootMap[paintpoint[1][i]][paintpoint[0][i]]!=0)
-
Feasibility=false;
-
}
-
else
-
{
-
return false;
-
}
-
break;
-
default:
-
Feasibility=false;
-
}
-
-
if(paintpoint[0][0]==0)
-
Feasibility=false;
-
return Feasibility;
-
}
-
-
private void Block(int paintmode)
-
{
-
boolean flag=false;
-
curGraphic=curTetris.getGraphics();
-
switch(paintmode)
-
{
-
case 0: //绘制活动方块
-
flag=true;
-
case 1: //绘制固定方块
-
for(int i=1;i<=paintpoint[0][0];i++)
-
curGraphic.fill3DRect(paintpoint[0][i]*20,paintpoint[1][i]*20,20,20,flag);
-
break;
-
case 2: //用背景色填充
-
curGraphic.setColor(bg);
-
for(int j=1;j<=paintpoint[0][0];j++)
-
curGraphic.fillRect(paintpoint[0][j]*20,paintpoint[1][j]*20,20,20);
-
curGraphic.setColor(fg);
-
break;
-
default:
-
return;
-
}
-
curTetris.refresh(); //刷新
-
return;
-
}
-
//(x,y)是方块的基准坐标,blockmode是方块样式共七种,changemode={0,1,2,3}
-
//paintmode={0,1,2}
-
private void paintBlock(int x,int y,int blockmode,int changemode,int paintmode)
-
{
-
switch(blockmode)
-
{
-
case 0:
-
paintpoint[0][0]=4;
-
switch(changemode)
-
{
-
case 0:
-
paintpoint[0][1]=x;
-
paintpoint[1][1]=y-1;
-
paintpoint[0][2]=x;
-
paintpoint[1][2]=y;
-
paintpoint[0][3]=x+1;
-
paintpoint[1][3]=y;
-
paintpoint[0][4]=x+1;
-
paintpoint[1][4]=y+1;
-
break;
-
case 1:
-
paintpoint[0][1]=x;
-
paintpoint[1][1]=y;
-
paintpoint[0][2]=x+1;
-
paintpoint[1][2]=y;
-
paintpoint[0][3]=x-1;
-
paintpoint[1][3]=y+1;
-
paintpoint[0][4]=x;
-
paintpoint[1][4]=y+1;
-
break;
-
case 2:
-
paintpoint[0][1]=x;
-
paintpoint[1][1]=y;
-
paintpoint[0][2]=x;
-
paintpoint[1][2]=y+1;
-
paintpoint[0][3]=x+1;
-
paintpoint[1][3]=y+1;
-
paintpoint[0][4]=x+1;
-
paintpoint[1][4]=y+2;
-
break;
-
case 3:
-
paintpoint[0][1]=x+1;
-
paintpoint[1][1]=y;
-
paintpoint[0][2]=x+2;
-
paintpoint[1][2]=y;
-
paintpoint[0][3]=x;
-
paintpoint[1][3]=y+1;
-
paintpoint[0][4]=x+1;
-
paintpoint[1][4]=y+1;
-
break;
-
default:
-
paintpoint[0][0]=0;
-
}
-
break;
-
case 1:
-
paintpoint[0][0]=4;
-
switch(changemode)
-
{
-
case 0:
-
paintpoint[0][1]=x+1;
-
paintpoint[1][1]=y-1;
-
paintpoint[0][2]=x;
-
paintpoint[1][2]=y;
-
paintpoint[0][3]=x+1;
-
paintpoint[1][3]=y;
-
paintpoint[0][4]=x;
-
paintpoint[1][4]=y+1;
-
break;
-
case 1:
-
paintpoint[0][1]=x-1;
-
paintpoint[1][1]=y;
-
paintpoint[0][2]=x;
-
paintpoint[1][2]=y;
-
paintpoint[0][3]=x;
-
paintpoint[1][3]=y+1;
-
paintpoint[0][4]=x+1;
-
paintpoint[1][4]=y+1;
-
break;
-
case 2:
-
paintpoint[0][1]=x+1;
-
paintpoint[1][1]=y;
-
paintpoint[0][2]=x;
-
paintpoint[1][2]=y+1;
-
paintpoint[0][3]=x+1;
-
paintpoint[1][3]=y+1;
-
paintpoint[0][4]=x;
-
paintpoint[1][4]=y+2;
-
break;
-
case 3:
-
paintpoint[0][1]=x;
-
paintpoint[1][1]=y;
-
paintpoint[0][2]=x+1;
-
paintpoint[1][2]=y;
-
paintpoint[0][3]=x+1;
-
paintpoint[1][3]=y+1;
-
paintpoint[0][4]=x+2;
-
paintpoint[1][4]=y+1;
-
break;
-
default:
-
paintpoint[0][0]=0;
-
}
-
break;
-
case 2: //田字方块
-
paintpoint[0][0]=4;
-
paintpoint[0][1]=x;
-
paintpoint[1][1]=y;
-
paintpoint[0][2]=x;
-
paintpoint[1][2]=y+1;
-
paintpoint[0][3]=x+1;
-
paintpoint[1][3]=y+1;
-
paintpoint[0][4]=x+1;
-
paintpoint[1][4]=y;
-
break;
-
case 3: //Shape of 7
-
paintpoint[0][0]=4;
-
switch(changemode)
-
{
-
case 0:
-
paintpoint[0][1]=x+1;
-
paintpoint[1][1]=y;
-
paintpoint[0][2]=x-1;
-
paintpoint[1][2]=y+1;
-
paintpoint[0][3]=x;
-
paintpoint[1][3]=y+1;
-
paintpoint[0][4]=x+1;
-
paintpoint[1][4]=y+1;
-
break;
-
case 1:
-
paintpoint[0][1]=x;
-
paintpoint[1][1]=y;
-
paintpoint[0][2]=x+1;
-
paintpoint[1][2]=y;
-
paintpoint[0][3]=x+1;
-
paintpoint[1][3]=y+1;
-
paintpoint[0][4]=x+1;
-
paintpoint[1][4]=y+2;
-
break;
-
case 2:
-
paintpoint[0][1]=x;
-
paintpoint[1][1]=y;
-
paintpoint[0][2]=x+1;
-
paintpoint[1][2]=y;
-
paintpoint[0][3]=x+2;
-
paintpoint[1][3]=y;
-
paintpoint[0][4]=x;
-
paintpoint[1][4]=y+1;
-
break;
-
case 3:
-
paintpoint[0][1]=x;
-
paintpoint[1][1]=y-1;
-
paintpoint[0][2]=x;
-
paintpoint[1][2]=y;
-
paintpoint[0][3]=x;
-
paintpoint[1][3]=y+1;
-
paintpoint[0][4]=x+1;
-
paintpoint[1][4]=y+1;
-
break;
-
default:
-
paintpoint[0][0]=0;
-
}
-
break;
-
case 4: //Shape of reverse 7
-
paintpoint[0][0]=4;
-
switch(changemode)
-
{
-
case 0:
-
paintpoint[0][1]=x-1;
-
paintpoint[1][1]=y;
-
paintpoint[0][2]=x;
-
paintpoint[1][2]=y;
-
paintpoint[0][3]=x+1;
-
paintpoint[1][3]=y;
-
paintpoint[0][4]=x+1;
-
paintpoint[1][4]=y+1;
-
break;
-
case 1:
-
paintpoint[0][1]=x;
-
paintpoint[1][1]=y;
-
paintpoint[0][2]=x+1;
-
paintpoint[1][2]=y;
-
paintpoint[0][3]=x;
-
paintpoint[1][3]=y+1;
-
paintpoint[0][4]=x;
-
paintpoint[1][4]=y+2;
-
break;
-
case 2:
-
paintpoint[0][1]=x;
-
paintpoint[1][1]=y;
-
paintpoint[0][2]=x;
-
paintpoint[1][2]=y+1;
-
paintpoint[0][3]=x+1;
-
paintpoint[1][3]=y+1;
-
paintpoint[0][4]=x+2;
-
paintpoint[1][4]=y+1;
-
break;
-
case 3:
-
paintpoint[0][1]=x+1;
-
paintpoint[1][1]=y-1;
-
paintpoint[0][2]=x+1;
-
paintpoint[1][2]=y;
-
paintpoint[0][3]=x+1;
-
paintpoint[1][3]=y+1;
-
paintpoint[0][4]=x;
-
paintpoint[1][4]=y+1;
-
break;
-
default:
-
paintpoint[0][0]=0;
-
}
-
break;
-
case 5: //Shape of T
-
paintpoint[0][0]=4;
-
switch(changemode)
-
{
-
case 0:
-
paintpoint[0][1]=x;
-
paintpoint[1][1]=y;
-
paintpoint[0][2]=x-1;
-
paintpoint[1][2]=y+1;
-
paintpoint[0][3]=x;
-
paintpoint[1][3]=y+1;
-
paintpoint[0][4]=x+1;
-
paintpoint[1][4]=y+1;
-
break;
-
case 1:
-
paintpoint[0][1]=x+1;
-
paintpoint[1][1]=y;
-
paintpoint[0][2]=x;
-
paintpoint[1][2]=y+1;
-
paintpoint[0][3]=x+1;
-
paintpoint[1][3]=y+1;
-
paintpoint[0][4]=x+1;
-
paintpoint[1][4]=y+2;
-
break;
-
case 2:
-
paintpoint[0][1]=x;
-
paintpoint[1][1]=y;
-
paintpoint[0][2]=x+1;
-
paintpoint[1][2]=y;
-
paintpoint[0][3]=x+2;
-
paintpoint[1][3]=y;
-
paintpoint[0][4]=x+1;
-
paintpoint[1][4]=y+1;
-
break;
-
case 3:
-
paintpoint[0][1]=x;
-
paintpoint[1][1]=y-1;
-
paintpoint[0][2]=x;
-
paintpoint[1][2]=y;
-
paintpoint[0][3]=x+1;
-
paintpoint[1][3]=y;
-
paintpoint[0][4]=x;
-
paintpoint[1][4]=y+1;
-
break;
-
default:
-
paintpoint[0][0]=0;
-
}
-
break;
-
case 6: //Shape of l
-
paintpoint[0][0]=4;
-
switch(changemode)
-
{
-
case 0:
-
paintpoint[0][1]=x-1;
-
paintpoint[1][1]=y;
-
paintpoint[0][2]=x;
-
paintpoint[1][2]=y;
-
paintpoint[0][3]=x+1;
-
paintpoint[1][3]=y;
-
paintpoint[0][4]=x+2;
-
paintpoint[1][4]=y;
-
break;
-
case 1:
-
paintpoint[0][1]=x;
-
paintpoint[1][1]=y-1;
-
paintpoint[0][2]=x;
-
paintpoint[1][2]=y;
-
paintpoint[0][3]=x;
-
paintpoint[1][3]=y+1;
-
paintpoint[0][4]=x;
-
paintpoint[1][4]=y+2;
-
break;
-
case 2:
-
paintpoint[0][1]=x-1;
-
paintpoint[1][1]=y+1;
-
paintpoint[0][2]=x;
-
paintpoint[1][2]=y+1;
-
paintpoint[0][3]=x+1;
-
paintpoint[1][3]=y+1;
-
paintpoint[0][4]=x+2;
-
paintpoint[1][4]=y+1;
-
break;
-
case 3:
-
paintpoint[0][1]=x+1;
-
paintpoint[1][1]=y-1;
-
paintpoint[0][2]=x+1;
-
paintpoint[1][2]=y;
-
paintpoint[0][3]=x+1;
-
paintpoint[1][3]=y+1;
-
paintpoint[0][4]=x+1;
-
paintpoint[1][4]=y+2;
-
break;
-
default:
-
paintpoint[0][0]=0;
-
}
-
break;
-
default:
-
return;
-
}
-
if(paintmode!=-1)
-
{
-
Block(paintmode);
-
return;
-
}
-
return;
-
}
-
}
需要和html页面在同一目录下:
-
<html>
-
<center>
-
<applet code = "mtetris.class" width=250 height=400>
-
</applet>
-
</center>
-
</html>
阅读(930) | 评论(0) | 转发(0) |