欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java實現(xiàn)對對碰小游戲

 更新時間:2019年12月14日 08:40:20   作者:angry_youth  
這篇文章主要為大家詳細(xì)介紹了java實現(xiàn)對對碰小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了java實現(xiàn)對對碰的具體代碼,供大家參考,具體內(nèi)容如下

- 游戲?qū)崿F(xiàn)功能:分別點擊兩張相鄰的圖像按鈕進行交換(重點相鄰),交換后的兩個圖像按鈕的相鄰水平或者垂直方向上,與之相同的圖像超過規(guī)定個數(shù)后(這里規(guī)定為3個)就將其全部消除(置為空白按鈕),上面的圖像按鈕也分別隨之向下移動,將空白補齊(這里我們可以理解為圖像按鈕和空白按鈕進行交換)。

- 游戲設(shè)計思路:

1.創(chuàng)建圖像按鈕數(shù)組,設(shè)置其基本屬性;
2.給每個圖像按鈕配置相應(yīng)的ID,用來標(biāo)記圖像信息,對于ID相同的按鈕,圖像也相同;
3.設(shè)置遍歷全局判斷是否可以建立相連按鈕的函數(shù);
4.設(shè)置遍歷全局將可以建立相連的按鈕圖像ID置為EMPTY,將按鈕置為空白按鈕;
5.設(shè)置移動函數(shù),將空白按鈕與上層非空白按鈕相互交換的函數(shù),將空白按鈕移動到上層;
6.設(shè)置更新函數(shù),將移動到上層的空白按鈕再隨機匹配圖像,繼續(xù)使用;
7.設(shè)置交換按鈕函數(shù);
8.記錄游戲得分,給一個確定的目標(biāo)成績,達(dá)到即可贏得游戲;
9.設(shè)置一個進度條,記錄游戲進行的時間;
10.設(shè)置一個時間記錄器(定時器);
11.設(shè)計游戲界面基本信息(根據(jù)個人愛好設(shè)計即可);

- 游戲代碼

---mybutton類,設(shè)置了每個按鈕對象的基本信息
package supperzzle;

import javax.swing.Icon;
import javax.swing.JButton;

public class MyButton extends JButton{
  private final int Width = 30;//設(shè)置按鈕的寬度
  private final int Height = 30;
  private int ID;//設(shè)置按鈕的ID-----ID代表每一個按鈕里面存放的數(shù)據(jù)

  private int buttonPosX = 0;
  private int buttonPosY = 0;

  public MyButton(int id, Icon icon)//構(gòu)造函數(shù)
  {
    this.setIcon(icon);
    this.ID = id;
    this.setSize(Width, Height);//設(shè)置按鈕的邊框大小
    this.setFocusable(true);//去掉按鈕的聚焦框
    this.setBorderPainted(false);//去掉邊框
    this.setContentAreaFilled(false);//不顯示外圍矩形邊框
  }

  public int GetID()
  {
    return ID;
  }
  public void SetID(int id)
  {
    this.ID = id;
  }
}
//-----這是游戲的重點了,基本游戲界面設(shè)計--GamePanel類---對游戲的界面進行了基本的設(shè)置(寫的有點挫,,有什么好的建議請盡情提的不要拘謹(jǐn),哈哈)
package supperzzle;

