波多野结衣 蜜桃视频,国产在线精品露脸ponn,a v麻豆成人,AV在线免费小电影

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

本文介紹了在Java中玩蛇游戲,但我的重新啟動按鈕不起作用的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我的游戲重新啟動按鈕不起作用,當它被點擊時它會加倍。我不太懂Java,我認為自己很好。

游戲主打

package snake_game;

public class snake {

  public static void main(String arg[]) {

    new GameFrame();
    // is exacly the same as frame f = new frame();
    // this is shorter and does the same job

  }
}

GamePanel

package snake_game;

// import java.awt.event.ActionEvent;
// import java.awt.event.ActionListener;
// import java.awt.Graphics;
// import java.awt.event.KeyAdapter;
// import java.awt.event.KeyEvent;
// or I could write simply 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;

import javax.swing.JPanel;

public class GamePanel extends JPanel implements ActionListener {

  // panal dimentions
  static final int SCREEN_WIDTH = 600;
  static final int SCREEN_HEIGHT = 600;
  // panal dimentions
  // size
  static final int UNIT_SIZE = 25;
  // size to make 600 * 600 = 1200 px equel between 25 px
  static final int GAME_UNITS = (SCREEN_WIDTH * SCREEN_HEIGHT) / UNIT_SIZE;
  // size
  // delay how fast the game will be
  static final int DELAY = 75;
  // delay
  // dimentions
  final int x[] = new int[GAME_UNITS];
  final int y[] = new int[GAME_UNITS];
  // dimentions
  // snake
  int bodyParts = 6;
  // snake
  // apple
  int appleEaten;
  int appleX;
  int appleY;
  // apple
  char direction = 'R';
  boolean running = false;
  Timer timer;
  Random random;
  GamePanel game;
  JButton resetButton;

  GamePanel() {

    random = new Random();
    this.setPreferredSize(new Dimension(SCREEN_WIDTH, SCREEN_HEIGHT));
    this.setBackground(Color.black);
    this.setFocusable(true);
    this.addKeyListener(new MyKeyAdapter());
    startGame();
    // when we want to make the program to continie we must say what the programm
    // must execute next

  }

