JAVA小程序贪吃蛇源代码.docx
《JAVA小程序贪吃蛇源代码.docx》由会员分享,可在线阅读,更多相关《JAVA小程序贪吃蛇源代码.docx(17页珍藏版)》请在冰豆网上搜索。
JAVA小程序贪吃蛇源代码
JAVA贪吃蛇源代码
SnakeGame.java
packageSnakeGame;
importjavax.swing.*;
publicclassSnakeGame
{
publicstaticvoidmain(String[]args)
{
JDialog.setDefaultLookAndFeelDecorated(true);
GameFrametemp=newGameFrame();
}
}
Snake.java
packageSnakeGame;
importjava.awt.*;
importjava.util.*;
classSnakeextendsLinkedList
{
publicintsnakeDirection=2;
publicintsnakeReDirection=4;
publicSnake()
{
this.add(newPoint(3,3));
this.add(newPoint(4,3));
this.add(newPoint(5,3));
this.add(newPoint(6,3));
this.add(newPoint(7,3));
this.add(newPoint(8,3));
this.add(newPoint(9,3));
this.add(newPoint(10,3));
}
publicvoidchangeDirection(Pointtemp,intdirection)
{
this.snakeDirection=direction;
switch(direction)
{
case1:
//up
this.snakeReDirection=3;
this.add(newPoint(temp.x,temp.y-1));
break;
case2:
//right
this.snakeReDirection=4;
this.add(newPoint(temp.x+1,temp.y));
break;
case3:
//down
this.snakeReDirection=1;
this.add(newPoint(temp.x,temp.y+1));
break;
case4:
//left
this.snakeReDirection=2;
this.add(newPoint(temp.x-1,temp.y));
break;
}
}
publicbooleancheckBeanIn(Pointbean)
{
Pointtemp=(Point)this.getLast();
if(temp.equals(bean))
{
returntrue;
}
returnfalse;
}
publicvoidremoveTail()
{
this.remove(0);
}
publicvoiddrawSnake(Graphicsg,intsingleWidthX,intsingleHeightY,intcooPos)
{
g.setColor(ColorGroup.COLOR_SNAKE);
IteratorsnakeSq=this.iterator();
while(snakeSq.hasNext())
{
PointtempPoint=(Point)snakeSq.next();
this.drawSnakePiece(g,tempPoint.x,tempPoint.y,
singleWidthX,singleHeightY,cooPos);
}
}
publicvoiddrawSnakePiece(Graphicsg,inttemp1,inttemp2,
intsingleWidthX,intsingleHeightY,intcooPos)
{
g.fillRoundRect(singleWidthX*temp1+1,
singleHeightY*temp2+1,
singleWidthX-2,
singleHeightY-2,
cooPos,
cooPos);
}
publicvoidclearEndSnakePiece(Graphicsg,inttemp1,inttemp2,
intsingleWidthX,intsingleHeightY,intcooPos)
{
g.setColor(ColorGroup.COLOR_BACK);
g.fillRoundRect(singleWidthX*temp1+1,
singleHeightY*temp2+1,
singleWidthX-2,
singleHeightY-2,
cooPos,
cooPos);
}
}
GameFrame.java
packageSnakeGame;
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
importjava.util.*;
importjava.awt.geom.*;
classGameFrameextendsJFrame
{
privateToolkittempKit;
privateinthorizontalGrid,verticalGrid;
privateintsingleWidthX,singleHeightY;
privateintcooPos;
privateSnakemainSnake;
privateLinkedListeatedBean;
{
eatedBean=newLinkedList();
}
privateIteratorsnakeSq;
publicjavax.swing.TimersnakeTimer;
privateintdirection=2;
privateintscore;
privateStringinfo;
privatePointbean,eatBean;
{
bean=newPoint();
}
privatebooleanflag;
privateJMenuBarinfoMenu;
privateJMenu[]tempMenu;
privateJMenuItem[]tempMenuItem;
privateJRadioButtonMenuItem[]levelMenuItem,versionMenuItem;
privateJLabelscoreLabel;
{
scoreLabel=newJLabel();
}
privateGraphics2Dg;
privateImageIconsnakeHead;
{
snakeHead=newImageIcon("LOGO.gif");
}
privateConfigMenuconfigMenu;
privatebooleanhasStoped=true;
publicGameFrame()
{
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(newKeyHandler());
GameFrame.this.snakeTimer=newjavax.swing.Timer(80,newTimerHandler());
this.getContentPane().add(scoreLabel,BorderLayout.SOUTH);
this.scoreLabel.setFont(newFont("Fixedsys",Font.BOLD,15));
this.scoreLabel.setText("Pause[SPACE]-Exit[ESC]");
this.configMenu=newConfigMenu(this);
this.setVisible(true);
}
publicvoidsetGrid(inttemp1,inttemp2,inttemp3)
{
this.horizontalGrid=temp1;
this.verticalGrid=temp2;
this.singleWidthX=this.getWidth()/temp1;
this.singleHeightY=this.getHeight()/temp2;
this.cooPos=temp3;
}
privateclassKeyHandlerextendsKeyAdapter
{
publicvoidkeyPressed(KeyEvente)
{
if(e.getKeyCode()==27)
{
snakeTimer.stop();
if(JOptionPane.showConfirmDialog(null,"Areyousuretoexit?
")==0)
{
System.exit(0);
}
snakeTimer.start();
}
elseif(e.getKeyCode()==37&&mainSnake.snakeDirection!
=2)//left
{
direction=4;
}
elseif(e.getKeyCode()==39&&mainSnake.snakeDirection!
=4)//right
{
direction=2;
}
elseif(e.getKeyCode()==38&&mainSnake.snakeDirection!
=3)//up
{
direction=1;
}
elseif(e.getKeyCode()==40&&mainSnake.snakeDirection!
=1)//down
{
direction=3;
}
elseif(e.getKeyCode()==32)
{
if(!
hasStoped)
{
if(!
flag)
{
snakeTimer.stop();
configMenu.setVisible(true);
configMenu.setMenuEnable(false);
flag=true;
}
else
{
snak