📄 gameinterface.java~91~
字号:
package russion;
import java.awt.*;
/**
* <p>Title: RussionCube</p>
* <p>Description: TheClassicGame</p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: Cqu</p>
* @author Ghostliang
* @version 1.0
*/
public class GameInterface{
private int startX,startY;
private Block[][] interBlock;
private int totalRow,totalCol;
private int blockSize;
private boolean isAlive,isRun;
private Color backGround;
private Color[] colors;
public boolean changed = false;
public GameInterface()
{
this(20,20,20,11,20,Color.white);
}
public GameInterface(int startX,int startY,int totalRow,int totalCol,int blockSize,Color backGround)
{
this.startX = startX;
this.startY = startY;
this.totalRow = totalRow;
this.totalCol = totalCol;
this.blockSize = blockSize;
this.backGround = backGround;
this.isAlive = true;
this.isRun = false;
this.interBlock = new Block[totalRow][totalCol];
for(int row = 0;row < totalRow;row++)
for(int col = 0;col < totalCol;col++)
interBlock[row][col] = new Block();
}
public void setStartXY(int x,int y)
{
startX = x;
startY = y;
}
public int getStartX()
{
return startX;
}
public int getStartY()
{
return startY;
}
public void setBlockSize(int newSize)
{
blockSize = newSize;
}
public int getBlockSize()
{
return blockSize;
}
public int getTotalRow()
{
return totalRow;
}
public int getTotalCol()
{
return totalCol;
}
public void setBackGround(Color newColor)
{
backGround = newColor;
}
public Color getBackGround()
{
return backGround;
}
public void setAlive(boolean isAlive)
{
this.isAlive = isAlive;
}
public boolean isAlived()
{
return isAlive;
}
public void setRun(boolean isRun)
{
this.isRun = isRun;
}
public boolean isRunning()
{
return isRun;
}
public void setBlock(Block block,int row,int col)
{
interBlock[row][col] = block;
}
public Block getBlock(int row,int col)
{
return interBlock[row][col];
}
public boolean initCurrentCube(Cube currentCube)
{
currentCube.setStartRowCol(0,totalCol / 2 - 1);
int row;
int col;
for(int i = 0;i < 4;i++)
{
row = currentCube.getBlock(i).getRow();
col = currentCube.getBlock(i).getColumn() + totalCol / 2 - 1;
if(this.getBlock(row,col).isFilled())
return false;
}
currentCube.setBlockSize(blockSize);
return true;
}
public void setCurrentCube(Cube currentCube)
{
int row;
int column;
Color color;
for(int i = 0;i < 4;i++)
{
row = currentCube.getBlock(i).getRow() + currentCube.getStartRow();
column = currentCube.getBlock(i).getColumn() + currentCube.getStartCol();
color = currentCube.getBackGround();
this.setBlock(new Block(true,true,color),row,column);
}
}
public void clearCurrentCube()
{
for(int row = 0;row < totalRow;row++)
for(int column = 0;column < totalCol;column++)
if(this.getBlock(row,column).isActived())
this.getBlock(row,column).clear();
}
public void fireCurrentCube()
{
for(int row = 0;row < totalRow;row++)
for(int column = 0;column < totalCol;column++)
if(this.getBlock(row,column).isActived())
this.getBlock(row,column).setActive(false);
}
public void clear()
{
for(int row = 0;row < totalRow;row++)
for(int column = 0;column < totalCol;column++)
this.getBlock(row,column).clear();
}
public boolean canGoLeft(Cube cube)
{
int row = cube.getStartRow();
int col = cube.getStartCol();
Block now;
for(int i = 0;i < 4;i++)
{
if((col + cube.getBlock(i).getColumn() - 1) < 0)
return false;
now = this.getBlock(row + cube.getBlock(i).getRow(),col + cube.getBlock(i).getColumn() - 1);
if((now.isFilled() && !now.isActived()))
return false;
}
return true;
}
public boolean canGoRight(Cube cube)
{
int row = cube.getStartRow();
int col = cube.getStartCol();
Block now;
for(int i = 0;i < 4;i++)
{
if((col + cube.getBlock(i).getColumn() + 1) > totalCol - 1)
return false;
now = this.getBlock(row + cube.getBlock(i).getRow(),col + cube.getBlock(i).getColumn() + 1);
if((now.isFilled() && !now.isActived()))
return false;
}
return true;
}
public boolean canGoDown(Cube cube)
{
int row = cube.getStartRow();
int col = cube.getStartCol();
Block now;
for(int i = 0;i < 4;i++)
{
if((row + cube.getBlock(i).getRow() + 1) > totalRow - 1)
return false;
now = this.getBlock(row + cube.getBlock(i).getRow() + 1,col + cube.getBlock(i).getColumn());
if((now.isFilled() && !now.isActived()))
return false;
}
return true;
}
public boolean canTranslate(Cube cube)
{
int row = cube.getStartRow();
int col = cube.getStartCol();
for(int i = 0;i < 4;i++)
if(this.getBlock(cube.getBlock(i).getColumn() + row,cube.getTotal() - cube.getBlock(i).getRow() - 1 + col).isFilled() || cube.getBlock(i).getColumn() + row > totalRow - 1 || cube.getTotal() - cube.getBlock(i).getRow() + col - 1 < 0 || cube.getTotal() - cube.getBlock(i).getRow() + col - 1 > totalCol - 1)
return false;
return true;
}
public boolean rowIsFull(int row)
{
for(int i = 0;i < totalCol;i++)
if(!interBlock[row][i].isFilled())
return false;
return true;
}
public void deleteRow(int row)
{
for(int i = row;i > 0;i--)
for(int j = 0;j < totalCol;j++)
interBlock[i][j] = interBlock[i - 1][j];
for(int i = 0;i < totalCol;i++)
interBlock[0][i] = new Block();
}
public int clearRows()
{
int rows = 0;
for(int i = 0;i < totalRow;i++)
if(this.rowIsFull(i))
{
this.deleteRow(i);
rows++;
}
return rows;
}
public void paint(Graphics g)
{
g.translate(startX,startY);
g.setColor(Color.black);
g.drawRect(0,0,totalCol * blockSize,totalRow * blockSize);
g.setColor(backGround);
g.fillRect(1,1,totalCol * blockSize - 1,totalRow * blockSize -1 );
for(int row = 0;row < totalRow;row++)
for(int col = 0;col < totalCol;col++)
if(interBlock[row][col].isFilled())
{
g.setColor(interBlock[row][col].getColor());
g.fillRect(col * blockSize + 1,row * blockSize + 1,blockSize - 1,blockSize - 1);
}
else
{
g.setColor(this.backGround);
g.fillRect(col * blockSize + 1,row * blockSize + 1,blockSize - 1,blockSize - 1);
}
g.translate(-startX,-startY);
}
public void repaint(Graphics g)
{
g.translate(startX,startY);
for(int row = 0;row < totalRow;row++)
for(int col = 0;col < totalCol;col++)
if(interBlock[row][col].isFilled())
{
g.setColor(interBlock[row][col].getColor());
g.fillRect(col * blockSize + 1,row * blockSize + 1,blockSize - 1,blockSize - 1);
}
else
{
g.setColor(this.backGround);
g.fillRect(col * blockSize + 1,row * blockSize + 1,blockSize - 1,blockSize - 1);
}
g.translate(-startX,-startY);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -