1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關(guān)咨詢
      選擇下列產(chǎn)品馬上在線溝通
      服務(wù)時間:8:30-17:00
      你可能遇到了下面的問題
      關(guān)閉右側(cè)工具欄

      新聞中心

      這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
      GUI怎么制作java貪吃蛇游戲

      這篇文章主要講解了“GUI怎么制作java貪吃蛇游戲”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“GUI怎么制作java貪吃蛇游戲”吧!

      創(chuàng)新互聯(lián)長期為上千家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為新?lián)崞髽I(yè)提供專業(yè)的成都做網(wǎng)站、成都網(wǎng)站建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)新?lián)峋W(wǎng)站改版等技術(shù)服務(wù)。擁有十多年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。

      項目結(jié)構(gòu)

      GUI怎么制作java貪吃蛇游戲

      新建一個JFrame窗口,作為程序入口

      public class GameStart{
       public static void main(String[] args) {
        JFrame jFrame = new JFrame();
        jFrame.setBounds(100,100,900,720);
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jFrame.setResizable(false);
        jFrame.add(new GameJPanel());
        jFrame.setVisible(true);
       }
      }

      創(chuàng)建一個面板類

      //面板
      public class GameJPanel extends JPanel implements ActionListener {
      
       int length;//蛇的長度
       int[] snakeX = new int[600];//蛇的X的最大長度
       int[] snakeY = new int[500];//蛇的Y的最大長度
       String fx = "R";
       boolean isStart = false;//默認(rèn)不開始
       //定時器
       Timer timer = new Timer(100, this);//100毫秒=1秒
       int foodX;
       int foodY;
       Random random = new Random();//隨機(jī)數(shù)
       boolean isFail = false;//失敗條件
       int score;
      
       public GameJPanel() {
        init();
        this.setFocusable(true);//獲取焦點事件
        addKeyListener(new GameKeyListener(this));
        //開啟定時器
        timer.start();
       }
       //初始化
       void init() {
        length = 3;
        snakeX[0] = 100;
        snakeY[0] = 100;//第一個身體
        snakeX[1] = 75;
        snakeY[1] = 100;//第二個身體
        snakeX[2] = 50;
        snakeY[2] = 100;//第三個身體
        fx = "R";
        //食物隨機(jī)分布
        foodX = 25 + 25 * random.nextInt(34);
        foodY = 75 + 25 * random.nextInt(24);
        score = 0;
       }
       //繪制面板,所有東西都是通過graphics這個畫筆繪制
       @Override
       protected void paintComponent(Graphics graphics) {
        super.paintComponent(graphics);//清屏
        //添加靜態(tài)布局
        GameData.header.paintIcon(this, graphics, 25, 11);
        graphics.fillRect(25, 75, 850, 600);
        //積分板
        graphics.setColor(Color.white);
        graphics.setFont(new Font("微軟雅黑", Font.BOLD, 18));
        graphics.drawString("長度:"+length,750,35);
        graphics.drawString("得分:"+score,750,55);
        //先畫食物,防止吃掉有延遲
        GameData.food.paintIcon(this, graphics, foodX, foodY);
        //畫小蛇
        switch (fx) {
         case "R":
          GameData.right.paintIcon(this, graphics, snakeX[0], snakeY[0]);
          break;
         case "L":
          GameData.left.paintIcon(this, graphics, snakeX[0], snakeY[0]);
          break;
         case "U":
          GameData.up.paintIcon(this, graphics, snakeX[0], snakeY[0]);
          break;
         case "D":
          GameData.down.paintIcon(this, graphics, snakeX[0], snakeY[0]);
          break;
        }
        //身體
        for (int i = 1; i < length; i++) {
         GameData.body.paintIcon(this, graphics, snakeX[i], snakeY[i]);
        }
        //游戲狀態(tài)
        if (!isStart) {
         graphics.setColor(Color.white);
         graphics.setFont(new Font("微軟雅黑", Font.BOLD, 40));//設(shè)置字體
         graphics.drawString("按下空格,開始游戲", 300, 300);
        }
        //游戲失敗
        if (isFail) {
         graphics.setColor(Color.red);
         graphics.setFont(new Font("微軟雅黑", Font.BOLD, 40));
         graphics.drawString("游戲失敗,請按空格繼續(xù)", 300, 300);
        }
       }
       //事件監(jiān)聽--固定事件刷新一次,1s=100ms
       @Override
       public void actionPerformed(ActionEvent actionEvent) {
        if (isStart && !isFail) {
         //吃食物
         if (snakeX[0] == foodX && snakeY[0] == foodY) {
          //長度+1
          length++;
          score+=10;
          //重新隨機(jī)繪制食物
          foodX = 25 + 25 * random.nextInt(34);
          foodY = 75 + 25 * random.nextInt(24);
         }
         //后一節(jié)移動到前一節(jié),從而由頭帶動身體移動
         for (int i = length - 1; i > 0; i--) {
          snakeX[i] = snakeX[i - 1];
          snakeY[i] = snakeY[i - 1];
         }
         switch (fx) {
          case "R":
           snakeX[0] += 25;//頭部移動
           // 邊界判斷
           if (snakeX[0] > 850) { snakeX[0] = 25; }
           break;
          case "L":
           snakeX[0] -= 25;//頭部移動
           if (snakeX[0] < 25) { snakeX[0] = 850; }
           break;
          case "U":
           snakeY[0] -= 25;//頭部移動
           if (snakeY[0] < 75) { snakeY[0] = 650; }
           break;
          case "D":
           snakeY[0] += 25;//頭部移動
           if (snakeY[0] > 650) { snakeY[0] = 75; }
           break;
         }
         //失敗判定
         for (int i = 1; i < length; i++) {
          if (snakeX[0] == snakeX[i] && snakeY[0] == snakeY[i]) {
           isFail = true;
           break;
          }
         }
         repaint();//重繪
        }
        //開啟定時器
        timer.start();
       }
      }

      所有組件添加流程基本一致,即先定義數(shù)據(jù),然后在畫筆paintComponent方法中繪制,最后添加到監(jiān)聽事件。

      感謝各位的閱讀,以上就是“GUI怎么制作java貪吃蛇游戲”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對GUI怎么制作java貪吃蛇游戲這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!


      新聞標(biāo)題:GUI怎么制作java貪吃蛇游戲
      標(biāo)題URL:http://ef60e0e.cn/article/gchsgi.html
      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
      1. <ul id="0c1fb"></ul>

        <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
        <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

        南华县| 沈阳市| 新郑市| 台州市| 嘉祥县| 延庆县| 万荣县| 神农架林区| 昌图县| 射洪县| 大丰市| 新营市| 温州市| 巫山县| 大新县| 郴州市| 梅州市| 章丘市| 醴陵市| 股票| 江川县| 繁昌县| 黄大仙区| 张掖市| 惠东县| 大冶市| 西吉县| 通辽市| 桐城市| 民勤县| 台前县| 漯河市| 阜南县| 竹山县| 永宁县| 屏山县| 将乐县| 门头沟区| 涿州市| 大埔县| 宜春市|