package snack;import java.awt.*;
import java.awt.event.*;public class yard extends Frame { public static int col = 50; public static int row = 50; public static int Block_size = 10; Snack s = new Snack(); Image offScreamImage = null; private static final long serialVersionUID = 1L; public static void main(String args[]) {
new yard().launch();
} public void launch() {
this.setLocation(300, 400);
this.setSize(col * Block_size, row * Block_size);
// this.setBackground(Color.YELLOW);
this.setVisible(true);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}); } public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(Color.gray);
g.fillRect(0, 0, col * Block_size, row * Block_size);
g.setColor(Color.DARK_GRAY);
for (int i = 0; i < row; i++) {
g.drawLine(0, Block_size * i, col * Block_size, Block_size * i);
}
for (int i = 0; i < col; i++) {
g.drawLine(Block_size * i, 0, i * Block_size, Block_size * row);
}
g.setColor(c); s.draw(g); new Thread(new paintThread()).start(); } public void update(Graphics g) { if (offScreamImage == null) { offScreamImage = this.createImage(col * Block_size, row
* Block_size);
} Graphics ofg = offScreamImage.getGraphics(); paint(ofg); g.drawImage(offScreamImage, 0, 0, null); } private class paintThread implements Runnable { public void run() {
while (true) {
repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
} }
    }
private class KeyMonitor extends KeyAdapter
{ public void keyPressed(KeyEvent e) {
s.keyPressed(e);

}

}}
package snack;import java.awt.*;
import java.awt.event.*;public class Snack{
Node head = null; Node tail = null; int size = 0; Node n = new Node(20, 40,Dir.R); public Snack(Node node) {
head = node; tail = node; size = 1;
} public Snack() {

head = n; tail = n; size = 1; } public void addTOtail() { Node node = null; switch (tail.dir) {
case L: {
node = new Node(tail.row, tail.col + 1, tail.dir);
break;
}
case U: {
node = new Node(tail.row + 1, tail.col, tail.dir);
break;
}
case D: {
node = new Node(tail.row - 1, tail.col, tail.dir);
break;
}
case R: {
node = new Node(tail.row, tail.col - 1, tail.dir);
break; }
}
tail.next = node;

    node.prev = tail;

tail = node;
} public void addTOHead() {
Node node = null; switch (head.dir) {
case L: {
node = new Node(head.row, head.col - 1, head.dir);
break;
}
case U: {
node = new Node(head.row- 1, head.col, head.dir);
break;
}
case D: {
node = new Node(head.row + 1, head.col, head.dir);
break;
}
case R: {
node = new Node(head.row, head.col + 1, head.dir);
break; }
}
node.next = head;

head.prev = node;

head = node;
} public void draw(Graphics g) {
if (size <0) { return; } for (Node n = head; n != null; n = n.next) {
n.draw(g);
}
} private class Node {
int w = yard.Block_size; int h = yard.Block_size; int row, col; Dir dir = Dir.L; Node next = null; Node prev = null; public Node(int row, int col, Dir dir) { this.row = row; this.col = col; }
public void draw(Graphics g) {

Color c = g.getColor(); g.setColor(Color.blue); g.fillRect(yard.Block_size * col, yard.Block_size * row, w, h); g.setColor(c); move();
} public void move() { addTOHead();

deleteFromTail();

} private void deleteFromTail() { if (size == 0) {
return;
}

tail = tail.prev;

tail.next = null; }
} public void keyPressed(KeyEvent e) { int key = e.getKeyCode();

switch (key)
{
case KeyEvent.VK_LEFT:
head.dir = Dir.L;
break;
case KeyEvent.VK_RIGHT:
head.dir = Dir.R;
break;
case KeyEvent.VK_UP:
head.dir = Dir.U;
break;
case KeyEvent.VK_DOWN:
head.dir = Dir.D;
break;
}
}
}一个贪吃蛇的游戏雏形但是蛇的身体总是移动的不对,不是不移动就是一直向右上下键控制不住他!!!这是什么原因????还有这是什么造成的!!!在线等大虾帮助!!!立給20分!!!