📄 foodmaker.java
字号:
/********************************************************************************************/
/* */
/* FoodMaker.java */
/* */
/* 果实建造线程,与蛇的运动独立 */
/* */
/* Programed by Luo Pengkui on 2004-9 */
/* */
/********************************************************************************************/
import java.util.Observable;
import java.awt.Point;
import java.io.PrintStream;
import java.util.Calendar;
import java.util.Random;
//------------------------------------ BEGIN ------------------------------------------------
public class FoodMaker extends Observable implements Runnable
{
private int m_amountofFoods; // 果实数量
private FoodModel m_foods[];
private MatrixModel m_matrix;
private int m_raw;
private int m_col;
private boolean m_fruitAutoGrown; // 果实被吃后是否“立即”生成
private Random m_rand;
public static final int EMPTY = 0; // 格点的四种状态
public static final int FRUIT = 1;
public static final int SNAKE = 2;
public static final int WALL = 3;
public FoodMaker( MatrixModel canvasMatrix )
{
m_matrix = canvasMatrix;
}
public void mb_start( int canvasCol, int canvasRow, // 初始化果实建造器
int amountofFoods, boolean fruitAutoGrown )
{
m_raw = canvasRow;
m_col = canvasCol;
m_amountofFoods = amountofFoods;
m_fruitAutoGrown = fruitAutoGrown;
m_foods = new FoodModel[m_amountofFoods];
m_rand = new Random();
for(int i = 0; i < m_amountofFoods; i++) // 果实的初次建造
{
int x,y;
m_foods[i] = mb_makeFood();
do{
x = m_rand.nextInt(m_col-1); // 位置为伪随机数
y = m_rand.nextInt(m_raw-1);
} while ( m_matrix.mb_getGridState(x,y) != EMPTY );
Point position = new Point();
position.setLocation( x,y );
m_foods[i].mb_setPosition( position );
m_matrix.mb_setGridState( x,y,FRUIT );
}
(new Thread( this )).start(); // 开启果实复查建造线程
}
public FoodModel mb_makeFood() // 建造单个果实,随机设定生存期,用到Calendar类
{
Calendar rightNow = Calendar.getInstance();
int seconds = rightNow.get(10) * 60 * 60 + rightNow.get(12) * 60 + rightNow.get(13);
FoodModel food = new FoodModel();
food.mb_setLife(7 + m_rand.nextInt(7));
food.mb_setOverTime(seconds + food.mb_getLife());
return food;
}
public FoodModel[] mb_getFoods() // 返回所有果实的引用,
{ // MainFrame中启动View监视器要用到
return m_foods;
}
public void foodCheck() // 检查果实是否“腐败”,重新生成之
{ // 现存果实的总量不变
Calendar rightNow = Calendar.getInstance();
int seconds = rightNow.get(10) * 60 * 60 + rightNow.get(12) * 60 + rightNow.get(13);
m_rand = new Random();
for(int i = 0; i < m_amountofFoods; i++)
{
int x = m_foods[i].mb_getPosition().x;
int y = m_foods[i].mb_getPosition().y;
if(( (m_foods[i].mb_getOverTime()<seconds) )
|| (m_matrix.mb_getGridState(x,y) != FRUIT ) ) // 如果果实“腐败”或者被蛇吃就重新生成
{
m_matrix.mb_setGridState( x,y,EMPTY ); // 释放原来果实所占格点
m_foods[i] = mb_makeFood();
do{
x = m_rand.nextInt(m_col);
y = m_rand.nextInt(m_raw);
} while ( m_matrix.mb_getGridState(x,y) != EMPTY );// 只能在空的地方生成
Point position = new Point();
position.setLocation( x,y );
m_foods[i].mb_setPosition( position );
m_matrix.mb_setGridState( x,y,FRUIT );
}
}
setChanged(); // Model通知View数据已经更新, in Class Observable
notifyObservers();
}
public void run() // 运行线程定期检查果实状态
{
do
{
foodCheck();
try
{
Thread.sleep(100L);
}
catch(InterruptedException e)
{
System.out.println("Exception Message: ".concat(String.valueOf(e)));
}
} while(true);
}
}
//------------------------------------- END -------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -