项目结构
MainFrame
package my; import javax.swing.*; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.util.Timer; import java.awt.*; import java.util.LinkedList; import java.util.TimerTask; public class MainFrame extends JFrame { private boolean isLiving = true; private JPanel jPanel; private Snake snake; private Timer timer; // 食物 private Node food=new Node(); public static void main(String[] args) { new MainFrame().setVisible(true); } public MainFrame() throws HeadlessException { initFrame(); initGamePanel(); initSnake(); initTimer(); setKeyListener(); initfood(); } private void initfood(){ food=new Node(30,30); food.random(); } private void setKeyListener(){ addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { switch (e.getKeyCode()){ case KeyEvent.VK_UP: if (snake.getDirection()!=Direction.DOWN){ snake.setDirection(Direction.UP); } break; case KeyEvent.VK_DOWN: if (snake.getDirection()!=Direction.UP){ snake.setDirection(Direction.DOWN); } break; case KeyEvent.VK_LEFT: if (snake.getDirection()!=Direction.RIGHT){ snake.setDirection(Direction.LEFT); } break; case KeyEvent.VK_RIGHT: if (snake.getDirection()!=Direction.LEFT){ snake.setDirection(Direction.RIGHT); } break; } } }); } private void initTimer(){ timer= new Timer(); TimerTask timerTask=new TimerTask() { @Override public void run() { snake.move(); Node head = snake.getBody().getFirst(); if(head.getX()==food.getX()&&head.getY()==food.getY()){ snake.eat(food); food.random(); } jPanel.repaint(); } }; timer.scheduleAtFixedRate(timerTask,0,100); } private void initSnake(){ snake=new Snake(); } private void initGamePanel() { jPanel = new JPanel() { @Override public void paint(Graphics g) { g.clearRect(0,0,600,600); g.setColor(Color.white); for(int i=0;i<=40;i++){ g.drawLine(0,15*i,600,15*i); } for (int i=0;i<=40;i++){ g.drawLine(15*i,0,15*i,600); } LinkedList<Node> body=snake.getBody(); g.setColor(Color.black); for (Node node:body){ g.fillRect(node.getX()*15,node.getY()*15,15,16); } g.setColor(Color.red); g.fillRect(food.getX()*15,food.getY()*15,15,15); } }; add(jPanel); } private void initFrame(){ setTitle("一条小蛇蛇"); setSize(615,640); setLocation(400,100); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setResizable(false); } }
Direction
package my; public enum Direction { UP,DOWN,LEFT,RIGHT }
Node
package my;
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(40);
this.y=r.nextInt(40);
}
}
Snake
package my;
import java.util.LinkedList;
public class Snake {
private boolean isLiving=true;
private Direction direction=Direction.LEFT;
public Direction getDirection() {
return direction;
}
public void setDirection(Direction direction) {
this.direction = direction;
}
//body
private LinkedList<Node> body;
public Snake() {
initSnake();
}
public void initSnake(){
body=new LinkedList<>();
body.add(new Node(20,20));
body.add(new Node(21,20));
body.add(new Node(22,20));
body.add(new Node(23,20));
body.add(new Node(24,20));
body.add(new Node(25,20));
}
public LinkedList<Node> getBody(){
return body;
}
public void setBody(LinkedList<Node> body){
this.body=body;
}
public void move(){
if (isLiving){
Node head=body.getFirst();
switch (direction){
case UP:
body.addFirst(new Node(head.getX(),head.getY()-1));
break;
case DOWN:
body.addFirst(new Node(head.getX(),head.getY()+1));
break;
case LEFT:
body.addFirst(new Node(head.getX()-1,head.getY()));
break;
case RIGHT:
body.addFirst(new Node(head.getX()+1,head.getY()));
break;
}
body.removeLast();
head=body.getFirst();
if (head.getX()<0||head.getY()<0||head.getY()>40||head.getX()>40){
isLiving=false;
}
for (int i=1;i<body.size();i++){
Node node=body.get(i);
if (head.getX()==node.getX()&&head.getY()==node.getY()){
isLiving=false;
}
}
}
}
public void eat(Node food){
Node head=body.getFirst();
switch (direction){
case UP:
body.addFirst(new Node(head.getX(),head.getY()-1));
break;
case DOWN:
body.addFirst(new Node(head.getX(),head.getY()+1));
break;
case LEFT:
body.addFirst(new Node(head.getX()-1,head.getY()));
break;
case RIGHT:
body.addFirst(new Node(head.getX()+1,head.getY()));
break;
}
}
}
这个是运行效果