更新時間:2021-06-04 10:51:21 來源:動力節點 瀏覽867次
游戲說明:
設計一款拼圖游戲,要求點擊圖片按鈕,實現圖片按鈕的移動,直到每一個按鈕都到達指定位置游戲終止退出。
游戲設計思路:
1.準備一張圖像文件;
2.創建N個按鈕圖標,每個按鈕圖標里面存入一張分割后的圖片信息;
3.創建一個空白按鈕用于和圖標按鈕交換位置,達到移動的效果;
4.亂序,將按鈕圖標亂序,完成游戲效果;
5.創建一個面板添加游戲開始和游戲結束按鈕;
6.設計游戲窗口;
代碼實現:
Cell類----設計每個按鈕對象應該具備的屬性功能---針對按鈕
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;//設置按鈕的寬度大小
private static int IMAGEHEIGHT;
private int ID = 0;//設置當前按鈕的指向坐標
public Cell(Icon icon, int id, int imagewidth, int height)//構造函數初始化,傳入兩個參數,一個是圖像的圖標,一個是該按鈕的數組ID
{
this.setIcon(icon);
this.ID = id;
this.IMAGEWIDTH = imagewidth;
this.IMAGEHEIGHT = height;
this.setSize(IMAGEWIDTH, IMAGEHEIGHT);
}
public void move(Direction dir)//移動
{
Rectangle rec = this.getBounds();//獲取當前對象的這個邊框
switch(dir)
{
case UP://向上移動,改變坐標
this.setLocation(rec.x, rec.y + IMAGEHEIGHT);
break;
case DOWN://向下移動
this.setLocation(rec.x, rec.y - IMAGEHEIGHT);
break;
case LEFT://向左移動
this.setLocation(rec.x - IMAGEWIDTH, rec.y);
break;
case RIGHT://向右移動
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類------方向枚舉類,存放移動的方向
package puzzle_game;
public enum Direction {
UP,
DOWN,
LEFT,
RIGHT
}
GamePanel類-----游戲面板設計類,真正的游戲思想從此開始
主要實現的功能有:
1.初始化面板按鈕數組,將圖像轉化成圖標然后存入按鈕中;
2.打亂數組面板中的按鈕排序,實現游戲娛樂功能;
3.每個按鈕添加監聽機制,實現點擊按鈕后的移動功能;
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;//設置這個拼圖的行列
private Cell cells[] = new Cell[row*col];//創建一個按鈕對象數組
int ImageWidth;
int ImageHeight;
public GamePanel()//構造函數
{
this.setLayout(null);
init();
}
public void init()//初始化完成以下功能--完成圖像的切割,完成圖像到圖標的轉換,完成按鈕圖標的添加,將按鈕添加到面板上,并且給每一個按鈕添加監聽機制
{
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;//表示當前這個圖像的坐標id,在數組中的下標
if(num < row*col-1)
{
bufnew = buf.getSubimage(width*j, height*i, width, height);
icon = new ImageIcon(bufnew);//將圖像轉化成圖標
}
else//使最后一張圖像為空白圖像
{
icon = new ImageIcon("F:/Image/Puzzle_game/background2.jpg");//一張空白圖像
}
cells[num] = new Cell(icon, num, width, height);//添加圖標到每一個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]);//將每一個按鈕添加到當前這個面板上面
if(i < cells.length-1)
cells[i].addMouseListener(this);//空白格不添加監聽機制
}
}
public void OutOfOrder()//亂序----打亂圖片的排布順序
{
Random random = new Random();
for(int i = 0 ; i < cells.length ; i++)
{
int index1 = random.nextInt(cells.length);//cells的長度是9,但是他的上限是9,取不到9,所取值范圍是0-8
int index2 = random.nextInt(cells.length);
int x = cells[index1].getX();
int y = cells[index1].getY();//獲取下標是index1的數組元素按鈕的坐標
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, "恭喜您過關了是否還來一局?", "提示", 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類------設置游戲的打開窗口類,創建了一個菜單面板存放游戲開始和游戲結束兩個按鈕,并且對游戲的窗口進行了基本設置,這是整個游戲的入口。
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("游戲結束");
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(); }
}
以上就是動力節點小編介紹的"Java拼圖游戲的代碼",希望對大家有幫助,如有疑問,請在線咨詢,有專業老師隨時為您服務。
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習