怎么用java實(shí)現(xiàn)拼圖游戲-創(chuàng)新互聯(lián)

本篇內(nèi)容主要講解“怎么用java實(shí)現(xiàn)拼圖游戲”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“怎么用java實(shí)現(xiàn)拼圖游戲”吧!

成都服務(wù)器托管,創(chuàng)新互聯(lián)提供包括服務(wù)器租用、成都西云數(shù)據(jù)中心、帶寬租用、云主機(jī)、機(jī)柜租用、主機(jī)租用托管、CDN網(wǎng)站加速、空間域名等業(yè)務(wù)的一體化完整服務(wù)。電話咨詢:13518219792

游戲說(shuō)明:


設(shè)計(jì)一款拼圖游戲,要求點(diǎn)擊圖片按鈕,實(shí)現(xiàn)圖片按鈕的移動(dòng),直到每一個(gè)按鈕都到達(dá)指定位置游戲終止退出。

游戲設(shè)計(jì)思路:

1.準(zhǔn)備一張圖像文件; 2.創(chuàng)建N個(gè)按鈕圖標(biāo),每個(gè)按鈕圖標(biāo)里面存入一張分割后的圖片信息; 3.創(chuàng)建一個(gè)空白按鈕用于和圖標(biāo)按鈕交換位置,達(dá)到移動(dòng)的效果; 4.亂序,將按鈕圖標(biāo)亂序,完成游戲效果; 5.創(chuàng)建一個(gè)面板添加游戲開始和游戲結(jié)束按鈕; 6.設(shè)計(jì)游戲窗口;

游戲界面設(shè)計(jì)基本結(jié)構(gòu):

代碼實(shí)現(xiàn):

Cell類----設(shè)計(jì)每個(gè)按鈕對(duì)象應(yīng)該具備的屬性功能---針對(duì)按鈕

