Java模板游戏代码.docx
- 文档编号:10057213
- 上传时间:2023-02-08
- 格式:DOCX
- 页数:19
- 大小:17.82KB
Java模板游戏代码.docx
《Java模板游戏代码.docx》由会员分享,可在线阅读,更多相关《Java模板游戏代码.docx(19页珍藏版)》请在冰豆网上搜索。
Java模板游戏代码
魔板游戏代码
publicclassPuzzleFrameextendsJFrameimplementsActionListener
{
PuzzlePadpuzzlePad;
JButton开始;
JMenuBarbar;
JMenufileMenu;
JMenuItem初级,高级;
JRadioButton数字玩法,图像玩法;
ButtonGroupgroup=null;
Containercon=null;
publicPuzzleFrame()
{
bar=newJMenuBar();
fileMenu=newJMenu("魔板游戏");
初级=newJMenuItem("初级");
高级=newJMenuItem("高级");
fileMenu.add(初级);
fileMenu.add(高级);
bar.add(fileMenu);
setJMenuBar(bar);
初级.addActionListener(this);
高级.addActionListener(this);
开始=newJButton("开始玩");
开始.addActionListener(this);
group=newButtonGroup();
数字玩法=newJRadioButton("数字玩法",true);
图像玩法=newJRadioButton("图像玩法",false);
group.add(数字玩法);
group.add(图像玩法);
puzzlePad=newPuzzlePad(3,3,50,50);
con=getContentPane();
con.add(puzzlePad,BorderLayout.CENTER);
JPanelpNorth=newJPanel();
pNorth.add(数字玩法);
pNorth.add(图像玩法);
pNorth.add(开始);
con.add(pNorth,BorderLayout.NORTH);
con.validate();
addWindowListener(newWindowAdapter()
{publicvoidwindowClosing(WindowEvente)
{
System.exit(0);
}
});
setVisible(true);
setBounds(100,50,440,360);
validate();
}
publicvoidactionPerformed(ActionEvente)
{if(e.getSource()==开始)
{
if(数字玩法.isSelected())
{
puzzlePad.随机排列数字();
}
elseif(图像玩法.isSelected())
{
puzzlePad.随机排列图像();
}
}
elseif(e.getSource()==初级)
{
con.remove(puzzlePad);
puzzlePad=newPuzzlePad(3,3,50,50);
con.add(puzzlePad,BorderLayout.CENTER);
con.validate();
this.validate();
}
elseif(e.getSource()==高级)
{
con.remove(puzzlePad);
puzzlePad=newPuzzlePad(4,4,50,50);
con.add(puzzlePad,BorderLayout.CENTER);
con.validate();
this.validate();
}
}
publicstaticvoidmain(Stringargs[])
{
newPuzzleFrame();
}
}
MobanFrame类:
importjava.awt.*;
importjava.awt.event.*;
import.*;
importjava.applet.Applet;
importjavax.imageio.ImageIO;
importjava.io.File;
importjava.awt.image.*;
importjavax.swing.JOptionPane;
publicclassMobanFrameextendsFrameimplementsActionListener,MouseListener,ItemListener
{
MobanSquare[][]position;
PointstartPoint=newPoint(100,130);
PointrightStartPoint=newPoint(400,90);
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("鱼");
chChoiceImage.add("熊猫");
chChoiceImage.add("大象");
chChoiceImage.add("长颈鹿");
chChoiceImage.add("牛");
chChoiceImage.add("小猫");
chChoiceImage.add("小猫2");
chChoiceImage.add("羊");
chChoiceImage.add("猴");
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(600,300);
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);
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) { intclick_X,click_Y; click_X=e.getX(); click_Y=e.getY(); tfx.setText(Integer.toString((e.getY()-1-startPoint.x)/squareSize+1)); tfy.setText(Integer.toString((e.getX()-1-startPoint.y)/squareSize+1)); if
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java 模板 游戏 代码
![提示](https://static.bdocx.com/images/bang_tan.gif)