java拼图游戏排行榜
创始人
2024-06-10 15:56:17
0

一、关于java拼图游戏的问题 高手进

你的代码没有注释,实在不想看.

按钮和小图片的匹配不会写?把小图片和按钮按里面的编号都取出来,分别组成2个字符串,然后用equals()方法来实现匹配.

建议把小图片和按钮都放在table里面,这样比较好操作.

我以前做的时候,在每个"拼图部分"里面都是有一个字符串作为编号的,鼠标点击单元格的图片的时候(比如该单元格在表格坐标是(m,n)),则取(m+1,n),(m-1,n),(m,n+1),(m,n-1),四个单元格,检查里面是不是有"拼图部分",如果全都有,就return,如果有空格,就传点击的这个"拼图部分"进去,删除当前单元格的"拼图部分",然后把所有"拼图部分"的编号拿出来匹配.

这样做的话好像比较容易

PS:代码拿出来给别人看的时候要写注释,这个是很必须的习惯!!!

楼上说的纯属胡说八道,你见过9个拼图部分中只要A块回到原来的位置就成功的游戏么?

二、1200分跪求JAVA数字拼图游戏源代码!

1:

import java.io.IOException;

import javax.sound.sampled.LineUnavailableException;

import javax.sound.sampled.UnsupportedAudioFileException;

//华容道原理的拼图游戏。利用轻组建的套用。

import java.awt.BorderLayout;

import java.awt.Button;

import java.awt.Choice;

import java.awt.Color;

import java.awt.Container;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.Icon;

import javax.swing.ImageIcon;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

public class MyMainFrame extends JFrame implements ActionListener{

/**

*

*/

private static final long serialVersionUID= 1L;

MyCanvas myCanvas;

JPanel panelNorth,panelPreview;

Button start,preview,set;

Container container;

public MyMainFrame(){//初使化

container=this.getContentPane();

start=new Button("开始");

start.addActionListener(this);

preview=new Button("预览");

preview.addActionListener(this);

set= new Button("设置");

set.addActionListener(this);

panelPreview=new JPanel();

panelPreview.setLayout(null);

Icon icon=new ImageIcon("images/pic_"+MyCanvas.pictureID+".jpg");

JLabel label=new JLabel(icon);

label.setBounds(0,0,400,400);

panelPreview.add(label);

panelNorth=new JPanel();

panelNorth.setBackground(Color.yellow);

panelNorth.add(start);

panelNorth.add(preview);

panelNorth.add(set);

myCanvas=new MyCanvas();

container.add(myCanvas,BorderLayout.CENTER);

container.add(panelNorth,BorderLayout.NORTH);

this.setTitle("成型拼图小游戏-1212");

this.setLocation(300,200);

this.setSize(408,465);

this.setResizable(false);

this.setVisible(true);

this.setDefaultCloseOperation(3);

}//end of初始化构造函数

public void actionPerformed(ActionEvent e){

Button button=(Button)e.getSource();

if(button==start){

myCanvas.Start();

}else if(button==preview){

if(button.getLabel()=="预览"){

container.remove(myCanvas);

container.add(panelPreview);

panelPreview.updateUI();

container.repaint();

button.setLabel("返回");

}else{

container.remove(panelPreview);

container.add(myCanvas);

container.repaint();

button.setLabel("预览");

}

}else if(button==set){

Choice pic= new Choice();

//pic.add("QQ");

pic.add("美女");

int i=JOptionPane.showConfirmDialog(this,pic,"选择图片", JOptionPane.OK_CANCEL_OPTION);

//使用选择对话框来进行选择图片。

if(i==JOptionPane.YES_OPTION){

MyCanvas.pictureID=pic.getSelectedIndex()+5;

myCanvas.reLoadPictrue();

Icon icon=new ImageIcon("images/pic_"+MyCanvas.pictureID+".jpg");

JLabel label=new JLabel(icon);

label.setBounds(0,0,400,400);

panelPreview.removeAll();

panelPreview.add(label);

panelPreview.repaint();

}

}

}

public static void main(String[] args) throws UnsupportedAudioFileException, LineUnavailableException, IOException

{

new MyMainFrame();

}//end of main

}

2:

import java.awt.Rectangle;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import javax.swing.Icon;

import javax.swing.ImageIcon;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

public class MyCanvas extends JPanel implements MouseListener

