java实习俄罗斯方块.docx

上传人:b****1 文档编号:300371 上传时间:2023-04-28 格式:DOCX 页数:56 大小:176.31KB
下载 相关 举报
java实习俄罗斯方块.docx_第1页
第1页 / 共56页
java实习俄罗斯方块.docx_第2页
第2页 / 共56页
java实习俄罗斯方块.docx_第3页
第3页 / 共56页
java实习俄罗斯方块.docx_第4页
第4页 / 共56页
java实习俄罗斯方块.docx_第5页
第5页 / 共56页
java实习俄罗斯方块.docx_第6页
第6页 / 共56页
java实习俄罗斯方块.docx_第7页
第7页 / 共56页
java实习俄罗斯方块.docx_第8页
第8页 / 共56页
java实习俄罗斯方块.docx_第9页
第9页 / 共56页
java实习俄罗斯方块.docx_第10页
第10页 / 共56页
java实习俄罗斯方块.docx_第11页
第11页 / 共56页
java实习俄罗斯方块.docx_第12页
第12页 / 共56页
java实习俄罗斯方块.docx_第13页
第13页 / 共56页
java实习俄罗斯方块.docx_第14页
第14页 / 共56页
java实习俄罗斯方块.docx_第15页
第15页 / 共56页
java实习俄罗斯方块.docx_第16页
第16页 / 共56页
java实习俄罗斯方块.docx_第17页
第17页 / 共56页
java实习俄罗斯方块.docx_第18页
第18页 / 共56页
java实习俄罗斯方块.docx_第19页
第19页 / 共56页
java实习俄罗斯方块.docx_第20页
第20页 / 共56页
亲,该文档总共56页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

java实习俄罗斯方块.docx

《java实习俄罗斯方块.docx》由会员分享,可在线阅读,更多相关《java实习俄罗斯方块.docx(56页珍藏版)》请在冰点文库上搜索。

java实习俄罗斯方块.docx

java实习俄罗斯方块

俄罗斯方块

摘要

俄罗斯方块是一款风靡全球的电视游戏机和掌上游戏机游戏,它曾经造成的轰动与造成的经济价值可以说是游戏史上的一件大事。

现在我运用JAVA语言设计简单的俄罗斯方块游戏,能实现方块的下落,变形,加速下落,向左向右移动记忆改变界面颜色,俄罗斯方块颜色等等。

整个设计包括界面布局设计和功能实现,程序主要运用了for语句,监听器,以及布尔变量,if语句等等。

1概要设计

1.1开发工具及环境

本游戏对运行环境及系统性能的需求如表1所示:

表1游戏对运行环境及系统性能需求分析表

操作系统

Windows98/me/2000/XP/2003

内存容量

64MB

显卡要求

8M

声卡要求

支持DirectX8.0声卡

交互工具

键盘/鼠标

CPU

奔腾133以上

光驱

8倍速以上

硬盘空间

400MB

显示器

VGA以上显示器

开发及运行软件

JDK1.5以上版本

1.2任务及需求分析

设计简单的俄罗斯方块游戏程序,需要良好的用户界面设计,实现方块下落时,可通过键盘方向键(上、下、左、右键)对该方块进行向上(变形),向下(加速)、向左、向右移动。

随机给出不同的形状(长条形、Z字形、反Z形、田字形、7字形、反7形、T字型)下落填充给定的区域,若填满一条便消掉,记分,当达到一定的分数时,过关,一共设置十关,每关方块下落的速度不同,游戏中先结束的一方为本局输家,十关过后,胜出局数多的为赢家。

用户可以设置从第几关开始,可以设置方块颜色,背景颜色,并接在游戏过程中,按“rusume”暂停及开始,“stop”停止,“Turnharder”“Turneasily”改变当前过关级别。

具体任务如下:

(1)设计控制界面

(2)设计游戏画布

(3)设计俄罗斯方块的形状、下落速度

(4)设计游戏按钮的功能

(5)设计方块的颜色

1.3功能模块设计

(1)方块的算法流程图如图1所示:

(图1)

(2)方块翻转处理的流程图如图2所示:

(图2)

(3)方块消行的流程图如图3所示:

(图3)

(4)相应的流程如图4所示:

(图4)

 

 

1.4工作原理

点击“play”用户就可以看到随机而形成的方块会根据设计的速度,用户可以通过“”“”“”“”对方块进行控制,同时可以按“stop”停止,按“Rusume”进行暂停或开始,按“Turnharder”“Turneasily”调节要玩的级别数,同时当分值达到设置的分数就可以进入到下一关,知道方块铺满整个界面,游戏结束。

2源代码

=================1======================

packagerussia;

/*

*控制面板类

*/

importjava.awt.*;

importjava.awt.event.*;

importjavax.swing.*;

importjavax.swing.border.*;

publicclassControlPanelextendsJPanel

