魔板游戏内含源代码.docx

上传人:b****1 文档编号:15229391 上传时间:2023-07-02 格式:DOCX 页数:20 大小:21.29KB
下载 相关 举报
魔板游戏内含源代码.docx_第1页
第1页 / 共20页
魔板游戏内含源代码.docx_第2页
第2页 / 共20页
魔板游戏内含源代码.docx_第3页
第3页 / 共20页
魔板游戏内含源代码.docx_第4页
第4页 / 共20页
魔板游戏内含源代码.docx_第5页
第5页 / 共20页
魔板游戏内含源代码.docx_第6页
第6页 / 共20页
魔板游戏内含源代码.docx_第7页
第7页 / 共20页
魔板游戏内含源代码.docx_第8页
第8页 / 共20页
魔板游戏内含源代码.docx_第9页
第9页 / 共20页
魔板游戏内含源代码.docx_第10页
第10页 / 共20页
魔板游戏内含源代码.docx_第11页
第11页 / 共20页
魔板游戏内含源代码.docx_第12页
第12页 / 共20页
魔板游戏内含源代码.docx_第13页
第13页 / 共20页
魔板游戏内含源代码.docx_第14页
第14页 / 共20页
魔板游戏内含源代码.docx_第15页
第15页 / 共20页
魔板游戏内含源代码.docx_第16页
第16页 / 共20页
魔板游戏内含源代码.docx_第17页
第17页 / 共20页
魔板游戏内含源代码.docx_第18页
第18页 / 共20页
魔板游戏内含源代码.docx_第19页
第19页 / 共20页
魔板游戏内含源代码.docx_第20页
第20页 / 共20页
亲,该文档总共20页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

魔板游戏内含源代码.docx

《魔板游戏内含源代码.docx》由会员分享,可在线阅读,更多相关《魔板游戏内含源代码.docx(20页珍藏版)》请在冰点文库上搜索。

魔板游戏内含源代码.docx

魔板游戏内含源代码

XX学院计算机科学与技术系

实验报告

课程名称

程序设计基础实验

(2)

实验项目

应用系统的面向对象设计与分析

实验类型

综合性实验

完成人

所在班级

指导教师

完成日期

2009年11月30日

一、实验报告简表………………………………………………………02

二、设计内容与要求…………………………………………………02

1、设计内容

2、设计要求

三、总体设计……………………………………………………………03

1、需求分析

2、功能描述

3、设计工具与运行环境

4、类及类间关系描述

四、详细设计………………………………………………………………05

1、类图

2、源代码

五、实例运行及应用效果………………………………………………20

1、魔板游戏界面

2、测试游戏及输出结果

六、体会与总结…………………………………………………………24

实验报告正文

一.实验报告简表

实验课题名称

魔板游戏

综合性实验项目

应用系统的面向对象设计与分析

服务课程

程序设计基础实验

(2)

实验小组组长

指导教师

参与实验人员及其在实验课题中承担的主要任务(包括组长)

学号

小组成员姓名

班级

在实验课题中的主要作用

二.设计内容与要求

1、设计内容:

用面向对象的Java程序设计语言开发设计GUI界面的魔板游戏程序。

2、设计要求:

(1)、魔板由3*3或4*4的方格组成,其中一个格子是空的。

对于3*3的魔板,其他格子里随机放置8个编号为0-7的方块;对于4*4的魔板,其他格子里随即放置15个编号为0-14的方块,如果是n*n的方格原理相同。

(2)、用鼠标单击任何与格子相邻的方块可以把该方块移入空格子。

通过不断地移动方块,可以将方块一行一行按数字顺序依次排序。

(3)、魔板游戏也可以使用图像来代替数字,对于3*3的魔板,将图像分成3*3个方块,打乱顺序后,放入魔板,并将其中一个方格置为空。

最终目标是通过移动方块来恢复为原始图像。

4*4的魔板与3*3的方法相似。

三.总体设计

1、需求分析:

