题外话:
本人第一次写技术文章,希望寻求鼓励啊,发了四篇了一个评论还都没有,真心凉
/* -------------------------------------------------正文分隔条--------------------------------------------- */
上一节我们已经将Snake从原来的类中抽离出来,现在我们继续研究庞大的GamePanel类,继续封装其他的内容。
现在有这样一个需求:我需要提供不同大小,不同样式的地图,可能还有一些阻挡物,然后根据需要选择地图。
单单只靠GamePanel是做不到的,我们需要单独的地图类,也就是GameMap。
按照上一节的思路,下面是GameMap接口和一个简单的实现类SimpleGameMap类。
package snakes;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
/**
* 贪吃蛇地图
*
* @author Chris
*
*/
public interface GameMap
{
/**
* 绘制地图
*
* @param g
* 画布
*/
public void draw(Graphics g);
/**
* 获得地图高度
*
* @return
*/
public int getHeight();
/**
* 获取一个点在地图上的位置,用作制作无边界地图
*
* @param p
* @return 返回一个点在地图上的位置,如果没有对应地址则返回 null
*/
public Point getPointOnMap(Point p);
/**
* 获得地图尺寸
*
* @return
*/
public Dimension getSize();
/**
* 获得地图宽度
*
* @return
*/
public int getWidth();
/**
* 判断输入点是否会装到地图墙壁
*
* @param p
* @return
*/
public boolean isAgainstWall(Point p);
/**
* 从地图上获取一个可行(不撞墙)的点,而且要保证此点沿着<code> direction </code>方向连续
* <code> needLength </code>个(算上本身)点也不撞墙
*
* @param direction
* @param needLength
* @return
*/
public Point getAvailablePoint(Direction direction, int needLength);
/**
* 从地图上获取一个可行(不撞墙)的点
*
* @return
*/
public Point getAvailablePoint();
}
package snakes;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.util.Random;
public class SimpleGameMap implements GameMap
{
private static final Random random = new Random();
private GamePanel panel;
/**
* 地图横向包含的单元格数
*/
private int width;
/**
* 地图纵向包含的单元格数
*/
private int height;
public SimpleGameMap (GamePanel panel, int width, int height)
{
this.panel = panel;
this.width = width;
this.height = height;
}
@Override
public void draw(Graphics g)
{
int sellSize = panel.getSellSize();
g.setColor(new Color(0x555555));
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; ++j)
{
g.drawRect(i * sellSize, j * sellSize, sellSize, sellSize);
}
}
}
@Override
public int getHeight()
{
return height;
}
@Override
public Point getPointOnMap(Point p)
{
if (!isAgainstWall(p))
return new Point(p);
return null;
}
@Override
public Dimension getSize()
{
return new Dimension(width, height);
}
@Override
public int getWidth()
{
return width;
}
@Override
public boolean isAgainstWall(Point p)
{
return !(p.x >= 0 && p.x < width && p.y >= 0 && p.y < height);
}
@Override
public Point getAvailablePoint(Direction direction, int needLength)
{
int x = 0, y = 0;
switch (direction)
{
case UP:
x = random.nextInt(width);
y = random.nextInt(height - needLength) + needLength;
break;
case DOWN:
x = random.nextInt(width);
y = random.nextInt(height - needLength);
break;
case LEFT:
x = random.nextInt(width - needLength) + needLength;
y = random.nextInt(height);
break;
case RIGHT:
x = random.nextInt(width - needLength);
y = random.nextInt(height);
break;
}
return new Point(x, y);
}
@Override
public Point getAvailablePoint()
{
return new Point(random.nextInt(width), random.nextInt(height));
}
}
另外根据需要,为Direction添加了获取反方向的方法,还修改了Snake的部分逻辑。Direction比较简单,有兴趣的可以查看附件。以下是Snake修改的部分。
/**
* 贪吃蛇长度
*/
private volatile int length = 3;
public Snake (GamePanel panel, int length)
{
this.length = length;
this.panel = panel;
initDirection();
initList();
}
/**
* 初始化蛇链表
*/
private void initList()
{
snakeList = new LinkedList<Point>();
Point p = panel.getGameMap().getAvailablePoint(direction.getOpposite(), length);
snakeList.add(p);
for (int i = 0; i < length - 1; ++i)
{
p = getDirection().getPreviousPoint(p);
snakeList.add(p);
}
}
/**
* 移动贪吃蛇,包括吃虫
*/
public void move()
{
snakeList.addFirst(getDirection().getNextPoint(snakeList.getFirst()));
if (isAgainstTarget())
{
++length;
panel.resetTarget();
panel.increaseScore();
panel.increaseSpeed();
}
else
{
snakeList.removeLast();
}
}
下节预告:分数管理ScoreManager和速度管理SpeedManager
分享到:
相关推荐
在本篇博客“代码重构-以贪吃蛇为示例(五)-封装Scoring和SpeedManager”中,作者通过重构一个贪吃蛇游戏的代码,深入探讨了软件开发中的两个重要概念:分数管理(Scoring)和速度管理(SpeedManager)。...
在本篇博文中,我们将深入探讨“代码重构”的概念,并以经典的贪吃蛇游戏为例,进行实际的重构实践,特别是如何对游戏中的主角——Snake(贪吃蛇)进行封装。这个过程将涉及到软件工程中的模块化、面向对象设计原则...
- **代码重构**:分析已有的游戏代码,优化结构,提升代码质量。 - **参与开源项目**:加入开源游戏项目,与其他开发者合作,共同完成更复杂的任务。 总之,"C语言经典小游戏 c 小游戏"不仅是一个学习C语言编程的...
{5.2.1}将浮点数四舍五入到指定精度}{98}{subsection.5.2.1} {6}Exception}{99}{chapter.6} {6.1}\ttfamily try-catch}{99}{section.6.1} {6.2}\ttfamily finally}{100}{section.6.2} {6.3}\ttfamily throws}{...