欢迎光临
我们一直在努力

java小游戏-贪吃蛇-JSP教程,Java技巧及代码

建站超值云服务器,限时71元/月

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 );

}

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.scorelabel.settext( "pause[space] – exit[esc]" );

}

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", "speed-2", "speed-3", "speed-4", "speed-5"};

speeditem = new jradiobuttonmenuitem[speeditem_name.length];

for( int i = 0; i < speeditem_name.length; i++ )

{

speeditem[i] = new jradiobuttonmenuitem( speeditem_name[i] );

inner_menu[0].add( speeditem[i] );

speeditem[i].setfont( new font( "courier", font.plain, 12 ) );

speeditem[i].additemlistener( new itemhandler() );

temp1.add( speeditem[i] );

}

buttongroup temp2 = new buttongroup();

string[] modelitem_name = { "linux", "mac", "windows" };

modelitem = new jradiobuttonmenuitem[modelitem_name.length];

for( int i = 0; i < modelitem_name.length; i++ )

{

modelitem[i] = new jradiobuttonmenuitem( modelitem_name[i] );

inner_menu[1].add( modelitem[i] );

modelitem[i].setfont( new font( "courier", font.plain, 12 ) );

modelitem[i].additemlistener( new itemhandler() );

temp2.add( modelitem[i] );

}

buttongroup temp3 = new buttongroup();

string[] standarditem_name = { "60 * 40", "45 * 30", "30 * 20" };

standarditem = new jradiobuttonmenuitem[standarditem_name.length];

for( int i = 0; i < standarditem_name.length; i++ )

{

standarditem[i] = new jradiobuttonmenuitem( standarditem_name[i] );

inner_menu[2].add( standarditem[i] );

standarditem[i].setfont( new font( "courier", font.plain, 12 ) );

standarditem[i].additemlistener( new itemhandler() );

temp3.add( standarditem[i] );

}

looks = uimanager.getinstalledlookandfeels();

}

private class actionhandler implements actionlistener

{

public void actionperformed( actionevent e )

{

if( e.getsource() == menuitem[0] )

{

owner.resetgame();

configmenu.this.setvisible( false );

}

else if( e.getsource() == menuitem[1] )

{

owner.stopgame();

configmenu.this.setvisible( true );

configmenu.this.setmenuenable( true );

}

else if( e.getsource() == menuitem[2] )

{

system.exit( 0 );

}

else if( e.getsource() == menuitem[3] )

{

configdialog temp = new configdialog( owner );

temp.setvisible( true );

}

else if( e.getsource() == menuitem[4] )

{

joptionpane.showmessagedialog( null, "sanke game 2.0 version!\n\n" +

"author: finalcore\n\n" );

}

}

}

private class itemhandler implements itemlistener

{

public void itemstatechanged( itemevent e )

{

for( int i = 0; i < speeditem.length; i++ )

{

if( e.getsource() == speeditem[i] )

{

owner.snaketimer.setdelay( 150 – 30 * i );

}

}

if( e.getsource() == standarditem[0] )

{

owner.setgrid( 60, 40, 5 );

}

else if( e.getsource() == standarditem[1] )

{

owner.setgrid( 45, 30, 10 );

}

else if( e.getsource() == standarditem[2] )

{

owner.setgrid( 30, 20, 15 );

}

for( int i = 0; i < modelitem.length; i++ )

{

if( e.getsource() == modelitem[i] )

{

try

{

uimanager.setlookandfeel( looks[i].getclassname() );

}catch(exception ex){}

}

}

}

}

public void setmenuenable( boolean temp )

{

menu[1].setenabled( temp );

}

}

configdialog.java

package snakegame;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class configdialog extends jdialog

