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; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)