基本信息
源码名称:java贪吃蛇小游戏源码
源码大小:0.06M
文件格式:.rar
开发语言:Java
更新时间:2019-12-23
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 6 元×
微信扫码支付:6 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
能够流畅运行的贪吃蛇小游戏
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; } }