俄罗斯方块java代码.docx
importjavax.swing.暂停标记*/private boolean isPause = true;/ *;importjavax.swing.Timer;importjava.awt.*;importjava.awt.event.*;importjava.util.;*默认构造函数/public SquaresFrameO *记分面板InfoPanel infoPanel = new InfoPanel();Title:俄罗斯方块*Description:俄罗斯方块*Copyright:俄罗斯方块*Company:俄罗斯方块* authorZhUYong* version1.OpublicclassSquaresGamepublicstaticvoidmain(Stringargs)newSquaresFrame().play();)classSqUareSFrameextendsJFramepublicfinalstaticintWIDTH=500;publicfinalstaticintHEIGHT=600;*游戏区*/privateBoxPanelboxPanel;boxPanel=newBoxPanel(infoPanel);JMenuBarbar=newJMenuBar();JMenumenu=newJMenU(“菜单”);JMenuItembegin=newJMenU工tem(“新游戏”);finalJMenuItempause=newJMenU工tem("暂停");JMenuItemstop=newJMenU工tem(“结束”);SetJMenuBar(bar);bar.add(menu);menu.add(begin);menu.add(pause);menu.add(stop);stop.addActIonListener(newActionListenerOpublicvoidactionPerformed(ActionEvente)System.exit(O););pause.addActIonListener(newActionListenerOpublicvoidactionPerformed(ActionEvente)if(isPause)boxPanel.supend(false);isPause=!isPause;pause.setText(“恢复”);elseboxPanel.supend(true);isPause=!isPause;pause.setText(“暂停“););begin.addActIonListener(newActionListenerOpublicvoidactionPerformed(ActionEvente)boxPanel.newGame();)boxPanel.SetBorder(BorderFactory.CreateTitledBorder(BorderFactory.ereateEtChedBorder(),Box);infoPanel.SetBorder(BorderFactory.CreateTitlecfBorcfer(BorderFactory.createEtchedBorder)r,Infon);add(boxPanel,BorderLayout.CENTER);add(infoPanel,BorderLayout.EAST);SetTitle(nSquaresGame',);setSize(WIDTHfHEIGHT);/不可改变框架大小SetResizable(false);/定位显示到屏幕中间SetLocation(int)Toolkit.getDefaultToolkit().getScreenSize().getWidth()-IVlDTH)/2,(int)Toolkit.getDefaultToolkit().getScreenSize().getHeight()-HEIGHT)/2);SetDefaultcioseOperation(JFrame.EXIT_ON_CLOSE);SetVisible(true);publicvoidplay()boxPanel.newGame();)* authorZhUYong* 游戏区面板* /classBoxPaneIextendsJPanel*定义行*/publicfinalstaticintROWS=20;*定义列public final staticint COLS = 10;publicfinalstaticColorDISPLAY=newColor(128z128z255);* 没有方块时显示的颜色* /publicfinalstaticColorHIDE=newColor(238z238z238);/大记分面板上显示的下一个方块的颜色* /publicfinalstaticColorNEXTDISPLAY=Color.ORANGE;* 是否显示网络publicfinalstaticbooleanPAINTGRID=false;/ Level用定时器的延时控制* 用4个按钮表示一个方块* /privateJButtonsquares=newJButtonROWSCOLS;/* 定义对前位置是否有方块.用颜色区别.* /privateintStateTable=newintROWSCOLS;privateInfoPanelScorePanel;privateABoxCurrentBox;privateABoxnextBox;* 各方块颜色数组.其中0号颜色为无方块时颜色.共7种方块.最后一个消行时颜色*/privatestaticColorboxColor=newColornewColor(238f238f238)znewColor(128z128,255)znewColor(189z73z23)znewColor(154z105f22),newColor(101r124z52),newColor(49r100128),newColor(84z57z119),newColor(Illz54z102)fnewColor(1265057)rColor.RED;* 定时器,负责方块自动下移* /privateTimertimer;privateintlevel;* 初时化时的延时* /privatefinalstaticintINITDELAY=940;* 每一级别延时减少的步进值* /privatefinalstaticintINC=90;publicBoxPanel(InfoPanelpanel)SetLayout(newGridLayout(RoWS,COLSr1,1);/可能获取焦点,才能接受键盘事件SetFocusable(true);/setCursor(newCursor(1);*初时化,生成JBUtton,设置颜色.JButton数组按照先第一行再第二行的顺序添加.下标为行列和x丫正好相反.*/for(inti=0;i<ROWS;i+)for(intj=0;j<COLS;j+)JButtonsquare=newJButton(,);square.SetEnabled(false);square.SetBorderPainted(PAINTGRID);squaresij=square;StateTableij=0;add(square);)ScorePanel=panel;timer=newTimer(INITDELAYrnewautoDownHandler();/注册键盘事件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<ROWS;i+)for(intj=0;j<COLS;j+)squaresij.SetBackground(boxColorStateTableij);*只重新显示当前方块privatevoidShowCurrentBoxOpointlocation=CurrentBox.getLocation();for(inti=0;i<4;i+)introw=locationi.getY();intcol=locationi.getX();squaresrowcol.SetBackground(boxColorStateTablerowcol);)/* 消行记分* /privatevoidClearBox()intClearLines=O;for(intI=ROWS-I;i>=0;i-)for(intj=0;j<COLS;j+)if(StateTableij=0)break;if(j=(COLS-I)removeLine(i);ClearLines+;i+;)if(clearLines>O)ScorePanel.WinScore(ClearLines);upgrade();)/* 消行,重置属性表.下移* paramline* /publicvoidremoveLine(intline)timer.stop();for(intj=0;j<COLS;j+)StateTablelinej=8;/System.out.println(squareslinej.getBackground();squareslinej.SetBackground(Color.RED);/System.out.printin(squareslinej.gtBackground();/validate();ShowBox();for(inti=line;i>=0;i一一)for(intj=0;j<COLS;j+)if(i!=0)StateTableij=StateTablei-1j;elseStateTableij=O;ShowBox();timer.start();)* 检查把方块移动到(x,y)时是否合法.* paramx* paramy*