import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.EventListener;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class GamePanel extends JPanel implements ActionListener{
  private final int row = 10;
  private final int col = 10;
  private final int lineCount = 3;//設(shè)置幾連可碰消除
  private int grade = 0;//記錄得分
  private final int score = 10;//設(shè)置每次消去一個方塊獲得的分?jǐn)?shù)

  public MyButton mybutton[] = new MyButton[row*col];//這里只是開辟了相應(yīng)的空間

  private final int countImage = 7;
  private ImageIcon imageIcon[] = new ImageIcon[countImage];//設(shè)置圖標(biāo)數(shù)組
  private final int EMPTY = -1;

  private Random random = new Random();

  private int posx = 0;
  private int posy = 0;//保存第一次按鈕按下去的坐標(biāo)
  private boolean IsSecond = false;


  public GamePanel()//游戲面板的構(gòu)造函數(shù)----實現(xiàn)圖片加載,數(shù)組圖標(biāo)的加載,以及按鈕的基本設(shè)置
  {
    this.setLayout(new GridLayout(row,col,0,0));//創(chuàng)建一個網(wǎng)絡(luò)布局管理格式---row行col列
    for(int i = 0; i < countImage; i++)//圖標(biāo)數(shù)組初始化
    {
//     Image image = Toolkit.getDefaultToolkit().getImage("F:/Image/supperzzle/angrybird"+i+".png");
      Image image = Toolkit.getDefaultToolkit().getImage("F:/Image/LinkGame/pic"+i+".png");
      imageIcon[i] = new ImageIcon(image);//每一個數(shù)組元素都得到了相應(yīng)的圖標(biāo)
    }
    for(int i = 0; i < row; i++)
    {
      for(int j = 0; j < col; j++)
      {
        int index = random.nextInt(countImage);//隨機生成一個數(shù)作為圖標(biāo)數(shù)組的下標(biāo)
        mybutton[i*col+j] = new MyButton(index,imageIcon[index]);//給每個元素都進行了初始化
        mybutton[i*col+j].addActionListener(this);//按鈕添加監(jiān)聽機制
        mybutton[i*col+j].setEnabled(false);//設(shè)置按鈕為無效
        this.add(mybutton[i*col+j]);//將按鈕加載在該面板中
      }
    }
  }

  public boolean YesOrNoThreeLink(int x, int y)//判斷該坐標(biāo)同一個方向上是否可以建立連續(xù)相同id的方塊按鈕
  {
    int linked = 1;
    //判斷垂直方向上面是否有連續(xù)相同的
    for(int i = x-1; i >= 0; i--)
    {
      if(mybutton[i*col+y].GetID() == mybutton[x*col+y].GetID())
      {
        linked++;
      }
      else
      {
        break;
      }
    }
    for(int i = x+1; i < row; i++)//判斷該坐標(biāo)的下面
    {
      if(mybutton[i*col+y].GetID() == mybutton[x*col+y].GetID())
      {
        linked++;
      }
      else
      {
        break;
      }
    }
    if(linked >= lineCount)
    {
      return true;
    }
    //判斷水平方向上面是否有里連續(xù)相同的方塊
    linked = 1;
    for(int i = y-1; i >= 0; i--)//判斷水平向左
    {
      if(mybutton[x*col+i].GetID() == mybutton[x*col+y].GetID())
      {
        linked++;
      }
      else
        break;
    }
    for(int i = y+1; i < col; i++)//判斷水平向右
    {
      if(mybutton[x*col+i].GetID() == mybutton[x*col+y].GetID())
      {
        linked++;
      }
      else
        break;
    }
    if(linked >= lineCount)//說明滿足條件建立了連線
    {
      return true;
    }
    return false;
  }

  public void RemoveLink(int x, int y)//移除相同ID的方塊設(shè)置相同的方塊ID為EMPTY,并計算得分
  {
    int linked1 = 0;
    int linked2 = 0;
    int tempxStart = x;
    int tempxEnd = x;//分別保存第一個和最后一個與該點ID相同的x坐標(biāo)
    int tempyStart = y;
    int tempyEnd = y;//分別保存第一個和最后一個與該點ID相同的y坐標(biāo)
    //先判斷垂直方向上面的---上下行
    for(int i = x-1; i >= 0; i--)//判斷該坐標(biāo)的上面
    {
      if(mybutton[i*col+y].GetID() == mybutton[x*col+y].GetID())
      {
        linked1++;
        tempxStart = i;
      }
      else
      {
        break;
      }
    }
    for(int i = x+1; i < row; i++)//判斷該坐標(biāo)的下面
    {
      if(mybutton[i*col+y].GetID() == mybutton[x*col+y].GetID())
      {
        linked1++;
        tempxEnd = i;
      }
      else
      {
        break;
      }
    }
    if(linked1 + 1 >= lineCount)
    {
      for(int i = tempxStart; i <= tempxEnd; i++)
      {
        mybutton[i*col+y].SetID(EMPTY);
      }
//     grade += linked*score;
      grade += linked1*score;
    }
    //判斷水平方向上面的---左右列
    for(int i = y-1; i >= 0; i--)//判斷水平向左
    {
      if(mybutton[x*col+i].GetID() == mybutton[x*col+y].GetID())
      {
        linked2++;
        tempyStart = i;
      }
      else
        break;
    }
    for(int i = y+1; i < col; i++)//判斷水平向右
    {
      if(mybutton[x*col+i].GetID() == mybutton[x*col+y].GetID())
      {
        linked2++;
        tempyEnd = i;
      }
      else
        break;
    }
    if(linked2+1 >= lineCount)//說明滿足條件建立了連線
    {
      for(int i = tempyStart; i <= tempyEnd; i++)
      {
        mybutton[x*col+i].SetID(EMPTY);
      }
//     grade += score*linked;
      grade += score*linked2;
    }
    grade += score;
  }

  public int GetGrade()//獲取得分
  {
    return grade;
  }

  public void SetGrade(int n)
  {
    this.grade = n;
  }

  public void SwapElement(int x, int y)//交換元素
  {
    if(x >= 0 && x < row && y >= 0 && y < col)
    {
      if(!IsSecond)//第一次點擊按鈕
      {
        posx = x;
        posy = y;
        IsSecond = true;
        System.out.println("第一次點擊:posx->"+posx+" posy->"+posy);
      }
      else//第二次點擊按鈕
      {
        //判斷是否是相鄰的兩個方塊
        System.out.println("第2次點擊:x->"+x+" y->"+y);
        if(1 == Math.abs(posx - x) && posy == y
        || 1 == Math.abs(posy - y) && posx == x)//判斷是否是相鄰的坐標(biāo)
        {
          //先交換
          System.out.println("交換");
          int temp = mybutton[posx*col+posy].GetID();
          mybutton[posx*col+posy].SetID(mybutton[x*col+y].GetID()); 
          mybutton[x*col+y].SetID(temp);
//         showImageButton();
          mybutton[x*col+y].setIcon(imageIcon[temp]);
          mybutton[posx*col+posy].setIcon(imageIcon[mybutton[posx*col+posy].GetID()]);
          //再判斷
          if(YesOrNoThreeLink(x,y) || YesOrNoThreeLink(posx,posy))
          {
            if(YesOrNoThreeLink(x,y))
            {
              RemoveLink(x,y);
              GameFrame.texteara.setText(Integer.toString(GetGrade()));
            }
            if(YesOrNoThreeLink(posx,posy))
            {
              RemoveLink(posx,posy);
              GameFrame.texteara.setText(Integer.toString(GetGrade()));
            }
            //開始掉方塊,全盤處理所有下面的空白方塊
            DownButton();
            //更新
            UpDateButton();
            showImageButton();
            GameFrame.texteara.setText(Integer.toString(GetGrade()));
            while(GlobalSearch(1))//掃描全盤
            {
              GlobalSearch(0);//消除
              DownButton();
              UpDateButton();
              showImageButton();
              GameFrame.texteara.setText(Integer.toString(GetGrade()));
            }
          }
          else
          {
            //沒有相同的方塊就再換回來
            temp = mybutton[posx*col+posy].GetID();
            mybutton[posx*col+posy].SetID(mybutton[x*col+y].GetID()); 
            mybutton[x*col+y].SetID(temp);
//           showImageButton();
            mybutton[x*col+y].setIcon(imageIcon[temp]);
            mybutton[posx*col+posy].setIcon(imageIcon[mybutton[posx*col+posy].GetID()]);
          }
        }
        IsSecond = false;
      }
    }
  }

  public void DownButton()//將底層的空白的方塊全部上移,將上面的非空白方塊下移補齊
  {
    for(int i = row-1; i > 0; i--)//行--從最后一行開始
    {
      for(int j = 0; j < col; j++)//列---從第一列開始
      {
        if(mybutton[i*col+j].GetID() == EMPTY)//交換每個按鈕決定圖像的ID號即可
        {
          //交換同列上面非EMPTY的結(jié)點
          for(int k = i-1; k >= 0; k--)
          {
            if(mybutton[k*col+j].GetID() != EMPTY)
            {
              int id = mybutton[i*col+j].GetID();
              mybutton[i*col+j].SetID(mybutton[k*col+j].GetID());
              mybutton[k*col+j].SetID(id);
              break;
            }
          }
        }
      }
    }
  }

  public boolean GlobalSearch(int flag)//全盤掃描
  {
    if(flag == 1)//----------只是掃描所有的元素是否存在相鄰的連續(xù)方塊
    {
      for(int i = 0; i < row; i++)
      {
        for(int j = 0; j < col; j++)
        {
          if(YesOrNoThreeLink(i,j))
          {
            return true;
          }
        }
      }
    }
    else if(flag == 0)//-------掃描加消除該節(jié)點置為EMPTY
    {
      for(int i = 0; i < row; i++)
      {
        for(int j = 0; j < col; j++)
        {
          RemoveLink(i,j);
        }
      }
      return true;
    }

    return false;
  }

  public void showImageButton()//將所有的按鈕的圖標(biāo)重新繪制一次
  {
    for(int i = 0; i < row; i++)
    {
      for(int j = 0; j < col; j++)
      {
        mybutton[i*col+j].setIcon(imageIcon[mybutton[i*col+j].GetID()]);
      }
    }
  }

  public void UpDateButton()//更新按鈕里面的坐標(biāo)id-----讓EMPTY的按鈕重新獲得隨機ID
  {
    for(int i = 0; i < row; i++)
    {
      for(int j = 0; j < col; j++)
      {
        if(mybutton[i*col+j].GetID() == EMPTY)
        {
          int index = random.nextInt(countImage);
          mybutton[i*col+j].SetID(index);
        }
      }
    }
  }

  public int GetRow()
  {
    return row;
  }
  public int GetCol()
  {
    return col;
  }

  public void actionPerformed(ActionEvent e)//監(jiān)聽機制
  {
    for(int i = 0; i < row; i++)
    {
      for(int j = 0; j < col; j++)
      {
        if(e.getSource() == mybutton[i*col+j])//交換元素對象
        {
          SwapElement(i,j);
          GameFrame.texteara.setText(Integer.toString(GetGrade()));
          break;
        }
      }
    }
    if(grade > 8000 && GameFrame.timer.isRunning())
    {
      JOptionPane.showConfirmDialog(null, "恭喜您過關(guān)", "Win", JOptionPane.CLOSED_OPTION);
      for(int i = 0; i < row; i++)
      {
        for(int j = 0; j < col; j++)
        {
          mybutton[i*col+j].setEnabled(false);
          GameFrame.buttonstart.setEnabled(true);
          GameFrame.timer.stop();
        }
      }
    }
  }

}
//游戲走到這里就是真正的游戲接口了,游戲開始的入口--GameFrame類,設(shè)計游戲窗口和創(chuàng)建游戲
package supperzzle;

