知识大全 源代码--Java写小游戏,吞食蛇
Posted 文件
篇首语:丈夫志四海,万里犹比邻。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 源代码--Java写小游戏,吞食蛇相关的知识,希望对你有一定的参考价值。
源代码--Java写小游戏,吞食蛇 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!
java新手 简单的吞食蛇游戏 一边写一边查书 写了半天才弄出来 没法把jar文件附在文章后面 想试试的人自己编译一下吧 有意见尽管提 高手多多指导 不过骂人就算了 /////////////////////////// // 文件 ////////////////////////// import java awt *; import java awt event *; import javax swing *; import java util *; public class GreedSnake implements KeyListener JFrame mainFrame; Canvas paintCanvas; JLabel labelScore; SnakeModel snakeModel = null; public static final int canvasWidth = ; public static final int canvasHeight = ; public static final int nodeWidth = ; public static final int nodeHeight = ; public GreedSnake() mainFrame = new JFrame( GreedSnake ); Container cp = mainFrame getContentPane(); labelScore = new JLabel( Score: ); cp add(labelScore BorderLayout NORTH); paintCanvas = new Canvas(); paintCanvas setSize(canvasWidth+ canvasHeight+ ); paintCanvas addKeyListener(this); cp add(paintCanvas BorderLayout CENTER); JPanel panelButtom = new JPanel(); panelButtom setLayout(new BorderLayout()); JLabel labelHelp; labelHelp = new JLabel( PageUp PageDown for speed; JLabel CENTER); panelButtom add(labelHelp BorderLayout NORTH); labelHelp = new JLabel( ENTER or R or S for start; JLabel CENTER); panelButtom add(labelHelp BorderLayout CENTER); labelHelp = new JLabel( SPACE or P for pause JLabel CENTER); panelButtom add(labelHelp BorderLayout SOUTH); cp add(panelButtom BorderLayout SOUTH); mainFrame addKeyListener(this); mainFrame pack(); mainFrame setResizable(false); mainFrame setDefaultCloseOperation(JFrame EXIT_ON_CLOSE); mainFrame setVisible(true); begin(); public void keyPressed(KeyEvent e) int keyCode = e getKeyCode(); if (snakeModel running) switch(keyCode) case KeyEvent VK_UP: snakeModel changeDirection(SnakeModel UP); break; case KeyEvent VK_DOWN: snakeModel changeDirection(SnakeModel DOWN); break; case KeyEvent VK_LEFT: snakeModel changeDirection(SnakeModel LEFT); break; case KeyEvent VK_RIGHT: snakeModel changeDirection(SnakeModel RIGHT); break; case KeyEvent VK_ADD: case KeyEvent VK_PAGE_UP: snakeModel speedUp(); break; case KeyEvent VK_SUBTRACT: case KeyEvent VK_PAGE_DOWN: snakeModel speedDown(); break; case KeyEvent VK_SPACE: case KeyEvent VK_P: snakeModel changePauseState(); break; default: if (keyCode == KeyEvent VK_R || keyCode == KeyEvent VK_S || keyCode == KeyEvent VK_ENTER) snakeModel running = false; begin(); public void keyReleased(KeyEvent e) public void keyTyped(KeyEvent e) void repaint() Graphics g = paintCanvas getGraphics(); //draw background g setColor(Color WHITE); g fillRect( canvasWidth canvasHeight); // draw the snake g setColor(Color BLACK); LinkedList na = snakeModel nodeArray; Iterator it = erator(); while(it hasNext()) Node n = (Node)it next(); drawNode(g n); // draw the food g setColor(Color RED); Node n = snakeModel food; drawNode(g n); updateScore(); private void drawNode(Graphics g Node n) g fillRect(n x*nodeWidth n y*nodeHeight nodeWidth nodeHeight ); public void updateScore() String s = Score: + snakeModel score; labelScore setText(s); void begin() if (snakeModel == null || !snakeModel running) snakeModel = new SnakeModel(this canvasWidth/nodeWidth canvasHeight/nodeHeight); (new Thread(snakeModel)) start(); public static void main(String[] args) GreedSnake gs = new GreedSnake(); /////////////////////////////////////////////////// // 文件 /////////////////////////////////////////////////// import java util *; import javax swing *; class SnakeModel implements Runnable GreedSnake gs; boolean[][] matrix; LinkedList nodeArray = new LinkedList(); Node food; int maxX; int maxY; int direction = ; boolean running = false; int timeInterval = ; double speedChangeRate = ; boolean paused = false; int score = ; int countMove = ; // UP and DOWN should be even // RIGHT and LEFT should be odd public static final int UP = ; public static final int DOWN = ; public static final int LEFT = ; public static final int RIGHT = ; public SnakeModel(GreedSnake gs int maxX int maxY) this gs = gs; this maxX = maxX; this maxY = maxY; // initial matirx matrix = new boolean[maxX][]; for(int i= ; i<maxX; ++i) matrix[i] = new boolean[maxY]; Arrays fill(matrix[i] false); // initial the snake int initArrayLength = maxX > ? : maxX/ ; for(int i = ; i < initArrayLength; ++i) int x = maxX/ +i; int y = maxY/ ; nodeArray addLast(new Node(x y)); matrix[x][y] = true; food = createFood(); matrix[food x][food y] = true; public void changeDirection(int newDirection) if (direction % != newDirection % ) direction = newDirection; public boolean moveOn() Node n = (Node)nodeArray getFirst(); int x = n x; int y = n y; switch(direction) case UP: y ; break; case DOWN: y++; break; case LEFT: x ; break; case RIGHT: x++; break; if (( <= x && x < maxX) && ( <= y && y < maxY)) if (matrix[x][y]) if(x == food x && y == food y) nodeArray addFirst(food); int scoreGet = ( * countMove) / timeInterval; score += scoreGet > ? scoreGet : ; countMove = ; food = createFood(); matrix[food x][food y] = true; return true; else return false; else nodeArray addFirst(new Node(x y)); matrix[x][y] = true; n = (Node)nodeArray removeLast(); matrix[n x][n y] = false; countMove++; return true; return false; public void run() running = true; while (running) try Thread sleep(timeInterval); catch(Exception e) break; if(!paused) if (moveOn()) gs repaint(); else JOptionPane showMessageDialog( null you failed Game Over JOptionPane INFORMATION_MESSAGE); break; running = false; private Node createFood() int x = ; int y = ; do Random r = new Random(); x = r nextInt(maxX); y = r nextInt(maxY); while(matrix[x][y]); return new Node(x y); public void speedUp() cha138/Article/program/Java/hx/201311/27025相关参考
知识大全 ant - java 构建工具 - hello ant
管理你的代码doc文件编译配置等开发工作你是怎末做的自己管理源目录编译文件copy实施在window写bat在unix写shell? 可惜我不怎末会写所以我制作一个ejb的过程是这样的写代码
天气预报调用代码 最近要做一个网站需要用到天气预报本来是想找到API自己写一个自己的天气预报小程序的没有成功只好去找现成的代码调用经过测试我找到了几种现在还可以使用的调用代码由于大部分效果使用的
用Java写一个在线金山词霸程序 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 代码贴上其它的就
JS写的贪吃蛇游戏 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! JS贪吃蛇游戏个人练习之用放在
十二生肖中游戏接龙代表什么动物蛇十二生肖财代表什么动物蛇吧。周公解梦里说道:梦见一对蛇,很快会分家。商人梦见一对蛇,能发大财。梦见蛇咬人主得大财。有蛇就有财,应该是蛇吧~十二生肖事代表什么动物十二生肖
Java中数组的转型和范型 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 今天写代码遇到一个奇怪
java游戏开发需要学习什么那些类java游戏开发可以通过学习JITCompiler技术、HotSpot技术,当然了,还有许多探讨如何撰写更有效率Java的书籍,这样Java就具备开发游戏的实力。游戏
利用DES加密算法保护Java源代码 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! Java语言
拷贝文件的Java源代码 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! //ClassCopyF
Java线程控制权源代码的深入探讨 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! Java线程控