{

/**

*

*/

private static final long serialVersionUID= 1L;

boolean hasAddActionListener=false;//设置方格的动作监听器的标志位,TRUE为已经添加上动作事件

Cell cell[];//定义方格

Rectangle cellNull;//定义空方格区域是一个矩形类

public static int pictureID=4;//当前选择的图片代号

public MyCanvas(){

this.setLayout(null);

this.setSize(400,400);

cellNull=new Rectangle(300,300,100,100);//空方格区域在第三行每三列

cell=new Cell[16];

Icon icon;

for(int i= 0; i< 4; i++){

for(int j=0;j<4;j++){

icon=new ImageIcon("images/pic_"+pictureID+"_"+(i*4+j+1)+".jpg");

cell[i*4+j]=new Cell(icon);

cell[i*4+j].setLocation(j*100,i*100);

this.add(cell[i*4+j]);

}

}

this.remove(cell[15]);//移除最后一个多余的方格

}//放置9张小图片并且移调最后一张

public void reLoadPictrue(){//当选择其它图形进行拼图时,需重新加载新图片

Icon icon;

for(int i= 0; i< 4; i++){

for(int j=0;j<4;j++){

icon=new ImageIcon("images/pic_"+pictureID+"_"+(i*4+j+1)+".jpg");

cell[i*4+j].setIcon(icon);

}

}

}

public boolean isFinish(){//判断是否拼合成功

for(int i=0;i<15;i++)

{ int x=cell[i].getBounds().x;

int y=cell[i].getBounds().y;

if(y/100*4+x/100!=i)

return false;

}//end of for

return true;

}

public void Start(){//对方格进行重新排列,打乱顺序

while(cell[0].getBounds().x<=100&&cell[0].getBounds().y<=100){//当第一个方格距左上角较近时

int x=cellNull.getBounds().x;

int y=cellNull.getBounds().y;

int direction=(int)(Math.random()*4);//产生0-4,对应空方格的上下左右移动

if(direction==0){//空方格左移动,与左侧方格互换位置,左侧方格右移动

x-=100;

if(test(x,y)){

for(int j=0;j<15;j++){

if((cell[j].getBounds().x==x)&&(cell[j].getBounds().y==y)){//依次寻找左侧的按钮

cell[j].move("RIGHT",100);

cellNull.setLocation(x,y);

break;//找到后跳出for循环

}

}

}

}else if(direction==1){//RIGHT

x+=100;

if(test(x,y)){

for(int j=0;j<15;j++){

if((cell[j].getBounds().x==x)&&(cell[j].getBounds().y==y)){

cell[j].move("LEFT",100);

cellNull.setLocation(x,y);

break;

}

}

}

}else if(direction==2){//UP

y-=100;

if(test(x,y)){

for(int j=0;j<15;j++){

if((cell[j].getBounds().x==x)&&(cell[j].getBounds().y==y)){

cell[j].move("DOWN",100);

cellNull.setLocation(x,y);

break;

}

}

}

}else{//DOWN

y+=100;

if(test(x,y)){

for(int j=0;j<15;j++){

if((cell[j].getBounds().x==x)&&(cell[j].getBounds().y==y)){

cell[j].move("UP",100);

cellNull.setLocation(x,y);

break;

}

}

}

}

}

if(!hasAddActionListener)//如果尚未添加动作事件,则添加

for(int i=0;i<15;i++)//为第个方格添加动作事件,这样单击按钮就能移动了

cell[i].addMouseListener(this);

hasAddActionListener=true;

}

private boolean test(int x,int y){

if((x>=0&&x<=200)||(y>=0&&y<=200))

return true;

else

return false;

}

public void mouseClicked(MouseEvent e){}

public void mouseEntered(MouseEvent e){}

public void mouseExited(MouseEvent e){}

public void mouseReleased(MouseEvent e){}

public void mousePressed(MouseEvent e){

//方格的鼠标事件,因为用到了MyCanvas中的一些方法,因此没有在Cell类中处理鼠标事件

Cell button=(Cell)e.getSource();

int x1=button.getBounds().x;//得到所单击方格的坐标

int y1=button.getBounds().y;

int x2=cellNull.getBounds().x;//得到空方格的坐标

int y2=cellNull.getBounds().y;

if(x1==x2&&y1-y2==100)//进行比较,如果满足条件则进行交换

button.move("UP",100);

else if(x1==x2&&y1-y2==-100)

button.move("DOWN",100);

else if(x1-x2==100&y1==y2)

button.move("LEFT",100);

else if(x1-x2==-100&&y1==y2)

button.move("RIGHT",100);

else

return;//不满足就不进行任何处理

cellNull.setLocation(x1,y1);

this.repaint();

if(this.isFinish()){//进行是否完成的判断

JOptionPane.showMessageDialog(this,"景锋恭喜你完成拼图,加油!想继续下一关么?");

for(int i=0;i<15;i++)

cell[i].removeMouseListener(this);//如果已完成,撤消鼠标事件,鼠标单击方格不在起作用

hasAddActionListener=false;

}

}

}