魔板游戏就是大家以前常玩的拼板游戏!

就是将一幅完整的图片分成小块,打乱顺序,再拼好,并且只能将图块和临界的空格交换位置。

用最短的时间和最省的步骤将图形拼好,此玩法虽然简单,但特别考验智力,魔板游戏与魔方游戏相似属于益智游戏。

此项目我们需要通过面向对象的Java程序设计来实现,游戏设计中需要用到GUI图形界面的设计,产生随机数生成打乱的图片,各种事件(动作事件、鼠标事件和窗口事件等)的响应和处理等面向对象的知识来实现游戏的各项功能。

2、功能描述:

我们所完成的魔板游戏可以实现基本的魔板游戏的功能。

此项目魔板游戏可由用户选择3*3和4*4两个不同难度,N*N的魔板游戏原理与此相同,游戏可以通过下拉列表实现选择不同的游戏图像,增加游戏的趣味性。

点击“开始游戏”按钮后可以实现游戏步骤的计数,拼图右边有一个完整的图像可以供用户参照,用鼠标单击任何与格子相邻的方块可以把该方块移入空格子。

通过不断地移动方块,可以将方块一行一行按数字或图片依次拼图。

若成功完成拼图(即左边的拼图与右边的参照图像一样),则有提示:

“恭喜您,成功完成”的字样出现。

3、设计工具与运行环境:

(1)、设计工具:

Editplus。

(2)、运行环境:

JDK,WindowsXP。

4、类及类间关系描述:

此游戏项目创建了两个类:

MobanFrame类和MobanSquare类,其中MobanFrame类调用MobanSquare类。

四.详细设计(包括类图及源代码)

1、类图:

2、源代码:

MobanFrame类:

importjava.awt.*;

import.*;

publicclassMobanFrameextendsFrameimplementsActionListener,MouseListener,ItemListener

