📄 singlebar.java
字号:
/*
* The class encapsulating the data needed to describe a bar,
* also containing the needed operations to control the movement
* of the bar.
*/
public class SingleBar {
public SingleLocation[] squares;//each bar has three squares
//private boolean isBottom;
public SingleGamePool gamePool;//the referrence to the SingleGamePool
//it contains all the information of the gameFace
public SingleBar()
{
squares=new SingleLocation[3];
for(int i=0;i<3;i++)
{
squares[i]=new SingleLocation();
}
this.gamePool=null;
}
public SingleBar(SingleGamePool pGamePool)
{
squares=new SingleLocation[3];
for(int i=0;i<3;i++)
{
squares[i]=new SingleLocation();
}
this.gamePool=pGamePool;
}
public SingleBar(SingleLocation[] pSquares)
{
this.squares=pSquares;
this.gamePool=null;
}
/**
* get and set gamePool
*/
public void setGamePool(SingleGamePool pGamePool)
{
this.gamePool=pGamePool;
}
public SingleGamePool getGamePool()
{
return this.gamePool;
}
/**
*get squares
*/
public SingleLocation[] getSquares()
{
return this.squares;
}
/**
* Check if the bar hits the ground or other bar
*/
public boolean checkBottom()
{
if(squares[2].getX()+1==SingleGamePool.getRow()
||SingleGamePool.get(this.squares[2].getX()+1, this.squares[2].getY())!=0)
return true;
else return false;
}
/**
* Judge if the bar is still in the field
*/
public boolean isInside()
{
if(squares[0].getX()>=0&&squares[2].getX()<SingleGamePool.getRow()
&&squares[0].getY()>=0&&squares[2].getY()<SingleGamePool.getColumn())
return true;
else return false;
}
/**
* Exchange the color of the bar
*/
public synchronized void shift()
{
SingleGamePool.clear(this);
int temp=squares[2].getColor();
squares[2].setColor(squares[1].getColor());
squares[1].setColor(squares[0].getColor());
squares[0].setColor(temp);
SingleGamePool.set(this);
}
/**
* Move the bar to the left
*/
public synchronized void left()
{
if(squares[0].getY()>0)
{
if(gamePool.gamePool[squares[0].x][squares[0].y-1]==0
&&gamePool.gamePool[squares[1].x][squares[1].y-1]==0
&&gamePool.gamePool[squares[2].x][squares[2].y-1]==0)
{
SingleGamePool.clear(this);
for(int i=0;i<3;i++)
{
squares[i].decreaseY();
}
SingleGamePool.set(this);
}
}
}
/**
* Move the bar to the right
*/
public synchronized void right()
{
if(squares[0].getY()<SingleGamePool.WIDTH-1)
{
if(gamePool.gamePool[squares[0].x][squares[0].y+1]==0
&&gamePool.gamePool[squares[1].x][squares[1].y+1]==0
&&gamePool.gamePool[squares[2].x][squares[2].y+1]==0)
{
SingleGamePool.clear(this);
for(int i=0;i<3;i++)
{
squares[i].increaseY();
}
SingleGamePool.set(this);
}
}
}
/**
* Drop the bar down one step
*/
public synchronized boolean dropOneStep()
{
if(squares[2].getX()+1<SingleGamePool.getRow()
&&SingleGamePool.get(this.squares[2].getX()+1, this.squares[2].getY())==0)
{
SingleGamePool.clear(this);
for(int i=0;i<3;i++)
{
squares[i].increaseX();
}
SingleGamePool.set(this);
return true;
}
else return false;
}
/**
* Drop the bar down till to the bottom
*/
public synchronized void drop()
{
boolean tag=dropOneStep();
while(tag)
{
tag=dropOneStep();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -