谁有java写的俄罗斯方块代码,要自己写的。跪求。急。

 我来答
检永贞4k
2016-09-11 · TA获得超过1784个赞
知道小有建树答主
回答量:508
采纳率:0%
帮助的人:221万
展开全部
俄罗斯方块——java源代码提供importjava.awt.*;importjava.awt.event.*;//俄罗斯方块类publicclassERS_BlockextendsFrame{publicstaticbooleanisPlay=false;publicstaticintlevel=1,score=0;publicstaticTextFieldscoreField,levelField;publicstaticMyTimertimer;GameCanvasgameScr;publicstaticvoidmain(String[]argus){ERS_Blockers=newERS_Block("俄罗斯方块游戏V1.0Author:Vincent");WindowListenerwin_listener=newWinListener();ers.addWindowListener(win_listener);}//俄罗斯方块类的构造方法ERS_Block(Stringtitle){super(title);setSize(600,480);setLayout(newGridLayout(1,2));gameScr=newGameCanvas();gameScr.addKeyListener(gameScr);timer=newMyTimer(gameScr);timer.setDaemon(true);timer.start();timer.suspend();add(gameScr);PanelrightScr=newPanel();rightScr.setLayout(newGridLayout(2,1,0,30));rightScr.setSize(120,500);add(rightScr);//右边信息窗体的布局MyPanelinfoScr=newMyPanel();infoScr.setLayout(newGridLayout(4,1,0,5));infoScr.setSize(120,300);rightScr.add(infoScr);//定义标签和初始值Labelscorep=newLabel("分数:",Label.LEFT);Labellevelp=newLabel("级数:",Label.LEFT);scoreField=newTextField(8);levelField=newTextField(8);scoreField.setEditable(false);levelField.setEditable(false);infoScr.add(scorep);infoScr.add(scoreField);infoScr.add(levelp);infoScr.add(levelField);scorep.setSize(newDimension(20,60));scoreField.setSize(newDimension(20,60));levelp.setSize(newDimension(20,60));levelField.setSize(newDimension(20,60));scoreField.setText("0");levelField.setText("1");//右边控制按钮窗体的布局MyPanelcontrolScr=newMyPanel();controlScr.setLayout(newGridLayout(5,1,0,5));rightScr.add(controlScr);//定义按钮playButtonplay_b=newButton("开始游戏");play_b.setSize(newDimension(50,200));play_b.addActionListener(newCommand(Command.button_play,gameScr));//定义按钮LevelUPButtonlevel_up_b=newButton("提高级数");level_up_b.setSize(newDimension(50,200));level_up_b.addActionListener(newCommand(Command.button_levelup,gameScr));//定义按钮LevelDownButtonlevel_down_b=newButton("降低级数");level_down_b.setSize(newDimension(50,200));level_down_b.addActionListener(newCommand(Command.button_leveldown,gameScr));//定义按钮LevelPauseButtonpause_b=newButton("游戏暂停");pause_b.setSize(newDimension(50,200));pause_b.addActionListener(newCommand(Command.button_pause,gameScr));//定义按钮QuitButtonquit_b=newButton("退出游戏");quit_b.setSize(newDimension(50,200));quit_b.addActionListener(newCommand(Command.button_quit,gameScr));controlScr.add(play_b);controlScr.add(level_up_b);controlScr.add(level_down_b);controlScr.add(pause_b);controlScr.add(quit_b);setVisible(true);gameScr.requestFocus();}}//重写MyPanel类,使Panel的四周留空间classMyPanelextendsPanel{publicInsetsgetInsets(){returnnewInsets(30,50,30,50);}}//游戏画布类classGameCanvasextendsCanvasimplementsKeyListener{finalintunitSize=30;//小方块边长introwNum;//正方格的行数intcolumnNum;//正方格的列数intmaxAllowRowNum;//允许有多少行未削intblockInitRow;//新出现块的起始行坐标intblockInitCol;//新出现块的起始列坐标int[][]scrArr;//屏幕数组Blockb;//对方快的引用//画布类的构造方法GameCanvas(){rowNum=15;columnNum=10;maxAllowRowNum=rowNum-2;b=newBlock(this);blockInitRow=rowNum-1;blockInitCol=columnNum/2-2;scrArr=newint[32][32];}//初始化屏幕,并将屏幕数组清零的方法voidinitScr(){for(inti=0;i=rowNum||col=columnNum)return(-1);elsereturn(scrArr[row][col]);}//返回新块的初始行坐标方法publicintgetInitRow(){return(blockInitRow);//返回新块的初始行坐标}//返回新块的初始列坐标方法publicintgetInitCol(){return(blockInitCol);//返回新块的初始列坐标}//满行删除方法voiddeleteFullLine(){intfull_line_num=0;intk=0;for(inti=0;i1){ERS_Block.level--;ERS_Block.levelField.setText(""+ERS_Block.level);ERS_Block.score=0;ERS_Block.scoreField.setText(""+ERS_Block.score);}scr.requestFocus();break;casebutton_pause:if(pause_resume){ERS_Block.timer.suspend();pause_resume=false;}else{ERS_Block.timer.resume();pause_resume=true;}scr.requestFocus();break;casebutton_quit:System.exit(0);}}}//方块类classBlock{staticint[][]pattern={{0x0f00,0x4444,0x0f00,0x4444},//用十六进至表示,本行表示长条四种状态{0x04e0,0x0464,0x00e4,0x04c4},{0x4620,0x6c00,0x4620,0x6c00},{0x2640,0xc600,0x2640,0xc600},{0x6220,0x1700,0x2230,0x0740},{0x6440,0x0e20,0x44c0,0x8e00},{0x0660,0x0660,0x0660,0x0660}};intblockType;//块的模式号(0-6)intturnState;//块的翻转状态(0-3)intblockState;//快的下落状态introw,col;//块在画布上的坐标GameCanvasscr;//块类的构造方法Block(GameCanvasscr){this.scr=scr;blockType=(int)(Math.random()*1000)%7;turnState=(int)(Math.random()*1000)%4;blockState=1;row=scr.getInitRow();col=scr.getInitCol();}//重新初始化块,并显示新块publicvoidreset(){blockType=(int)(Math.random()*1000)%7;turnState=(int)(Math.random()*1000)%4;blockState=1;row=scr.getInitRow();col=scr.getInitCol();dispBlock(1);}//实现“块”翻转的方法publicvoidleftTurn(){if(assertValid(blockType,(turnState+1)%4,row,col)){dispBlock(0);turnState=(turnState+1)%4;dispBlock(1);}}//实现“块”的左移的方法publicvoidleftMove(){if(assertValid(blockType,turnState,row,col-1)){dispBlock(0);col--;dispBlock(1);}}//实现块的右移publicvoidrightMove(){if(assertValid(blockType,turnState,row,col+1)){dispBlock(0);col++;dispBlock(1);}}//实现块落下的操作的方法publicbooleanfallDown(){if(blockState==2)return(false);if(assertValid(blockType,turnState,row-1,col)){dispBlock(0);row--;dispBlock(1);return(true);}else{blockState=2;dispBlock(2);return(false);}}//判断是否正确的方法booleanassertValid(intt,ints,introw,intcol){intk=0x8000;for(inti=0;i>1;}}returntrue;}//同步显示的方法publicsynchronizedvoiddispBlock(ints){intk=0x8000;for(inti=0;i>1;}}}}//定时线程classMyTimerextendsThread{GameCanvasscr;publicMyTimer(GameCanvasscr){this.scr=scr;}publicvoidrun(){while(true){try{sleep((10-ERS_Block.level+1)*100);}catch(InterruptedExceptione){}if(!scr.getBlock().fallDown()){scr.deleteFullLine();if(scr.isGameEnd()){ERS_Block.isPlay=false;suspend();}elsescr.getBlock().reset();}}}}classWinListenerextendsWindowAdapter{publicvoidwindowClosing(WindowEventl){System.exit(0);}}------------------有机会一起学习~
追问
运行不了。。。
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式