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
* @download http://www.codefans.net
*/
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;
}
}
最近下载更多
微信网友_7556302635110400 LV1
6月16日
鲁一宇 LV5
2024年12月24日
mechanic LV1
2024年4月11日
雨梦岚 LV1
2024年3月3日
Hao Pan LV1
2024年1月5日
心比扎软 LV1
2023年12月21日
ClydeSon LV5
2023年12月18日
微信网友_6770780673069056 LV2
2023年12月9日
steven101 LV1
2023年8月4日
zwj2750514489 LV1
2023年6月29日
最近浏览更多
微信网友_7556302635110400 LV1
6月16日
鲁一宇 LV5
2024年12月24日
xiao peppa
2024年11月10日
暂无贡献等级
1661950467 LV2
2024年10月12日
3456788 LV1
2024年7月4日
188073301 LV1
2024年6月15日
qweasd669785
2024年6月14日
暂无贡献等级
liqizuiai
2024年5月20日
暂无贡献等级
微信网友_6979151039729664 LV2
2024年5月5日
Dominick LV14
2024年4月29日

