package snake;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* 贪吃蛇展示页面
*
* @author hjn
*
*/
public class MainFrame extends Frame {
/**
* 版本
*/
private static final long serialVersionUID = -5227266702753583633L;
/**
* 背景颜色
*/
Color color = Color.gray;
/**
* 蛋
*/
static Egg egg = new Egg();
/**
* 蛇
*/
Snake snake = new Snake();
/**
* 游戏是否失败
*/
boolean gameOver = false;
/**
* 给画笔起一个线程
*/
PaintThread paintThread = new PaintThread();
/**
* 构造方法
*/
public MainFrame() {
init();
}
/**
* 界面初始化
*/
void init() {
this.setBounds(200, 200, Constant.COLS * Constant.BODER_SIZE,
Constant.ROWS * Constant.BODER_SIZE);
this.setResizable(true);
this.repaint();
/**
* 窗口关闭监听事件
*/
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
/**
* 添加键盘监听事件
*/
this.addKeyListener(new KeyMomiter());
/**
* 画笔线程启动
*/
new Thread(paintThread).start();
}
/**
* 画笔画界面
*/
public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(Color.GRAY);
g.fillRect(0, 0, Constant.COLS * Constant.BODER_SIZE, Constant.ROWS
* Constant.BODER_SIZE);
g.setColor(Color.DARK_GRAY);
for (int i = 0; i < Constant.ROWS; i++) {
g.drawLine(0, i * Constant.BODER_SIZE, Constant.COLS
* Constant.BODER_SIZE, i * Constant.BODER_SIZE);
}
for (int i = 0; i < Constant.COLS; i++) {
g.drawLine(i * Constant.BODER_SIZE, 0, i * Constant.BODER_SIZE,
Constant.ROWS * Constant.BODER_SIZE);
}
g.setColor(Color.yellow);
g.setFont(new Font("宋体", Font.BOLD, 20));
g.drawString("score:" + getScore(), 10, 60);
if (gameOver) {
g.setColor(Color.red);
g.drawString("GAME OVER", 100, 60);
this.paintThread.pause = true;
}
g.setColor(c);
if (snake.eatEgg(egg)) {
egg = new Egg();
}
snake.draw(g);
egg.draw(g);
}
/**
* 获取分数
*
* @return int 分数
*/
int getScore() {
return snake.getNodeList().size();
}
/**
* 画笔的线程
*
* @author hjn
*/
class PaintThread implements Runnable {
private boolean isRun = true;
private boolean pause = false;
@Override
public void run() {
while (isRun) {
if (pause) {
continue;
} else {
if (snake.isOverstep == true) {
gameOver = true;
}
repaint();
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
* 暂停
*/
public void pause() {
this.pause = true;
}
/**
* 重新开始
*/
public void restart() {
this.pause = true;
snake = new Snake();
}
/**
* 游戏结束
*/
public void gameOver() {
isRun = false;
}
}
/**
* 停止
*/
void stop() {
gameOver = true;
}
/**
* 键盘监听器
*
* @author hjn
*
*/
class KeyMomiter extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
super.keyPressed(e);
int key = e.getKeyCode();
if (key == KeyEvent.VK_F2) {
paintThread.restart();
} else {
snake.keyPress(e);
}
}
}
/**
* 启动程序入口
*
* @param args
*/
@SuppressWarnings("deprecation")
public static void main(String[] args) {
MainFrame mainFrame = new MainFrame();
mainFrame.show();
}
}