能够流畅运行的贪吃蛇小游戏
import java.awt.Color;
import java.awt.Font;
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.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* 这个类代表贪吃蛇的活动场所
* @author bjsxt
* @version 1.0
*/
public class Yard extends Frame {
PaintThread paintThread = new PaintThread();
private boolean gameOver = false; //游戏是否结束
/**
* 行数
*/
public static final int ROWS = 30;
public static final int COLS = 30;
public static final int BLOCK_SIZE = 15;
private Font fontGameOver = new Font("宋体", Font.BOLD, 50);
private int score = 0;
Snake s = new Snake(this);
Egg e = new Egg();
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(paintThread).start();
}
public static void main(String[] args) {
new Yard().launch();
}
public void stop() {
gameOver = true;
}
@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=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(Color.YELLOW);
g.drawString("score:" score, 10, 60);
if(gameOver) {
g.setFont(fontGameOver);
g.drawString("游戏结束", 120, 180);
paintThread.pause();
}
g.setColor(c);
s.eat(e);
e.draw(g);
s.draw(g);
}
@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);
}
private class PaintThread implements Runnable {
private boolean running = true;
private boolean pause = false;
public void run() {
while(running) {
if(pause) continue;
else repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void pause() {
this.pause = true;
}
public void reStart() {
this.pause = false;
s = new Snake(Yard.this);
gameOver = false;
}
public void gameOver() {
running = false;
}
}
private class KeyMonitor extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if(key == KeyEvent.VK_F2) {
paintThread.reStart();
}
s.keyPressed(e);
}
}
/**
* 拿到所得的分数
* @return 分数
*/
public int getScore() {
return score;
}
/**
* 设置所得的分数
* @param score 分数
*/
public void setScore(int score) {
this.score = score;
}
}
资源文件列表
Snake/.classpath , 296
Snake/.project , 381
Snake/bin/Dir.class , 933
Snake/bin/Egg.class , 1752
Snake/bin/Snake$Node.class , 1067
Snake/bin/Snake.class , 3480
Snake/bin/Yard$1.class , 599
Snake/bin/Yard$KeyMonitor.class , 917
Snake/bin/Yard$PaintThread.class , 1250
Snake/bin/Yard.class , 3368
Snake/doc/allclasses-frame.html , 925
Snake/doc/allclasses-noframe.html , 845
Snake/doc/class-use/Dir.html , 6616
Snake/doc/class-use/Egg.html , 6118
Snake/doc/class-use/Snake.html , 5076
Snake/doc/class-use/Yard.html , 6032
Snake/doc/constant-values.html , 6356
Snake/doc/deprecated-list.html , 4848
Snake/doc/Dir.html , 12083
Snake/doc/Egg.html , 11929
Snake/doc/help-doc.html , 7893
Snake/doc/index-files/index-1.html , 6101
Snake/doc/index-files/index-10.html , 6032
Snake/doc/index-files/index-11.html , 6154
Snake/doc/index-files/index-12.html , 6783
Snake/doc/index-files/index-13.html , 6036
Snake/doc/index-files/index-14.html , 6274
Snake/doc/index-files/index-15.html , 6106
Snake/doc/index-files/index-2.html , 6011
Snake/doc/index-files/index-3.html , 5999
Snake/doc/index-files/index-4.html , 6352
Snake/doc/index-files/index-5.html , 6449
Snake/doc/index-files/index-6.html , 6452
Snake/doc/index-files/index-7.html , 6045
Snake/doc/index-files/index-8.html , 6003
Snake/doc/index-files/index-9.html , 6031
Snake/doc/index.html , 1075
Snake/doc/overview-tree.html , 5780
Snake/doc/package-frame.html , 1287
Snake/doc/package-list , 2
Snake/doc/package-summary.html , 5935
Snake/doc/package-tree.html , 5793
Snake/doc/package-use.html , 5418
Snake/doc/resources/inherit.gif , 57
Snake/doc/serialized-form.html , 6463
Snake/doc/Snake.html , 11045
Snake/doc/stylesheet.css , 1137
Snake/doc/Yard.html , 22634
Snake/src/Dir.java , 98
Snake/src/Egg.java , 1174
Snake/src/Snake.java , 3199
Snake/src/Yard.java , 3358
