java swing實現貪吃蛇雙人遊戲
本文實例為大傢分享瞭java swing實現貪吃蛇遊戲的具體代碼,供大傢參考,具體內容如下
遊戲截圖
代碼 Java14寫的哦低版本會報錯
文件列表
package Sys; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.LinkedList; import java.util.Timer; import java.util.TimerTask; public class MainFame extends JFrame implements Runnable { private Snake snake;//蛇 private Snake snake1;//蛇 private JPanel jpanel;//遊戲棋盤 private Node[] food;//食物 private int signSnake = 1;//標記蛇 final Object cluck = 2; private boolean moveBoolean = true; // private boolean firstTime = true; public MainFame() { // 初始化框體 initFrame(); // 初始化網格 initGamePanel(); // 初始化蛇 initSnake(); // 初始化食物 initFood(); setVisible(true); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } // 初始化定時器 // initTimer(); // 鍵盤監聽 setKyeListener(); // 啟動多線程 // Runnable R = this;----- // new Thread(R).start(); // new Thread(R).start(); } private void initFood() { food = new Node[3]; for (int i = 0; i < food.length; i++) { food[i] = new Node(); food[i].random(); } } //鍵盤監聽 private void setKyeListener() { addKeyListener(new KeyAdapter() { // 按下鍵盤 @Override public void keyPressed(KeyEvent e) { if(moveBoolean){ switch (e.getKeyCode()) { case KeyEvent.VK_UP -> snake.setDirection(Direction.UP); case KeyEvent.VK_DOWN -> snake.setDirection(Direction.DOWN); case KeyEvent.VK_LEFT -> snake.setDirection(Direction.LEFT); case KeyEvent.VK_RIGHT -> snake.setDirection(Direction.RIGHT); case KeyEvent.VK_W -> snake1.setDirection(Direction.UP); case KeyEvent.VK_S -> snake1.setDirection(Direction.DOWN); case KeyEvent.VK_A -> snake1.setDirection(Direction.LEFT); case KeyEvent.VK_D -> snake1.setDirection(Direction.RIGHT); } } moveBoolean = true; } }); } // 多線程船艦蛇 @Override public void run() { if (signSnake == 1) { signSnake++; Thread.currentThread().setName("->紅色方<-"); initTimer(snake, snake1, Thread.currentThread().getName()); } else if (signSnake == 2) { Thread.currentThread().setName("->綠色方<-"); initTimer(snake1, snake, Thread.currentThread().getName()); } } // 初始化定時器 private void initTimer(Snake snake, Snake snake1, String name) { // 創建定時器對象 //定時器 Timer timer = new Timer(); // 初始化定時任務 TimerTask timerTask = new TimerTask() { @Override public void run() { if (snake.getIslive()) { synchronized (cluck) { snake.move(snake1);//1, moveBoolean = true; } // 吃食物,臨界區處理 Node head = snake.getBody().getFirst(); for (Node node : food) { if (head.getX() == node.getX() && head.getY() == node.getY()) { snake.eat(); node.random(); } } // 更新棋盤 jpanel.repaint(); } else { timer.cancel(); System.out.println("蛇線程" + name + "結束!!!"); System.out.println("蛇線程" + name + "得分:" + snake.scor); } } }; // 每100毫秒執行一次定時任務 timer.scheduleAtFixedRate(timerTask, 0, 100); } private void initSnake() { this.snake = new Snake(18); this.snake1 = new Snake(22); } // 初始化網格 private void initGamePanel() { jpanel = new JPanel() { @Override public void paint(Graphics g) { super.paint(g); // 清空棋盤 g.clearRect(0, 0, 600, 600); // 畫線 for (int i = 0; i < 40; i++) { g.drawLine(0, 15 * i, 600, 15 * i); g.drawLine(15 * i, 0, 15 * i, 600); } // 繪制蛇 g.setColor(Color.blue); LinkedList<Node> body = snake.getBody(); for (int i = 1; i <= body.size(); i++) { if(i == body.size()){ g.setColor(Color.RED); } g.fillRect(body.get(body.size() - i).getX() * 15, body.get(body.size() - i).getY() * 15, 15, 15); } // for (Node node : body) { // if(p == 1){ // g.setColor(Color.MAGENTA); // p++; // }else{ // g.setColor(Color.RED); // } // g.fillRect(node.getX() * 15, node.getY() * 15, 15, 15); // } g.setColor(Color.PINK); LinkedList<Node> body1 = snake1.getBody(); for (int i = 1; i <= body1.size(); i++) { if(i == body1.size()){ g.setColor(Color.GREEN); } g.fillRect(body1.get(body1.size() - i).getX() * 15, body1.get(body1.size() - i).getY() * 15, 15, 15); } // for (Node node : body1) { // if(p == 1){ // g.setColor(Color.PINK); // p++; // }else{ // g.setColor(Color.GREEN); // } // g.fillRect(node.getX() * 15, node.getY() * 15, 15, 15); // } // 繪制食物 g.setColor(Color.BLACK); for (Node node : food) { g.fillRect(node.getX() * 15, node.getY() * 15, 15, 15); } } }; // 添加網格到框體中 add(jpanel); } // 初始化框體 public void initFrame() { // 標題 setTitle("胡柯洋的貪吃蛇"); // 大小 setSize(616, 639); // 不可調節大小 setResizable(false); // 設置位置 setLocation(0, 200); // 點x關閉 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void start(){ JFrame jFrame = new JFrame(); JButton jButton = new JButton("點擊開始單人遊戲"); JButton jButton1 = new JButton("點擊開始雙人遊戲"); jButton.addActionListener(e -> { Runnable R = new MainFame(); new Thread(R).start(); }); jButton1.addActionListener(e -> { Runnable R = new MainFame(); new Thread(R).start(); new Thread(R).start(); }); jFrame.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 20)); jFrame.add(jButton1); jFrame.add(jButton); jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jFrame.pack(); jFrame.setVisible(true); jFrame.setLocation(900, 200); } public static void main(String[] args) { start(); System.out.println("主線程結束!!!"); } }
package Sys; import java.util.LinkedList; public class Snake { //方向 private Direction direction = Direction.LEFT; //蛇身體 private LinkedList<Node> body; //蛇生命 private boolean islive = true; //分數 int scor = 0; // int sign; // Snake snake1; public Snake(int y) { initSnake(y); } // 有移動蛇 public void move(Snake snake1) {//int sign, if (islive) {//&& sign == 1 // 獲取蛇頭 Node head = body.getFirst(); Node newHead; body.removeLast();//去掉蛇尾 switch (direction) { case UP -> { newHead = new Node(head.getX() % 40, (head.getY() - 1 + 40) % 40); body.addFirst(newHead); } case LEFT -> { newHead = new Node((head.getX() - 1 + 40) % 40, head.getY() % 40); body.addFirst(newHead); } case DOWN -> { newHead = new Node(head.getX() % 40, (head.getY() + 1) % 40); body.addFirst(newHead); } case RIGHT -> { newHead = new Node((head.getX() + 1) % 40, head.getY() % 40); body.addFirst(newHead); } } head = body.getFirst(); if (head.getX() < 0 || head.getY() < 0 || head.getX() >= 40 || head.getY() >= 40) { this.islive = false; } // for (int i = 1; i < body.size(); i++) { // Node node = body.get(i); // if (node.getX() == head.getX() && node.getY() == head.getY()) { // this.islive = false; // } // } int t = snake1.getBody().size(); for (int i = 0; i < t; i++) { Node node = snake1.getBody().get(i);//error if (node.getX() == head.getX() && node.getY() == head.getY()) { this.islive = false; break; } } } } // 初始化蛇 private void initSnake(int y) { // 初始化蛇的實例 body = new LinkedList<>(); // 添加節點 body.add(new Node(16, y)); body.add(new Node(17, y)); body.add(new Node(18, y)); // body.add(new Node(19, 20)); // body.add(new Node(20, 20)); // body.add(new Node(21, 20)); // body.add(new Node(22, 20)); } public LinkedList<Node> getBody() { return body; } public void setDirection(Direction direction) { this.direction = direction; } public Direction getDirection() { return direction; } // 吃食物 public void eat() { scor ++; // 獲取蛇頭 Node head = body.getFirst(); Node newHead; switch (direction) { case UP -> { newHead = new Node(head.getX(), head.getY() - 1); body.addFirst(newHead); } case LEFT -> { newHead = new Node(head.getX() - 1, head.getY()); body.addFirst(newHead); } case DOWN -> { newHead = new Node(head.getX(), head.getY() + 1); body.addFirst(newHead); } case RIGHT -> { newHead = new Node(head.getX() + 1, head.getY()); body.addFirst(newHead); } } } public boolean getIslive() { return islive; } }
package Sys; import java.util.Random; public class Node { private int x; private int y; // public boolean isEat = false; public Node() { } public Node(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } public void random() { Random r = new Random(); this.x = r.nextInt(34) + 3; this.y = r.nextInt(34) + 3; } }
package Sys; public enum Direction{ UP, DOWN, LEFT, RIGHT; }
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。