package cn.itcast.tetris.entities;
import java.awt.Color;
import java.awt.Graphics;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import cn.itcast.tetris.listener.GroundListener;
import cn.itcast.tetris.util.Global;
/**
* 可以叫做地形, 或地面<BR>
* 维护障碍物的信息<BR>
* 可以使用提供的 addObstacle(int, int) 和 addStubbornObstacle(int ,int) 方法添加障碍物。<BR>
* <BR>
* 可以通过setObstacleColor(), setStubbornObstacleColor() 或
* setGriddingColor()方法更改障碍物或网格的颜色<BR>
* 通过setDrawGridding() 方法设置是否画空白(网格)<BR>
* 用 setColorfulSupport() 方法设置是否支持彩色显示<BR>
* <BR>
* 覆盖 drawObstacle(Graphics, int, int, int, int) 方法可以改变障碍物的显示方式<BR>
* 覆盖 drawStubbornObstacle(Graphics, int, int, int, int) 方法可以改变不可消除的障碍物的显示方式<BR>
* <BR>
*
* @version 1.0, 01/01/08
*
* @author 汤阳光
*
*/
public class Ground {
/**
* 监听器
*/
protected Set<GroundListener> listeners = new HashSet<GroundListener>();
/**
* 容器
*/
protected UnitType[][] obstacles = new UnitType[Global.WIDTH][Global.HEIGHT];
/**
* 不可消除的障碍物的颜色
*/
protected Color stubbornObstacleColor = UnitType.STUBBORN_OBSTACLE
.getColor();
/**
* 默认的网格颜色
*/
public static final Color DEFAULT_GRIDDING_COLOR = Color.LIGHT_GRAY;
/**
* 网格的颜色
*/
protected Color griddingColor = DEFAULT_GRIDDING_COLOR;
public static final Color DEFAULT_OBSTACLE_COLOR = UnitType.OBSTACLE
.getColor();
/**
* 障碍物的颜色
*/
protected Color obstacleColor = DEFAULT_OBSTACLE_COLOR;
public static final Color DEFAULT_FULL_LINE_COLOR = Color.DARK_GRAY;
/**
* 满行的颜色
*/
protected Color fullLineColor = DEFAULT_FULL_LINE_COLOR;
/**
* 是否画网格 的开关
*/
protected boolean drawGridding;
/**
* 是否支持彩色石头
*/
protected boolean colorfulSupport;
/**
* 是否还能接受石头
*/
protected boolean full;
protected Random random = new Random();
public Ground() {
init();
}
/**
* 初始化,将会调用clear() 方法<BR>
*
*/
public void init() {
clear();
full = false;
}
/**
* 清空容器
*/
public void clear() {
/**
* 初始化数组
*/
for (int x = 0; x < Global.WIDTH; x++)
for (int y = 0; y < Global.HEIGHT; y++)
obstacles[x][y] = UnitType.BLANK.clone();
}
/**
* 随机生成一个不可消除的障碍物, 这个随机的坐标的y 坐标不小于5
*/
public void genernateAStubbornStochasticObstacle() {
Random random = new Random();
if (Global.HEIGHT < 5)
return;
int y = random.nextInt(5) + Global.HEIGHT - 5;
int x = random.nextInt(Global.WIDTH);
addStubbornObstacle(x, y);
}
/**
* 在指定的范围内随机生成一些障碍物<BR>
* 产生的区域行是1 - lineNum
*
* @param amount
* 要生成的数量
* @param lineNum
* 行号, 从1开始
*/
public void generateSomeStochasticObstacle(int amount, int lineNum) {
if (lineNum < 1)
return;
if (lineNum > Global.HEIGHT)
lineNum = Global.HEIGHT;
for (int i = 0; i < amount; i++) {
int x = random.nextInt(Global.WIDTH);
int y = random.nextInt(lineNum) + Global.HEIGHT - lineNum;
obstacles[x][y] = UnitType.OBSTACLE.clone();
obstacles[x][y].setColor(Global.getRandomColor());
}
}
/**
* 把指定的图形变成石头<BR>
* 然后将会调用 deleteFullLine() 方法扫描并删除满行
*
* @param shape
*/
public void accept(Shape shape) {
/**
* 把图形对应的坐标变成石头
*/
int left = shape.getLeft();
int top = shape.getTop();
for (int x = 0; x < 4; x++)
for (int y = 0; y < 4; y++)
if (left + x < Global.WIDTH && top + y < Global.HEIGHT) {
if (shape.isMember(x, y, false))
/**
* 如果超出上边界了, 就是放满了
*/
if (top + y < 0) {
full = true;
for (GroundListener l : listeners)
l.groundIsFull(this);
} else {
/**
* 先变成障碍物
*/
obstacles[left + x][top + y]
.cloneProperties(UnitType.OBSTACLE);
obstacles[left + x][top + y]
.setColor(colorfulSupport ? shape
.getColor() : obstacleColor);
}
}
/**
* 扫描并删除满行
*/
deleteFullLine();
}
/**
* 扫描并删除满行<BR>
* 将调用 deleteLine(int) 方法删除满行
*/
public void deleteFullLine() {
/**
* 这次一共消了几行
*/
int deletedLineCount = 0;
/**
* 从最后一行开始, 一直到第一行
*/
for (int y = Global.HEIGHT - 1; y >= 0; y--) {
boolean isFull = true;
for (int x = 0; x < Global.WIDTH; x++) {
if (obstacles[x][y].equals(UnitType.BLANK))
isFull = false;
}
/**
* 如果当前行满了
*/
if (isFull) {
/**
* 删除满(当前)行并且当前扫描行号加 1
*/
deleteLine(y++);
deletedLineCount++;
}
}
/**
* 如果消行了, 则触发消行事件
*/
if (deletedLineCount > 0)
for (GroundListener l : listeners)
l.fullLineDeleted(this, deletedLineCount);
}
/**
* 删除指定的行(这一行上面所有的石头整体下移一行)
*
* @param lineNum
*/
public void deleteLine(int lineNum) {
/**
* 触发将要消行事件
*/
for (GroundListener l : listeners)
l.beforeDeleteFullLine(this, lineNum);
for (int y = lineNum; y > 0; y--)
for (int x = 0; x < Global.WIDTH; x++)
if (!obstacles[x][y].equals(UnitType.STUBBORN_OBSTACLE))
if (obstacles[x][y - 1].equals(UnitType.STUBBORN_OBSTACLE)) {
obstacles[x][y].cloneProperties(UnitType.BLANK);
obstacles[x][y].setColor(this.griddingColor);
} else
obstacles[x][y].cloneProperties(obstacles[x][y - 1]);
/**
* 第一行变成空白
*/
for (int x = 0; x < Global.WIDTH; x++)
if (!obstacles[x][0].equals(UnitType.STUBBORN_OBSTACLE))
obstacles[x][0] = UnitType.BLANK.clone();
}
/**
* 容器是否放满了(是否还能接受图形)<BR>
*
* @return
*/
public boolean isFull() {
return full;
}
/**
* 根据图形的动作,判断是否会碰到障碍物或不可消除的障碍物, 或是否会超出边界,
*
* @param shape
* @param action
* 图形将要做的动作
* @return 图形是否可以做这个动作
*/
public synchronized boolean isMoveable(Shape shape, int action) {
int left = shape.getLeft();
int top = shape.getTop();
/**
* 根据动作,得到最新信息
*/
switch (action) {
case Shape.UP:
top--;
break;
case Shape.DOWN:
top++;
break;
case Shape.LEFT:
left--;
break;
case Shape.RIGHT:
left++;
break;
}
if (top < 0 - shape.getHeight()) {
return false;
}
for (int x = 0; x < 4; x++)
for (int y = 0; y < 4; y++)
/**
* 如果这个位置超出边界又是图形的一部分
*/
if ((left + x < 0 || left + x >= Global.WIDTH || top + y >= Global.HEIGHT)
&& shape.isMember(x, y, action == Shape.ROTATE))
return false;
else if (top + y < 0)
continue;
else {
/**
* 或者位置不是空白(是障碍物或不可消除的障碍物)又是图形的一部分
*/
if (shape.isMember(x, y, action == Shape.ROTATE))
if (!obstacles[left + x][top + y]
.equals(UnitType.BLANK))
return false;
}
return true;
}
/**
* 改变指定行的颜色,(可以做为消行前的效果)
*
* @param lineNum
*/
public void changeFullLineColor(int lineNum) {
for (int x = 0; x < Global.WIDTH; x++)
obstacles[x][lineNum].setColor(fullLineColor);
}
/**
* 在指定的位置添加一个障碍物
*
* @param x
* x 格子坐标
* @param y
* y 格子坐标
*/
public void addObstacle(int x, int y) {
if (x < 0 || x >= Global.WIDTH || y < 0 || y >= Global.HEIGHT)
throw new RuntimeException("这个位置超出了显示区域 (x:" + x + " y:" + y + ")");
obstacles[x][y].cloneProperties(UnitType.OBSTACLE);
}
/**
* 在指定的位置添加一块不可消除的障碍物
*
* @param x
没有合适的资源?快使用搜索试试~ 我知道了~
eluosi.rar_JAVA俄罗斯方块
共142个文件
html:84个
class:31个
java:15个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 182 浏览量
2022-09-20
15:29:55
上传
评论
收藏 635KB RAR 举报
温馨提示
一个复杂的俄罗斯方块程序,用JAVA编码
资源详情
资源评论
资源推荐
收起资源包目录
eluosi.rar_JAVA俄罗斯方块 (142个子文件)
游戏开发学习指南.chm 373KB
Ground.class 9KB
MainFrame.class 9KB
Controller.class 8KB
GameOptionPanel.class 8KB
Global.class 6KB
Shape.class 5KB
ShapeFactory.class 2KB
MainFrame$14.class 2KB
GamePanel.class 2KB
UnitType.class 2KB
CommonShapeFactory.class 1KB
MainFrame$3.class 1KB
MainFrame$5.class 1KB
Shape$ShapeSwiftDriver.class 1KB
Shape$ShapeDriver.class 1KB
MainFrame$2.class 1KB
MainFrame$1.class 1KB
MainFrame$9.class 1KB
MainFrame$13.class 1KB
MainFrame$12.class 1KB
MainFrame$11.class 1KB
MainFrame$10.class 1KB
MainFrame$8.class 1KB
MainFrame$6.class 1KB
MainFrame$7.class 1KB
MainFrame$4.class 927B
MyGroundListener.class 891B
GroundAdapter.class 801B
GroundListener.class 296B
ShapeListener.class 266B
GameListener.class 219B
.classpath 232B
stylesheet.css 1KB
inherit.gif 57B
致高校IT教师的一封信.htm 36KB
先就业后付款.htm 18KB
传智播客系列视频下载.htm 14KB
Ground.html 37KB
GameOptionPanel.html 34KB
Controller.html 33KB
Shape.html 33KB
index-7.html 25KB
MainFrame.html 25KB
GamePanel.html 23KB
Ground.html 20KB
index-17.html 18KB
Global.html 18KB
Shape.html 17KB
UnitType.html 17KB
ShapeFactory.html 15KB
package-use.html 14KB
GroundAdapter.html 14KB
CommonShapeFactory.html 13KB
GroundListener.html 13KB
index-9.html 13KB
MyGroundListener.html 12KB
ShapeFactory.html 12KB
index-3.html 12KB
ShapeListener.html 12KB
index-4.html 12KB
serialized-form.html 11KB
GroundListener.html 11KB
package-use.html 11KB
GamePanel.html 11KB
UnitType.html 11KB
GameListener.html 10KB
help-doc.html 10KB
GameListener.html 10KB
ShapeListener.html 9KB
overview-tree.html 9KB
index-12.html 9KB
index-1.html 8KB
index-16.html 8KB
package-tree.html 8KB
index-6.html 8KB
package-summary.html 8KB
index-2.html 8KB
GroundAdapter.html 8KB
Controller.html 8KB
constant-values.html 7KB
package-summary.html 7KB
index-19.html 7KB
package-summary.html 7KB
index-8.html 7KB
index-15.html 7KB
index-10.html 7KB
package-tree.html 7KB
package-use.html 7KB
package-use.html 7KB
index-5.html 7KB
index-20.html 7KB
index-13.html 7KB
index-14.html 7KB
package-summary.html 7KB
index-18.html 7KB
index-11.html 7KB
package-tree.html 7KB
package-tree.html 6KB
overview-summary.html 6KB
共 142 条
- 1
- 2
weixin_42653672
- 粉丝: 93
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0