java俄罗斯方块实验报告.docx
- 文档编号:24145497
- 上传时间:2023-05-24
- 格式:DOCX
- 页数:16
- 大小:17.97KB
java俄罗斯方块实验报告.docx
《java俄罗斯方块实验报告.docx》由会员分享,可在线阅读,更多相关《java俄罗斯方块实验报告.docx(16页珍藏版)》请在冰豆网上搜索。
java俄罗斯方块实验报告
importjavax.swing.JFrame;
publicclassGame{
publicstaticvoidmain(String[]args){
ShapeFactoryshapeFactory=newShapeFactory();
Groundground=newGround();
GamePanelgamePanel=newGamePanel();
Controllercontroller=newController(shapeFactory,ground,gamePanel);
JFrameframe=newJFrame("俄罗斯方块");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(gamePanel.getSize().width+10,gamePanel.getSize().height+35);
frame.add(gamePanel);
gamePanel.addKeyListener(controller);
frame.addKeyListener(controller);
frame.setVisible(true);
controller.newGame();
}
}
importjava.awt.event.KeyAdapter;
importjava.awt.event.KeyEvent;
//处理按键事件,处理逻辑
publicclassControllerextendsKeyAdapterimplementsShapeListener{
privateShapeshape;
privateShapeFactoryshapeFactory;
privateGroundground;
privateGamePanelgamePanel;
publicvoidkeyPressed(KeyEvente){
switch(e.getKeyCode()){
caseKeyEvent.VK_UP:
if(ground.isMoveable(shape,Shape.ROTATE))
shape.roate();
break;
caseKeyEvent.VK_DOWN:
if(ground.isMoveable(shape,Shape.DOWN))
shape.moveDown();
break;
caseKeyEvent.VK_LEFT:
if(ground.isMoveable(shape,Shape.LEFT))
shape.moveLeft();
break;
caseKeyEvent.VK_RIGHT:
if(ground.isMoveable(shape,Shape.RIGHT))
shape.moveRight();
break;
}
gamePanel.display(ground,shape);
}
//下面这个方法“synchronized”是同步的意思。
因为有多个线程用到这个方法
publicsynchronizedbooleanisShapeMoveDown(Shapeshape){
if(ground.isMoveable(shape,Shape.DOWN))
returntrue;
ground.accept(this.shape);//图形变成障碍物
if(!
ground.isFull()){
this.shape=shapeFactory.getShape(this);//产生一个新的图形
}
if(ground.isFull()){
System.exit(0);
}
returnfalse;
}
@Override
publicvoidshapeMoveDown(Shapeshape){
gamePanel.display(ground,shape);
}
publicvoidnewGame(){
shape=shapeFactory.getShape(this);
}
publicController(ShapeFactoryshapeFactory,Groundground,GamePanelgamePanel){
this.gamePanel=gamePanel;
this.ground=ground;
this.shapeFactory=shapeFactory;
}
}
importjava.awt.Color;
importjava.awt.Graphics;
importjavax.swing.JPanel;
//整个游戏面板
publicclassGamePanelextendsJPanel{
privateGroundground;
privateShapeshape;
//显示的方法
publicvoiddisplay(Groundground,Shapeshape){
System.out.println("GamePanel'sdisplay");
this.repaint();
this.shape=shape;
this.ground=ground;
}
protectedvoidpaintComponent(Graphicsg){
//重新显示
g.setColor(newColor(0xcfcfcf));
g.fillRect(0,0,Global.WIDTH*Global.CELL_SIZE,Global.HEIGHT*Global.CELL_SIZE);
if(shape!
=null&&ground!
=null){
shape.drawMe(g);
ground.drawMe(g);
}
}
publicGamePanel(){
this.setSize(Global.WIDTH*Global.CELL_SIZE,Global.HEIGHT*Global.CELL_SIZE);
}
}
publicclassGlobal{
publicstaticfinalintCELL_SIZE=20;
publicstaticfinalintWIDTH=15;
publicstaticfinalintHEIGHT=15;
}
importjava.awt.Graphics;
//存放障碍物的类
publicclassGround{
privateint[][]obstacles=newint[Global.WIDTH][Global.HEIGHT];
//障碍物
publicvoidaccept(Shapeshape){//接受图形,把图形变成障碍物
System.out.println("Ground'saccept");
for(intx=0;x<4;x++){
for(inty=0;y<4;y++){
if(shape.isMember(x,y,false)){
obstacles[shape.getLeft()+x][shape.getTop()+y]=1;
}
}
}
for(inty=Global.HEIGHT-1;y>=0;y--){
deleteFullLine();
}
}
privatevoiddeleteFullLine(){
for(inty=Global.HEIGHT-1;y>=0;y--){//判断是否满行一行一行的判断,从最下面一行开始判断
booleanfull=true;
for(intx=0;x if(obstacles[x][y]==0) full=false;//如果有一列有空白。 。 则full=false } if(full){ deleteLine(y);//消除这一行 } } } privatevoiddeleteLine(intlineNum){//消除这一行的方法 for(inty=lineNum;y>0;y--){//满行的地方起,以上的行总体下一行 for(intx=0;x obstacles[x][y]=obstacles[x][y-1]; } } for(intx=0;x obstacles[x][0]=0; } } publicvoiddrawMe(Graphicsg){ System.out.println("Ground'sdrawMe"); for(intx=0;x for(inty=0;y if(obstacles[x][y]==1){ g.fill3DRect(x*Global.CELL_SIZE,y*Global.CELL_SIZE, Global.CELL_SIZE,Global.CELL_SIZE,true); } } } } publicbooleanisMoveable(Shapeshape,intaction){ intleft=shape.getLeft(); inttop=shape.getTop(); switch(action){ caseShape.LEFT: left--; break; caseShape.RIGHT: left++; break; caseShape.DOWN: top++; break; } for(intx=0;x<4;x++){ for(inty=0;y<4;y++){ if(shape.isMember(x,y,action==Shape.ROTATE)){ if(top+y>=Global.HEIGHT||left+x<0||left+x>=Global.WIDTH|| obstacles[left+x][top+y]==1)//超出边界或者对应位置是障碍物返回false returnfalse; } } } returntrue; } publicbooleanisFull(){ for(intx=0;x if(obstacles[x][0]==1)//判断第一行是否有障碍物 returntrue; } returnfalse; } } importjava.awt.Color; importjava.awt.Graphics; //各种形状的方块 publicclassShape{ publicstaticfinalintROTATE=0; publicstaticfinalintLEFT=1; publicstaticfinalintRIGHT=2; publicstaticfinalintDOWN=3; privateint[][]body; privateintstatus; privateintleft; privateinttop; privateShapeListenerlistener; //3个移动的方法: left,right,down。 。 没有向上的还有一个旋转的方法 publicvoidmoveLeft(){ System.out.println("Shape'smoveLeft"); left--; } publicvoidmoveRight(){ System.out.println("Shape'smoveRight"); left++; } publicvoidmoveDown(){ System.out.println("Shape'smoveDown"); top++; } publicvoidroate(){ System.out.println("Shape'sroate"); status=(status+1)%body.length; } //显示方块的方法 publicvoiddrawMe(Graphicsg){ System.out.println("Shape'sfrawMe"); g.setColor(Color.BLUE); for(intx=0;x<4;x++){ for(inty=0;y<4;y++){ if(getFlagByPoint(x,y)){ g.fill3DRect((left+x)*Global.CELL_SIZE,(y+top)*Global.CELL_SIZE,Global.CELL_SIZE,Global.CELL_SIZE,true); } } } } privatebooleangetFlagByPoint(intx,inty){ returnbody[status][y*4+x]==1; } publicbooleanisMember(intx,inty,booleanrotate){ inttempstatus=status; if(rotate){ tempstatus=(status+1)%body.length; } returnbody[tempstatus][y*4+x]==1; } privateclassShapeDriverimplementsRunnable{ publicvoidrun(){ while(listener.isShapeMoveDown(Shape.this)){ moveDown(); listener.shapeMoveDown(Shape.this); try{ Thread.sleep(1000); }catch(InterruptedExceptione){ e.printStackTrace(); } } } } publicShape(){ newThread(newShapeDriver()).start(); } publicvoidaddShapeListener(ShapeListenerl){ if(l! =null) this.listener=l; } publicvoidsetBody(intbody[][]){ this.body=body; } publicvoidsetStatus(intstatus){ this.status=status; } publicintgetTop(){ returntop; } publicintgetLeft(){ returnleft; } } importjava.util.Random; publicclassShapeFactory{ privateintshapes[][][]=newint[][][]{//7个 { {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, }, {0,1,0,0,0,1,0,0, 1,1,0,0,0,0,0,0, } }, { {0,0,1,0,1,1,1,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, } }, { {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,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,1,1,0,0,0,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,1,1,0,0,0,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, } }, { {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, } } }; publicShapegetShape(ShapeListenerlistener){//产生不同形状的图形 System.out.println("ShapeFactory'sgetShape"); Shapeshape=newShape(); shape.addShapeListener(listener); inttype=newRandom().nextInt(shapes.length); shape.setBody(shapes[type]); shape.setStatus(0); returnshape; } } publicinterfaceShapeListener{ voidshapeMoveDown(Shapeshape); booleanisShapeMoveDown(Shapeshape); }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java 俄罗斯方块 实验 报告