当前位置:首页 > 代码 > 正文

java数字拼图游戏代码(拼图游戏代码html5)

admin 发布:2022-12-19 21:01 182


本篇文章给大家谈谈java数字拼图游戏代码,以及拼图游戏代码html5对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

用JAVA编写一个小程序 比如拼图游戏 推箱子 猜数字 。。。。。。请附详细注释 因为要发表 谢谢大家!

我写的一个猜数字游戏,希望对你有用,代码如下:

import java.awt.BorderLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.Random;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;

import javax.swing.JTextField;

public class GuessNumber {

static int trys, A, B;

static String r, t;

public static int[] MakeGuessNumber(){//随机生成一个无重复数字的四位数

Random r = new Random();

int guess[] = new int[4];

for(int i=0; i4; i++){

guess[i] = r.nextInt(10);

for(int j=i-1; j=0; j--){

if(guess[i]==guess[j]){

i--;

break;}

}

}

return guess;

}

public static String getRundom(){//将此四位数转化为字符串

int guess[]=MakeGuessNumber();

return ""+guess[0]+guess[1]+guess[2]+guess[3];

}

public static void messageDialog(Object o){

JOptionPane.showMessageDialog(null, o);

}

public static void guessNumber(){//主要算法实现部分

r=getRundom();

//System.out.println(r);

JFrame jf=new JFrame();

JButton b1=new JButton("新游戏");

JLabel l1=new JLabel("输入:");

final JTextField jtf=new JTextField(10);

JButton b2=new JButton("提交");

final JTextArea jta=new JTextArea(10,10);

jta.append(" "+"Guess"+" "+"Result"+"\n");

JScrollPane scrollPane=new JScrollPane(jta);

JPanel jp1=new JPanel();

jp1.add(l1);

jp1.add(jtf);

jp1.add(b2);

jf.add(b1,BorderLayout.NORTH);

jf.add(jp1,BorderLayout.CENTER);

jf.add(scrollPane,BorderLayout.SOUTH);

b1.addActionListener(new ActionListener(){

@Override

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

trys=0;

A=0;

B=0;

jta.setText(" "+"Guess"+" "+"Result"+"\n");

jtf.setText("");

r=getRundom();

//System.out.println(r);

}

});

b2.addActionListener(new ActionListener(){

@Override

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

t=jtf.getText();

A=0;

B=0;

if(t.length()!=4||t.substring(0, 1).equals(t.substring(1, 2))

||t.substring(0, 1).equals(t.substring(2, 3))

||t.substring(0, 1).equals(t.substring(3, 4))

||t.substring(1, 2).equals(t.substring(2, 3))

||t.substring(1, 2).equals(t.substring(3, 4))

||t.substring(2, 3).equals(t.substring(3, 4))

||!t.matches("[0-9]*"))

messageDialog("Wrong Input!");

else{

jtf.setText("");

trys++;

if(t.substring(0, 1).equals(r.substring(0, 1)))

A++;

if(t.substring(0, 1).equals(r.substring(1, 2)))

B++;

if(t.substring(0, 1).equals(r.substring(2, 3)))

B++;

if(t.substring(0, 1).equals(r.substring(3, 4)))

B++;

if(t.substring(1, 2).equals(r.substring(1, 2)))

A++;

if(t.substring(1, 2).equals(r.substring(0, 1)))

B++;

if(t.substring(1, 2).equals(r.substring(2, 3)))

B++;

if(t.substring(1, 2).equals(r.substring(3, 4)))

B++;

if(t.substring(2, 3).equals(r.substring(2, 3)))

A++;

if(t.substring(2, 3).equals(r.substring(0, 1)))

B++;

if(t.substring(2, 3).equals(r.substring(1, 2)))

B++;

if(t.substring(2, 3).equals(r.substring(3, 4)))

B++;

if(t.substring(3, 4).equals(r.substring(3, 4)))

A++;

if(t.substring(3, 4).equals(r.substring(0, 1)))

B++;

if(t.substring(3, 4).equals(r.substring(1, 2)))

B++;

if(t.substring(3, 4).equals(r.substring(2, 3)))

B++;

jta.append(trys+" "+t+" "+A+"A"+B+"B"+"\n");

if(A==4){

if(trys=4)

messageDialog("You win after "+trys+" trys!");

else if(trys=3)

messageDialog("You win after only "+trys+" trys!");

}

}

}

});

jf.setSize(300, 300);

jf.setVisible(true);

jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public static void main(String args[]){

guessNumber();

}

}

我没有进行详细注释,这个程序挺好理解的,你可以自己再看一下

怎么用Java实现拼图游戏,求思维和代码

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.FlowLayout;

import java.awt.Font;

import java.awt.GridLayout;

import java.awt.Image;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.image.BufferedImage;

import javax.swing.GroupLayout.Alignment;

import javax.swing.Icon;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JCheckBoxMenuItem;

import javax.swing.JFileChooser;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

