java贪吃蛇移动问题求解 1C

Snake.java------------------------------------------------------------

```import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;

public class Snake {

``````private Node head = null;
private Node tail = null;
private int size;
Node n = new Node(15, 10, Dir.L);
private Garden gd;

public Snake(Garden gd) {
tail = n;
size = 1;
this.gd = gd;
}

// 添尾
Node node = null;

switch (tail.dir) {
case L:
node = new Node(tail.x + 1, tail.y, Dir.L);
break;
case R:
node = new Node(tail.x - 1, tail.y, Dir.R);
break;
case U:
node = new Node(tail.x, tail.y + 1, Dir.U);
break;
case D:
node = new Node(tail.x, tail.y - 1, Dir.L);
break;
}
node.prev = tail;
tail.next = node;
tail = node;

size++;
}

// 去尾
public void deleteTail() {
if (size == 0)
return;
tail = tail.prev;
tail.next = null;

}

// 添头

Node node = null;

case L:
break;
case R:
break;
case U:
break;
case D:
break;
}
size++;
}

public void move() {
if (size == 0)
return;
deleteTail();
}

Node n1 = new Node(n.x-5, n.y, n.dir);
Node n2 = new Node(n.x-10, n.y, n.dir);
public void draw(Graphics g) {
if (size <= 0)
return;
move();
for (Node theNode = head; theNode != null; theNode = theNode.next) {
theNode.draw(g);
}
}

private class Node {
private int x, y;
Dir dir = null;
private int w = Garden.wide;
Node next = null;
Node prev = null;

public Node(int x, int y, Dir dir) {
this.x = x;
this.y = y;
this.dir = dir;
}

public void draw(Graphics g) {
Color c = g.getColor();
g.setColor(Color.RED);
g.fillRect(x * w, y * w, w, w);
g.setColor(c);
}
}

public void KeyPressed(KeyEvent e) {
int key = e.getKeyCode();
switch (key) {
case KeyEvent.VK_W:
break;
case KeyEvent.VK_S:
break;
case KeyEvent.VK_A:
break;
case KeyEvent.VK_D:
break;
}
}
``````

}

``````Garden.java-----------------------------------------------------------------------

import java.awt.*;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Garden extends JPanel {
private JFrame jf = null;
// 格子大小
public static int wide = 20;
// 窗口大小
private int gWidth = 500;
private int gHeigth = 500;

Snake s = new Snake(this);
Egg e = new Egg();

public static void main(String[] args) {
new Garden().init();
}

// 初始化窗口
private void init() {
jf = new JFrame();
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
jf.setLocation(20, 20);
jf.setSize(gWidth + 7, gHeigth + 30);
jf.setResizable(false);
jf.setVisible(true);

}

public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(Color.GREEN);
// 画格子
for (int i = 0; i < gHeigth; i += wide) {
for (int j = 0; j < gWidth; j += wide) {
g.drawRect(i, j, wide, wide);
}
}
g.setColor(c);
s.draw(g);
e.drawEgg(g);
}

private class PaintThread implements Runnable {

@Override
public void run() {
while(true) {
repaint();
try {
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

}

public class KeyMonitor extends KeyAdapter {

@Override
public void keyPressed(KeyEvent e) {
s.KeyPressed(e);
}

}
}

Dir.java---------------------------------------------------------------------------

public enum Dir {
L,R,U,D;
}

``````

1个回答

Tiger_Zhao 回复千叶鸟: 这也是一种方案。看你原先的代码以为是不想全部重绘，所以要加个`lastTail.clear(g)`

Tiger_Zhao 更正：`lastTail.clear(g)`