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 );
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 );
}
闽公网安备 35060202000074号