import java.awt.BorderLayout;
import java.awt.Container;
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.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import javax.swing.Timer;

public class GameFrame extends JFrame implements ActionListener{
  private JPanel paneone = new JPanel();
  public static JButton buttonstart = new JButton("游戲開始");
  private JButton buttonend = new JButton("游戲結(jié)束");
  private JLabel labelGrade = new JLabel("游戲得分");
  private JLabel labelTime = new JLabel("游戲時間");
  public static JTextField texteara = new JTextField(10);//設(shè)置文本編輯域
  GamePanel gamepanel = new GamePanel();
  private int gamerow = gamepanel.GetRow();
  private int gamecol = gamepanel.GetCol();
  private JProgressBar progressbar = new JProgressBar();//創(chuàng)建一個進度條
  public static Timer timer;//創(chuàng)建一個時間計時器


  public GameFrame()
  {
    Container con = this.getContentPane();
    con.setLayout(new BorderLayout());
    paneone.setLayout(new FlowLayout());
    paneone.add(buttonstart);//添加開始按鈕
    paneone.add(labelGrade);//添加得分編輯框
    paneone.add(texteara);//添加文本域
    paneone.add(labelTime);//添加時間編輯框
    paneone.add(progressbar);//添加一個進度條
    paneone.add(buttonend);//添加結(jié)束按鈕
    con.add(paneone,BorderLayout.NORTH);
    con.add(gamepanel,BorderLayout.CENTER);
    this.setBounds(300, 0, 600, 700);
    this.setTitle("對對碰游戲");
    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設(shè)置窗口關(guān)閉

    buttonstart.addActionListener(this);
    buttonend.addActionListener(this);

  }