3:

import javax.swing.Icon;

import javax.swing.JButton;

public class Cell extends JButton{

/**

*

*/

private static final long serialVersionUID= 1L;

Cell(Icon icon){//实际为ICON

super(icon);

this.setSize(100,100);

}

public void move(String direction,int sleep){//方格的移动

if(direction=="UP"){

this.setLocation(this.getBounds().x,this.getBounds().y-100);

}else if(direction=="DOWN"){

this.setLocation(this.getBounds().x,this.getBounds().y+100);

}else if(direction=="LEFT"){

this.setLocation(this.getBounds().x-100,this.getBounds().y);

}else{

this.setLocation(this.getBounds().x+100,this.getBounds().y);

}

}

}

一、关于java拼图游戏的问题 高手进

你的代码没有注释,实在不想看.

按钮和小图片的匹配不会写?把小图片和按钮按里面的编号都取出来,分别组成2个字符串,然后用equals()方法来实现匹配.

建议把小图片和按钮都放在table里面,这样比较好操作.

我以前做的时候,在每个"拼图部分"里面都是有一个字符串作为编号的,鼠标点击单元格的图片的时候(比如该单元格在表格坐标是(m,n)),则取(m+1,n),(m-1,n),(m,n+1),(m,n-1),四个单元格,检查里面是不是有"拼图部分",如果全都有,就return,如果有空格,就传点击的这个"拼图部分"进去,删除当前单元格的"拼图部分",然后把所有"拼图部分"的编号拿出来匹配.

这样做的话好像比较容易

PS:代码拿出来给别人看的时候要写注释,这个是很必须的习惯!!!

楼上说的纯属胡说八道,你见过9个拼图部分中只要A块回到原来的位置就成功的游戏么?

二、1200分跪求JAVA数字拼图游戏源代码!

1:

import java.io.IOException;

import javax.sound.sampled.LineUnavailableException;

import javax.sound.sampled.UnsupportedAudioFileException;

//华容道原理的拼图游戏。利用轻组建的套用。

import java.awt.BorderLayout;

import java.awt.Button;

import java.awt.Choice;

import java.awt.Color;

import java.awt.Container;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.Icon;

import javax.swing.ImageIcon;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

public class MyMainFrame extends JFrame implements ActionListener{

/**

*

*/

private static final long serialVersionUID= 1L;

MyCanvas myCanvas;

JPanel panelNorth,panelPreview;

Button start,preview,set;

Container container;

public MyMainFrame(){//初使化

container=this.getContentPane();

start=new Button("开始");

start.addActionListener(this);

preview=new Button("预览");

preview.addActionListener(this);

set= new Button("设置");

set.addActionListener(this);

panelPreview=new JPanel();

panelPreview.setLayout(null);

Icon icon=new ImageIcon("images/pic_"+MyCanvas.pictureID+".jpg");

JLabel label=new JLabel(icon);

label.setBounds(0,0,400,400);

panelPreview.add(label);

panelNorth=new JPanel();

panelNorth.setBackground(Color.yellow);

panelNorth.add(start);

panelNorth.add(preview);

panelNorth.add(set);

myCanvas=new MyCanvas();

container.add(myCanvas,BorderLayout.CENTER);

container.add(panelNorth,BorderLayout.NORTH);

this.setTitle("成型拼图小游戏-1212");

this.setLocation(300,200);

this.setSize(408,465);

this.setResizable(false);

this.setVisible(true);

this.setDefaultCloseOperation(3);

}//end of初始化构造函数

public void actionPerformed(ActionEvent e){

Button button=(Button)e.getSource();

if(button==start){

myCanvas.Start();

}else if(button==preview){

if(button.getLabel()=="预览"){

container.remove(myCanvas);

container.add(panelPreview);

panelPreview.updateUI();

container.repaint();

button.setLabel("返回");

}else{

container.remove(panelPreview);

container.add(myCanvas);

container.repaint();

button.setLabel("预览");

}

}else if(button==set){

Choice pic= new Choice();

//pic.add("QQ");

pic.add("美女");

int i=JOptionPane.showConfirmDialog(this,pic,"选择图片", JOptionPane.OK_CANCEL_OPTION);

//使用选择对话框来进行选择图片。

if(i==JOptionPane.YES_OPTION){

MyCanvas.pictureID=pic.getSelectedIndex()+5;

myCanvas.reLoadPictrue();

Icon icon=new ImageIcon("images/pic_"+MyCanvas.pictureID+".jpg");

JLabel label=new JLabel(icon);

label.setBounds(0,0,400,400);

panelPreview.removeAll();

panelPreview.add(label);

panelPreview.repaint();

}

}

}

public static void main(String[] args) throws UnsupportedAudioFileException, LineUnavailableException, IOException

{

new MyMainFrame();

}//end of main

}

