求助:贪吃蛇的两段代码问题

清峰 2010-03-05 01:35:01
第一段

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

public class Yard extends Frame {

public static void main(String[] args) {
new Yard().launch();
}

Image offScreen = null;

public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(Color.GRAY);
g.fillRect(0, 0, ROWS*BLOCK_SIZE, COLS*BLOCK_SIZE);
g.setColor(Color.DARK_GRAY);
for(int i=1; i<ROWS; i++) {
g.drawLine(0,BLOCK_SIZE*i,ROWS*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.drawSnake(g);
new Thread(new PaintThread()).start();
}

public static final int ROWS = 50;
public static final int COLS = 50;
public static final int BLOCK_SIZE = 10;
Snake s = new Snake();

public void launch() {
this.setLocation(100,100);
this.setSize(ROWS*BLOCK_SIZE, COLS*BLOCK_SIZE);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}

});
this.addKeyListener(new KeyMonitor());
this.setVisible(true);
}


@Override
public void update(Graphics g) {
if(offScreen==null) {
offScreen = this.createImage(ROWS*BLOCK_SIZE, COLS*BLOCK_SIZE);
}
Graphics goff = offScreen.getGraphics();
paint(goff);
g.drawImage(offScreen,0,0,null );
}

private class PaintThread implements Runnable {

public void run() {
while(true) {
repaint();
try {
Thread.sleep(50);
}catch(Exception e) {
e.printStackTrace();
}
}

}

}

private class KeyMonitor extends KeyAdapter {


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

}

}


第二段
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 = 0;
Node n = new Node(20,30,Dir.L);

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

void drawSnake(Graphics g) {
if(size<=0) return;
move();
for(Node n=head; n!=null; n=n.next ) {
n.draw(g);
}

}

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 R :
node = new Node(tail.row, tail.col-1,tail.dir);
break;
case D :
node = new Node(tail.row-1,tail.col,tail.dir);
break;
}
tail.next = node;
node.prev = tail;
tail = node;
size ++;
}

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 R :
node = new Node(head.row,head.col+1,head.dir);
break;
case D :
node = new Node(head.row+1,head.col,head.dir);
break;
}
node.next = head ;
head.prev = node ;
head = node;
size ++;
}

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;

Node(int row, int col, Dir dir) {
this.row = row;
this.col = col;
this.dir = dir;
}

public void draw(Graphics g) {
Color c = g.getColor();
g.setColor(Color.BLACK);
g.fillRect(Yard.BLOCK_SIZE*col,Yard.BLOCK_SIZE*row , Yard.BLOCK_SIZE, Yard.BLOCK_SIZE);
g.setColor(c);

}

}



public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
switch(key) {
case KeyEvent.VK_LEFT :
head.dir = Dir.L;
break;
case KeyEvent.VK_UP :
head.dir = Dir.U;
break;
case KeyEvent.VK_RIGHT :
head.dir = Dir.R;
break;
case KeyEvent.VK_DOWN :
head.dir = Dir.D;
break;
}
}

public void move() {
addToHead();
deleteFromTail();
}

private void deleteFromTail() {
if(size == 0) return;
tail = tail.prev;
tail.next = null ;

}

}

运行结果:蛇不见了,不知道哪里出错?排查了很久,找不到错误
...全文
190 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
zliuzz 2010-03-05
  • 打赏
  • 举报
回复
抓条蛇来,放电脑上就可以了.
kingkwj 2010-03-05
  • 打赏
  • 举报
回复
抓条蛇来,放电脑上就可以了.
梦_枫 2010-03-05
  • 打赏
  • 举报
回复
你怎么能在Paint()方法中new Thread(new PaintThread()).start()啊。。。

这样重画会有问题的。。。

让Yard 类实现Runnable接口,把PaintThread中的代码放到Yard类中应该就没问题了。。
清峰 2010-03-05
  • 打赏
  • 举报
回复
引用 2 楼 zliuzz 的回复:
抓条蛇来,放电脑上就可以了.


LS两位很幽默~~那我的代码怎么解决呢??朋友??

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