{

MobanSquare[][]position;//声明方块

PointstartPoint=newPoint(110,120);//绘制左边图像的起始点

PointrightStartPoint=newPoint(450,120);//绘制右边图像的起始点

int[]num;//存储随机数的数组

Point[]pointMove;

inttotalStep=0;//总共已走的步数

StringselectedImage="金牛座";//当前选中的要游戏图像

StringgamingImage="";//正在游戏的图像

booleanstartGame=false;

intsquareNumber=3;

intlevel=1;//游戏等级

intsquareSize=80;

ImagesourceImage;//游戏图像的来源图像

ImagespaceSourceImage;//空格图像的来源图像

ImagespaceImage;

Image[]myImage;//存储打乱的图像的数组

Graphics2Dggg;

/*--------------------*/

/*显示在界面上的一些组件*/

/*--------------------*/

TextFieldtfx;

TextFieldtfy;

MenuBarmnbMyMenuBar;

MenumnChooseLevel;

MenuItemmniLevel1,mniLevel2;

LabellbStep;//显示所走的步数

LabellbSuccess=newLabel("");

ButtonbtnStartGame;

ChoicechChoiceImage;

publicMobanFrame()

{

super("魔板游戏");

pointMove=newPoint[4];//方块可以移动的四个方向

pointMove[0]=newPoint(-1,0);

pointMove[1]=newPoint(1,0);

pointMove[2]=newPoint(0,-1);

pointMove[3]=newPoint(0,1);

setLayout(newFlowLayout());

chChoiceImage=newChoice();

chChoiceImage.add("金牛座");

chChoiceImage.add("迈克尔乔丹");

chChoiceImage.add("多啦a梦");

chChoiceImage.add("射手座");

chChoiceImage.add("功夫熊猫");

chChoiceImage.add("蜡笔小新");

chChoiceImage.add("柯南");

chChoiceImage.add("小狗");

chChoiceImage.add("小狗2");

chChoiceImage.add("小猫");

chChoiceImage.add("Baby");

chChoiceImage.add("数字");

mniLevel1=newMenuItem("简单");

mniLevel2=newMenuItem("困难");

mnChooseLevel=newMenu("难度");

mnbMyMenuBar=newMenuBar();

tfx=newTextField(8);

tfy=newTextField(8);

tfx.setText("0");

tfy.setText("1");

tfx.setVisible(false);

tfy.setVisible(false);

lbStep=newLabel("已走步数:

"+Integer.toString(totalStep));

btnStartGame=newButton("开始游戏");

mnChooseLevel.add(mniLevel1);

mnChooseLevel.add(mniLevel2);

mnbMyMenuBar.add(mnChooseLevel);

this.setMenuBar(mnbMyMenuBar);

add(tfx);

add(tfy);

add(lbSuccess);

add(lbStep);

add(chChoiceImage);

add(btnStartGame);

mniLevel1.addActionListener(this);

mniLevel2.addActionListener(this);

btnStartGame.addActionListener(this);

chChoiceImage.addItemListener(this);

this.addMouseListener(this);

addWindowListener(newWindowAdapter()

{

publicvoidwindowClosing(WindowEvente)

{

System.exit(0);

}

});

ProInit();

GetImage();

GetRandom();

setSize(800,500);

setVisible(true);

}

publicvoidGetImage()//获取要完成的拼图,并将拼图打乱

{

myImage=newImage[squareNumber*squareNumber];

ImageFiltercropFilter1;

ImageFiltercropFilter2;

ImageFiltercropFilter3;

Toolkittool=getToolkit();

sourceImage=tool.createImage(selectedImage+".jpg");

spaceSourceImage=tool.createImage("space.jpg");

sourceImage=sourceImage.getScaledInstance(squareSize*squareNumber,squareSize*squareNumber,Image.SCALE_DEFAULT);//得到一个指定大小的图像

cropFilter1=newCropImageFilter(0,0,squareSize*squareNumber,squareSize*squareNumber);//四个参数分别为图像起点坐标和宽高,即CropImageFilter(intx,inty,intwidth,intheight),详细情况请参考API

sourceImage=createImage(newFilteredImageSource(sourceImage.getSource(),cropFilter1));

for(inti=0;i

{

cropFilter2=newCropImageFilter((i%squareNumber)*squareSize,(i/squareNumber)*squareSize,squareSize,squareSize);

myImage[i]=createImage(newFilteredImageSource(sourceImage.getSource(),cropFilter2));

}

cropFilter3=newCropImageFilter(0,0,squareSize,squareSize);

spaceImage=createImage(newFilteredImageSource(spaceSourceImage.getSource(),cropFilter3));

myImage[squareNumber*squareNumber-1]=spaceImage;

}

publicvoidProInit()

{

num=newint[squareNumber*squareNumber];

position=newMobanSquare[squareNumber+2][squareNumber+2];

}

publicvoidpaint(Graphicsg)//绘制拼图

{

booleanshowSuccess=true;

lbStep.setText("已走步数:

"+Integer.toString(totalStep));

if(startGame)

{

for(inti=1;i

{

for(intj=1;j

{

g.drawImage(position[i][j].GetImage(),startPoint.x+(position[i][j].y-1)*squareSize,startPoint.y+(position[i][j].x-1)*squareSize,this);//绘制左边打乱的方块

}

}

g.setColor(Color.cyan);

for(inti=0;i

{

g.drawLine(startPoint.x+squareSize*i,startPoint.y,startPoint.x+squareSize*i,startPoint.y+squareNumber*squareSize);

g.drawLine(startPoint.x,startPoint.y+squareSize*i,startPoint.x+squareNumber*squareSize,startPoint.y+squareSize*i);

}

}

g.drawImage(sourceImage,rightStartPoint.x,rightStartPoint.y,this);//绘制右边参照图

//g.drawImage(spaceImage,rightStartPoint.x+(squareNumber-1)*squareSize,rightStartPoint.y+(squareNumber-1)*squareSize,this);

for(inti=1;i

{

for(intj=1;j

{

if(position[i][j].GetOrder()!

=(i-1)*squareNumber+(j-1))

{

showSuccess=false;

}

}

}

if(showSuccess)//用户完成拼图,弹出完成对话框

{

showSuccess=false;

lbSuccess.setText("恭喜您,成功完成");

}

}

publicvoidGetRandom()//生成随机数,并将随机图像赋给每个方块

{

intk=squareNumber*squareNumber;

booleannumExist=false;

inttotal=0;

for(intj=0;j

{

num[j]=-1;

}

while(total

{

k=((int)(Math.random()*10)+(int)(Math.random()*10))%(squareNumber*squareNumber);

for(intj=0;j

{

if(k==num[j])

{

numExist=true;

break;

}

}

if(!

numExist)

{

num[total]=k;

total++;

}

numExist=false;

}

for(inti=0;i

{

for(intj=0;j

{

if(i==0||i==squareNumber+1||j==0||j==squareNumber+1)//实例化外围方块,这些方块是不可见的

{

this.position[i][j]=newMobanSquare(i,j,-1,squareNumber*squareNumber-1);

}

}

}

for(inti=1;i

{

for(intj=1;j

{

this.position[i][j]=newMobanSquare(i,j,num[(i-1)*squareNumber+j-1],squareNumber*squareNumber-1);

this.position[i][j].SetImage(myImage[this.position[i][j].GetOrder()]);

}

}

}

publicvoidMove(intx,inty)//移动方块

{

intsquare_X,square_Y,order_Old,order_New;

square_X=(y-1-startPoint.y)/squareSize+1;

square_Y=(x-1-startPoint.x)/squareSize+1;

for(inti=0;i<4;i++)

{

if(position[square_X+pointMove[i].x][square_Y+pointMove[i].y].arrive)//当前方块可以移动,即空白块是它的邻块

{

this.totalStep++;

PointnewPoint=newPoint(square_X+pointMove[i].x,square_Y+pointMove[i].y);

order_Old=position[square_X][square_Y].GetOrder();

order_New=position[newPoint.x][newPoint.y].GetOrder();

position[square_X][square_Y].SetOrder(order_New);

position[square_X][square_Y].SetImage(myImage[order_New]);

position[newPoint.x][newPoint.y].SetOrder(order_Old);

position[newPoint.x][newPoint.y].SetImage(myImage[order_Old]);

repaint();

break;

}

}

}

publicvoidactionPerformed(ActionEvente)

{

if(e.getSource()==btnStartGame)//点击"开始游戏按钮"

{

lbSuccess.setText("");

totalStep=0;

if(level==1)//难度为简单

{

squareNumber=3;

}

if(level==2)//难度为困难

{

squareNumber=4;

}

startGame=true;

gamingImage=selectedImage;

ProInit();

GetImage();

GetRandom();

repaint();

}

if(e.getSource()==mniLevel1)//在"难度"菜单项里选择了"简单"菜单子项

{

level=1;

startPoint=newPoint(110,120);//绘制左边图像的起始点

rightStartPoint=newPoint(450,120);//绘制右边图像的起始点

}

if(e.getSource()==mniLevel2)//在"难度"菜单项里选择了"困难"菜单子项

{

level=2;

startPoint=newPoint(40,100);//绘制左边图像的起始点

rightStartPoint=newPoint(440,100);//绘制右边图像的起始点

}

}

publicvoiditemStateChanged(ItemEvente)//处理下拉框事件的方法

{

if(e.getItemSelectable()instanceofChoice)

{

selectedImage=((Choice)e.getItemSelectable()).getSelectedItem();

tfx.setText(selectedImage);

tfy.setText(gamingImage);

if(selectedImage!

=gamingImage)

{

GetImage();

startGame=false;

repaint();

}

}

}

publicvoidmouseClicked(MouseEvente)//处理在

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

当前位置:首页 > 总结汇报 > 学习总结

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

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