{

privateTipBlockPaneltipBlockPanel;

privateJPaneltipPanel,InfoPanel,buttonPanel;

privatefinalJTextFieldlevelField,scoreField;

privateJButtonplayButton,pauseButton,stopButton,

turnHarderButton,turnEasilyButton;

privateEtchedBorderborder=newEtchedBorder(EtchedBorder.RAISED,Color.WHITE,newColor(148,145,140));

privateRussiaBlocksGamegame;

privateTimertimer;

publicControlPanel(finalRussiaBlocksGamegame)

{

this.game=game;

/*

*图形界面部分

*/

setLayout(newGridLayout(3,1,0,4));

tipBlockPanel=newTipBlockPanel();

tipPanel=newJPanel(newBorderLayout());

tipPanel.add(newJLabel("NextBlock:

"),BorderLayout.NORTH);

tipPanel.add(tipBlockPanel,BorderLayout.CENTER);

tipPanel.setBorder(border);

InfoPanel=newJPanel(newGridLayout(4,1,0,0));

levelField=newJTextField(""+RussiaBlocksGame.DEFAULT_LEVEL);

levelField.setEditable(false);

scoreField=newJTextField("0");

scoreField.setEditable(false);

InfoPanel.add(newJLabel("Level:

"));

InfoPanel.add(levelField);

InfoPanel.add(newJLabel("Score:

"));

InfoPanel.add(scoreField);

InfoPanel.setBorder(border);

buttonPanel=newJPanel(newGridLayout(5,1,0,0));

playButton=newJButton("Play");

pauseButton=newJButton("Pause");

stopButton=newJButton("Stop");

turnHarderButton=newJButton("Turnharder");

turnEasilyButton=newJButton("Turneasily");

buttonPanel.add(playButton);

buttonPanel.add(pauseButton);

buttonPanel.add(stopButton);

buttonPanel.add(turnHarderButton);

buttonPanel.add(turnEasilyButton);

buttonPanel.setBorder(border);

addKeyListener(newControlKeyListener());//添加

add(tipPanel);

add(InfoPanel);

add(buttonPanel);

/*

*添加事件监听器

*/

playButton.addActionListener(

newActionListener()

{

publicvoidactionPerformed(ActionEventevent)

{

game.playGame();

requestFocus();//让ControlPanel重新获得焦点以响应键盘事件

}

});

pauseButton.addActionListener(

newActionListener()

{

publicvoidactionPerformed(ActionEventevent)

{

if(pauseButton.getText().equals("Pause"))

game.pauseGame();

else

game.resumeGame();

requestFocus();//让ControlPanel重新获得焦点以响应键盘事件

}

}

);

stopButton.addActionListener(

newActionListener()

{

publicvoidactionPerformed(ActionEventevent)

{

game.stopGame();

requestFocus();//让ControlPanel重新获得焦点以响应键盘事件

}

});

turnHarderButton.addActionListener(

newActionListener()

{

publicvoidactionPerformed(ActionEventevent)

{

intlevel=0;

try{

level=Integer.parseInt(levelField.getText());

setLevel(level+1);

}catch(NumberFormatExceptione)

{

e.printStackTrace();

}

requestFocus();//让ControlPanel重新获得焦点以响应键盘事件

}

});

turnEasilyButton.addActionListener(

newActionListener()

{

publicvoidactionPerformed(ActionEventevent)

{

intlevel=0;

try{

level=Integer.parseInt(levelField.getText());

setLevel(level-1);

}catch(NumberFormatExceptione)

{

e.printStackTrace();

}

requestFocus();//让ControlPanel重新获得焦点以响应键盘事件

}

});

/*

*时间驱动程序,每格500毫秒对level,score值进行更新

*/

timer=newTimer(500,

newActionListener()

{

publicvoidactionPerformed(ActionEventevent)

{

scoreField.setText(""+game.getScore());

game.levelUpdate();

}

}

);

timer.start();

}

/*

*设置预显方块的样式

*/

publicvoidsetBlockStyle(intstyle)

{

tipBlockPanel.setStyle(style);

tipBlockPanel.repaint();

}

/*

*重置,将所有数据恢复到最初值

*/

publicvoidreset()

{

levelField.setText(""+RussiaBlocksGame.DEFAULT_LEVEL);

scoreField.setText("0");

setPlayButtonEnabled(true);

setPauseButtonLabel(true);

tipBlockPanel.setStyle(0);

}

/*

*设置playButton是否可用

*/

publicvoidsetPlayButtonEnabled(booleanenable)

{

playButton.setEnabled(enable);

}

/*

*设置pauseButton的文本

*/

publicvoidsetPauseButtonLabel(booleanpause)

{

pauseButton.setText(pause?

"Pause":

"Rusume");

}

/*

*设置方块的大小,改变窗体大小时调用可自动调整方块到合适的尺寸

*/

publicvoidfanning()

{

tipBlockPanel.fanning();

}

/*

*根据level文本域的值返回当前的级别

*/

publicintgetLevel()

{

intlevel=0;

try

{

level=Integer.parseInt(levelField.getText());

}catch(NumberFormatExceptione)

{

e.printStackTrace();

}

returnlevel;

}

/*

*设置level文本域的值

*/

publicvoidsetLevel(intlevel)

{

if(level>0&&level<=RussiaBlocksGame.MAX_LEVEL)

levelField.setText(""+level);

}

/*

*内部类为预显方块的显示区域

*/

privateclassTipBlockPanelextendsJPanel

{

privateColorbgColor=Color.darkGray,

blockColor=Color.lightGray;

privateRussiaBox[][]boxes=newRussiaBox[RussiaBlock.ROWS][RussiaBlock.COLS];

privateintboxWidth,boxHeight,style;

privatebooleanisTiled=false;

/*

*构造函数

*/

publicTipBlockPanel()

{

for(inti=0;i

for(intj=0;j

{

boxes[i][j]=newRussiaBox(false);

}

style=0x0000;

}

/*

*构造函数

*/

publicTipBlockPanel(ColorbgColor,ColorblockColor)

{

this();

this.bgColor=bgColor;

this.blockColor=blockColor;

}

/*

*设置方块的风格

*/

publicvoidsetStyle(intstyle)

{

this.style=style;

repaint();

}

/*

*绘制预显方块

*/

publicvoidpaintComponent(Graphicsg)

{

super.paintComponent(g);

intkey=0x8000;

if(!

isTiled)

fanning();

for(inti=0;i

for(intj=0;j

{

Colorcolor=(style&key)!

=0?

blockColor:

bgColor;

g.setColor(color);

g.fill3DRect(j*boxWidth,i*boxHeight,boxWidth,boxHeight,true);

key>>=1;

}

}

/*

*设置方块的大小,改变窗体大小时调用可自动调整方块到合适的尺寸

*/

publicvoidfanning()

{

boxWidth=getSize().width/RussiaBlock.COLS;

boxHeight=getSize().height/RussiaBlock.ROWS;

isTiled=true;

}

}

/*

*内部类键盘键听器,响应键盘事件

*/

classControlKeyListenerextendsKeyAdapter{

publicvoidkeyPressed(KeyEventke)

{

if(!

game.isPlaying())return;

RussiaBlockblock=game.getCurBlock();

switch(ke.getKeyCode()){

caseKeyEvent.VK_DOWN:

block.moveDown();

break;

caseKeyEvent.VK_LEFT:

block.moveLeft();

break;

caseKeyEvent.VK_RIGHT:

block.moveRight();

break;

caseKeyEvent.VK_UP:

block.turnNext();

break;

caseKeyEvent.VK_SPACE:

//一键到底

while(block.moveDown())

{

}

break;

default:

break;

}

}

}

}

=================2===================================

packagerussia;

/*

*游戏中方块显示的画布类

*/

importjava.awt.*;

importjava.awt.event.*;

importjavax.swing.*;

importjavax.swing.border.*;

publicclassGameCanvasextendsJPanel

{

privateRussiaBox[][]boxes;

privateintrows=20,cols=12;

privatestaticGameCanvascanvas=null;

privateintboxWidth,boxHeight;//默认为零需要调用fanning函数设置

privateColorblockColor=Color.RED,bgColor=newColor(0,204,204);

privateEtchedBorderborder=newEtchedBorder(EtchedBorder.RAISED,Color.WHITE,newColor(148,145,140));

/*

*采用单件模式,构造函数私有

*/

privateGameCanvas()

{

boxes=newRussiaBox[rows][cols];

for(inti=0;i

for(intj=0;j

boxes[i][j]=newRussiaBox(false);

setBorder(border);

}

/*

*获得GameCanvas实例

*/

publicstaticGameCanvasgetCanvasInstance()

{

if(canvas==null)

canvas=newGameCanvas();

returncanvas;

}

/*

*设置画布的背景色

*/

publicvoidsetBgColor(ColorbgColor)

{

this.bgColor=bgColor;

}

/*

*获得画布的背景色

*/

publicColorgetBgColor()

{

returnbgColor;

}

/*

*设置方块的颜色

*/

publicvoidsetBlockColor(ColorblockColor)

{

this.blockColor=blockColor;

}

/*

*方块的颜色

*/

publicColorgetBlockColor()

{

returnblockColor;

}

/*

*设置画布中方块的行数

*/

publicvoidsetRows(introws)

{

this.rows=rows;

}

/*

*得到画布中方块的行数

*/

publicintgetRows()

{

returnrows;

}

/*

*设置画布中方块的列数

*/

publicvoidsetCols(intcols)

{

this.cols=cols;

}

/*

*得到画布中方块的列数

*/

publicintgetCols()

{

returncols;

}

/*

*得到row行,col列的方格

*/

publicRussiaBoxgetBox(introw,intcol)

{

if(row>=0&&row=0&&col

returnboxes[row][col];

else

returnnull;

}

/*

*在画布中绘制方块

*/

publicvoidpaintComponent(Graphicsg)

{

super.paintComponent(g);

fanning();

for(inti=0;i

for(intj=0;j

{

Colorcolor=boxes[i][j].isColorBox()?

blockColor:

bgColor;

g.setColor(color);

g.fill3DRect(j*boxWidth,i*boxHeight,boxWidth,boxHeight,true);

}

}

/*

*清除第row行

*/

publicvoidremoveLine(introw

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

当前位置:首页 > 初中教育 > 语文

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2