贪吃蛇的算法分析(2)
2008-04-25 13:59
295 查看
贪吃蛇的算法分析(2)James @ www.chenshen.com下面重点介绍下Worm类中的几个方法:
l public void setDirection(byte direction)这个方法用来改变贪吃蛇运动的方向,只能90度。看下面的实现代码:
if ((direction != currentDirection) && !needUpdate) { // 取出列表中的最后一个元素(蛇的头部) WormLink sl = (WormLink)worm.lastElement(); int x = sl.getEndX(); int y = sl.getEndY(); // 不同的运动方向坐标的改变也不一样 switch (direction) { case UP: // 当这段向上运动的时候 if (currentDirection != DOWN) { y--; needUpdate = true; } break; case DOWN: // 当这段向下运动的时候 if (currentDirection != UP) { y++; needUpdate = true; } break; case LEFT: // 当这段向左运动的时候 if (currentDirection != RIGHT) { x--; needUpdate = true; } break; case RIGHT: // 当这段向右运动的时候 if (currentDirection != LEFT) { x++; needUpdate = true; } break; } // 当更改方向后需要更新 if (needUpdate == true) { worm.addElement(new WormLink(x, y, 0, direction)); currentDirection = direction; } }
l public void update(Graphics g)这个函数是更新贪吃蛇状态。每次更新都把头部增加一节,尾部减少一节。如果它吃到食物尾部段就不减少一节。看起来就像整只蛇长了一节。
// 把贪吃蛇头部增加一格 head = (WormLink)worm.lastElement(); head.increaseLength(); // 如果没有吃到食物则尾部减少一格 if (!hasEaten) { WormLink tail; tail = (WormLink)worm.firstElement(); int tailX = tail.getX(); int tailY = tail.getY(); // 如果尾部块长度为0就删除 tail.decreaseLength(); if (tail.getLength() == 0) { worm.removeElement(tail); } // 尾部减少一格 g.setColor(WormPit.ERASE_COLOUR); drawLink(g, tailX, tailY, tailX, tailY, 1); } else { // 如果吃到食物就不删除尾部 hasEaten = false; } needUpdate = false; // 确认是否在边界中 if (!WormPit.isInBounds(head.getEndX(), head.getEndY())) { // 如果不在,就死了 throw new WormException("over the edge"); } headX = (byte)head.getEndX(); headY = (byte)head.getEndY(); //贪吃蛇的头部增加一格 g.setColor(WormPit.DRAW_COLOUR); drawLink(g, headX, headY, headX, headY, 1); // 判断是否吃到自己 for (int i = 0; i < worm.size()-1; i++) { sl = (WormLink)worm.elementAt(i); if (sl.contains(headX, headY)) { throw new WormException("you ate yourself"); } }
l void drawLink(Graphics g, int x1, int y1, int x2, int y2, int len)这个函数用来画蛇的一段,一只完整的蛇是一段一段组成的。
// 把长度转换成像素长度 len *= WormPit.CELL_SIZE; // (x1 == x2)说明这一段是垂直的 if (x1 == x2) { // 把x1转成像素长度 x1 *= WormPit.CELL_SIZE; // (y2 < y1)说明是向上运动 if (y2 < y1) {// 就把头、尾左边交换并转成像素 y1 = y2 * WormPit.CELL_SIZE; } else { // 把y1转成像素 y1 *= WormPit.CELL_SIZE; } g.fillRect(x1, y1, WormPit.CELL_SIZE, len); } else { // 这是水平的一段 y1 *= WormPit.CELL_SIZE;
l public void setDirection(byte direction)这个方法用来改变贪吃蛇运动的方向,只能90度。看下面的实现代码:
if ((direction != currentDirection) && !needUpdate) { // 取出列表中的最后一个元素(蛇的头部) WormLink sl = (WormLink)worm.lastElement(); int x = sl.getEndX(); int y = sl.getEndY(); // 不同的运动方向坐标的改变也不一样 switch (direction) { case UP: // 当这段向上运动的时候 if (currentDirection != DOWN) { y--; needUpdate = true; } break; case DOWN: // 当这段向下运动的时候 if (currentDirection != UP) { y++; needUpdate = true; } break; case LEFT: // 当这段向左运动的时候 if (currentDirection != RIGHT) { x--; needUpdate = true; } break; case RIGHT: // 当这段向右运动的时候 if (currentDirection != LEFT) { x++; needUpdate = true; } break; } // 当更改方向后需要更新 if (needUpdate == true) { worm.addElement(new WormLink(x, y, 0, direction)); currentDirection = direction; } }
l public void update(Graphics g)这个函数是更新贪吃蛇状态。每次更新都把头部增加一节,尾部减少一节。如果它吃到食物尾部段就不减少一节。看起来就像整只蛇长了一节。
// 把贪吃蛇头部增加一格 head = (WormLink)worm.lastElement(); head.increaseLength(); // 如果没有吃到食物则尾部减少一格 if (!hasEaten) { WormLink tail; tail = (WormLink)worm.firstElement(); int tailX = tail.getX(); int tailY = tail.getY(); // 如果尾部块长度为0就删除 tail.decreaseLength(); if (tail.getLength() == 0) { worm.removeElement(tail); } // 尾部减少一格 g.setColor(WormPit.ERASE_COLOUR); drawLink(g, tailX, tailY, tailX, tailY, 1); } else { // 如果吃到食物就不删除尾部 hasEaten = false; } needUpdate = false; // 确认是否在边界中 if (!WormPit.isInBounds(head.getEndX(), head.getEndY())) { // 如果不在,就死了 throw new WormException("over the edge"); } headX = (byte)head.getEndX(); headY = (byte)head.getEndY(); //贪吃蛇的头部增加一格 g.setColor(WormPit.DRAW_COLOUR); drawLink(g, headX, headY, headX, headY, 1); // 判断是否吃到自己 for (int i = 0; i < worm.size()-1; i++) { sl = (WormLink)worm.elementAt(i); if (sl.contains(headX, headY)) { throw new WormException("you ate yourself"); } }
l void drawLink(Graphics g, int x1, int y1, int x2, int y2, int len)这个函数用来画蛇的一段,一只完整的蛇是一段一段组成的。
// 把长度转换成像素长度 len *= WormPit.CELL_SIZE; // (x1 == x2)说明这一段是垂直的 if (x1 == x2) { // 把x1转成像素长度 x1 *= WormPit.CELL_SIZE; // (y2 < y1)说明是向上运动 if (y2 < y1) {// 就把头、尾左边交换并转成像素 y1 = y2 * WormPit.CELL_SIZE; } else { // 把y1转成像素 y1 *= WormPit.CELL_SIZE; } g.fillRect(x1, y1, WormPit.CELL_SIZE, len); } else { // 这是水平的一段 y1 *= WormPit.CELL_SIZE;
相关文章推荐
- 贪吃蛇的算法分析(5)
- 贪吃蛇的算法分析(3)
- 贪吃蛇的算法分析(5)
- 贪吃蛇的算法分析(1)
- 贪吃蛇的算法分析(4)
- 贪吃蛇的算法分析(2)
- 贪吃蛇的算法分析
- 贪吃蛇的算法分析(1)
- 贪吃蛇的算法分析(3)
- 贪吃蛇的算法分析(4)
- STL堆算法性能分析与优化方法
- javascript动画、运动算法详细解释与分析 (三、Tween 运动算法:二次方曲线算法)
- 数据结构和算法分析(11)树的简介
- 数据挖掘:Apriori 关联规则分析算法原理分析与代码实现
- 网络分析优化旅行商问题TSP算法资源
- 中文转换为完整拼音算法原理分析
- 【OpenStack源码分析之十一】分布式事务和相关算法
- 小白学数据分析之关联分析算法篇Apriori
- 算法分析与设计第十六周:582. kill process
- 算法分析与设计第八周