📄 gamecanvas.java
字号:
repaint();
}
public void addscore( int number)
{
score += number ;
repaint();
}
public void left()
{
int tempx , tempy ;
int i , j;
tempx = this.currentblockx - 1 ;
tempy = this.currentblocky ;
//看看有没有超过左边的边界
if( tempx < 0)
{
int outcount = -tempx - 1 ;//出来的那一列部分
for( j = 0; j < 4 ; j ++ )
if(this.currentblock[j][outcount] == 1)
return ;
}
//加入没有出界的话就检测有没有和其他的相碰
for( j = 0; j < 4; j ++ ) //|
for( i = 0; i < 4 ; i ++)//-
{
if( this.currentblock[j][i] == 0 )
continue ;
if(tempx + i > 9 || tempx + i < 0)
continue ;
if( tempy + j > 17 || tempy + j < 0)
continue ;
if( chess[tempx + i][tempy + j] == 1 )
return ;
}
//加入左边没有什么问题的话,就开始往左移动
this.currentblockx -- ;
repaint();
}
public void right()
{
int tempx , tempy ;
int i , j;
tempx = this.currentblockx + 1 ;
tempy = this.currentblocky ;
//看看有没有超过右边的边界
if( tempx + 3 > 9)
{
int outcount = 10 - tempx ;//出来的那一列部分
for( j = 0; j < 4 ; j ++ )
if(this.currentblock[j][outcount] == 1)
return ;
}
//加入没有出界的话就检测有没有和其他的相碰
for( j = 0; j < 4; j ++ )
for( i = 0; i < 4; i ++)
{
if( this.currentblock[j][i] == 0 )
continue ;
if(tempx + i > 9 || tempx + i < 0)
continue ;
if( tempy + j > 17 || tempy + j < 0)
continue ;
if( chess[tempx + i][tempy + j] == 1 )
return ;
}
//加入左边没有什么问题的话,就开始往左移动
this.currentblockx ++ ;
repaint();
}
public void Timerrun()
{
if( this.currentstate != this.GAME_START)
return ;
Timercount = ( Timercount + 1 ) % level ;
if(Timercount == 0)
{
if(!down())
this.getnextblock() ;
}
}
public boolean down()
{
int tempx , tempy ;
int i , j;
tempx = this.currentblockx ;
tempy = this.currentblocky + 1;
//看看有没有超过下边的边界
if( tempy + 3 > 17)
{
int outcount = 18 - tempy ;//出来的那一列部分
for( j = 0; j < 4 ; j ++ )
if(this.currentblock[outcount][j] == 1)
return false;
}
//加入没有出界的话就检测有没有和其他的相碰
for( j = 0; j < 4; j ++ )
for( i = 0; i < 4; i ++)
{
if( this.currentblock[j][i] == 0 )
continue ;
if(tempx + i > 9 || tempx + i < 0)
continue ;
if( tempy + j > 17 || tempy + j < 0)
continue ;
if( chess[tempx + i][tempy + j] == 1 )
return false;
}
//加入左边没有什么问题的话,就开始往左移动
this.currentblocky ++ ;
repaint();
return true ;
}
public GameCanvas()
{
//读取图片数据
int i , j;
for( i = 0 ;i < 10; i ++)
for( j = 0; j < 18; j ++)
chess[i][j] = 0 ;
if( !readpictures())
{
currentstate = this.GAME_ERR;
return ;
}
//初始化数据
wide = this.getWidth() ;
height = this.getHeight() ;
if(wide > imageground.getWidth())
{
backgroundx = ( wide - imageground.getWidth()) / 2 ;
backgroundy = ( height - imageground.getHeight()) / 2 ;
}
else
{
backgroundx = backgroundy = 0 ;
}
offscreen = Image.createImage(wide,height);
//产生下一个方块
this.createnextblock() ;
this.getnextblock() ;
// 初始化当前的方块,并且产生下一个方块
this.updatecurrentblock() ;
//test
timer = new Timer();
gametask = new GameTask(this);
timer.schedule(gametask, 300, 300);
this.addCommand(pausecommand);
this.addCommand(exitcommand) ;
this.setCommandListener(this);
}
public GameCanvas( Block block)
{
this.block = block ;
// 读取图片数据
int i , j;
for( i = 0 ;i < 10; i ++)
for( j = 0; j < 18; j ++)
chess[i][j] = 0 ;
if( !readpictures())
{
currentstate = this.GAME_ERR;
return ;
}
//初始化数据
wide = this.getWidth() ;
height = this.getHeight() ;
if(wide > imageground.getWidth())
{
backgroundx = ( wide - imageground.getWidth()) / 2 ;
backgroundy = ( height - imageground.getHeight()) / 2 ;
}
else
{
backgroundx = backgroundy = 0 ;
}
offscreen = Image.createImage(wide,height);
//产生下一个方块
this.createnextblock() ;
this.getnextblock() ;
// 初始化当前的方块,并且产生下一个方块
this.updatecurrentblock() ;
//test
timer = new Timer();
gametask = new GameTask(this);
timer.schedule(gametask, 300, 300);
this.addCommand(pausecommand);
this.addCommand(exitcommand) ;
this.setCommandListener(this);
}
public void gamestart( Graphics g)
{
int i , j ;
Graphics gg = offscreen.getGraphics() ;
this.clearCanvas(gg);
gg.drawImage(imageground,backgroundx,backgroundy,0);
//gg.setColor(175,75,168);
gg.setColor(this.colorlib[this.currentcolor][0],this.colorlib[this.currentcolor][1],this.colorlib[this.currentcolor][2]);
for( i = 0 ;i < 4 ; i ++ )
for( j = 0 ; j < 4 ; j ++ )
if( this.currentblock[i][j] == 1)
{
gg.fillRect(convertx(this.currentblockx+j),converty(this.currentblocky+i),7,7);
}
for( i = 0 ;i < 10 ;i ++)
for( j = 0 ; j < 18 ;j ++)
if( chess[i][j] == 1)
{
gg.setColor(this.colorlib[this.chesscolor[i][j]][0],this.colorlib[this.chesscolor[i][j]][1],this.colorlib[this.chesscolor[i][j]][2]);
gg.fillRect(convertx(i),converty(j),7,7);
}
drawscore(gg) ;
drawnextblock(gg);
g.drawImage(offscreen,0,0,0);
}
protected void paint(Graphics g) {
// TODO Auto-generated method stub
switch( currentstate)
{
case 0: // GAME_START
gamestart(g);
break ;
case 1://GAME_PAUSE:
break ;
case 2://GAME_HIGHLIGHT:
break ;
case 3://GAME_EXIT:
break ;
case 4://GAME_ERR:
break ;
case 5: //GAME_OVER
char Buffer[] = {'G','a','m','e',' ','O','v','e','r','!'};
g.drawChars(Buffer,0,9,wide/2-20,height/2,0);
this.removeCommand(pausecommand) ;
}
}
protected synchronized void keyPressed(int keyCode)
{
int i ;
if(this.currentstate != GAME_START) return ;
switch(this.getGameAction(keyCode))
{
case Canvas.LEFT:
left();
break ;
case Canvas.RIGHT:
right();
break ;
case Canvas.UP:
turn();
break ;
case Canvas.DOWN:
while(true)
{
if( !down() )
break;
}
this.getnextblock() ;
}
//System.out.print(this.currentblockx + ":" + this.currentblocky);
///System.out.println("AND "+this.currentblocklibx+":"+this.currentblockliby);
}
public void pause()
{
this.currentstate = this.GAME_PAUSE ;
}
public void start()
{
this.currentstate = this.GAME_START ;
}
public void commandAction(Command c, Displayable d)
{
if(c.getLabel().equals("Start"))
{
this.removeCommand(startcommand) ;
this.addCommand(pausecommand) ;
this.start();
repaint() ;
}
else if(c.getLabel().equals("Pause"))
{
this.pause() ;
this.removeCommand(pausecommand) ;
this.addCommand(startcommand) ;
}
else if(c.getLabel().equals("Exit"))
{
block.mangeaction(2,1);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -