這篇文章給大家分享的是有關(guān)java如何實(shí)現(xiàn)拼圖游戲的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。

具體內(nèi)容如下
游戲說明:
設(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)游戲娛樂功能;
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ān)了是否還來一局?", "提示", JOptionPane.YES_NO_OPTION);
if(choice == 0)//表示再來一局
{
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)在就可以玩你自己的游戲了:

感謝各位的閱讀!關(guān)于“java如何實(shí)現(xiàn)拼圖游戲”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站chinadenli.net,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
網(wǎng)頁(yè)名稱:java如何實(shí)現(xiàn)拼圖游戲-創(chuàng)新互聯(lián)
當(dāng)前URL:http://chinadenli.net/article18/dijedp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)、云服務(wù)器、網(wǎng)站改版、虛擬主機(jī)、動(dòng)態(tài)網(wǎng)站、網(wǎng)站設(shè)計(jì)公司
聲明:本網(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í)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容