魔板游戏Java课程设计报告Word文档格式.docx

上传人:b****6 文档编号:17524234 上传时间:2022-12-07 格式:DOCX 页数:21 大小:142.46KB
下载 相关 举报
魔板游戏Java课程设计报告Word文档格式.docx_第1页
第1页 / 共21页
魔板游戏Java课程设计报告Word文档格式.docx_第2页
第2页 / 共21页
魔板游戏Java课程设计报告Word文档格式.docx_第3页
第3页 / 共21页
魔板游戏Java课程设计报告Word文档格式.docx_第4页
第4页 / 共21页
魔板游戏Java课程设计报告Word文档格式.docx_第5页
第5页 / 共21页
点击查看更多>>
下载资源
资源描述

魔板游戏Java课程设计报告Word文档格式.docx

《魔板游戏Java课程设计报告Word文档格式.docx》由会员分享,可在线阅读,更多相关《魔板游戏Java课程设计报告Word文档格式.docx(21页珍藏版)》请在冰豆网上搜索。

魔板游戏Java课程设计报告Word文档格式.docx

类模块

PuzzleFrame类负责创建魔板游戏的主窗口,该类含有main方法,程序从该类开始。

PuzzleFrame类的成员变量中有4种重要类型的对象:

PuzzlePad、JmenuItem、JRadioButton和JButton。

PuzzleFrame类的主要成员的作用将在后面的详细设计中阐述,PuzzleFrame类主要成员变量(属性)和主要方法如表3-1、3-2所示。

成员变量描述

变量类型

名称

魔板

开始游戏的按钮

选择玩法

PuzzlePad

Jbutton

JradioButton

开始

数字玩法,图像玩法

表3-1主要成员变量(属性)

功能

备注

PuzzleFrame

actionPerformed

main

创建程序主窗口

处理ActionEvent事件

程序开始运行的方法

构造方法

接口方法

表3-2主要方法

PuzzlePad类创建的对象是PuzzleFrame类最重要的成员之一,代表“魔板”。

该类的成员变量中有5种重要类型的对象:

Point、Block和Image。

PuzzlePad类类主要成员变量(属性)和主要方法如表3-3、3-4所示。

描述

魔板中的行数和列数

魔板中方块的宽和高

魔板中二维点数组

魔板中而为方块数组

模板可能用到的图像

int

SquarePoint[][]

JButton

Image

rows,colums

width,height

point

block

image

表3-3主要成员变量(属性)

随机排列数字

随机排列图像

paintComponent

创建魔板对象

随机排列魔板中方块上的数字

随机排列魔板中方块上的图像

绘制图像

表3-4主要方法

SquarePoint类负责创建确定位置的对象,使用Point对象可以确定Block对象在PuzzlePad对象中的位置,即确定“方块”在“魔板”中的位置。

类主要成员变量(属性)和主要方法如表3-5、3-6所示。

判断点上是否有方块

点上的方块对象

boolean

有方块

表3-5主要成员变量(属性)

SquarePoint

get有方块

set有方块

setBlock

getBlock

创建“点”对象

设置点上是否有方块

在点上放置方块

获取点上的方块

表3-6主要方法

4.程序流程

根据对模板游戏的功能分析,设计得到魔板游戏总流程,如图4-1所示:

图4-1系统流程图

流程解释:

程序分为两种玩法,数字玩法和图片玩法,又分为初级和高级两种模式,进入游戏选择游戏难度,后选择玩法,数字或图片玩法,系统将各切片顺序打乱,移动切片,判断顺序是否正确,正确游戏结束,不正确继续移动,程序结束。

5主要源代码

模块源程序

import.*;

publicclassPuzzleFrameextendsJFrameimplementsActionListener

