java源代码 俄罗斯方块.docx
- 文档编号:8571183
- 上传时间:2023-01-31
- 格式:DOCX
- 页数:13
- 大小:17.74KB
java源代码 俄罗斯方块.docx
《java源代码 俄罗斯方块.docx》由会员分享,可在线阅读,更多相关《java源代码 俄罗斯方块.docx(13页珍藏版)》请在冰豆网上搜索。
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);
//¶¨Òå°´Å¥play
Buttonplay_b=newButton("¿ªÊ¼ÓÎÏ·");
play_b.setSize(newDimension(50,200));
play_b.addActionListener(newCommand(Command.button_play,gameScr));
//¶¨Òå°´Å¥LevelUP
Buttonlevel_up_b=newButton("Ìá¸ß¼¶Êý");
level_up_b.setSize(newDimension(50,200));
level_up_b.addActionListener(newCommand(Command.button_levelup,gameScr));
//¶¨Òå°´Å¥LevelDown
Buttonlevel_down_b=newButton("½µµÍ¼¶Êý");
level_down_b.setSize(newDimension(50,200));
level_down_b.addActionListener(newCommand(Command.button_leveldown,gameScr));
//¶¨Òå°´Å¥LevelPause
Buttonpause_b=newButton("ÓÎÏ·ÔÝÍ£");
pause_b.setSize(newDimension(50,200));
pause_b.addActionListener(newCommand(Command.button_pause,gameScr));
//¶¨Òå°´Å¥Quit
Buttonquit_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 for(intj=0;j scrArr[j]=0; b.reset(); repaint(); } //ÖØÐÂˢл²¼·½·¨ publicvoidpaint(Graphicsg){ for(inti=0;i for(intj=0;j drawUnit(i,j,scrArr[j]); } //»·½¿éµÄ·½·¨ publicvoiddrawUnit(introw,intcol,inttype){ scrArr[row][col]=type; Graphicsg=getGraphics(); tch(type){//±íʾ»·½¿ìµÄ·½·¨ case0: g.setColor(Color.black);break;//ÒÔ±³¾°ÎªÑÕÉ«» case1: g.setColor(Color.blue);break;//»ÕýÔÚÏÂÂäµÄ·½¿é case2: g.setColor(Color.magenta);break;//»ÒѾÂäϵķ½·¨ } g.fill3DRect(col*unitSize,getSize().height-(row+1)*unitSize,unitSize,unitSize,true); g.dispose(); } publicBlockgetBlock(){ returnb;//·µ»ØblockʵÀýµÄÒýÓà } //·µ»ØÆÁÄ»Êý×éÖÐ(row,col)λÖõÄÊôÐÔÖµ publicintgetScrArrXY(introw,intcol){ if(row<0||row>=rowNum||col<0||col>=columnNum) return(-1); else return(scrArr[row][col]); } //·µ»ØпéµÄ³õʼÐÐ×ø±ê·½·¨ publicintgetInitRow(){ return(blockInitRow);//·µ»ØпéµÄ³õʼÐÐ×ø±ê } //·µ»ØпéµÄ³õʼÁÐ×ø±ê·½·¨ publicintgetInitCol(){ return(blockInitCol);//·µ»ØпéµÄ³õʼÁÐ×ø±ê } //ÂúÐÐɾ³ý·½·¨ voiddeleteFullLine(){ intfull_line_num=0; intk=0; for(inti=0;i booleanisfull=true; L1: for(intj=0;j if(scrArr[j]==0){ k++; isfull=false; breakL1; } if(isfull)full_line_num++; if(k! =0&&k-1! =i&&! isfull) for(intj=0;j if(scrArr[j]==0) drawUnit(k-1,j,0); else drawUnit(k-1,j,2); scrArr[k-1][j]=scrArr[j]; } } for(inti=k-1;i for(intj=0;j drawUnit(i,j,0); scrArr[j]=0; } } ERS_Block.score+=full_line_num; ERS_Block.scoreField.setText(""+ERS_Block.score); } //ÅжÏÓÎÏ·ÊÇ·ñ½áÊø·½·¨ booleanisGameEnd(){ for(intcol=0;col if(scrArr[maxAllowRowNum][col]! =0) returntrue; } returnfalse; } publicvoidkeyTyped(KeyEvente){ } publicvoidkeyReleased(KeyEvente){ } //´¦Àí¼üÅÌÊäÈëµÄ·½·¨ publicvoidkeyPressed(KeyEvente){ if(! ERS_Block.isPlay) return; tch(e.getKeyCode()){ caseKeyEvent.VK_DOWN: b.fallDown();break; caseKeyEvent.VK_LEFT: b.leftMove();break; caseKeyEvent.VK_RIGHT: b.rightMove();break; caseKeyEvent.VK_SPACE: b.leftTurn();break; } } } //´¦Àí¿ØÖÆÀà classCommandimplementsActionListener{ staticfinalintbutton_play=1;//¸ø°´Å¥·ÖÅä±àºÅ staticfinalintbutton_levelup=2; staticfinalintbutton_leveldown=3; staticfinalintbutton_quit=4; staticfinalintbutton_pause=5; staticbooleanpause_resume=true; intcurButton;//µ±Ç°°´Å¥ GameCanvasscr; //¿ØÖÆ°´Å¥ÀàµÄ¹¹Ôì·½·¨ Command(intbutton,GameCanvasscr){ curButton=button; this.scr=scr; } //°´Å¥Ö´Ðз½·¨ publicvoidactionPerformed(ActionEvente){ tch(curButton){ casebutton_play: if(! ERS_Block.isPlay){ scr.initScr(); ERS_Block.isPlay=true; ERS_Block.score=0; ERS_Block.scoreField.setText("0"); ERS_Block.timer.resume(); } scr.requestFocus(); break; casebutton_levelup: if(ERS_Block.level<10){ 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_leveldown: if(ERS_Block.level>1){ 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); } } //ʵÏÖ¿éÂäϵIJÙ×÷µÄ·½·¨ 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<4;i++){ for(intj=0;j<4;j++){ if((int)(pattern[t][s]&k)! =0){ inttemp=scr.getScrArrXY(row-i,col+j); if(temp<0||temp==2) returnfalse; } k=k>>1; } } returntrue; } //ͬ²½ÏÔʾµÄ·½·¨ publicsynchronizedvoiddispBlock(ints){ intk=0x8000; for(inti=0;i<4;i++){ for(intj=0;j<4;j++){ if(((int)pattern[blockType][turnState]&k)! =0){ scr.drawUnit(row-i,col+j,s); } k=k>>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(); }else scr.getBlock().reset(); } } } classWinListenerextendsWindowAdapter{ publicvoidwindowClosing(WindowEventl){ System.exit(0); } }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java源代码 俄罗斯方块 java 源代码
![提示](https://static.bdocx.com/images/bang_tan.gif)