2:

import java.awt.Rectangle;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import javax.swing.Icon;

import javax.swing.ImageIcon;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

public class MyCanvas extends JPanel implements MouseListener

{

/**

*

*/

private static final long serialVersionUID= 1L;

boolean hasAddActionListener=false;//设置方格的动作监听器的标志位,TRUE为已经添加上动作事件

Cell cell[];//定义方格

Rectangle cellNull;//定义空方格区域是一个矩形类

public static int pictureID=4;//当前选择的图片代号

public MyCanvas(){

this.setLayout(null);

this.setSize(400,400);

cellNull=new Rectangle(300,300,100,100);//空方格区域在第三行每三列

cell=new Cell[16];

Icon icon;

for(int i= 0; i< 4; i++){

for(int j=0;j<4;j++){

icon=new ImageIcon("images/pic_"+pictureID+"_"+(i*4+j+1)+".jpg");

cell[i*4+j]=new Cell(icon);

cell[i*4+j].setLocation(j*100,i*100);

this.add(cell[i*4+j]);

}

}

this.remove(cell[15]);//移除最后一个多余的方格

}//放置9张小图片并且移调最后一张

public void reLoadPictrue(){//当选择其它图形进行拼图时,需重新加载新图片

Icon icon;

for(int i= 0; i< 4; i++){

for(int j=0;j<4;j++){

icon=new ImageIcon("images/pic_"+pictureID+"_"+(i*4+j+1)+".jpg");

cell[i*4+j].setIcon(icon);

}

}

}

public boolean isFinish(){//判断是否拼合成功

for(int i=0;i<15;i++)

{ int x=cell[i].getBounds().x;

int y=cell[i].getBounds().y;

if(y/100*4+x/100!=i)

return false;

}//end of for

return true;

}

public void Start(){//对方格进行重新排列,打乱顺序

while(cell[0].getBounds().x<=100&&cell[0].getBounds().y<=100){//当第一个方格距左上角较近时

int x=cellNull.getBounds().x;

int y=cellNull.getBounds().y;

int direction=(int)(Math.random()*4);//产生0-4,对应空方格的上下左右移动

if(direction==0){//空方格左移动,与左侧方格互换位置,左侧方格右移动

x-=100;

if(test(x,y)){

for(int j=0;j<15;j++){

if((cell[j].getBounds().x==x)&&(cell[j].getBounds().y==y)){//依次寻找左侧的按钮

cell[j].move("RIGHT",100);

cellNull.setLocation(x,y);

break;//找到后跳出for循环

}

}

}

}else if(direction==1){//RIGHT

x+=100;

if(test(x,y)){

for(int j=0;j<15;j++){

if((cell[j].getBounds().x==x)&&(cell[j].getBounds().y==y)){

cell[j].move("LEFT",100);

cellNull.setLocation(x,y);

break;

}

}

}

}else if(direction==2){//UP

y-=100;

if(test(x,y)){

for(int j=0;j<15;j++){

if((cell[j].getBounds().x==x)&&(cell[j].getBounds().y==y)){

cell[j].move("DOWN",100);

cellNull.setLocation(x,y);

break;

java拼图游戏排行榜

}

}

}

}else{//DOWN

y+=100;

if(test(x,y)){

for(int j=0;j<15;j++){

if((cell[j].getBounds().x==x)&&(cell[j].getBounds().y==y)){

cell[j].move("UP",100);

cellNull.setLocation(x,y);

break;

}

}

}

}

}

if(!hasAddActionListener)//如果尚未添加动作事件,则添加

for(int i=0;i<15;i++)//为第个方格添加动作事件,这样单击按钮就能移动了

cell[i].addMouseListener(this);

hasAddActionListener=true;

}

private boolean test(int x,int y){

if((x>=0&&x<=200)||(y>=0&&y<=200))

return true;

else

return false;

}

public void mouseClicked(MouseEvent e){}

public void mouseEntered(MouseEvent e){}

public void mouseExited(MouseEvent e){}

public void mouseReleased(MouseEvent e){}

public void mousePressed(MouseEvent e){

//方格的鼠标事件,因为用到了MyCanvas中的一些方法,因此没有在Cell类中处理鼠标事件

Cell button=(Cell)e.getSource();

int x1=button.getBounds().x;//得到所单击方格的坐标

int y1=button.getBounds().y;

int x2=cellNull.getBounds().x;//得到空方格的坐标

int y2=cellNull.getBounds().y;

if(x1==x2&&y1-y2==100)//进行比较,如果满足条件则进行交换

button.move("UP",100);

else if(x1==x2&&y1-y2==-100)

button.move("DOWN",100);

else if(x1-x2==100&y1==y2)

button.move("LEFT",100);

else if(x1-x2==-100&&y1==y2)

button.move("RIGHT",100);

else

return;//不满足就不进行任何处理

cellNull.setLocation(x1,y1);

this.repaint();

if(this.isFinish()){//进行是否完成的判断

JOptionPane.showMessageDialog(this,"景锋恭喜你完成拼图,加油!想继续下一关么?");

for(int i=0;i<15;i++)

cell[i].removeMouseListener(this);//如果已完成,撤消鼠标事件,鼠标单击方格不在起作用

hasAddActionListener=false;

}

}

}

