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; } }
最近下载更多
雨梦岚 LV1
3月3日
Hao Pan LV1
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日
339339 LV1
2023年6月28日
豚骨叉烧拉面 LV1
2023年6月24日
summer17904 LV1
2023年6月6日
最近浏览更多
雨梦岚 LV1
3月3日
Hao Pan LV1
1月5日
2890892945 LV2
2023年12月26日
周俊杰 LV1
2023年12月25日
1474371745 LV1
2023年12月23日
心比扎软 LV1
2023年12月21日
ClydeSon LV5
2023年12月18日
微信网友_6724295393169408
2023年11月6日
暂无贡献等级
xingshizourou
2023年11月5日
暂无贡献等级
koichenchen LV1
2023年11月3日