📄 ground.java
字号:
import javax.microedition.lcdui.Graphics;
public class Ground
{
private int[][] obstacles =
new int[GamePanel.WIDTH][GamePanel.HEIGHT];
public void accept(Shape shape)
{
for(int x=0; x<4; x++)
{
for(int y=0; y<4; y++)
{
if(shape.isMember(x, y, false))
{
obstacles[shape.getLeft()+x][shape.getTop()+y] = 1;
}
}
}
deleteFullLine();
}
private void deleteFullLine()
{
for(int y=GamePanel.HEIGHT-1; y>=0; y--)
{ //先从最下面一行开始
boolean isFull = true;
for(int x=0; x<GamePanel.WIDTH; x++)
{
if(obstacles[x][y] == 0){
isFull = false;
break;
}
}
if(isFull){
deleteLine( y );
y++;
}
}
}
private void deleteLine(int lineNum)
{
for(int y=lineNum; y>0; y--)
{
for(int x=0; x<GamePanel.WIDTH; x++)
{
obstacles[x][y] = obstacles[x][y-1];
}
}
for(int x=0; x<GamePanel.WIDTH; x++)
{
obstacles[x][0] = 0;
}
}
public void paint(Graphics g)
{
g.setColor(0x0000FF);
for(int x=0; x<GamePanel.WIDTH; x++)
{
for(int y=0; y<GamePanel.HEIGHT; y++)
{
if(obstacles[x][y] == 1)
{
g.fillRoundRect(x*Shape.CELL_SIZE, y*Shape.CELL_SIZE,
Shape.CELL_SIZE, Shape.CELL_SIZE, 5, 5);
}
}
}
}
public boolean isMoveable(Shape shape, int action)
{
int left = shape.getLeft();//保留下次移动之前的left
int top = shape.getTop();//保留下次移动之前的top
switch(action)//根据状态,进行假设移动
{
case Shape.LEFT:
left--;
break;
case Shape.RIGHT:
left++;
break;
case Shape.DOWN:
top++;
break;
}
for(int x=0; x<4; x++)
{
for(int y=0; y<4; y++)
{
//需要获取当前格是1的标志再做判断
if(shape.isMember(x, y, action==Shape.ROTATE))
{
if ( (y+top >= GamePanel.HEIGHT) ||
(x+left >= GamePanel.WIDTH) ||
(x+left < 0) ||
(obstacles[x+left][y+top] == 1)
)
{
return false;
}
}
}
}
return true;
}
public boolean isFull()
{
for(int x=0; x<GamePanel.WIDTH; x++)
{
if(obstacles[x][0] == 1)
return true;
}
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -