java源代码 俄罗斯方块Word格式.docx
- 文档编号:21707405
- 上传时间:2023-01-31
- 格式:DOCX
- 页数:13
- 大小:17.74KB
java源代码 俄罗斯方块Word格式.docx
《java源代码 俄罗斯方块Word格式.docx》由会员分享,可在线阅读,更多相关《java源代码 俄罗斯方块Word格式.docx(13页珍藏版)》请在冰豆网上搜索。
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("
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<
rowNum;
i++)
for(intj=0;
j<
columnNum;
j++)
scrArr[j]=0;
b.reset();
repaint();
publicvoidpaint(Graphicsg){
for(inti=0;
i<
rowNum;
i++)
for(intj=0;
j<
columnNum;
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);
//»
Õ
Ú
case2:
g.setColor(Color.magenta);
Ò
Â
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)
j++){
if(scrArr[j]==0)
drawUnit(k-1,j,0);
drawUnit(k-1,j,2);
scrArr[k-1][j]=scrArr[j];
for(inti=k-1;
i<
i++){
drawUnit(i,j,0);
ERS_Block.score+=full_line_num;
ERS_Block.scoreField.setText("
+ERS_Block.score);
//Å
booleanisGameEnd(){
for(intcol=0;
col<
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();
caseKeyEvent.VK_LEFT:
b.leftMove();
caseKeyEvent.VK_RIGHT:
b.rightMove();
caseKeyEvent.VK_SPACE:
b.leftTurn();
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:
ERS_Block.isPlay){
scr.initScr();
ERS_Block.isPlay=true;
ERS_Block.score=0;
ERS_Block.timer.resume();
scr.requestFocus();
casebutton_levelup:
if(ERS_Block.level<
10){
ERS_Block.level++;
ERS_Block.levelField.setText("
+ERS_Block.level);
casebutton_leveldown:
if(ERS_Block.level>
1){
ERS_Block.level--;
casebutton_pause:
if(pause_resume){
ERS_Block.timer.suspend();
pause_resume=false;
}else{
pause_resume=true;
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;
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(){
dispBlock
(1);
//Ê
publicvoidleftTurn(){
if(assertValid(blockType,(turnState+1)%4,row,col)){
dispBlock(0);
turnState=(turnState+1)%4;
ó
publicvoidleftMove(){
if(assertValid(blockType,turnState,row,col-1)){
col--;
publicvoidrightMove(){
if(assertValid(blockType,turnState,row,col+1)){
col++;
publicbooleanfallDown(){
if(blockState==2)
return(false);
if(assertValid(blockType,turnState,row-1,col)){
row--;
return(true);
blockState=2;
dispBlock
(2);
booleanassertValid(intt,ints,introw,intcol){
intk=0x8000;
4;
if((int)(pattern[t][s]&
k)!
=0){
inttemp=scr.getScrArrXY(row-i,col+j);
if(temp<
0||temp==2)
k=k>
>
1;
//Í
publicsynchronizedvoiddispBlock(ints){
for(inti=0;
if(((int)pattern[blockType][turnState]&
scr.drawUnit(row-i,col+j,s);
k=k>
1;
classMyTimerextendsThread{
publicMyTimer(GameCanvasscr){
publicvoidrun(){
while(true){
try{
sleep((10-ERS_Block.level+1)*100);
catch(InterruptedExceptione){}
scr.getBlock().fallDown()){
scr.deleteFullLine();
if(scr.isGameEnd()){
ERS_Block.isPlay=false;
suspend();
}else
scr.getBlock().reset();
classWinListenerextendsWindowAdapter{
publicvoidwindowClosing(WindowEventl){
}
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java源代码 俄罗斯方块 java 源代码