  public void actionPerformed(ActionEvent e)
  {
    if(e.getSource() == buttonstart)//點擊開始按鈕
    {
      gamepanel.SetGrade(0);//重新設(shè)置得分
      buttonstart.setEnabled(false);
      progressbar.setMaximum(100);//設(shè)置進度條的最大最小范圍
      progressbar.setMinimum(0);
      progressbar.setStringPainted(true);
      timer = new Timer(800,new TimeListener());
      timer.start();
      for(int i = 0; i < gamerow; i++)
      {
        for(int j = 0; j < gamecol; j++)
        {
          gamepanel.mybutton[i*gamecol+j].setEnabled(true);
        }
      }
      initGame();
      texteara.setText(Integer.toString(gamepanel.GetGrade()));
    }
    if(e.getSource() == buttonend)
    {
      System.exit(1);
    }
  }

  public void initGame()
  {
    while(gamepanel.GlobalSearch(1))
    {
      gamepanel.GlobalSearch(0);
      gamepanel.DownButton();
      gamepanel.UpDateButton();
      gamepanel.showImageButton();
    }
  }

  class TimeListener implements ActionListener//創(chuàng)建了一個自定義的時間監(jiān)聽機制
  {
    int times = 0;
    public void actionPerformed(ActionEvent e)
    {
      progressbar.setValue(times++);
      if(times > 100)
      {
        timer.stop();
        for(int i = 0; i < gamerow; i++)
        {
          for(int j = 0; j < gamecol; j++)
          {
            gamepanel.mybutton[i*gamecol+j].setEnabled(false);
          }
        }
        buttonstart.setEnabled(true);
      }
    }
  }

