snakegame.java
package snakegame;
import javax.swing.*;
public class snakegame
{
public static void main( string[] args )
{
jdialog.setdefaultlookandfeeldecorated( true );
gameframe temp = new gameframe();
}
}
snake.java
package snakegame;
import java.awt.*;
import java.util.*;
class snake extends linkedlist
{
public int snakedirection = 2;
public int snakeredirection = 4;
public snake()
{
this.add( new point( 3, 3 ) );
this.add( new point( 4, 3 ) );
this.add( new point( 5, 3 ) );
this.add( new point( 6, 3 ) );
this.add( new point( 7, 3 ) );
this.add( new point( 8, 3 ) );
this.add( new point( 9, 3 ) );
this.add( new point( 10, 3 ) );
}
public void changedirection( point temp, int direction )
{
this.snakedirection = direction;
switch( direction )
{
case 1://up
this.snakeredirection = 3;
this.add( new point( temp.x, temp.y - 1 ) );
break;
case 2://right
this.snakeredirection = 4;
this.add( new point( temp.x + 1, temp.y ) );
break;
case 3://down
this.snakeredirection = 1;
this.add( new point( temp.x, temp.y + 1 ) );
break;
case 4://left
this.snakeredirection = 2;
this.add( new point( temp.x - 1, temp.y ) );
break;
}
}
public boolean checkbeanin( point bean )
{
point temp = (point) this.getlast();
if( temp.equals( bean ) )
{
return true;
}
return false;
}
public void removetail()
{
this.remove( 0 );
}
public void drawsnake( graphics g, int singlewidthx, int singleheighty, int coopos )
{
g.setcolor( colorgroup.color_snake );
iterator snakesq = this.iterator();
while ( snakesq.hasnext() )
{
point temppoint = (point)snakesq.next();
this.drawsnakepiece( g, temppoint.x, temppoint.y,
singlewidthx, singleheighty, coopos );
}
}
public void drawsnakepiece( graphics g, int temp1, int temp2,
int singlewidthx, int singleheighty, int coopos )
{
g.fillroundrect( singlewidthx * temp1 + 1,
singleheighty * temp2 + 1,
singlewidthx - 2,
singleheighty - 2,
coopos,
coopos );
}
public void clearendsnakepiece( graphics g, int temp1, int temp2,
int singlewidthx, int singleheighty, int coopos )
{
g.setcolor( colorgroup.color_back );
g.fillroundrect( singlewidthx * temp1 + 1,
singleheighty * temp2 + 1,
singlewidthx - 2,
singleheighty - 2,
coopos,
coopos );
}
}
gameframe.java
package snakegame;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.awt.geom.*;
class gameframe extends jframe
{
private toolkit tempkit;
private int horizontalgrid, verticalgrid;
private int singlewidthx, singleheighty;
private int coopos;
private snake mainsnake;
private linkedlist eatedbean;
{
eatedbean = new linkedlist();
}
private iterator snakesq;
public javax.swing.timer snaketimer;
private int direction = 2;
private int score;
private string info;
private point bean, eatbean;
{
bean = new point();
}
private boolean flag;
private jmenubar infomenu;
private jmenu[] tempmenu;
private jmenuitem[] tempmenuitem;
private jradiobuttonmenuitem[] levelmenuitem, versionmenuitem;
private jlabel scorelabel;
{
scorelabel = new jlabel();
}
private graphics2d g;
private imageicon snakehead;
{
snakehead = new imageicon( "logo.gif" );
}
private configmenu configmenu;
private boolean hasstoped = true;
public gameframe()
{
this.tempkit = this.gettoolkit();
this.setsize( tempkit.getscreensize() );
this.setgrid( 60, 40, 5