  public void startGame() {
    newApple();
    running = true;
    timer = new Timer(DELAY, this);
    timer.start();
  }

  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    draw(g);

  }

  public void draw(Graphics g) {

    if (running) {

      for (int i = 0; i < SCREEN_HEIGHT / UNIT_SIZE; i++) {
        g.drawLine(i * UNIT_SIZE, 0, i * UNIT_SIZE, SCREEN_HEIGHT);
        g.drawLine(0, i * UNIT_SIZE, i * SCREEN_WIDTH, i * UNIT_SIZE);

      }
      g.setColor(Color.red);
      g.fillOval(appleX, appleY, UNIT_SIZE, UNIT_SIZE);

      for (int i = 0; i < bodyParts; i++) {
        if (i == 0) {
          g.setColor(Color.green);
          g.fillRect(x[i], y[i], UNIT_SIZE, UNIT_SIZE);
        } else {
          g.setColor(new Color(45, 180, 0));
          // random color
          g.setColor(new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255)));
          // random color
          g.fillRect(x[i], y[i], UNIT_SIZE, UNIT_SIZE);
        }

      }
      g.setColor(Color.red);
      g.setFont(new Font("Ink Free", Font.BOLD, 30));
      FontMetrics metrics = getFontMetrics(g.getFont());
      g.drawString("SCORE:" + appleEaten, (SCREEN_WIDTH - metrics.stringWidth("SCORE:" + appleEaten)) / 2,
          g.getFont().getSize());
    } else {
      gameOver(g);
    }
  }

  public void newApple() {
    appleX = random.nextInt((int) (SCREEN_WIDTH / UNIT_SIZE)) * UNIT_SIZE;
    appleY = random.nextInt((int) (SCREEN_HEIGHT / UNIT_SIZE)) * UNIT_SIZE;
  }

  public void move() {

    for (int i = bodyParts; i > 0; i--) {
      x[i] = x[i - 1];
      y[i] = y[i - 1];

    }
    switch (direction) {
      case 'U':
        y[0] = y[0] - UNIT_SIZE;
        break;
      case 'D':
        y[0] = y[0] + UNIT_SIZE;
        break;
      case 'L':
        x[0] = x[0] - UNIT_SIZE;
        break;
      case 'R':
        x[0] = x[0] + UNIT_SIZE;
        break;
    }

  }

  public void checkApple() {
    if ((x[0] == appleX) && (y[0] == appleY)) {
      bodyParts++;
      appleEaten++;
      newApple();
    }
  }

  public void checkCollisions() {
    // check if head collides with body
    for (int i = bodyParts; i > 0; i--) {
      if ((x[0] == x[i]) && (y[0] == y[i])) {
        running = false;
      }

    }
    // check if head touches left border
    if (x[0] < 0) {
      running = false;
    }

    // check if head touches right border
    if (x[0] > SCREEN_WIDTH) {
      running = false;
    }
    // check if head touches top border
    if (y[0] < 0) {
      running = false;
    }
    // check if head touches bottom border
    if (y[0] > SCREEN_HEIGHT) {
      running = false;
    }
    if (!running) {
      timer.stop();
    }
  }

  public void gameOver(Graphics g) {
    // score
    g.setColor(Color.red);
    g.setFont(new Font("Ink Free", Font.BOLD, 30));
    FontMetrics metrics1 = getFontMetrics(g.getFont());
    g.drawString("SCORE:" + appleEaten, (SCREEN_WIDTH - metrics1.stringWidth("SCORE:" + appleEaten)) / 2,
        g.getFont().getSize());
    // game over text
    g.setColor(Color.red);
    g.setFont(new Font("Ink Free", Font.BOLD, 75));
    FontMetrics metrics2 = getFontMetrics(g.getFont());
    g.drawString("Game Over", (SCREEN_WIDTH - metrics2.stringWidth("Game Over")) / 2, SCREEN_HEIGHT / 2);
    // restart button
    resetButton = new JButton();
    resetButton.setText("Restart");
    resetButton.setSize(100, 50);
    resetButton.setLocation(150, 150);
    resetButton.addActionListener(this);

    game = new GamePanel();

    this.add(resetButton);
    this.add(game);
    this.setVisible(true);
    // restart button
  }

  @Override
  public void actionPerformed(ActionEvent e) {

    if (running) {
      move();
      checkApple();
      checkCollisions();

    }
    repaint();

    // restart button
    if (e.getSource() == resetButton) {
      this.remove(game);

      game = new GamePanel();
      this.add(game);
      resetButton.setVisible(false);
      SwingUtilities.updateComponentTreeUI(this);
      // restart button

    }

  }

  public class MyKeyAdapter extends KeyAdapter {

    @Override
    public void keyPressed(KeyEvent e) {

      switch (e.getKeyCode()) {
        case KeyEvent.VK_LEFT:
          if (direction != 'R') {
            direction = 'L';
          }
          break;
        case KeyEvent.VK_RIGHT:
          if (direction != 'L') {
            direction = 'R';
          }
          break;
        case KeyEvent.VK_UP:
          if (direction != 'D') {
            direction = 'U';
          }
          break;
        case KeyEvent.VK_DOWN:
          if (direction != 'U') {
            direction = 'D';
          }
          break;

      }

    }

  }

}

GameFrame

package snake_game;

import javax.swing.JFrame;

public class GameFrame extends JFrame {

  GameFrame() {
    GamePanel panel = new GamePanel();
    this.add(panel);
    this.setTitle("Snake");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setResizable(false);
    this.pack();
    this.setVisible(true);
    this.setLocationRelativeTo(null);
    this.setUndecorated(false);
  }
}

推薦答案

簡介

我將您的代碼復制到我的Eclipse IDE中,并按原樣運行。我收到以下運行時錯誤。

Exception in thread "main" java.awt.IllegalComponentStateException: The frame is displayable.
    at java.desktop/java.awt.Frame.setUndecorated(Frame.java:926)
    at com.ggl.testing.SnakeGame$GameFrame.<init>(SnakeGame.java:41)
    at com.ggl.testing.SnakeGame.main(SnakeGame.java:24)

Oracle有一個有用的教程Creating a GUI With Swing。跳過使用NetBeans IDE學習Swing一節。請密切關注Concurrency in Swing部分。

我編寫Swing代碼已經有10多年了,我在瀏覽器中為Oracle網站添加了書簽。我仍然會查找如何使用某些組件,以確保正確使用它們。

