📄 tetriscanvas.java
字号:
if (y < 0) {
gameState = GAME_OVER;
transmitMsg(GAME_OVER);
}
block.drawBlock(g);
block.drawNextBlock(g);
}
counter = 0;
}
else {
block.paint(g);
}
}
else if (gameState == GAME_OVER) {
g.setColor(BACKGROUND);
g.fillRect(0,0, getWidth(), getHeight());
g.setColor(255, 0, 0);
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD,
Font.SIZE_LARGE));
g.drawString("mGame Over",
TetrisCanvas.GAMEAREA_X + 8 * TetrisCanvas.BRICK_WIDTH,
TetrisCanvas.GAMEAREA_Y + 4 * TetrisCanvas.BRICK_WIDTH,
g.BASELINE | g.HCENTER);
transmitMsg(GAME_OVER);
}
else if (gameState == GAME_WIN)
{
g.setColor(BACKGROUND);
g.fillRect(0,0, getWidth(), getHeight());
g.setColor(255, 0, 0);
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD,
Font.SIZE_LARGE));
g.drawString("mYOU WIN",
TetrisCanvas.GAMEAREA_X + 8 * TetrisCanvas.BRICK_WIDTH,
TetrisCanvas.GAMEAREA_Y + 4 * TetrisCanvas.BRICK_WIDTH,
g.BASELINE | g.HCENTER);
}
if( gameState_Remote == GAME_OVER )
{
gameState = GAME_WIN;
}
//绘制远端用户的界面
if( !( Role.ROLE_SINGLE == role ) )
paint_Remote(g);
}//end paint
public void paint_Remote( Graphics g )
{
if( gameState == GAME_OVER )
{
gameState_Remote = GAME_WIN;
}
if (gameState_Remote == GAME_START_DEMO) { /* 游戏处于demo画面状态 */
if (!startDemoFlag_Remote) {
//游戏还没开始,显示demo画面
g.setColor(BACKGROUND);
g.fillRect(0 , 0, mainWidth_Remote, mainHeight_Remote);
g.setColor(0, 255, 0);
g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD,
Font.SIZE_LARGE));
g.drawString("s俄罗斯方块(远端)", mainWidth_Remote / 2, mainHeight_Remote / 4 ,
g.BASELINE | g.HCENTER);
}
/* 游戏第一次启动 */
}
else if (gameState_Remote == GAME_WAIT_REMOTE)
{
g.setColor(BACKGROUND);
g.fillRect(0, 0, mainWidth_Remote, mainHeight_Remote);
g.setColor(0, 255, 0);
g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD,
Font.SIZE_LARGE));
g.drawString("s等待(远端)", mainWidth_Remote / 2, mainHeight_Remote / 4 ,
g.BASELINE | g.HCENTER);
}
else if (gameState_Remote == GAME_INIT) {
map_Remote.init();
map_Remote.paint(g);
block_Remote.init();
block_Remote.drawBlock(g);
block_Remote.drawNextBlock(g);
gameState_Remote = GAME_RUN;
}
else if (gameState_Remote == GAME_RUN) {
if( b_receviedMapdata )
{
map_Remote.repaintMap_Remote(g);
b_receviedMapdata = false;
}
if( b_receviedPN )
{
block_Remote.eraseBlock(g);
block_Remote.drawNextBlock(g);
b_receviedPN = false;
}
}//end if( game == GAME_RUN)
else if (gameState_Remote == GAME_OVER) {
g.setColor(BACKGROUND);
g.fillRect(0, 0,
mainWidth_Remote, mainHeight_Remote);
g.setColor(255, 0, 0);
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD,
Font.SIZE_LARGE));
g.drawString("sGame Over",
GAMEAREA_X_REMOTE + 8 * BRICK_WIDTH_REMOTE,
GAMEAREA_Y_REMOTE + 4 * BRICK_WIDTH_REMOTE,
g.BASELINE | g.HCENTER);
}
else if( gameState_Remote == GAME_DISCONNECTED )
{
g.setColor(BACKGROUND);
g.fillRect(0, 0,
mainWidth_Remote, mainHeight_Remote);
g.setColor(255, 0, 0);
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD,
Font.SIZE_LARGE));
g.drawString("sOTHER DISCONNECTED!",
GAMEAREA_X_REMOTE + 8 * BRICK_WIDTH_REMOTE,
GAMEAREA_Y_REMOTE + 4 * BRICK_WIDTH_REMOTE,
g.BASELINE | g.HCENTER);
}
else if( gameState_Remote == GAME_EXIT )
{
g.setColor(BACKGROUND);
g.fillRect(0, 0,
mainWidth_Remote, mainHeight_Remote);
g.setColor(255, 0, 0);
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD,
Font.SIZE_LARGE));
g.drawString("sOTHER EXIT",
GAMEAREA_X_REMOTE + 8 * BRICK_WIDTH_REMOTE,
GAMEAREA_Y_REMOTE + 4 * BRICK_WIDTH_REMOTE,
g.BASELINE | g.HCENTER);
}
else if( gameState_Remote == GAME_WIN )
{
g.setColor(BACKGROUND);
g.fillRect(0, 0,
mainWidth_Remote, mainHeight_Remote);
g.setColor(255, 0, 0);
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD,
Font.SIZE_LARGE));
g.drawString("sOTHER WIN",
GAMEAREA_X_REMOTE + 8 * BRICK_WIDTH_REMOTE,
GAMEAREA_Y_REMOTE + 4 * BRICK_WIDTH_REMOTE,
g.BASELINE | g.HCENTER);
}
System.gc();
}
public static void clear(Graphics g) {
// 绘制整个背景
g.setColor(0x00000000);
g.fillRect(0, offSet, mainWidth, mainHeight);
//绘制面板背景
g.setColor(BACKGROUND);
g.fillRect(TetrisCanvas.GAMEAREA_X, TetrisCanvas.GAMEAREA_Y,
16 * TetrisCanvas.BRICK_WIDTH, 16 * TetrisCanvas.BRICK_WIDTH);
}
public static void clear_Remote(Graphics g)
{
//绘制整个背景
g.setColor(0x00000000);
g.fillRect(0, 0, mainWidth_Remote, mainHeight_Remote);
//绘制面板背景
g.setColor(BACKGROUND);
g.fillRect(TetrisCanvas.GAMEAREA_X_REMOTE, GAMEAREA_Y_REMOTE,
16 * TetrisCanvas.BRICK_WIDTH_REMOTE, 16 * TetrisCanvas.BRICK_WIDTH_REMOTE);
}
public void sendPN()
{
transmitMsg( SEND_PN );
transmitMsg( block.getPattern());
transmitMsg( block.getNext());
}
public synchronized void transmitMsg(int action)
{
if( Role.ROLE_SINGLE == role )
return;
/*
* 如果远端退出或断开,则不必再传送数据
*/
if( GAME_EXIT == gameState_Remote || GAME_DISCONNECTED == gameState_Remote )
return;
for( int i=0; i<btConnections.length; i++ )
{
try
{
OutputStream os = btConnections[i].getOutputStream();
os.write( action );
os.flush();
}
catch( IOException e )
{
btConnections[i].close();
if( checkIfAllClosed() )
{
return;
}
}
}
}
public void sendMapdata()
{
if( Role.ROLE_SINGLE == role )
return;
if( GAME_EXIT == gameState_Remote || GAME_DISCONNECTED == gameState_Remote )
return;
transmitMsg(SEND_MAPDATA);
for( int i=0; i<btConnections.length; i++ )
{
serialize(map,btConnections[i]);
}
}
public boolean checkIfAllClosed()
{
boolean allclosed = true;
for( int l=0; l<btConnections.length; l++ )
{
if( btConnections[l].isClosed() != true )
{
allclosed = false;
}
}
if( allclosed )
{
}
return allclosed;
}
/**
* The ReceiveThread is used to receive the remote keypresses. <br>
* For each remote device there exists an own RecieveThread.
*/
private class ReceiveThread
extends Thread
{
int index;
int receviedNum;
InputStream inputStream;
/**
* Constructor.
* @param i Index, that corresponds to the number of the BluetoothConnection.
*/
public ReceiveThread( int i )
{
// Store
index = i;
inputStream = btConnections[index].getInputStream();
}
/**
* Reads from stream until end of stream reached (disconnect).<br>
* The read character (which is the key the remote user pressed) is
* displayed to the local user.
*/
public void run()
{
// Read input stream (data from remote device)
int inp_Action;
while( true )
{
// Read (blocking)
try
{
//inp_Action = btConnections[index].getInputStream().read();
inp_Action = inputStream.read();
}
catch( IOException e )
{
btConnections[index].close();
checkIfAllClosed();
gameState_Remote = GAME_DISCONNECTED;
return;
}
if( inp_Action == -1 )
{
btConnections[index].close();
checkIfAllClosed();
bug.println("inp_action = -1, Remote user is disconnectioned!");
gameState_Remote = GAME_DISCONNECTED;
return;
}//end if
//add by wts 20060912
//这里应该分成四部分:
//一个是对传入命令的解析(响应commandAction),
//一个是对按键的响应(action)
//一个是接收patter,next;
//一个是通知游戏结束状态
switch( inp_Action )
{
case GAME_START_DEMO:
case GAME_WAIT:
case GAME_INIT:
case GAME_RUN:
case GAME_SUSPEND:
case GAME_OVER:
case GAME_EXIT:
updateRemoteGameState(inp_Action);
break;
case SEND_PN:
receivePN(inputStream, inp_Action);
break;
case Canvas.UP:
case Canvas.DOWN:
case Canvas.LEFT:
case Canvas.RIGHT:
case Canvas.FIRE:
//什么都不做
break;
case SEND_MAPDATA:
recevieMapdata();
default:
}
if( GAME_EXIT == gameState_Remote )
{
bug.println("Remote have exited!, ReceiveThread won't recevie data!");
break;
}
}// end while
}// end run
}// end ReceiveThread
//接收到的是远端用户的新的状态
public synchronized void updateRemoteGameState( int _gameState )
{
//如果对方发送来GAME_WAIT状态信息,则证明自己还没有点击"开始"键
//(仅建立连接)
if ( _gameState == GAME_WAIT )
{
gameState_Remote = GAME_WAIT_REMOTE;
return;
}else if( _gameState == GAME_INIT )
{
gameState = GAME_INIT;
gameState_Remote = GAME_INIT;
sendPN();
return;
}
gameState_Remote = _gameState;
}//end updateRemoteCommand
//判断是否接收到新的当前方块和下一个方块
boolean b_receviedPN ;
public synchronized boolean receivePN( InputStream inputStream, int inp_Action)
{
if( inp_Action == this.SEND_PN )
{
try
{
pattern_Remote = inputStream.read();
next_Remote = inputStream.read();
}
catch(IOException e )
{
try {
throw new Exception("ERROR In read Remote pattern and next");
} catch (Exception e1) {
e1.printStackTrace();
}
}
block_Remote.setPN(pattern_Remote,next_Remote);
block_Remote.init();
bug.println(" receivePN.recevied PN");
b_receviedPN = true;
return true;
}
return false;
}
boolean b_receviedMapdata ;
public void recevieMapdata()
{
//能设置一个变量进行检测最好,
bug.println("begin receviedMapdata");
deserialize(map_Remote,btConnections[0]);
b_receviedMapdata = true;
bug.println("end receviedMapdata");
}
public void serialize(Serialization serialization,
BluetoothConnection bluetoothConnection)
{
bug.println("beging serialize mapdata");
OutputStream os = bluetoothConnection.getOutputStream();
try {
data = serialization.serialize();
//数据传输:
//1、把数据序列化后的长度传过去
//2、byte数组传过去。
bluetoothConnection.writeInt(data.length);
bug.println("send mapdata length = " + data.length);
os.write(data);
os.flush();
data = null;
} catch (IOException e) {
System.out.println("Error coming from serialize");
}
}
public void deserialize(Serialization serialization,
BluetoothConnection bluetoothConnection)
{
InputStream is = bluetoothConnection.getInputStream();
try {
int length = bluetoothConnection.readInt();
bug.println("recevied data length = " + length);
data = new byte[length];
is.read(data);
serialization.deserialize(data);
data = null;
System.gc();
} catch (IOException e) {
System.out.println("Error:Coming from deserizlize");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -