在看马士兵的贪吃蛇视频,跟着视频写了一部分代码,目前可以实现只有一节的蛇移动起来,应该是可以改变方向的,但我的却只会沿着初始化的方向一直走,按方向键没反应,大神们看看问题出在哪
//Yard.java
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Yard extends Frame {
public static final int ROWS = 50;
public static final int COLS = 50;
public static final int BLOCK_SIZE = 10;
private boolean gameOver = false;
Snake s = new Snake();
Image offScreenImage = null;
public void lauch() {
this.setLocation(300, 300);
this.setSize(COLS * BLOCK_SIZE, ROWS * BLOCK_SIZE);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent arg0) {
System.exit(0);
}
});
this.setVisible(true);
new Thread(new PaintThread()).start();
}
public static void main(String[] args) {
new Yard().lauch();
}
public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(Color.GRAY);
g.fillRect(0, 0, COLS * BLOCK_SIZE, ROWS * BLOCK_SIZE);
g.setColor(Color.DARK_GRAY);
for(int i=1; i<ROWS; i++) {
g.drawLine(0, BLOCK_SIZE * i, COLS * BLOCK_SIZE, BLOCK_SIZE * i);
}
for(int i=1; i<COLS; i++) {
g.drawLine(BLOCK_SIZE * i, 0, BLOCK_SIZE * i, BLOCK_SIZE * ROWS);
}
g.setColor(c);
s.draw(g);
}
public void update(Graphics g) {
if(offScreenImage == null) {
offScreenImage = this.createImage(COLS * BLOCK_SIZE, ROWS * BLOCK_SIZE);
}
Graphics gOff = offScreenImage.getGraphics();
paint(gOff);
g.drawImage(offScreenImage, 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);
}
}
}
//Snake.java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
public class Snake {
Node head = null;
Node tail = null;
int size = 0;
Node n = new Node(20, 30, Dir.L);
public Snake() {
head = n;
tail = n;
size = 1;
}
public class Node {
int w = Yard.BLOCK_SIZE;
int h = Yard.BLOCK_SIZE;