public class Jigsaw implements ActionListener {

private JFrame frame;

private JPanel panel;

private JButtonx[] button;

private JPanel panel_right;

private int level = 4;

private JMenuBar menubar;

private ImageIcon imageicon;

private Image image;

private BufferedImage bufferedimage;

private BufferedImage[] image_button;

private JLabel label;

private JPanel panel_left;

private ImageIcon[] icon_button;

private JMenu menu1;

private JMenuItem menuitem1_1;

private JMenu menu2;

private JCheckBoxMenuItem menuitem2_1;

private JCheckBoxMenuItem menuitem2_2;

private JCheckBoxMenuItem menuitem2_3;

private int x=400;

private int y=400;

private JMenuItem menuitem1_2;

private JFileChooser jfc;

private String name="src/pic/1.jpg";         //起始图片位置,请自行设置

private JPanel panel_menu;

private JLabel label1_1;

private JLabel label1_2;

private JLabel label1_3;

private JLabel label2_1;

private JLabel label2_2;

private JLabel label2_3;

public void reset() {

frame = new JFrame("拼图");

panel = new JPanel();

panel_left = new JPanel();

panel_right = new JPanel();

panel.setLayout(new GridLayout(0, 2));

menubar = new JMenuBar();

menubar.setLayout(new BorderLayout());

panel_menu=new JPanel();

panel_menu.setLayout(null);

menu1 = new JMenu("开始游戏");

menuitem1_1=new JMenuItem("重新开始");

menuitem1_2=new JMenuItem("选择图片");

menu2=new JMenu("等级选择");

menuitem2_1=new JCheckBoxMenuItem("低级(3)");

menuitem2_2=new JCheckBoxMenuItem("高级(4)");

menuitem2_3=new JCheckBoxMenuItem("自定义");

imageicon = new ImageIcon(name);

image = imageicon.getImage();

bufferedimage=new BufferedImage(252, 405, BufferedImage.TYPE_INT_RGB);

bufferedimage.getGraphics().drawImage(image,0,0,252,405,null);

imageicon.setImage(bufferedimage);

label=new JLabel(imageicon);

panel_left.setLayout(new FlowLayout(FlowLayout.CENTER,0,0));

panel_left.add(label);

panel.add(panel_left);

jfc=new JFileChooser();

label1_1=new JLabel("您已经走了");

label1_1.setBounds(30, 0, 80, 25);

label1_2=new JLabel("0");

label1_2.setForeground(Color.RED);

label1_2.setBounds(120, 0, 40, 25);

label1_2.setFont(new Font("黑体",Font.BOLD,20));

label1_3=new JLabel("步");

label1_3.setBounds(160, 0, 30, 25);

label2_1=new JLabel("时间");

label2_1.setBounds(220, 0, 50, 25);

label2_2=new JLabel("0");

label2_2.setForeground(Color.RED);

label2_2.setBounds(280, 0, 40, 25);

label2_2.setFont(new Font("黑体",Font.BOLD,20));

label2_3=new JLabel("秒");

label2_3.setBounds(330, 0, 30, 25);

panel_right.setLayout(new GridLayout(level, level));

button = new JButtonx[level * level];

image_button=new BufferedImage[level*level];

for (int i = 0; i level*level/*-1*/; i++) {

image_button[i]=new BufferedImage(252/level, 405/level, BufferedImage.TYPE_INT_RGB);

image_button[i].getGraphics().drawImage(bufferedimage, 0,0,252/level,405/level,(i%level)*(252/level), (i/level)*(405/level), (i%level+1)*(252/level), (i/level+1)*(405/level),null);

}

icon_button=new ImageIcon[level * level];

for (int i = 0; i level * level; i++) {

button[i] = new JButtonx();

icon_button[i]=new ImageIcon(image_button[i],i+"");

button[i].setIcon(icon_button[i]);

button[i].num=i;

button[i].addActionListener(this);

panel_right.add(button[i]);

}

button[level*level-1].setVisible(false);

panel.add(panel_right);

menuitem1_1.addActionListener(this);

menuitem1_2.addActionListener(this);

menuitem2_1.addActionListener(this);

menuitem2_2.addActionListener(this);

menuitem2_3.addActionListener(this);

menu1.add(menuitem1_1);

menu1.add(menuitem1_2);

menu2.add(menuitem2_1);

menu2.add(menuitem2_2);

menu2.add(menuitem2_3);

menubar.add(menu1,BorderLayout.WEST);

menubar.add(menu2,BorderLayout.EAST);

menubar.add(panel_menu);

panel_menu.add(label1_1);

panel_menu.add(label1_2);

panel_menu.add(label1_3);

panel_menu.add(label2_1);

panel_menu.add(label2_2);

panel_menu.add(label2_3);

frame.setJMenuBar(menubar);

frame.add(panel);

frame.setBounds(x, y, 508+4, 459+5);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setResizable(false);

frame.setVisible(true);

}

public void actionPerformed(ActionEvent e) {

if(e.getSource()==menuitem1_1)

{

for (int i = 0; i level*level; i++)

button[i].setVisible(true);

int a[]=new int [level*level];

int index=level*level;

for (int i = 0; i level*level; i++) {

a[i]=i;

}

for (int i = 0; i level*level; i++) {

int x=(int)(Math.random()*index);

int temp=a[x];

a[x]=a[index-1];

a[index-1]=temp;

button[i].setIcon(icon_button[a[index-1]]);

if(a[index-1]==level*level-1)button[i].setVisible(false);

index--;

}

win();

}

else if(e.getSource()==menuitem1_2){if(jfc.showOpenDialog(panel)==0)name=jfc.getSelectedFile().getAbsolutePath();frame.setVisible(false);x=frame.getX();y=frame.getY();reset();}

else if(e.getSource()==menuitem2_1){level=4;frame.setVisible(false);x=frame.getX();y=frame.getY();reset();}

else if(e.getSource()==menuitem2_2){level=5;frame.setVisible(false);x=frame.getX();y=frame.getY();reset();}

else if(e.getSource()==menuitem2_3)try {level=Integer.parseInt(JOptionPane.showInputDialog(frame,"请输入行数:"));frame.setVisible(false);x=frame.getX();y=frame.getY();reset();} catch (Exception e2) {}

else

{

JButtonx but=(JButtonx)e.getSource();

if(but.isVisible())

{

if(but.num-level=0)judgemengt(but.num,but.num-level);

if(but.num+level=level*level-1)judgemengt(but.num,but.num+level);

if(but.num%level-1=0)judgemengt(but.num,but.num-1);

if(but.num%level+1=level-1)judgemengt(but.num,but.num+1);

}

win();

}

}

public void judgemengt(int i,int j)

{

if(button[j].isVisible()==false)

{

Icon icontemp_button=button[j].getIcon();

button[j].setIcon(button[i].getIcon());

button[i].setIcon(icontemp_button);

button[j].setVisible(true);

button[i].setVisible(false);

}

}

public void win()

{

int i;

for (i = 0; i level*level-1; i++) {

if(button[i].num!=Integer.parseInt(button[i].getIcon().toString()))break;

}

if(i==level*level-1)

{

JOptionPane.showMessageDialog(frame,"YOU WIN");

}

}

public static void main(String[] args) {

Jigsaw a=new Jigsaw();

a.reset();

}

}