{

PuzzlePadpuzzlePad;

JButton开始;

JMenuBarbar;

JMenufileMenu;

JMenuItem初级,高级;

JRadioButton数字玩法,图像玩法;

ButtonGroupgroup=null;

Containercon=null;

publicPuzzleFrame()

{

bar=newJMenuBar();

fileMenu=newJMenu("

魔板游戏"

);

初级=newJMenuItem("

初级"

高级=newJMenuItem("

高级"

(初级);

(高级);

(fileMenu);

setJMenuBar(bar);

初级.addActionListener(this);

高级.addActionListener(this);

开始=newJButton("

开始玩"

开始.addActionListener(this);

group=newButtonGroup();

数字玩法=newJRadioButton("

数字玩法"

true);

图像玩法=newJRadioButton("

图像玩法"

false);

(数字玩法);

(图像玩法);

puzzlePad=newPuzzlePad(3,3,50,50);

con=getContentPane();

(puzzlePad,;

JPanelpNorth=newJPanel();

(开始);

(pNorth,;

();

addWindowListener(newWindowAdapter()

{publicvoidwindowClosing(WindowEvente)

(0);

}

});

setVisible(true);

setBounds(100,50,440,360);

validate();

publicvoidactionPerformed(ActionEvente)

{if()==开始)

if(数字玩法.isSelected())

puzzlePad.随机排列数字();

elseif(图像玩法.isSelected())

puzzlePad.随机排列图像();

}

elseif()==初级)

(puzzlePad);

elseif()==高级)

puzzlePad=newPuzzlePad(4,4,50,50);

publicstaticvoidmain(Stringargs[])

newPuzzleFrame();

}

PuzzlePad模块源程序

importclassPuzzlePadextendsJPanelimplementsActionListener

introws,colums;

intwidth,height;

SquarePointpoint[][];

JButtonblock[][];

Vectorvector;

intleftX=50,leftY=50;

Imageimage=null;

Toolkittool=null;

MediaTrackermt;

int图像的宽,图像的高;

ImageblockImage[];

boolean画图像=true;

publicPuzzlePad(intr,intc,intw,inth)

setLayout(null);

vector=newVector();

rows=r;

colums=c;

width=w;

height=h;

tool=getToolkit();

try

image=("

"

(image,1);

catch(Exceptione)

block=newJButton[rows][colums];

point=newSquarePoint[rows][colums];

for(inti=0;

i<

rows;

i++)

{

for(intj=0;

j<

colums;

j++)

if((i==rows-1)&

&

(j==colums-1))

else

block[i][j]=newJButton();

block[i][j].setSize(width,height);

block[i][j].addActionListener(this);

(block[i][j]);

point[i][j]=newSquarePoint(50+j*width,50+i*height);

else

point[i][j].setBlock(block[i][j],this);

JButtonbutton=(JButton)();

intx=().x;

inty=().y;

intm=0,n=0;

if(point[i][j].getX()==x&

point[i][j].getY()==y)

m=i;

n=j;

int上=(0,m-1);

int下=(rows-1,m+1);

int左=(0,n-1);

int右=(colums-1,n+1);

if(point[上][n].get有方块()==false)

point[上][n].setBlock(button,this);

point[m][n].set有方块(false);

elseif(point[下][n].get有方块()==false)

point[下][n].setBlock(button,this);

elseif(point[m][左].get有方块()==false)

point[m][左].setBlock(button,this);

elseif(point[m][右].get有方块()==false)

point[m][右].setBlock(button,this);

publicvoid随机排列数字()

inti=1;

while()>

0)

intn=(int)()*());

JButtonb=(JButton)(n);

("

+i);

(null);

1));

(n);

i++;

画图像=false;

repaint();

publicvoid随机排列图像()

try

图像的宽=(this);

图像的高=(this);

intw=图像的宽/colums;

inth=图像的高/rows;

blockImage=newImage[rows*colums];

intk=0;

PixelGrabberpg=null;

ImageProducerip=null;

intpixels[]=newint[w*h];

pg=newPixelGrabber(image,j*w,i*h,w,h,pixels,0,w);

ip=newMemoryImageSource(w,h,pixels,0,w);

blockImage[k]=(ip);

k++;

intnum=0;

(newImageIcon(blockImage[num]));

num++;

画图像=true;

catch(Exceptionee)

publicvoidpaintComponent(Graphicsg)

(g);

;

(leftX,leftY,colums*width,rows*height);

if(画图像==true)

(image,colums*width+80,height,colums*width,rows*height,this);

(colums*width+80,height+i*height,

colums*width+80+colums*width,height+i*height);

(colums*width+80+j*width,height,

colums*width+80+j*width,height+rows*height);

(colums*width+80+(colums-1)*width,

height+(rows-1)*height,width,height);

SquarePoint类模块源程序

importclassSquarePoint

intx,y;

boolean有方块;

JButtonblock=null;

publicSquarePoint(intx,inty)

=x;

=y;

publicbooleanget有方块()

return有方块;

publicvoidset有方块(booleanboo)

有方块=boo;

publicintgetX()

returnx;

publicintgetY()

returny;

publicvoidsetBlock(JButtonblock,Containercon)

=con;

=block;

(block);

(x,y);

有方块=true;

publicJButtongetBlock()

returnblock;

6运行结果及结论

测试方案

游戏难度级别分别设置为数字玩法图片玩法,进行测试。

用java解释器运行主类:

E:

\Eclipse(实验)\javaPuzzleFrame

运行结果

图6-1数字玩法测试结果

图6-2图片玩法测试结果

本程序实现的游戏分为初级和高级模式,可以在单机选择级别之后进行选择级别模式。

初级的话共分为9格魔板,一个为空,8个魔板上下左右自由移动,直至排列完成;

高级的话分为16格魔板,一个为空,15个魔板上下左右自由移动,直至排列完成。

该游戏可分为数字玩法和图像玩法,在单击图像玩法或者数字玩法之后可以进行相应的玩法选择,选择之后单击开始。

如果玩家想重新开始,只需要再次单击开始按钮即可。

6.3小结

通过这次实验,使我在软件开发方面也累积了不少经验,对java的知识重要性的认识更深了。

通过设计过程的锻炼,自己分析问题和解决问题的能力都得到了锻炼和提高,完善了自己的知识结构,加深了对所学知识的理解。

  通过课程设计,我学习到不少的JAVA编程语言的知识,但由于自己的理论知识水平有限,实践知识和设计经验不足,在设计过程中难免存在一些问题,甚至错误。

这些我会在以后的工作学习和实践中加以改进和提高。

7.参考文献

1.Java面向对象的程序设计董小园清华大学出版社参考页数P160-P317

2.Java程序设计自学手册王国辉,李钟尉,王毅北京:

人民邮电出版社参考页数P288-P331

3.Java语言与面向对象程序设计刘洋,陈裼祯,李马宏北京:

机械工业出版社参考页数P67-P128

4.Java2从入门到精通陈国君,陈磊北京:

清华大学出版社参考页数P13-P98

5.Java程序设计实用教程朱站立,沈伟北京:

北京大学出版社参考页数P53-P60

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

当前位置:首页 > 小学教育 > 学科竞赛

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1