俄罗斯方块java代码.docx

上传人:夺命阿水 文档编号:1000905 上传时间:2024-02-25 格式:DOCX 页数:49 大小:182.35KB
返回 下载 相关 举报
俄罗斯方块java代码.docx_第1页
第1页 / 共49页
俄罗斯方块java代码.docx_第2页
第2页 / 共49页
俄罗斯方块java代码.docx_第3页
第3页 / 共49页
俄罗斯方块java代码.docx_第4页
第4页 / 共49页
俄罗斯方块java代码.docx_第5页
第5页 / 共49页
点击查看更多>>
资源描述

《俄罗斯方块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();tim

14、er.start();)* 检查把方块移动到(x,y)时是否合法.* paramx* paramy* return*/publicbooleanCheckAction(intx,inty)/先移除当前方块,再判断.当前方块和下一位置的方块有共用的位置.removecurrentBox();booleanret=true;pointlist=CurrentBox.getShape().getList();intxx=O;intyy=O;for(inti=0;i+xx:wyy);if(COLSIIxx=ROWSIIyy,+xx+:,+yy);if(StateTableyyxx!=0)ret=fal

15、se;break;)aCidCurrentBox();returnret;*左移publicvoidleft()if(CheckAction(CurrentBox.getHead().getX()-lzCurrentBox.getHead().getY()(removecurrentBox();ShowCurrentBox();CurrentBox.setHead(CurrentBox.getHead().getX()-lzCurrentBox.getHead().getY();addCurrentBox();/showBox();ShowCurrentBox();)/* 右移* /publi

16、cvoidright()if(CheckAction(CurrentBox.getHead().getX()lzCurrentBox.getHead().getY()(removeCurrentBox();ShowCurrentBox();CurrentBox.setHead(CurrentBox.getHead().getX()+lzCurrentBox.getHead().getY();addCurrentBox();/showBox();ShowCurrentBox();)/* 下移* return* /publicbooleandown()intchoose=0;if(CheckAct

17、ion(CurrentBox.getHead().getX(),CurrentBox.getHead().getY()+1)(removeCurrentBox();ShowCurrentBox();CurrentBox.setHead(currentBox.getHead().getX(),CurrentBox.getHead().getY()+l);addCurrentBox();ShowCurrentBox();/showBox();returntrue;elseClearBox();CurrentBox=nextBox.SetDefault();nextBox=BoxFactory.ge

18、tBox().SetDefault();ScorePanel.setNext(nextBox);/showBox();/检查是否游戏结束if(CheckGameOver()supend(false);choose=JOptionPane.ShowConfirmDialog(thsf”游戏结束n你的积分为+scorePanel.getSCore()+nn要开始新游戏吗?”,”GameOverJOptionPane.YES_NO_OPTIONfJOptionPane.WARNING_MESSAGE);if(choose=JOptionPane.YES_OPTION)newGame();)addCu

19、rrentBox();ShowCurrentBox();/showBox();returnfalse;)/* 丢下.* /publicvoiddropdown()while(down();)/* 判断是否结束* returnprivatebooleanCheckGameOver()pointlocation=CurrentBox.getLocation();for(inti=0;i4;i+)introw=locationi.getY();intcol=locationi.getX();if(StateTablerowcol!=0)returntrue;)returnfalse;)/* 变形*

20、/publicvoidrotate()if(CheckRotate()removecurrentBox();ShowCurrentBox();CurrentBox.rotate();addCurrentBox();ShowCurrentBox();/ShowBox();)/* 检查变形是否合法* return* /privatebooleanCheckRotate()removecurrentBox();intoldheadx=CurrentBox.getHead().getX();intOldheady=CurrentBox.getHead().getY();/System,out.prin

21、tIn(oldhead:,+oldheadx+,l,Oldheady);booleanret=true;pointshape=CurrentBox.getNextShape().getList();intx=0ry=0rnewheadx=0newheady=O;for(inti=0;i+x+:,+y);if(COLSIIx=ROWSyO)ret=false;break;/System,out.rintln(,lafterbreakcheckrotate”+i+”-“+x+”:”+y);if(StateTableyx!=0)ret=false;break;)addCurrentBox();ret

22、urnret;)* 移除当前方块.设置颜色为无色.主要用于移动,变形时.清除当前位置方块.显示下一位置方块.*/privatevoidremoveCurrentBox()/System,out.println(,removeCurrentBox:+Thread.CurrentThread().getName();for(inti=0;i4;i+)intX=CurrentBox.getLocation()i.getX();inty=currentBox.getLocation()i.getY();StateTableyx=O;)* 添加当前方块到属性表.* /privatevoidaddCurr

23、entBox()for(inti=0;i3000&level6000,&level10000&level15000&level21000&level28000&level35000&level43000&level51000&level10)SetLevel(10);)/* 重置,开始新游戏publicvoidnewGame()for(inti=0;iROWS;i+)for(intj=0;jCOLS;j+)StateTableij=0;ShowBox();SetLevel(1);ScorePanel.clean();CurrentBox=BoxFactory.getBox();nextBox=

24、BoxFactory.getBox();ScorePanel.setNext(nextBox);SetFocusable(true);requestFocus();addCurrentBox();ShowBox();timer.start();)/*记分面板*authorAdministratorclassInfoPanelextendsJPanelprivate private private privateJLabel JLabel JLabel JPanelScoreLabel;IineLabel;IevelLabel;nextPanel;*下一个方块privateJButtonnext

25、Box;publicInfoPanel()SetLayout(newBorderLayout();Boxvbox=Box.CreateVerticalBox();add(vbox,BorderLayout.CENTER);vbox.add(Box.ereateRigidArea(newDimension(12z50);Boxvboxl=Box.CreateVerticalBox();vbox,add(vboxl);ScoreLabel=newJLabel(,0);IineLabel=newJLabel(,0);IevelLabel=newJLabel(,0);nextPanel=newJPan

26、el();nextBox=newJButton44;nextPanel.SetLayout(newGridLayout(4,4,1,1);for(inti=0;i4;i+)for(intj=0;j4;j+)JButtonbt=newJButton(,);bt.SetEnabled(false);bt,SetBorderPainted(BoxPanel.PAINTGRID);nextPaneladd(bt);nextBoxji=bt;)vboxl.add(newJLabel(Next:,l);vboxl.add(nextPanel);vboxl.add(Box.CreateVerticalStr

27、ut(90);vboxl.add(newJLabel(,得分:”);vboxl.add(Box.CreateVerticalStrut(10);vboxl.add(ScoreLabel);vboxl.add(Box.CreateVerticalStrut(10);vboxl.add(newJLabel(,行数:”);vboxl.add(Box.CreateVerticalStrut(10);vboxl.add(IineLabel);vboxl.add(Box.CreateVerticalStrut(10);vboxl.add(newJLabel(Level:n);vbox1.add(BoxCr

28、eateVerticalStrut(10);vboxl.add(IevelLabel);vboxl.add(Box.CreateVerticalStrut(80);)/*记分.显示在面板上*paramlines*/publicvoidWinScore(intlines)intscore=O;IineLabel.setText(,+(Integer.parselnt(IineLabel.getText()+lines);if(Iines=I)score=100;elseif(lines=2)score=300;elseif(lines=3)score=500;elseif(lines=4)sco

29、re=1000;ScoreLabel.setText(,+(Integer.parseint(scoreLabel.getText()+score)设置下一个方块.paramnextpublicvoidsetNext(ABoxnext)for(inti=0;i4;i+)for(intj=0;j4;j+)nextBoxij.SetBackground(BoxPanel.HIDE);intheadx=2;intheady=O;/设置方块位置if(next.getld()=1)headx=2;heady=O;elseif(next.getld()=2)headx=1;heady=1;elseif(n

30、ext.getld()=3)headx=2;heady=1;)elseif(next.getld()=4)headx=1;heady=1;)elseif(next.getld()=5)headx=2;heady=1;elseif(next.getld()=6)headx=1;heady=1;)elseif(next.getld()=7)headx=1;heady=1;)pointshape=next.getShape(O).getList();nextBoxheadxheady.SetBackground(BoxPanel.NEXTDISPLAY);nextBoxheadx+shape1.ge

31、tX()heady+shape1.getY().setBackground(BoxPanel.NEXTDISPLAY);nextBoxheadx+shape2.getX()heady+shape2.getY().SetBackground(BoxPanel.NEXTDISPLAY);nextBoxheadx+shape3.getX()heady+shape3.getY().SetBackground(BOXPanel.NEXTDISPLAY);)/* 设置显示级别* paramlevel* /publicvoidSetLevel(intlevel)IevelLabel.setText(”,+l

32、evel);)/* 获取当前积分* return* /publicintgetScore()returnInteger.arselnt(ScoreLabel.getText();*重置publicvoidclean()ScoreLabel.setText(,+O);SetLevel(1);for(inti=0;i4;i+)for(intj=0;j4;j+)nextBoxij.SetBackground(BoxPanel.HIDE);*方块类*authorAdministrator/classABoxpublicfinalstaticintROWS=20;publicfinalstaticint

33、COLS=10;privateintid;*用于存放方块所有样子坐标.*/privateArrayListShapeTable;privateStringname;privateintCurrentShapeId;*当前方块位置.注意这里是坐标(x,y)和SquaresrStateTable数组的行列顺序正好相反.privatepointlocation;*方块起始位置.privatepointhead=newpoint(0zCOLS/2);publicABox(Stringname,intid)this.name=name;this.id=id;ShapeTable=newArrayList

34、();location=newpoint4;*方块变形,选取下一个坐标集.从小到大顺序选取.变形后重设方块位置(头结点)publicvoidrotate()CurrentShapeId=(currentShapeId+1)=shapeTable.size()?0:(CurrentShapeId+l);pointshapehead=ShapeTable.get(CurrentShapeId).getList()0;setHead(getHead().getX()+shapehead.getX(),getHead().getY()+shapehead.getY();)/* 添加方块坐标集* par

35、amshape*/publicvoidaddShape(BoxShapeshape)ShapeTable.add(shape);/* 重设方块位置定义新的头结点.根据坐标集计算下其它子方块位置.* paramX新位置头结点坐标X* paramy* /publicvoidsetHead(intx,inty)head.setxy(x,y);pointShapePoint=getShape().getList();locationO=head;location1=head.add(ShapePoint1);location2=head.add(ShapePoint2);location3=head.

36、add(ShapePoint3);)* 获取位置数组* return* /publicpointgetLocation()returnlocation;)* 获取头结点* return*/publicpointgetHead()returnhead;* 获取当前坐标集* return* /publicBoxShapegetShape()/getcurrentshapereturnShapeTable.get(CurrentShapeId);)* 获取下一个坐标集publicBoxShapegetNextShape()/intindex=currentShapeld+l;/if(index=sh

37、apeTable.size()index=0;returnShapeTable.get(currentShapeId+l=shapeTable.size()?0:currentShapeld+l);/* 重置当前方块.因为游戏中一直在重用方块对象* return* /publicABoxSetDefault()SetHead(4,O);CurrentShapeIci=O;returnthis;)/* 获取对应坐标集* paramindex* return* /publicBoxShapegetShape(intindex)returnShapeTable.get(index);)/* 用于调试* /publicStringtoString()return,+name+getHead();publicintgetld()returnid;* 产生方块* authorAdministrator* /classBoxFactory* ArrayL工St中存放每个方块的一个对象.随机获取.* /privatestaticArrayListaboxes=newArrayList();* 产生方块对象* /static/*其中包含4个点.都是相对坐标.第一个坐

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 在线阅读 > 生活休闲


备案号:宁ICP备20000045号-1

经营许可证:宁B2-20210002

宁公网安备 64010402000986号