我做的第一件事是讓你的蛇慢下來,這樣我就可以測試游戲了。我把延遲從75改成了750。這是您當前圖形用戶界面的屏幕截圖。

查看您的代碼,您擴展了一個JFrame。您不需要擴展JFrame。您沒有更改任何JFrame功能。使用JFrame要簡單得多。這導致了我的一條Java規則。

除非您有意,否則不要擴展Swing組件或任何Java類
重寫一個或多個類方法。

您確實擴展了JPanel。這很好,因為您覆蓋了paintComponent方法。

最后,您的JPanel類做了太多工作。您還大量使用靜態字段。盡管您將只創建一個JPanel,但將每個類視為將創建該類的多個實例是一個好習慣。這樣以后給自己帶來的問題就更少了。

您的想法很正確,創建了三個類。

讓我們使用一些基本模式和Swing最佳實踐重新編寫您的代碼。此時,我不知道我們最終將創建多少個類。

說明

編寫Swing圖形用戶界面時,我使用model–view–controller(MVC)模式。這個名稱意味著您首先創建模型,然后創建視圖,然后創建控制器。

應用程序模型由一個或多個純Java getter/setter類組成。

視圖由JFrame、一個或多個JPanels以及任何其他必要的Swing組件組成。

控制器由一個或多個ActionsActionListeners組成。在Swing中,通常不存在一個控制器來統治所有這些控制器。

總結:

該視圖從模型中讀取信息
視圖不更新模型
控制器更新模型并重新繪制/重新驗證您的視圖。

型號

我創建了兩個模型類,SnakeModelSnake

SnakeModel類是一個普通的Java getter/setter類,它包含一個Snake實例、吃蘋果的數量、蘋果的位置、游戲區域的大小和幾個布爾值。一個布爾值指示游戲循環是否正在運行,另一個布爾值指示游戲是否結束。

游戲區域使用java.awt.Dimension來保持游戲區域的寬度和高度。寬度和高度不必具有相同的值。游戲區域可以是矩形的。

游戲面積以單位計量。在該視圖中,我將單位轉換為像素。這與你的所作所為正好相反。如果要更改游戲區域,只需更改SnakeModel類中的尺寸。視圖中的所有內容都基于游戲區域尺寸。

Snake類包含java.util.Listjava.awt.Point對象和一個char方向。java.awt.Point對象包含X和Y值。因為我們處理的是對象,而不是int值,所以當我們需要一個新的Point時,必須小心克隆對象。

查看

所有Swing應用程序都必須從調用SwingUtilitiesinvokeLater方法開始。此方法確保在事件調度線程上創建和執行Swing組件。

我創建了一個JFrame、一個繪圖JPanel和一個單獨的按鈕JPanel。通常,將Swing組件添加到繪圖JPanel不是一個好主意。通過創建一個單獨的按鈕JPanel,我幾乎不需要額外的成本就可以獲得&quot;Start Game&quot;按鈕的附加功能。該按鈕在游戲運行時被禁用。

JFrame方法必須按特定順序調用。必須上次調用setVisible方法

我為分數添加了單獨的區域,從而使繪圖JPanel變得更加復雜。

我根據應用程序模型只繪制了游戲狀態,從而使繪圖JPanel變得不那么復雜。句號。別無他法。

我將隨機顏色限制在光譜的白色端,以保持蛇和繪畫背景之間的對比度JPanel

我使用了鍵綁定,而不是鍵偵聽器。一個優點是JPanel不需要對焦。因為我有一個單獨的按鈕JPanel,所以繪圖JPanel沒有焦點。

另一個好處是我可以用四行額外的代碼添加WASD鍵。

一個缺點是鍵綁定代碼看起來比鍵偵聽器更復雜。一旦您編寫了幾個鍵綁定,您就會意識到它的優勢。

控制器

我創建了三個控制器類,ButtonListenerTimerListenerMovementAction

類實現ActionListenerButtonListener類初始化游戲模型并重新啟動計時器。

類實現ActionListenerTimerListener類是游戲循環。這個類移動蛇,檢查蘋果是否被吃掉,檢查蛇是否移動到游戲區域之外或觸摸自己,并重新繪制繪圖JPanel。我將您的代碼用作此類代碼的模型。