class JButtonx extends JButton

{

int num;

}

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;j4;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;j4;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;i15;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=100cell[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;j15;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;j15;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;j15;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;j15;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;i15;i++)//为第个方格添加动作事件,这样单击按钮就能移动了

cell[i].addMouseListener(this);

hasAddActionListener=true;

}

private boolean test(int x,int y){

if((x=0x=200)||(y=0y=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==x2y1-y2==100)//进行比较,如果满足条件则进行交换

button.move("UP",100);

else if(x1==x2y1-y2==-100)

button.move("DOWN",100);

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

button.move("LEFT",100);

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

button.move("RIGHT",100);

else

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

cellNull.setLocation(x1,y1);

this.repaint();

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

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

for(int i=0;i15;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编写的图形化界面拼图小游戏代码!

个人见解,总体需要两个二维数组(一个存储正确图片排列 Array1 String[][],一个随机生成图片排列Array2 String[][]),一个一维数组来存储图片的名称Array3 String[],。

(1)如何实现图片移动

使用带图片的按钮(button =new button(getImage(Array[2][4]))),然后通过单击事件来更改按钮的图片来源。 把被点击的按钮的图片路径更新到空白按钮,并且把被点击的按钮图片更新的成空白。其实就是变换两个的二维数组成员的值。更新Array2中的值,然后重绘按钮

如 Array[2][3]=“3.image”

Array[2][4]=“”

图片3.image右移

Array[2][3]=“”

Array[2][4]=“3.image”

(2)如何判断被单击的网格与空白的网格是否相邻

后台使用一个二维数组Array2来做映射。通过二维数组的下标来判断,如Array[2][3]可以知道Array[2][4]是它右边的那个。

(3)如何实现图片的随机摆放

比如有9个图片,你可以命名1-9,然后初始化一个长度为9的一维String 数组Array3来存储图片的名称,

使用随机函数给二维数组Array2赋值,如Array2[2][3]=Array3[random()],这里要判断这个图片是否已被使用过,可以通过遍历Array2来确定当前Array3这个值是否已经在Array2中了

最后通过Array1 和Array2来比较,用户的拼图是否正确。

语言组织能力有限。讲不太清楚。

java数字拼图游戏代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于拼图游戏代码html5、java数字拼图游戏代码的信息别忘了在本站进行查找喔。

版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;

本文地址:http://ahzz.com.cn/post/23977.html


取消回复欢迎 发表评论:

分享到

温馨提示

下载成功了么?或者链接失效了?

联系我们反馈

立即下载