3:

import javax.swing.Icon;

import javax.swing.JButton;

public class Cell extends JButton{

/**

*

*/

private static final long serialVersionUID= 1L;

Cell(Icon icon){//实际为ICON

super(icon);

this.setSize(100,100);

}

public void move(String direction,int sleep){//方格的移动

if(direction=="UP"){

this.setLocation(this.getBounds().x,this.getBounds().y-100);

}else if(direction=="DOWN"){

this.setLocation(this.getBounds().x,this.getBounds().y+100);

}else if(direction=="LEFT"){

this.setLocation(this.getBounds().x-100,this.getBounds().y);

}else{

this.setLocation(this.getBounds().x+100,this.getBounds().y);

}

}

}

相关内容

热门资讯

十八禁游戏排行榜 一、十八禁游戏制作公司ILLUSIONILLUSION(イリュージョン)是日本株式会社Eye One...
steam手柄游戏排行榜 一、steam十大必玩手柄游戏列表:1、《真人快打11》《真人快打11》(Mortal Kombat...
h iphone游戏排行榜 各位老铁们好,相信很多人对h iphone游戏排行榜都不是特别的了解,因此呢,今天就来为大家分享下关...
日本好玩h游戏排行榜 一、日本安卓h性游戏下载.很无语有这么难谁告知!!!您好:成人色情游戏是国家明令禁止的游戏,正规的应...
steam游戏排行榜怎么查 一、steam游戏排行榜在哪里看steam游戏排行榜通过官方网站点击进入Steam官网查看。Stea...
hgame游戏排行榜 大家好,hgame游戏排行榜相信很多的网友都不是很明白,包括《怪异恐剧院》:令人惊艳的HGame,相...
美国app游戏排行榜 一、手游排行榜2023前十名手游排行榜2023前十名:《蛋仔派对》《桃源深处有人家》《泰拉瑞亚》《弹...
td游戏排行榜 一、2023好玩的td游戏好玩的td手游2023年,随着技术的不断发展,各种趣味性质的TD游戏也相继...
最新消消乐类的游戏排行榜 一、消消乐游戏排行榜消消乐游戏排行榜如下:《开心消消乐》、《宾果消消消》、《海滨消消乐》、《旋转消消...
变态版游戏盒子排行榜下载 一、十大变态游戏盒子排行榜今天小编要为大家带来的是十大变态游戏盒子排行榜,都是全网高人气的bt手游平...