}
private void movesnake( int direction )
{
if( mainsnake.checkbeanin( this.bean ) )
{
this.score += 100;
this.scorelabel.settext( this.info + " current score:" + this.score );
this.eatedbean.add( new point(this.bean) );
this.createbean( this.bean );
}
mainsnake.changedirection( (point) mainsnake.getlast(), direction );
point temp = (point) mainsnake.getfirst();
if( eatedbean.size() != 0 )
{
if( eatedbean.getfirst().equals( temp ) )
{
eatedbean.remove( 0 );
}
else
{
mainsnake.clearendsnakepiece( getgraphics(), temp.x, temp.y,
singlewidthx, singleheighty, coopos );
mainsnake.removetail();
}
}
else
{
mainsnake.clearendsnakepiece( getgraphics(), temp.x, temp.y,
singlewidthx, singleheighty, coopos );
mainsnake.removetail();
}
}
private void drawbeanandebean( graphics g )
{
g.setcolor( colorgroup.color_bean );
this.drawpiece( g, this.bean.x, this.bean.y );
g.setcolor( colorgroup.color_eatedbean );
snakesq = eatedbean.iterator();
while ( snakesq.hasnext() )
{
point temppoint = (point)snakesq.next();
this.drawpiece( g, temppoint.x, temppoint.y );
}
}
private void drawpiece( graphics g, int x, int y )
{
g.fillroundrect( this.singlewidthx * x + 1,
this.singleheighty * y + 1,
this.singlewidthx - 2,
this.singleheighty - 2,
this.coopos,
this.coopos );
}
private void createbean( point temp )
{
lp:
while( true )
{
temp.x = (int) (math.random() * this.horizontalgrid);
temp.y = (int) (math.random() * this.verticalgrid);
snakesq = mainsnake.iterator();
while ( snakesq.hasnext() )
{
if( snakesq.next().equals( new point( temp.x, temp.y ) ) )
{
continue lp;
}
}
break;
}
}
}
configmenu.java
package snakegame;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class configmenu extends jmenubar
{
gameframe owner;
jmenu[] menu;
jmenuitem[] menuitem;
jradiobuttonmenuitem[] speeditem, modelitem, standarditem;
private uimanager.lookandfeelinfo looks[];
public configmenu( gameframe owner )
{
this.owner = owner;
owner.setjmenubar( this );
string[] menu_name = {"snake game", "game configure", "game help"};
menu = new jmenu[menu_name.length];
for( int i = 0; i < menu_name.length; i++ )
{
menu[i] = new jmenu( menu_name[i] );
menu[i].setfont( new font( "courier", font.plain, 12 ) );
this.add( menu[i] );
}
string[] menuitem_name = {"start game", "stop game", "exit game",
"game color",
"about..."};
menuitem = new jmenuitem[menuitem_name.length];
for( int i = 0; i < menuitem_name.length; i++ )
{
menuitem[i] = new jmenuitem( menuitem_name[i] );
menuitem[i].setfont( new font( "courier", font.plain, 12 ) );
menuitem[i].addactionlistener( new actionhandler() );
}
menu[0].add( menuitem[0] );
menu[0].add( menuitem[1] );
menu[0].addseparator();
menu[0].add( menuitem[2] );
menu[1].add( menuitem[3] );
menu[2].add( menuitem[4] );
string[] inner_menu_name = {"game speed", "window model", "game standard "};
jmenu[] inner_menu = new jmenu[inner_menu_name.length];
for( int i = 0; i < inner_menu_name.length; i++ )
{
inner_menu[i] = new jmenu( inner_menu_name[i] );
inner_menu[i].setfont( new font( "courier", font.plain, 12 ) );
menu[1].add( inner_menu[i] );
}
buttongroup temp1 = new buttongroup();
string[] speeditem_name = {"speed-1", "