MovementAction類擴展AbstractActionAbstractAction類實現Action。此類根據按鍵更改蛇的方向。

我創建了MovementAction類的四個實例,每個方向一個。這使得類的actionPerformed方法更加簡單。

圖像

以下是您開始游戲時修改后的圖形用戶界面的外觀。

這是游戲期間修改后的圖形用戶界面。

這是游戲結束后修改后的圖形用戶界面。

代碼

以下是完整的可運行代碼。我將所有額外的類都放在內部類中,這樣我就可以將這段代碼作為一個塊發布。

您應該將單獨的類放在單獨的文件中。

在設置Swing圖形用戶界面項目時,我為模型、視圖和控制器創建了單獨的包。這有助于我保持代碼的條理性。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.BorderFactory;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class SnakeGame implements Runnable {

    public static void main(String arg[]) {
        SwingUtilities.invokeLater(new SnakeGame());
    }
    
    private final GamePanel gamePanel;
    
    private final JButton restartButton;
    
    private final SnakeModel model;
    
    public SnakeGame() {
        this.model = new SnakeModel();
        this.restartButton = new JButton("Start Game");
        this.gamePanel = new GamePanel(model);
    }

    @Override
    public void run() {
        JFrame frame = new JFrame("Snake");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        frame.add(gamePanel, BorderLayout.CENTER);
        frame.add(createButtonPanel(), BorderLayout.SOUTH);
        
        frame.pack();
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
    
    private JPanel createButtonPanel() {
        JPanel panel = new JPanel();
        panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        panel.setBackground(Color.black);
        
        restartButton.addActionListener(new ButtonListener(this, model));
        panel.add(restartButton);
        
        return panel;
    }
    
    public JButton getRestartButton() {
        return restartButton;
    }

    public void repaint() {
        gamePanel.repaint();
    }
    
    public class GamePanel extends JPanel {

        private static final long serialVersionUID = 1L;
        
        private final int margin, scoreAreaHeight, unitSize;
        
        private final Random random;
        
        private final SnakeModel model;
        
        public GamePanel(SnakeModel model) {
            this.model = model;
            this.margin = 10;
            this.unitSize = 25;
            this.scoreAreaHeight = 36 + margin;
            this.random = new Random();
            this.setBackground(Color.black);
            
            Dimension gameArea = model.getGameArea();
            int width = gameArea.width * unitSize + 2 * margin;
            int height = gameArea.height * unitSize + 2 * margin + scoreAreaHeight;
            this.setPreferredSize(new Dimension(width, height));
            setKeyBindings();
        }
        
        private void setKeyBindings() {
            InputMap inputMap = this.getInputMap(JPanel.WHEN_IN_FOCUSED_WINDOW);
            ActionMap actionMap = this.getActionMap();
            
            inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "up");
            inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "down");
            inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "left");
            inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "right");
            
            inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_W, 0), "up");
            inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_S, 0), "down");
            inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0), "left");
            inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0), "right");
            
            actionMap.put("up", new MovementAction(model, 'U', 'D'));
            actionMap.put("down", new MovementAction(model, 'D', 'U'));
            actionMap.put("left", new MovementAction(model, 'L', 'R'));
            actionMap.put("right", new MovementAction(model, 'R', 'L'));
        }
        
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            
            Dimension gameArea = model.getGameArea();
            drawHorizontalGridLines(g, gameArea);
            drawVerticalGridLines(g, gameArea);
            drawSnake(g);
            drawScore(g, gameArea);
            
            if (model.isGameOver) {
                drawGameOver(g, gameArea);
            } else {
                drawApple(g);
            }
        }

        private void drawHorizontalGridLines(Graphics g, Dimension gameArea) {
            int y1 = scoreAreaHeight + margin;
            int y2 = y1 + gameArea.height * unitSize;
            int x = margin;
            for (int index = 0; index <= gameArea.width; index++) {
                g.drawLine(x, y1, x, y2);
                x += unitSize;
            }
        }

        private void drawVerticalGridLines(Graphics g, Dimension gameArea) {
            int x1 = margin;
            int x2 = x1 + gameArea.width * unitSize;
            int y = margin + scoreAreaHeight;
            for (int index = 0; index <= gameArea.height; index++) {
                g.drawLine(x1, y, x2, y);
                y += unitSize;
            }
        }

        private void drawApple(Graphics g) {
            // Draw apple
            g.setColor(Color.red);
            Point point = model.getAppleLocation();
            if (point != null) {
                int a = point.x * unitSize + margin + 1;
                int b = point.y * unitSize + margin + scoreAreaHeight + 1;
                g.fillOval(a, b, unitSize - 2, unitSize - 2);
            }
        }

        private void drawScore(Graphics g, Dimension gameArea) {
            g.setColor(Color.red);
            g.setFont(new Font("Ink Free", Font.BOLD, 36));
            FontMetrics metrics = getFontMetrics(g.getFont());
            int width = 2 * margin + gameArea.width * unitSize;
            String text = "SCORE: " + model.getApplesEaten();
            int textWidth = metrics.stringWidth(text);
            g.drawString(text, (width - textWidth) / 2, g.getFont().getSize());
        }
        
        private void drawSnake(Graphics g) {
            // Draw snake
            Snake snake = model.getSnake();
            List<Point> cells = snake.getCells();
            Point cell = cells.get(0);
            drawSnakeCell(g, cell, Color.green);
            for (int index = 1; index < cells.size(); index++) {
//              Color color = new Color(45, 180, 0);
                // random color
                Color color = new Color(getColorValue(), getColorValue(), 
                        getColorValue());
                cell = cells.get(index);
                drawSnakeCell(g, cell, color);
            }
        }
        
        private void drawSnakeCell(Graphics g, Point point, Color color) {
            int x = margin + point.x * unitSize;
            int y = margin + scoreAreaHeight + point.y * unitSize;
            if (point.y >= 0) {
                g.setColor(color);
                g.fillRect(x, y, unitSize, unitSize);
            }
        }
        
        private int getColorValue() {
            // White has color values of 255
            return random.nextInt(64) + 191;
        }
        
        private void drawGameOver(Graphics g, Dimension gameArea) {
            g.setColor(Color.red);
            g.setFont(new Font("Ink Free", Font.BOLD, 72));
            FontMetrics metrics = getFontMetrics(g.getFont());
            String text = "Game Over";
            int textWidth = metrics.stringWidth(text);
            g.drawString(text, (getWidth() - textWidth) / 2, getHeight() / 2);
        }
        
    }
    
    public class ButtonListener implements ActionListener {
        
        private final int delay;
        
        private final SnakeGame view;
        
        private final SnakeModel model;
        
        private final Timer timer;

        public ButtonListener(SnakeGame view, SnakeModel model) {
            this.view = view;
            this.model = model;
            this.delay = 750;
            this.timer = new Timer(delay, new TimerListener(view, model));
        }

        @Override
        public void actionPerformed(ActionEvent event) {
            JButton button = (JButton) event.getSource();
            String text = button.getText();
            
            if (text.equals("Start Game")) {
                button.setText("Restart Game");
            } 
            
            button.setEnabled(false);
            model.initialize();
            timer.restart();
        }
        
    }
    
    public class TimerListener implements ActionListener {
        
        private final SnakeGame view;
        
        private final SnakeModel model;

        public TimerListener(SnakeGame view, SnakeModel model) {
            this.view = view;
            this.model = model;
        }

        @Override
        public void actionPerformed(ActionEvent event) {
            moveSnake();
            checkApple();
            model.checkCollisions();
            if (model.isGameOver()) {
                Timer timer = (Timer) event.getSource();
                timer.stop();
                model.setRunning(false);
                view.getRestartButton().setEnabled(true);
            }
            view.repaint();
        }
        
        private void moveSnake() {
            Snake snake = model.getSnake();
            Point head = (Point) snake.getHead().clone();
            
            switch (snake.getDirection()) {
            case 'U':
                head.y--;
                break;
            case 'D':
                head.y++;
                break;
            case 'L':
                head.x--;
                break;
            case 'R':
                head.x++;
                break;
            }
            
            snake.removeTail();
            snake.addHead(head);
            
//          System.out.println(Arrays.toString(cells.toArray()));
        }
        
        private void checkApple() {
            Point appleLocation = model.getAppleLocation();
            Snake snake = model.getSnake();
            Point head = snake.getHead();
            Point tail = (Point) snake.getTail().clone();
            
            if (head.x == appleLocation.x && head.y == appleLocation.y) {
                model.incrementApplesEaten();
                snake.addTail(tail);
                model.generateRandomAppleLocation();
            }
        }
        
    }
    
    public class MovementAction extends AbstractAction {

        private static final long serialVersionUID = 1L;
        
        private final char newDirection, oppositeDirection;
        
        private final SnakeModel model;

        public MovementAction(SnakeModel model, char newDirection,
                char oppositeDirection) {
            this.model = model;
            this.newDirection = newDirection;
            this.oppositeDirection = oppositeDirection;
        }

        @Override
        public void actionPerformed(ActionEvent event) {
            if (model.isRunning()) {
                Snake snake = model.getSnake();
                char direction = snake.getDirection();
                if (direction != oppositeDirection && direction != newDirection) {
                    snake.setDirection(newDirection);
//                  System.out.println("New direction: " + newDirection);
                }
            }
        }
        
    }
    
    public class SnakeModel {
        
        private boolean isGameOver, isRunning;
        
        private int applesEaten;
        
        private Dimension gameArea;
        
        private Point appleLocation;
        
        private Random random;
        
        private Snake snake;
        
        public SnakeModel() {
            this.random = new Random();
            this.snake = new Snake();
            this.gameArea = new Dimension(24, 24);
        }
        
        public void initialize() {
            this.isRunning = true;
            this.isGameOver = false;
            this.snake.initialize();
            this.applesEaten = 0;
            
            Point point = generateRandomAppleLocation();
            // Make sure first apple isn't under snake
            int y = (point.y == 0) ? 1 : point.y;
            this.appleLocation = new Point(point.x, y);
        }
        
        public void checkCollisions() {
            Point head = snake.getHead();
            
            // Check for snake going out of the game area
            if (head.x < 0 || head.x > gameArea.width) {
                isGameOver = true;
                return;
            }
            
            if (head.y < 0 || head.y > gameArea.height) {
                isGameOver = true;
                return;
            }
            
            // Check for snake touching itself
            List<Point> cells = snake.getCells();
            for (int index = 1; index < cells.size(); index++) {
                Point cell = cells.get(index);
                if (head.x == cell.x && head.y == cell.y) {
                    isGameOver = true;
                    return;
                }
            }
        }
        
        public Point generateRandomAppleLocation() {
            int x = random.nextInt(gameArea.width);
            int y = random.nextInt(gameArea.height);
            this.appleLocation = new Point(x, y);
            return getAppleLocation();
        }
        
        public void incrementApplesEaten() {
            this.applesEaten++;
        }
        
        public boolean isRunning() {
            return isRunning;
        }

        public void setRunning(boolean isRunning) {
            this.isRunning = isRunning;
        }

        public boolean isGameOver() {
            return isGameOver;
        }

        public void setGameOver(boolean isGameOver) {
            this.isGameOver = isGameOver;
        }

        public Dimension getGameArea() {
            return gameArea;
        }

        public int getApplesEaten() {
            return applesEaten;
        }

        public Point getAppleLocation() {
            return appleLocation;
        }

        public Snake getSnake() {
            return snake;
        }
        
    }
    
    public class Snake {
        
        private char direction;
        
        private List<Point> cells;
        
        public Snake() {
            this.cells = new ArrayList<>();
            initialize();
        }
        
        public void initialize() {
            this.direction = 'R';
            cells.clear();
            for (int x = 5; x >= 0; x--) {
                cells.add(new Point(x, 0));
            }
        }

        public void addHead(Point head) {
            cells.add(0, head);
        }
        
        public void addTail(Point tail) {
            cells.add(tail);
        }
        
        public void removeTail() {
            cells.remove(cells.size() - 1);
        }
        
        public Point getHead() {
            return cells.get(0);
        }
        
        public Point getTail() {
            return cells.get(cells.size() - 1);
        }

        public char getDirection() {
            return direction;
        }

        public void setDirection(char direction) {
            this.direction = direction;
        }

        public List<Point> getCells() {
            return cells;
        }
        
    }

}

這篇關于在Java中玩蛇游戲,但我的重新啟動按鈕不起作用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,

分享到:
標簽:Java 不起作用 中玩蛇 按鈕 游戲 重新啟動
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網站吧!
最新入駐小程序

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定