this.getcontentpane().setbackground( colorgroup.color_back );
this.setundecorated( true );
this.setresizable( false );
this.addkeylistener( new keyhandler() );
gameframe.this.snaketimer = new javax.swing.timer( 80, new timerhandler() );
this.getcontentpane().add( scorelabel, borderlayout.south );
this.scorelabel.setfont( new font( "fixedsys", font.bold, 15 ) );
this.scorelabel.settext( "pause[space] - exit[esc]" );
this.configmenu = new configmenu( this );
this.setvisible( true );
}
public void setgrid( int temp1, int temp2, int temp3 )
{
this.horizontalgrid = temp1;
this.verticalgrid = temp2;
this.singlewidthx = this.getwidth() / temp1;
this.singleheighty = this.getheight() / temp2;
this.coopos = temp3;
}
private class keyhandler extends keyadapter
{
public void keypressed( keyevent e )
{
if( e.getkeycode() == 27 )
{
snaketimer.stop();
if( joptionpane.showconfirmdialog( null, "are you sure to exit?" ) == 0 )
{
system.exit( 0 );
}
snaketimer.start();
}
else if( e.getkeycode() == 37 && mainsnake.snakedirection != 2 )//left
{
direction = 4;
}
else if( e.getkeycode() == 39 && mainsnake.snakedirection != 4 )//right
{
direction = 2;
}
else if( e.getkeycode() == 38 && mainsnake.snakedirection != 3 )//up
{
direction = 1;
}
else if( e.getkeycode() == 40 && mainsnake.snakedirection != 1 )//down
{
direction = 3;
}
else if( e.getkeycode() == 32 )
{
if( !hasstoped )
{
if( !flag )
{
snaketimer.stop();
configmenu.setvisible( true );
configmenu.setmenuenable( false );
flag = true;
}
else
{
snaketimer.start();
configmenu.setvisible( false );
configmenu.setmenuenable( true );
flag = false;
}
}
}
}
}
private class timerhandler implements actionlistener
{
public synchronized void actionperformed( actionevent e )
{
point temp = (point) mainsnake.getlast();
snakesq = mainsnake.iterator();
while ( snakesq.hasnext() )
{
point temppoint = (point)snakesq.next();
if( temp.equals( temppoint ) && snakesq.hasnext() != false )
{
snaketimer.stop();
stopgame();
joptionpane.showmessagedialog( null,
"your score is " + score + "\n\nyou loss!" );
}
}
system.out.println( temp.x + " " + temp.y );
if( (temp.x == 0 && direction == 4) ||
(temp.x == horizontalgrid-1 && direction == 2) ||
(temp.y == 0 && direction == 1) ||
(temp.y == verticalgrid-1 && direction == 3) )
{
snaketimer.stop();
stopgame();
joptionpane.showmessagedialog( null,
"your score is " + score + "\n\nyou loss!" );
}
if( direction != mainsnake.snakeredirection )
{
movesnake( direction );
}
mainsnake.drawsnake( getgraphics(), singlewidthx, singleheighty, coopos );
drawbeanandebean( getgraphics() );
}
}
public void stopgame()
{
this.hasstoped = true;
this.snaketimer.stop();
graphics2d g = (graphics2d) gameframe.this.getgraphics();
g.setcolor( colorgroup.color_back );
super.paint( g );
configmenu.setvisible( true );
}
public void resetgame()
{
system.gc();
this.hasstoped = false;
graphics2d g = (graphics2d) gameframe.this.getgraphics();
g.setcolor( colorgroup.color_back );
super.paint( g );
this.mainsnake = new snake();
this.createbean( bean );
this.eatedbean.clear();
mainsnake.drawsnake( getgraphics(), singlewidthx, singleheighty, coopos );
this.snaketimer.start();
this.direction = 2;
this.score = 0;
this.scorela