《俄罗斯方块java代码.docx》由会员分享,可在线阅读,更多相关《俄罗斯方块java代码.docx(49页珍藏版)》请在优知文库上搜索。
1、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.Opu
2、blicclassSquaresGamepublicstaticvoidmain(Stringargs)newSquaresFrame().play();)classSqUareSFrameextendsJFramepublicfinalstaticintWIDTH=500;publicfinalstaticintHEIGHT=600;*游戏区*/privateBoxPanelboxPanel;boxPanel=newBoxPanel(infoPanel);JMenuBarbar=newJMenuBar();JMenumenu=newJMenU(“菜单”);JMenuItembegin=new
3、JMenU工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(newActionListe
4、nerOpublicvoidactionPerformed(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(B
5、orderFactory.CreateTitledBorder(BorderFactory.ereateEtChedBorder(),Box);infoPanel.SetBorder(BorderFactory.CreateTitlecfBorcfer(BorderFactory.createEtchedBorder)r,Infon);add(boxPanel,BorderLayout.CENTER);add(infoPanel,BorderLayout.EAST);SetTitle(nSquaresGame,);setSize(WIDTHfHEIGHT);/不可改变框架大小SetResiza
6、ble(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* 游戏区面板* /c
7、lassBoxPaneIextendsJPanel*定义行*/publicfinalstaticintROWS=20;*定义列public final staticint COLS = 10;publicfinalstaticColorDISPLAY=newColor(128z128z255);* 没有方块时显示的颜色* /publicfinalstaticColorHIDE=newColor(238z238z238);/大记分面板上显示的下一个方块的颜色* /publicfinalstaticColorNEXTDISPLAY=Color.ORANGE;* 是否显示网络publicfinals
8、taticbooleanPAINTGRID=false;/ Level用定时器的延时控制* 用4个按钮表示一个方块* /privateJButtonsquares=newJButtonROWSCOLS;/* 定义对前位置是否有方块.用颜色区别.* /privateintStateTable=newintROWSCOLS;privateInfoPanelScorePanel;privateABoxCurrentBox;privateABoxnextBox;* 各方块颜色数组.其中0号颜色为无方块时颜色.共7种方块.最后一个消行时颜色*/privatestaticColorboxColor=new
9、ColornewColor(238f238f238)znewColor(128z128,255)znewColor(189z73z23)znewColor(154z105f22),newColor(101r124z52),newColor(49r100128),newColor(84z57z119),newColor(Illz54z102)fnewColor(1265057)rColor.RED;* 定时器,负责方块自动下移* /privateTimertimer;privateintlevel;* 初时化时的延时* /privatefinalstaticintINITDELAY=940;*
10、每一级别延时减少的步进值* /privatefinalstaticintINC=90;publicBoxPanel(InfoPanelpanel)SetLayout(newGridLayout(RoWS,COLSr1,1);/可能获取焦点,才能接受键盘事件SetFocusable(true);/setCursor(newCursor(1);*初时化,生成JBUtton,设置颜色.JButton数组按照先第一行再第二行的顺序添加.下标为行列和x丫正好相反.*/for(inti=0;iROWS;i+)for(intj=0;jCOLS;j+)JButtonsquare=newJButton(,);s
11、quare.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();i
12、f(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;iROWS;i+)for(intj=0;jCOLS;j+)squaresij.SetBackground(boxColorStateTableij);*只重新显示当前方块privatevoidShowCu
13、rrentBoxOpointlocation=CurrentBox.getLocation();for(inti=0;i=0;i-)for(intj=0;jO)ScorePanel.WinScore(ClearLines);upgrade();)/* 消行,重置属性表.下移* paramline* /publicvoidremoveLine(intline)timer.stop();for(intj=0;j=0;i一一)for(intj=0;jCOLS;j+)if(i!=0)StateTableij=StateTablei-1j;elseStateTableij=O;ShowBox();timer.start();)* 检查把方块移动到(x,y)时是否合法.* paramx* paramy*