游戏截图
代码 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;
public MainFame() {
initFrame();
initGamePanel();
initSnake();
initFood();
setVisible(true);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
setKyeListener();
}
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);
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);
}
}
};
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);
}
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);
}
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);
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;
public Snake(int y) {
initSnake(y);
}
public void move(Snake snake1) {
if (islive) {
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;
}
int t = snake1.getBody().size();
for (int i = 0; i < t; i++) {
Node node = snake1.getBody().get(i);
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));
}
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 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;
}