📄 windowgrid.java
字号:
public Rectangle getCellRect(Cell cell)// throws BlockException
{
//if(loc.row < 0 || loc.row >= ROWS || loc.col < 0 || loc.col >= COLS)
// throw new BlockException(BETable.getMessage(8) );
Rectangle rc = new Rectangle();
rc.x = cell.col * blockWidth;
rc.y = cell.row * blockWidth;
rc.width = blockWidth;
rc.height = blockWidth;
return rc;
}
//清除满行===================================================
public void clearRow()
{
pause(); //暂停产生新方块
int lowLine = 0; //底线, 已删除的最大行号,此线以上行要下降,
//最低下降到lowLine
int clearedRows = 0; //本次清除的行数
for(int row=0; row<ROWS; row++)
{
//判断此行是否为满行
boolean full =true;
for(int col=0; col<COLS; col++)
{
if(table[row][col] == null)
{
full = false;
break;
}
}
//删除整行
if(full)
{
for(int col=0; col<COLS;col++)
{
table[row][col].setVisible(false);
this.remove(table[row][col]);
table[row][col] = null;
this.repaint();
}
if(row > lowLine)
lowLine = row;
clearedRows++;
}
}
//加分,加速度
if(edtScore != null && edtSpeed != null)
{
int score = Integer.parseInt(edtScore.getText()) +
100*clearedRows;
if(clearedRows > 1)
score += (clearedRows - 1) * 100;
edtScore.setText(String.valueOf(score) );
if(score != 0 && score % 10000 == 0)
{
int curInterval = timer.getInterval();
if(curInterval > 100)
{
timer.setInterval(curInterval-100);
int curSpeed = Integer.parseInt(edtSpeed.getText());
curSpeed++;
edtSpeed.setText("" + curSpeed );
}
}
}
//向下降行
try{
if(lowLine-1 >= 0)
downRow(lowLine);
else
continueRun();
}
catch(BlockException e)
{
System.out.println(e.getMessage() );
stop();
}
}
//向下移动可移行==================================================
//只移动startRow以上的行
//lowLine:底线, 已删除的最大行号,此线以上行要下降,
//最低下降到lowLine
public void downRow(int lowLine) throws BlockException
{
if(lowLine < 0 || lowLine >= ROWS)
throw new BlockException(BETable.getMessage(8) + "WindowGrid.310" );
boolean hasDown = false; //本次函数是否发生过移动
Cell originCell = new Cell(); //小方块移动前位置
Cell finalCell = new Cell(); //小方块移动后位置
for(int col=0; col<COLS; col++)
{
for(int row=lowLine-1; row>=0; row--)
{
if(table[row][col] == null)
continue;
Block b = table[row][col];
originCell = getCell(b);
finalCell = getCell(b);
finalCell.row++;
if(finalCell.row > lowLine)
continue;
//循环下移小方块,直至不能下移为止
while(blockCanTo(finalCell) )
{
b.moveDown(1);
finalCell = getCell(b);
finalCell.row++;
if(finalCell.row > lowLine)
break;
}
//小方块原位置与最终位置不相同,表明移动了
//将小方块赋给最终位置
if(!originCell.equals(finalCell) )
{
table[originCell.row][originCell.col] = null;
table[finalCell.row-1][finalCell.col] = b;
hasDown = true;
}
}
}
//减行
if(hasDown)
clearRow();
else
continueRun();
}
//设置显示分数的编辑框, 这个编辑框是主窗口中InfoPanel面板上的编辑框
public void setScoreEditor(JEditorPane edt)
{
this.edtScore = edt;
}
//设置显示速度的编辑框,这个是主窗口中InfoPanel面板上的编辑框
public void setSpeedEditor(JEditorPane edt)
{
this.edtSpeed = edt;
}
//设置计算游戏开始时间的时钟指针, 这是主窗口中InfoPanel面板上的时钟
public void setPassedTimer(TNewTimer tmr)
{
this.tmrPassed = tmr;
}
//设置显示下一次方块组类型的面板, 这是主窗口中InfoPanel面板上的一个子面板
public void setNextTypeContainer(JPanel pal)
{
this.palNextType = pal;
}
/************************************************************************
*时钟监听器
*时钟启动时,即为游戏启动,
*自动产生方块组,并向下移动,移动到底部后,将方块组中
*小方块放到2维数组table,置方块组为空,然后重新生成新的方块组
***********************************************************************/
class timerListener implements TNewTimerListener
{
public void action(TNewTimerEvent event)
{
//方块组为空,产生新的方块组-------------------------------
if(blockGroup == null)
{
try
{
//产生一组小方块, 将其组织到方块组中
Block[] blocks = generateBlocks(4);
blockGroup = new BlockGroup(blockWidth * COLS / 2,
-blockWidth, blocks, nextType, nextSubType);
//产生下一次方块组类型
generateNextType();
}
catch(BlockException e)
{
stop();
try
{
System.out.println(e.getMessage() );
}
catch(HeadlessException ee){
System.out.println(ee.getMessage() );
}
return;
}
}
//方块组不空,向下移动方块组--------------------------------------
else
{
try{
if(groupCanTo("DOWN", 1) ) //向下移动方块组
blockGroup.moveDown(false);
else
{ //无法向下移动方块组, 解散方块组,将其小方块交由table管理
for(int i=0; i<blockGroup.getCount(); i++)
{
//判断是否为死局
Block block = blockGroup.get(i);
Cell cell = getCell(block);
if(cell.row < 0)
{
dead = true;
stop();
//游戏结束,信息面板中的时钟停止计时
if(tmrPassed != null)
tmrPassed.stop();
JOptionPane.showMessageDialog(null, "GAME OVER");
return;
}
//将小方块交由矩阵table
table[cell.row][cell.col] = block;
}
//清除满行
int startRow = blockGroup.getBottom() / blockWidth - 1;
clearRow();
//清空方块组,以使形成下次新的方块组
blockGroup = null;
}
}
catch(BlockException e)
{
timer.stop();
System.out.println(e.getMessage() );
}
}
}
}
/************************************************************************
*当前类的键盘监听器
*接受键盘输入:
*up: 变形
*left: 左移一格
*right: 右移一格
*down: 下移一格
***********************************************************************/
class panel_KeyListener extends KeyAdapter
{
public void keyPressed(KeyEvent event)
{
if(dead)
return;
if(blockGroup == null)
return;
int keyCode = event.getKeyCode();
try
{
if(keyCode == KeyEvent.VK_LEFT)
{
if(groupCanTo("LEFT", 1) )
blockGroup.moveLeft();
}
else if (keyCode == KeyEvent.VK_RIGHT)
{
if(groupCanTo("RIGHT", 1))
blockGroup.moveRight();
}
else if(keyCode == KeyEvent.VK_DOWN )
{
if(groupCanTo("DOWN" , 2))
blockGroup.moveDown(true);
}
else if(keyCode == KeyEvent.VK_UP)
{
if(groupCanTranslate())
blockGroup.translate();
}
}
catch(BlockException e)
{
timer.stop();
System.out.println(e.getMessage() );
}
catch(CloneNotSupportedException ce)
{
timer.stop();
System.out.println(ce.getMessage());
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -