俄罗斯方块java代码.docx
- 文档编号:26246338
- 上传时间:2023-06-17
- 格式:DOCX
- 页数:68
- 大小:92.62KB
俄罗斯方块java代码.docx
《俄罗斯方块java代码.docx》由会员分享,可在线阅读,更多相关《俄罗斯方块java代码.docx(68页珍藏版)》请在冰豆网上搜索。
俄罗斯方块java代码
importjavax.swing.*;
importjavax.swing.Timer;
importjava.awt.*;
importjava.awt.event.*;
importjava.util.*;
/**Title:
俄罗斯方块
*Description:
俄罗斯方块
*Copyright:
俄罗斯方块
*Company:
俄罗斯方块
*
*@authorZhuYong
*@version1.0
*/
publicclassSquaresGame{
publicstaticvoidmain(String[]args){newSquaresFrame().play();
}
}
classSquaresFrameextendsJFrame{publicfinalstaticintWIDTH=500;publicfinalstaticintHEIGHT=600;/**
*游戏区
*/
privateBoxPanelboxPanel;
/**
*暂停标记
*/
privatebooleanisPause=true;
/**
*默认构造函数
*/
publicSquaresFrame(){
/**
*记分面板
*/
InfoPanelinfoPanel=newInfoPanel();
boxPanel=newBoxPanel(infoPanel);
JMenuBarbar=newJMenuBar();
JMenumenu=newJMenu("菜单");
JMenuItembegin=newJMenuItem("新游戏");
finalJMenuItempause=newJMenuItem("暂停");
JMenuItemstop=newJMenuItem("结束");
setJMenuBar(bar);
bar.add(menu);
menu.add(begin);
menu.add(pause);
menu.add(stop);
stop.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){
System.exit(0);
}
});
pause.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){
if(isPause){
boxPanel.supend(false);
isPause=!
isPause;
pause.setText("恢复");
}
else{
boxPanel.supend(true);
isPause=!
isPause;
pause.setText("暂停");
}
}
});
begin.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){
boxPanel.newGame();
}
});
boxPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"Box"));
infoPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"Info"));
add(boxPanel,BorderLayout.CENTER);
add(infoPanel,BorderLayout.EAST);
setTitle("SquaresGame");
setSize(WIDTH,HEIGHT);
//不可改变框架大小
setResizable(false);
//定位显示到屏幕中间
setLocation(((int)Toolkit.getDefaultToolkit().getScreenSize().getWidth()-WIDTH)/2,
((int)Toolkit.getDefaultToolkit().getScreenSize().getHeight()-HEIGHT)/2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setVisible(true);
}
publicvoidplay(){
boxPanel.newGame();
}
}
/**
*
*@authorZhuYong
*游戏区面板
*/
classBoxPanelextendsJPanel{
/**
*定义行
*/
publicfinalstaticintROWS=20;
/**
*定义列
*/
publicfinalstaticintCOLS=10;
publicfinalstaticColorDISPLAY=newColor(128,128,255);/**
*没有方块时显示的颜色
*/
publicfinalstaticColorHIDE=newColor(238,238,238);/**
*记分面板上显示的下一个方块的颜色
*/
publicfinalstaticColorNEXTDISPLAY=Color.ORANGE;/**
*是否显示网络
*/
publicfinalstaticbooleanPAINTGRID=false;/**
*用4个按钮表示一个方块
*/
privateJButton[][]squares=newJButton[ROWS][COLS];/**
*定义对前位置是否有方块.用颜色区别.
*/
privateint[][]stateTable=newint[ROWS][COLS];privateInfoPanelscorePanel;
privateABoxcurrentBox;
privateABoxnextBox;
/**
*各方块颜色数组.其中0号颜色为无方块时颜色.共7种方块.最后一个消行时颜色*/
privatestaticColor[]boxColor=newColor[]{newColor(238,238,238),newColor(128,128,255),newColor(189,73,23),
newColor(154,105,22),newColor(101,124,52),newColor(49,100,128),newColor(84,57,119),newColor(111,54,102),
newColor(126,50,57),Color.RED};
/**
*定时器,负责方块自动下移
*/
privateTimertimer;
/**
*Level用定时器的延时控制
*/
privateintlevel;
/**
*初时化时的延时
*/
privatefinalstaticintINITDELAY=940;/**
*每一级别延时减少的步进值
*/
privatefinalstaticintINC=90;
publicBoxPanel(InfoPanelpanel){
setLayout(newGridLayout(ROWS,COLS,1,1));
//可能获取焦点,才能接受键盘事件
setFocusable(true);
//setCursor(newCursor
(1));
/**
*初时化,生成JButton,设置颜色.JButton数组按照先第一行再第二行的顺序添加.下
标为[行][列]和[x][y]正好相反.
*/
for(inti=0;i for(intj=0;j JButtonsquare=newJButton(""); square.setEnabled(false); square.setBorderPainted(PAINTGRID); squares[i][j]=square; stateTable[i][j]=0; add(square); } scorePanel=panel; timer=newTimer(INITDELAY,newautoDownHandler()); //注册键盘事件 addKeyListener(newKeyAdapter(){publicvoidkeyPressed(KeyEvente){ intkey=e.getKeyCode(); //System.out.println(bt.getX()+": "+bt.getY());if(key==KeyEvent.VK_DOWN)dropdown();elseif(key==KeyEvent.VK_UP)rotate();elseif(key==KeyEvent.VK_RIGHT)right();elseif(key==KeyEvent.VK_LEFT)left(); } }); } /** *重新所有方块.即重新改变所有JButton背景色 */ privatevoidshowBox(){ for(inti=0;i for(intj=0;j squares[i][j].setBackground(boxColor[stateTable[i][j]]); } /** *只重新显示当前方块 */ privatevoidshowCurrentBox(){ point[]location=currentBox.getLocation();for(inti=0;i<4;i++){ introw=location[i].getY(); intcol=location[i].getX(); squares[row][col].setBackground(boxColor[stateTable[row][col]]); } } /** *消行记分 */ privatevoidclearBox(){ intclearLines=0; for(inti=ROWS-1;i>=0;i--) for(intj=0;j if(stateTable[i][j]==0)break; if(j==(COLS-1)){ removeLine(i); clearLines++; i++;} } if(clearLines>0){ scorePanel.winScore(clearLines); upgrade(); } } /** *消行,重置属性表.下移 *@paramline */ publicvoidremoveLine(intline){timer.stop(); for(intj=0;j stateTable[line][j]=8; //System.out.println(squares[line][j].getBackground()); squares[line][j].setBackground(Color.RED); //System.out.println(squares[line][j].getBackground()); //validate(); } showBox(); for(inti=line;i>=0;i--) for(intj=0;j if(i! =0) stateTable[i][j]=stateTable[i-1][j];else stateTable[i][j]=0; showBox(); timer.start(); } /** *检查把方块移动到(x,y)时是否合法. *@paramx *@paramy *@return */ publicbooleancheckAction(intx,inty){ //先移除当前方块,再判断.当前方块和下一位置的方块有共用的位置. removeCurrentBox(); booleanret=true; point[]list=currentBox.getShape().getList(); intxx=0; intyy=0; for(inti=0;i<4;i++){ if(i! =0){xx=list[i].getX()+x;yy=list[i].getY()+y;} else{xx=x;yy=y;} //System.out.println("check"+i+"->"+xx+": "+yy); if(xx>=COLS||xx<0){ret=false;break;}if(yy>=ROWS||yy<0){ret=false;break;}//System.out.println("afterbreakcheck"+i+"->"+xx+": "+yy);if(stateTable[yy][xx]! =0){ret=false;break;} } addCurrentBox(); returnret; } /** *左移 */ publicvoidleft(){ if (checkAction(currentBox.getHead().getX()-1,currentBox.getHead().getY())){ removeCurrentBox(); showCurrentBox(); currentBox.setHead(currentBox.getHead().getX()-1,currentBox.getHead() .getY()); addCurrentBox(); //showBox(); showCurrentBox(); } } /** *右移 */ publicvoidright(){ if (checkAction(currentBox.getHead().getX()+1,currentBox.getHead().getY())){ removeCurrentBox(); showCurrentBox(); currentBox.setHead(currentBox.getHead().getX()+1,currentBox.getHead() .getY()); addCurrentBox(); //showBox(); showCurrentBox(); } } /** *下移 *@return */ publicbooleandown(){ intchoose=0; if (checkAction(currentBox.getHead().getX(),currentBox.getHead().getY()+1)){ removeCurrentBox(); showCurrentBox(); currentBox.setHead(currentBox.getHead().getX(),currentBox.getHead().g etY()+1); addCurrentBox(); showCurrentBox(); //showBox(); returntrue; } else{ clearBox(); currentBox=nextBox.setDefault(); nextBox=BoxFactory.getBox().setDefault();scorePanel.setNext(nextBox);//showBox(); //检查是否游戏结束 if(checkGameOver()){ supend(false); choose=JOptionPane.showConfirmDialog(this,"游戏结束\n你的 积分为"+scorePanel.getScore()+"\n要开始新游戏吗? ", "Game Over",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE); if(choose==JOptionPane.YES_OPTION)newGame(); } addCurrentBox(); showCurrentBox(); //showBox(); returnfalse; } } /** *丢下. */ publicvoiddropdown(){ while(down()){}; } /** *判断是否结束 *@return */ privatebooleancheckGameOver(){ point[]location=currentBox.getLocation();for(inti=0;i<4;i++){ introw=location[i].getY(); intcol=location[i].getX(); if(stateTable[row][col]! =0)returntrue; } returnfalse; } /** *变形 */ publicvoidrotate(){ if(checkRotate()){ removeCurrentBox(); showCurrentBox(); currentBox.rotate(); addCurrentBox(); showCurrentBox(); //showBox(); } } /** *检查变形是否合法 *@return */ privatebooleancheckRotate(){ removeCurrentBox(); intoldheadx=currentBox.getHead().getX();intoldheady=currentBox.getHead().getY();//System.out.println("oldhead: "+oldheadx+","+oldheady);booleanret=true; point[]shape=currentBox.getNextShape().getList();intx=0,y=0,newheadx=0,newheady=0;for(inti=0;i<4;i++){ if(i! =0){ x=newheadx+shape[i].getX(); y=newheady+shape[i].getY(); } else{ newheadx=oldheadx+shape[i].getX(); newheady=oldheady+shape[i].getY(); x=newheadx; y=newheady; //System.out.println("newhead: "+newheadx+": "+newheady); } //System.out.println("checkrotate"+i+"->"+x+": "+y); if(x>=COLS||x<0){ret=false;break;}if(y>=ROWS||y<0){ret=false;break;}//System.out.println("afterbreakcheckrotate"+i+"->"+x+": "+y);if(stateTable[y][x]! =0){ret=false;break;} } addCurrentBox(); returnret; } /** *移除当前方块.设置颜色为无色.主要用于移动,变形时.清除当前位置方块.显示下一位置方 块. */ privatevoidremoveCurrentBox(){ //System.out.println("removeCurrentBox: "+Thread.currentThread().getName()); for(inti=0;i<4;i++){ intx=currentBox.getLocation()[i].getX();inty=currentBox.getLocation()[i].getY();stateTable[y][x]=0; } } /** *添加当前方块到属性表. */ privatevoidaddCurrentBox(){ for(inti=0;i<4;i++){ intx=currentBox.getLocation()[i].getX();inty=currentBox.getLocation()[i].getY();stateTable[y][x]=currentBox.getId(); } } /** *暂停游戏 *false时暂停 *@paramresume */ publicvoidsupend(booleanresume){ if(! resume){ timer.stop(); setFocusable(false); } else{ timer.start(); setFocusable(true); this.requestFocus(); } } /** *设置游戏级别.即改变定时器延时. *@paramlevel */ publicvoidsetLevel(intlevel){ this.level=level; scorePanel.setLevel(level); timer.setDelay(INITDELAY-INC*level); } /** *自动下移监听器 *@authorAdministrator * */ privateclassautoDownHandlerimplementsActionListener{publicvoidactionPerformed(ActionEvente){ down(); } } /** *根据积分自
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 俄罗斯方块 java 代码
