问题一 蛇死了 线程会停止 图形不能刷新 “游戏结束”显示不出来
问题二 蛇死了 还有命 怎么让它继续 原来的分数玩public class Yard extends Frame { private boolean flag=true;
public static final int ROWS = 30;
public static final int COLS = 30;
public static final int BLOCK_SIZE = 15;
public static boolean gameover=false;
private int score = 0;
public int getScore() {
return score;
} public void setScore(int score) {
this.score = score;
} Snake s = new Snake(this);
Eeg e = new Eeg(); Image offScreenImage = null; public void launch() {
this.setLocation(200, 200);
this.setSize(COLS * BLOCK_SIZE, ROWS * BLOCK_SIZE);
this.addWindowListener(new WindowAdapter() { @Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setVisible(true);
this.addKeyListener(new KeyMonitor());
new Thread(new PaintThread()).start(); } public static void main(String[] args) {
new Yard().launch();
} public void stop(){
flag=false;
}
@Override
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);
} @Override
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 = 0; i < ROWS; i++) {
g.drawLine(0, BLOCK_SIZE * i, COLS * BLOCK_SIZE, BLOCK_SIZE * i);
}
for (int i = 0; i < COLS; i++) {
g.drawLine(BLOCK_SIZE * i, 0, BLOCK_SIZE * i, BLOCK_SIZE * ROWS);
}
g.setColor(Color.YELLOW);
g.drawString("分数:"+score, 10, 60);
if(gameover==true){
g.setColor(Color.RED);
g.setFont(new Font("宋体",Font.BOLD,20));
g.drawString("游戏结束", 10, 80);
}
g.setColor(c);
s.eat(e);
e.draw(g);
s.draw(g);
} private class PaintThread implements Runnable { @Override
public void run() { while (flag) {
repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
} }
} } private class KeyMonitor extends KeyAdapter { @Override
public void keyPressed(KeyEvent e) {
s.keyPressed(e);
/**if(e.getKeyCode()==KeyEvent.VK_SPACE){
int oldScore=score;
System.out.println(oldScore);
new Yard().launch();
score=oldScore;
System.out.println(score);
}**/
} }}
问题二 蛇死了 还有命 怎么让它继续 原来的分数玩public class Yard extends Frame { private boolean flag=true;
public static final int ROWS = 30;
public static final int COLS = 30;
public static final int BLOCK_SIZE = 15;
public static boolean gameover=false;
private int score = 0;
public int getScore() {
return score;
} public void setScore(int score) {
this.score = score;
} Snake s = new Snake(this);
Eeg e = new Eeg(); Image offScreenImage = null; public void launch() {
this.setLocation(200, 200);
this.setSize(COLS * BLOCK_SIZE, ROWS * BLOCK_SIZE);
this.addWindowListener(new WindowAdapter() { @Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setVisible(true);
this.addKeyListener(new KeyMonitor());
new Thread(new PaintThread()).start(); } public static void main(String[] args) {
new Yard().launch();
} public void stop(){
flag=false;
}
@Override
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);
} @Override
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 = 0; i < ROWS; i++) {
g.drawLine(0, BLOCK_SIZE * i, COLS * BLOCK_SIZE, BLOCK_SIZE * i);
}
for (int i = 0; i < COLS; i++) {
g.drawLine(BLOCK_SIZE * i, 0, BLOCK_SIZE * i, BLOCK_SIZE * ROWS);
}
g.setColor(Color.YELLOW);
g.drawString("分数:"+score, 10, 60);
if(gameover==true){
g.setColor(Color.RED);
g.setFont(new Font("宋体",Font.BOLD,20));
g.drawString("游戏结束", 10, 80);
}
g.setColor(c);
s.eat(e);
e.draw(g);
s.draw(g);
} private class PaintThread implements Runnable { @Override
public void run() { while (flag) {
repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
} }
} } private class KeyMonitor extends KeyAdapter { @Override
public void keyPressed(KeyEvent e) {
s.keyPressed(e);
/**if(e.getKeyCode()==KeyEvent.VK_SPACE){
int oldScore=score;
System.out.println(oldScore);
new Yard().launch();
score=oldScore;
System.out.println(score);
}**/
} }}
private Node head = null;
private Node tail = null;
private int size = 0; private Node n = new Node(10, 10, Dir.D);
private Yard y; public Snake(Yard y) {
head = n;
tail = n;
size = 1;
this.y = y;
} 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 R:
node = new Node(tail.row, tail.col - 1, tail.dir);
break;
case D:
node = new Node(tail.row - 1, tail.col, tail.dir);
}
tail.next = node;
node.prev = tail;
tail = node;
size++;
} 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 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++;
} public void draw(Graphics g) {
if (size <= 0)
return;
move();
for (Node n = head; n != null; n = n.next) {
n.draw(g);
} } private void move() {
addToHead();
deleteFromTail();
checkDead();
} private void checkDead() {
if (head.row < 2 || head.col < 0 || head.row > Yard.ROWS
|| head.col > Yard.COLS) {
y.gameover=true;
y.stop();
} for (Node n = head.next; n != null; n = n.next) {
if (head.row == n.row && head.col == n.col) {
y.gameover=true;
y.stop();
}
} } private void deleteFromTail() {
if (size == 0)
return; tail = tail.prev;
tail.next = null; } 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) {
super();
this.row = row;
this.col = col;
this.dir = dir;
} void draw(Graphics g) {
Color c = g.getColor();
g.setColor(Color.BLACK);
g.fillRect(Yard.BLOCK_SIZE * col, Yard.BLOCK_SIZE * row, w, h); g.setColor(c);
} } public void eat(Eeg e) {
if (this.getRect().intersects(e.getRect())) {
e.reAppear();
this.addToHead();
y.setScore(y.getScore()+5);
} } private Rectangle getRect() {
return new Rectangle(Yard.BLOCK_SIZE * head.col, Yard.BLOCK_SIZE
* head.row, head.w, head.h);
} public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub int key = e.getKeyCode();
switch (key) {
case KeyEvent.VK_LEFT:
if (head.dir != Dir.R)
head.dir = Dir.L;
break;
case KeyEvent.VK_UP:
if (head.dir != Dir.D)
head.dir = Dir.U;
break;
case KeyEvent.VK_RIGHT:
if (head.dir != Dir.L)
head.dir = Dir.R;
break;
case KeyEvent.VK_DOWN:
if (head.dir != Dir.U)
head.dir = Dir.D;
break; }
}}