package puzzle_game;import java.awt.Rectangle;import javax.swing.Icon;import javax.swing.JButton;@SuppressWarnings("serial")public class Cell extends JButton{ private static int IMAGEWIDTH;//設(shè)置按鈕的寬度大小 private static int IMAGEHEIGHT; private int ID = 0;//設(shè)置當(dāng)前按鈕的指向坐標(biāo) public Cell(Icon icon, int id, int imagewidth, int height)//構(gòu)造函數(shù)初始化,傳入兩個(gè)參數(shù),一個(gè)是圖像的圖標(biāo),一個(gè)是該按鈕的數(shù)組ID {  this.setIcon(icon);  this.ID = id;  this.IMAGEWIDTH = imagewidth;  this.IMAGEHEIGHT = height;  this.setSize(IMAGEWIDTH, IMAGEHEIGHT); } public void move(Direction dir)//移動(dòng) {  Rectangle rec = this.getBounds();//獲取當(dāng)前對(duì)象的這個(gè)邊框  switch(dir)  {  case UP://向上移動(dòng),改變坐標(biāo)   this.setLocation(rec.x, rec.y + IMAGEHEIGHT);   break;  case DOWN://向下移動(dòng)   this.setLocation(rec.x, rec.y - IMAGEHEIGHT);   break;  case LEFT://向左移動(dòng)   this.setLocation(rec.x - IMAGEWIDTH, rec.y);   break;  case RIGHT://向右移動(dòng)   this.setLocation(rec.x + IMAGEWIDTH, rec.y);   break;  } } public int getID() {  return ID; } public int getX() {  return this.getBounds().x; } public int getY() {  return this.getBounds().y; }}

Direction類------方向枚舉類,存放移動(dòng)的方向

package puzzle_game;public enum Direction { UP, DOWN, LEFT, RIGHT}

GamePanel類-----游戲面板設(shè)計(jì)類,真正的游戲思想從此開始

主要實(shí)現(xiàn)的功能有:

1.初始化面板按鈕數(shù)組,將圖像轉(zhuǎn)化成圖標(biāo)然后存入按鈕中;2.打亂數(shù)組面板中的按鈕排序,實(shí)現(xiàn)游戲娛樂(lè)功能;3.每個(gè)按鈕添加監(jiān)聽機(jī)制,實(shí)現(xiàn)點(diǎn)擊按鈕后的移動(dòng)功能;

package puzzle_game;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.image.BufferedImage;import java.io.File;import java.util.Random;import javax.imageio.ImageIO;import javax.swing.ImageIcon;import javax.swing.JOptionPane;import javax.swing.JPanel;@SuppressWarnings("serial")public class GamePanel extends JPanel implements MouseListener{ private Cell BlankCell = null; private int row = 4; private int col = 4;//設(shè)置這個(gè)拼圖的行列 private Cell cells[] = new Cell[row*col];//創(chuàng)建一個(gè)按鈕對(duì)象數(shù)組 int ImageWidth; int ImageHeight; public GamePanel()//構(gòu)造函數(shù) {  this.setLayout(null);  init(); } public void init()//初始化完成以下功能--完成圖像的切割,完成圖像到圖標(biāo)的轉(zhuǎn)換,完成按鈕圖標(biāo)的添加,將按鈕添加到面板上,并且給每一個(gè)按鈕添加監(jiān)聽機(jī)制 {  int num = 0;  BufferedImage buf = null;  BufferedImage bufnew = null;  ImageIcon icon = null;  int width = 0;  int height = 0;  try  {   buf = ImageIO.read(new File("F:/Image/Puzzle_game/puze.jpg"));//讀取文件圖像   ImageWidth = buf.getWidth();   ImageHeight = buf.getHeight();   System.out.println("ImageWidth->"+ImageWidth+"ImageHeight->"+ImageHeight);   width = ImageWidth/col;   height = ImageHeight/row;  }catch(Exception e)  {   System.out.println(e);  }  for(int i = 0; i < row; i++)  {   for(int j = 0; j < col; j++)   {    num = i*col+j;//表示當(dāng)前這個(gè)圖像的坐標(biāo)id,在數(shù)組中的下標(biāo)    if(num < row*col-1)    {     bufnew = buf.getSubimage(width*j, height*i, width, height);     icon = new ImageIcon(bufnew);//將圖像轉(zhuǎn)化成圖標(biāo)    }    else//使最后一張圖像為空白圖像    {     icon = new ImageIcon("F:/Image/Puzzle_game/background2.jpg");//一張空白圖像    }    cells[num] = new Cell(icon, num, width, height);//添加圖標(biāo)到每一個(gè)BUTTON按鈕上面    cells[num].setLocation(width*j, height*i);   }  }  BlankCell = cells[cells.length-1];//初始化空白格  for(int i = 0; i < cells.length; i++)  {   this.add(cells[i]);//將每一個(gè)按鈕添加到當(dāng)前這個(gè)面板上面   if(i < cells.length-1)    cells[i].addMouseListener(this);//空白格不添加監(jiān)聽機(jī)制  } } public void OutOfOrder()//亂序----打亂圖片的排布順序 {  Random random = new Random();  for(int i = 0 ; i < cells.length ; i++)  {   int index1 = random.nextInt(cells.length);//cells的長(zhǎng)度是9,但是他的上限是9,取不到9,所取值范圍是0-8   int index2 = random.nextInt(cells.length);   int x = cells[index1].getX();   int y = cells[index1].getY();//獲取下標(biāo)是index1的數(shù)組元素按鈕的坐標(biāo)   cells[index1].setLocation(cells[index2].getX(), cells[index2].getY());   cells[index2].setLocation(x, y);  } } public boolean IsWin()//判斷游戲玩家是否贏 {  for(int i = 0; i < cells.length; i++)  {   int x = cells[i].getX();   int y = cells[i].getY();   if(x/(ImageWidth/col) + y/(ImageHeight/row) != i)   {    return false;   }  }  return true; } public void mouseClicked(MouseEvent e) {  Cell t = (Cell) e.getSource();  int x = BlankCell.getX();  int y = BlankCell.getY();  if(t.getY() == y && t.getX() + ImageWidth/col == x)//圖像向右走  {   t.move(Direction.RIGHT);   BlankCell.move(Direction.LEFT);  }  else if(t.getY() == y && t.getX() - ImageWidth/col == x)//圖像向左走  {   t.move(Direction.LEFT);   BlankCell.move(Direction.RIGHT);  }  else if(t.getX() == x && t.getY() + ImageHeight/row == y)//圖像向上走  {   t.move(Direction.UP);   BlankCell.move(Direction.DOWN);  }  else if(t.getX() == x && t.getY() - ImageHeight/row == y)//圖像向下走  {   t.move(Direction.DOWN);   BlankCell.move(Direction.UP);  }  if(IsWin())  {   int choice = JOptionPane.showConfirmDialog(null, "恭喜您過(guò)關(guān)了是否還來(lái)一局?", "提示", JOptionPane.YES_NO_OPTION);   if(choice == 0)//表示再來(lái)一局   {    this.OutOfOrder();   }   else//表示退出游戲    System.exit(1);  } } @Override public void mousePressed(MouseEvent e) { } @Override public void mouseReleased(MouseEvent e) { } @Override public void mouseEntered(MouseEvent e) { } @Override public void mouseExited(MouseEvent e) { }}

GameFrame類------設(shè)置游戲的打開窗口類,創(chuàng)建了一個(gè)菜單面板存放游戲開始和游戲結(jié)束兩個(gè)按鈕,并且對(duì)游戲的窗口進(jìn)行了基本設(shè)置,這是整個(gè)游戲的入口。

package puzzle_game;import java.awt.BorderLayout;import java.awt.Container;import java.awt.EventQueue;import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;public class GameFrame extends JFrame { public JPanel pane1 = new JPanel(); public JButton button1 = new JButton("游戲開始"); public JButton button2 = new JButton("游戲結(jié)束"); public GameFrame() {  super("拼圖游戲");  pane1.setLayout(new FlowLayout());  pane1.add(button1);  pane1.add(button2);  Container con = this.getContentPane();  con.add(pane1,BorderLayout.NORTH);  GamePanel gamepane = new GamePanel();  con.add(gamepane,BorderLayout.CENTER);  this.setBounds(300, 300, 600, 600);  this.setVisible(true);  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  button1.addActionListener(new ActionListener()  {   public void actionPerformed(final ActionEvent e)   {    gamepane.OutOfOrder();   }  });  button2.addActionListener(new ActionListener()  {   public void actionPerformed(final ActionEvent e)   {    System.exit(1);   }  }); } public static void main(String[] args) {  new GameFrame();  }}

這是剛運(yùn)行程序以后的界面,也是拼圖成功的界面,我設(shè)置的是4*4模式,你也可以根據(jù)自己的喜好設(shè)計(jì)模式諸如–2*3,3*4,都可以;

這是我點(diǎn)擊開始以后運(yùn)行的界面,當(dāng)然每次都不同,因?yàn)閬y序是隨機(jī)生成的順序,那么現(xiàn)在就可以玩你自己的游戲了。

到此,相信大家對(duì)“怎么用java實(shí)現(xiàn)拼圖游戲”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)建站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

分享名稱:怎么用java實(shí)現(xiàn)拼圖游戲-創(chuàng)新互聯(lián)
轉(zhuǎn)載源于:http://muchs.cn/article42/dpicec.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、定制網(wǎng)站、網(wǎng)站維護(hù)、域名注冊(cè)、網(wǎng)頁(yè)設(shè)計(jì)公司小程序開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

綿陽(yáng)服務(wù)器托管