{

private container c;

private jframe owner;

private ownpanel[] panel = new ownpanel[4];

box box1, box2;

private jbutton commitbutton, cancelbutton;

color[] color = new color[4];

public configdialog( frame owner )

{

this.owner = (jframe) owner;

this.setsize( 400, 200 );

this.setresizable( false );

this.settitle( "config your game" );

this.c = this.getcontentpane();

this.c.setbackground( color.white );

this.c.setlayout( new flowlayout() );

this.box1 = box.createverticalbox();

for( int i = 0; i < panel.length; i++ )

{

panel[i] = new ownpanel();

panel[i].addactionlistener( new actionhandler() );

this.box1.add( panel[i] );

this.box1.add( box.createverticalstrut( 4 ) );

}

this.panel[0].settext( " background" );

this.panel[1].settext( " snake" );

this.panel[2].settext( " bean" );

this.panel[3].settext( " eatedbean" );

this.panel[0].setback( colorgroup.color_back );

this.panel[1].setback( colorgroup.color_snake );

this.panel[2].setback( colorgroup.color_bean );

this.panel[3].setback( colorgroup.color_eatedbean );

this.box2 = box.createhorizontalbox();

this.commitbutton = new jbutton( "确定" );

this.commitbutton.setfont( font.getfont( "fixedsys" ) );

this.commitbutton.addactionlistener( new actionhandler() );

this.cancelbutton = new jbutton( "取消" );

this.cancelbutton.setfont( font.getfont( "fixedsys" ) );

this.cancelbutton.addactionlistener( new actionhandler() );

this.box2.add( this.commitbutton );

this.box2.add( box.createhorizontalstrut( 20 ) );

this.box2.add( this.cancelbutton );

this.box1.add( this.box2 );

this.c.add( this.box1, borderlayout.north );

this.setlocation( ( this.gettoolkit().getscreensize().width – this.getwidth() )/2,

( this.gettoolkit().getscreensize().height – this.getheight() )/2 );

this.setvisible( true );

}

public void setownercolor( color temp )

{

this.owner.getcontentpane().setbackground( temp );

}

private class actionhandler implements actionlistener

{

public void actionperformed( actionevent e )

{

for( int i = 0; i < color.length; i++ )

{

if( e.getsource() == panel[i].rebutton() )

{

color[i] = jcolorchooser.showdialog( configdialog.this,

"choose background color",

color.white );

if( color[i] != null )

{

panel[i].setback( color[i] );

}

}

}

if( e.getsource() == commitbutton )

{

color[0] = (color[0]==null?colorgroup.color_back:color[0]);

color[1] = (color[1]==null?colorgroup.color_snake:color[1]);

color[2] = (color[2]==null?colorgroup.color_bean:color[2]);

color[3] = (color[3]==null?colorgroup.color_eatedbean:color[3]);

configdialog.this.setvisible( false );

colorgroup.setcolor_back( color[0] );

owner.getcontentpane().setbackground( color[0] );

colorgroup.setcolor_snake( color[1] );

colorgroup.setcolor_bean( color[2] );

colorgroup.setcolor_eatedbean( color[3] );

configdialog.this.dispose();

}

else if( e.getsource() == cancelbutton )

{

configdialog.this.setvisible( false );

configdialog.this.dispose();

}

}

}

}

class ownpanel extends jpanel

{

private jlabel temp1;

private jtextfield temp2;

private jbutton temp3;

ownpanel()

{

temp1 = new jlabel();

temp1.setfont( font.getfont( "fixedsys" ) );

temp2 = new jtextfield();

temp3 = new jbutton( "change" );

temp3.setfont( font.getfont( "fixedsys" ) );

temp2.seteditable( false );

temp2.setcolumns( 10 );

this.add( temp1 );

this.add( temp2 );

this.add( temp3 );

this.setlayout( new gridlayout( 1, 3 ) );

}

public void setbuttonname( string temp )

{

temp3.setname( temp );

}

public void setback( color temp )

{

temp2.setbackground( temp );

}

public void settext( string temp )

{

temp1.settext( temp );

}

public object rebutton()

{

return temp3;

}

public void addactionlistener( actionlistener ac )

{

temp3.addactionlistener( ac );

}

}

tools.java

package snakegame;

import java.awt.*;

import javax.swing.*;

class colorgroup

{

static color color_back = color.white;

static color color_snake = new color( 43, 110, 187 );

static color color_bean = color.blue;

static color color_eatedbean = color.cyan;

static void setcolor_back( color temp )

{

color_back = temp;

}

static void setcolor_snake( color temp )

{

color_snake = temp;

}

static void setcolor_bean( color temp )

{

color_bean = temp;

}

static void setcolor_eatedbean( color temp )

{

color_eatedbean = temp;

}

}

final class menuget

{

public static jmenu getmenu( string temp )

{

jmenu remenu = new jmenu( temp );

remenu.setfont( font.getfont( "fixedsys" ) );

return remenu;

}

}

final class menuitemget

{

public static jmenuitem getmenuitem( string temp )

{

jmenuitem remenuitem = new jmenuitem( temp );

remenuitem.setfont( font.getfont( "fixedsys" ) );

return remenuitem;

}

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » java小游戏-贪吃蛇-JSP教程,Java技巧及代码
分享到: 更多 (0)