  public static void main(String[] args) {
    GameFrame gameframe = new GameFrame();
  }

}

- 運行結(jié)果

這是運行后的游戲開始界面:

這是點擊開始以后的界面,這樣你就可以開始玩你的小游戲了!

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 使用spring的websocket創(chuàng)建通信服務(wù)的示例代碼

    使用spring的websocket創(chuàng)建通信服務(wù)的示例代碼

    這篇文章主要介紹了使用spring的websocket創(chuàng)建通信服務(wù)的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-11-11
  • SpringBoot的jar包如何啟動的實現(xiàn)

    SpringBoot的jar包如何啟動的實現(xiàn)

    本文主要介紹了SpringBoot的jar包如何啟動的實現(xiàn),文中根據(jù)實例編碼詳細(xì)介紹的十分詳盡,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • MyBatis后端對數(shù)據(jù)庫進行增刪改查等操作實例

    MyBatis后端對數(shù)據(jù)庫進行增刪改查等操作實例

    Mybatis是appach下開源的一款持久層框架,通過xml與java文件的緊密配合,避免了JDBC所帶來的一系列問題,下面這篇文章主要給大家介紹了關(guān)于MyBatis后端對數(shù)據(jù)庫進行增刪改查等操作的相關(guān)資料,需要的朋友可以參考下
    2022-08-08
  • Spring Bean實例化實現(xiàn)過程解析

    Spring Bean實例化實現(xiàn)過程解析

    這篇文章主要介紹了Spring Bean實例化實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-02-02
  • java中LinkedList使用迭代器優(yōu)化移除批量元素原理

    java中LinkedList使用迭代器優(yōu)化移除批量元素原理

    本文主要介紹了java中LinkedList使用迭代器優(yōu)化移除批量元素原理,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • SpringBoot解析yml全流程詳解

    SpringBoot解析yml全流程詳解

    本文主要介紹了SpringBoot解析yml全流程詳解,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • SpringBoot 使用Mybatis分頁插件實現(xiàn)詳解

    SpringBoot 使用Mybatis分頁插件實現(xiàn)詳解

    這篇文章主要介紹了SpringBoot 使用Mybatis分頁插件實現(xiàn)詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-10-10
  • java實現(xiàn)窗口刷新的示例代碼

    java實現(xiàn)窗口刷新的示例代碼

    本文主要介紹了java實現(xiàn)窗口刷新的示例代碼,通過重寫paintComponent()方法和調(diào)用repaint()方法,可以實現(xiàn)窗口的即時刷新,具有一定的參考價值,感興趣的可以了解一下
    2024-02-02
  • 詳解Spring MVC/Boot 統(tǒng)一異常處理最佳實踐

    詳解Spring MVC/Boot 統(tǒng)一異常處理最佳實踐

    在 Web 開發(fā)中, 我們經(jīng)常會需要處理各種異常,這篇文章主要介紹了詳解Spring MVC/Boot 統(tǒng)一異常處理最佳實踐,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • java中int、double、char等變量的取值范圍詳析

    java中int、double、char等變量的取值范圍詳析

    這篇文章主要給大家介紹了關(guān)于java中int、double、char等變量取值范圍的相關(guān)資料,每個變量都給出了詳細(xì)的實例代碼,對大家學(xué)習(xí)或者使用java具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2021-10-10

最新評論