Javaeclipse游戏
导入Java . awt . *;
导入Java . awt . event . *;
类Game扩展JFrame实现KeyListener,Runnable
{
Thread Down10 =新线程(this);
game canvas test = new game canvas();
JLabel LabelTetris = new JLabel(“俄罗斯方块游戏”);
JLabel Score print 1 = new JLabel(" Score ");
JLabel score print 2 = new JLabel(" 0 "+test。分数);
游戏()
{
setSize(400,800);
set visible(true);
set layout(new flow layout());
test . addkey listener(this);
label teris . set background(color . white);
label teris . set Font((新字体(" Tahoma ",1,50)));
ScorePrint1.setFont((新字体(" Tahoma ",1,40)));
score print 2 . set background(color . white);
JLabel LM = new JLabel(" L . M ");
ScorePrint2.setFont((新字体(" Tahoma ",1,40)));
add(label teris);
添加(测试);
add(score print 1);
add(score print 2);
lm . set foreground(color . white);
add(LM);
validate();
down 10 . start();
}
公共无效运行()
{
while(真)
{
尝试
{
测试。down 1();
down 10 . sleep(700);
}
catch(interrupted exception I){ I . printstacktrace();}
}
}
公共void按键(按键事件K)
{
if(K.getKeyCode()==KeyEvent。VK _左)
测试。left 1();
else if(k . get key code()= = key event。VK_UP)
测试。转();
else if(k . get key code()= = key event。VK _右)
测试。right 1();
else if(k . get key code()= = key event。VK_DOWN)
{
测试。down 1();
如果(测试。ScoreBool==1)
{ score print 2 . settext("+test。分数);}
}
}
public void key released(key event e){ }
public void key typed(key event e){ }
}
类GameCanvas扩展画布
{
int x=5,y = 0;
int GameMap[][]= new int[50][100];
int BlockShape
int BlockWay
int得分= 0;
int score bool = 0;
int over pool = 0;
public final static int[][][]Blocks = { { { 0,0,0,0,1,1,1,0,0,0,0,0},
{0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0},
{0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0},
{0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0}},
{{0,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0},
{1,0,0,0,1,1,0,0,0,1,0,0,0,0,0,0},
{0,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0},
{1,0,0,0,1,1,0,0,0,1,0,0,0,0,0,0}},
{{1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0},
{0,1,0,0,1,1,0,0,1,0,0,0,0,0,0,0},
{1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0},
{0,1,0,0,1,1,0,0,1,0,0,0,0,0,0,0}},
{{0,1,0,0,0,1,0,0,1,1,0,0,0,0,0,0},
{1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0},
{1,1,0,0,1,0,0,0,1,0,0,0,0,0,0,0},
{1,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0}},
{{1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0},
{1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0},
{1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0},
{1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0}},
{{1,0,0,0,1,0,0,0,1,1,0,0,0,0,0,0},
{1,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0},
{1,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0},
{0,0,1,0,1,1,1,0,0,0,0,0,0,0,0,0}},
{{0,1,0,0,1,1,1,0,0,0,0,0,0,0,0,0},
{0,1,0,0,1,1,0,0,0,1,0,0,0,0,0,0},
{1,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0},
{0,1,0,0,0,1,1,0,0,1,0,0,0,0,0,0}}};
GameCanvas()
{
setSize(340,500);
setBackground(颜色。黑色);
for(int I = 0;我& lt25;i++)
{
游戏地图[0][I]= 2;
GameMap[16][I]= 2;
}
for(int I = 0;我& lt16;i++)
{
GameMap[I][24]= 2;
}
}
公共void新块()
{
x = 5;y = 0;
block shape =(int)(math . random()* 6);
block way =(int)(math . random()* 3);
if(IsOver(x,y)==true)
{ over bool = 1;}
}
public void Keep(int x,int y,int BlockShape,int BlockWay)
{
int n = 0;
for(int I = 0;我& lt4;i++)
for(int j = 0;j & lt4;j++)
{
if(GameMap[x+j+1][y+I]= = 0)
{
GameMap[x+j+1][y+I]= Blocks[block shape][block way][n];
}
n++;
}
}
公共空白油漆(图形G)
{
g . set color(color . white);
for(int I = 0;我& lt16;i++)
{
if(Blocks[block shape][block way][I]= = 1)
G.drawRect((i%4+x+1)*20,(i/4+y)*20,20,20);
}
for(int I = 0;我& lt30;i++)
for(int j = 0;j & lt50;j++)
if(GameMap[I][j]= = 1 | | GameMap[I][j]= = 2)
G.fillRect(i*20,j*20,20,20);
}
公共void Turn()
{
if(is可行(x,y,BlockShape,BlockWay)==1)
{
block way =(block way+1)% 4;
repaint();
}
}
公共void Left1()
{
if(is可行(x-1,y,BlockShape,BlockWay)==1)
{
x-= 1;
repaint();
}
}
公共作废权1()
{
if(is可行(x+1,y,BlockShape,BlockWay)==1)
{
x+= 1;
repaint();
}
}
公共void Down1()
{
if(is可行(x,y+1,BlockShape,BlockWay)==1)
{
y+= 1;
repaint();
}
else if(is可行(x,y+1,BlockShape,BlockWay)==0)
{
Keep(x,y,BlockShape,block way);
deline();
if(OverBool==0)
new blocks();
}
}
public int是可行的(int x,int y,int BlockShape,int BlockWay)
{
for(int I = 0;我& lt4;i++)
{
for(int j = 0;j & lt4;j++)
{
if(Blocks[block shape][block way][I * 4+j]= = 1 & amp;& amp游戏地图[x+j+1][y+i]==1)
返回0;
else if(Blocks[block shape][block way][I * 4+j]= = 1 & amp;& amp游戏地图[x+j+1][y+i]==2)
返回0;
}
}
返回1;
}
公共void Deline()
{
int blocks sum = 0;
for(int I = 0;我& lt25;i++)
{
for(int j = 0;j & lt16;j++)
{
if (GameMap[j][i]==1)
{
blocks sum++;
if (BlocksSum==15)
{
for(int p = I;p & gt0;p -)
{
for(int q = 0;q & lt16;q++)
{
游戏地图[q][p]=游戏地图[q][p-1];
}
}
得分+= 10;
score bool = 1;
}
}
}
blocks sum = 0;
}
}
public boolean IsOver(int x,int y)
{
if(is可行(x,y,BlockShape,BlockWay)==0)
返回true
其他
返回false
}
}
公开课俄罗斯方块
{
公共静态void main(String[] args)
{
Game test2 = new Game();
}
}
拿着它玩新的JAVA文件,名字叫Tetris,之前懒得修饰界面。