|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
java比较简单,没有C++的烦琐,但学习时最好有C++为基础.与JSP和SQL起应用,功能强大.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
{
snakeTimer.start();
configMenu.setVisible(false);
configMenu.setMenuEnable(true);
flag=false;
}
}
}
}
}
privateclassTimerHandlerimplementsActionListener
{
publicsynchronizedvoidactionPerformed(ActionEvente)
{
Pointtemp=(Point)mainSnake.getLast();
snakeSq=mainSnake.iterator();
while(snakeSq.hasNext())
{
PointtempPoint=(Point)snakeSq.next();
if(temp.equals(tempPoint)&&snakeSq.hasNext()!=false)
{
snakeTimer.stop();
stopGame();
JOptionPane.showMessageDialog(null,
"YourScoreis"+score+"
YouLoss!");
}
}
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,
"YourScoreis"+score+"
YouLoss!");
}
if(direction!=mainSnake.snakeReDirection)
{
moveSnake(direction);
}
mainSnake.drawSnake(getGraphics(),singleWidthX,singleHeightY,cooPos);
drawBeanAndEBean(getGraphics());
}
}
publicvoidstopGame()
{
this.hasStoped=true;
this.snakeTimer.stop();
Graphics2Dg=(Graphics2D)GameFrame.this.getGraphics();
g.setColor(ColorGroup.COLOR_BACK);
super.paint(g);
configMenu.setVisible(true);
}
publicvoidresetGame()
{
System.gc();
this.hasStoped=false;
Graphics2Dg=(Graphics2D)GameFrame.this.getGraphics();
g.setColor(ColorGroup.COLOR_BACK);
super.paint(g);
this.mainSnake=newSnake();
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]");
}
privatevoidmoveSnake(intdirection)
{
if(mainSnake.checkBeanIn(this.bean))
{
this.score+=100;
this.scoreLabel.setText(this.info+"CurrentScore:"+this.score);
this.eatedBean.add(newPoint(this.bean));
this.createBean(this.bean);
}
mainSnake.changeDirection((Point)mainSnake.getLast(),direction);
Pointtemp=(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();
}
}
privatevoiddrawBeanAndEBean(Graphicsg)
{
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())
{
PointtempPoint=(Point)snakeSq.next();
this.drawPiece(g,tempPoint.x,tempPoint.y);
}
}
privatevoiddrawPiece(Graphicsg,intx,inty)
{
g.fillRoundRect(this.singleWidthX*x+1,
this.singleHeightY*y+1,
this.singleWidthX-2,
this.singleHeightY-2,
this.cooPos,
this.cooPos);
}
privatevoidcreateBean(Pointtemp)
{
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(newPoint(temp.x,temp.y)))
{
continueLP;
}
}
break;
}
}
}
ConfigMenu.java
packageSnakeGame;
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
publicclassConfigMenuextendsJMenuBar
{
GameFrameowner;
JMenu[]menu;
JMenuItem[]menuItem;
JRadioButtonMenuItem[]speedItem,modelItem,standardItem;
privateUIManager.LookAndFeelInfolooks[];
publicConfigMenu(GameFrameowner)
{
this.owner=owner;
owner.setJMenuBar(this);
String[]menu_name={"SnakeGame","GameConfigure","GameHelp"};
menu=newJMenu[menu_name.length];
for(inti=0;i<menu_name.length;i++)
{
menu[i]=newJMenu(menu_name[i]);
menu[i].setFont(newFont("Courier",Font.PLAIN,12));
this.add(menu[i]);
}
String[]menuItem_name={"StartGame","StopGame","ExitGame",
"GameColor",
"About..."};
menuItem=newJMenuItem[menuItem_name.length];
for(inti=0;i<menuItem_name.length;i++)
{
menuItem[i]=newJMenuItem(menuItem_name[i]);
menuItem[i].setFont(newFont("Courier",Font.PLAIN,12));
menuItem[i].addActionListener(newActionHandler());
}
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={"GameSpeed","WindowModel","GameStandard"};
JMenu[]inner_menu=newJMenu[inner_menu_name.length];
for(inti=0;i<inner_menu_name.length;i++)
{
inner_menu[i]=newJMenu(inner_menu_name[i]);
inner_menu[i].setFont(newFont("Courier",Font.PLAIN,12));
menu[1].add(inner_menu[i]);
}
ButtonGrouptemp1=newButtonGroup();
String[]speedItem_name={"Speed-1","Speed-2","Speed-3","Speed-4","Speed-5"};
speedItem=newJRadioButtonMenuItem[speedItem_name.length];
for(inti=0;i<speedItem_name.length;i++)
{
speedItem[i]=newJRadioButtonMenuItem(speedItem_name[i]);
inner_menu[0].add(speedItem[i]);
speedItem[i].setFont(newFont("Courier",Font.PLAIN,12));
speedItem[i].addItemListener(newItemHandler());
temp1.add(speedItem[i]);
}
ButtonGrouptemp2=newButtonGroup();
String[]modelItem_name={"Linux","Mac","Windows"};
modelItem=newJRadioButtonMenuItem[modelItem_name.length];
for(inti=0;i<modelItem_name.length;i++)
{
modelItem[i]=newJRadioButtonMenuItem(modelItem_name[i]);
inner_menu[1].add(modelItem[i]);
modelItem[i].setFont(newFont("Courier",Font.PLAIN,12));
modelItem[i].addItemListener(newItemHandler());
temp2.add(modelItem[i]);
}
ButtonGrouptemp3=newButtonGroup();
String[]standardItem_name={"60*40","45*30","30*20"};
standardItem=newJRadioButtonMenuItem[standardItem_name.length];
for(inti=0;i<standardItem_name.length;i++)
{
standardItem[i]=newJRadioButtonMenuItem(standardItem_name[i]);
inner_menu[2].add(standardItem[i]);
standardItem[i].setFont(newFont("Courier",Font.PLAIN,12));
standardItem[i].addItemListener(newItemHandler());
temp3.add(standardItem[i]);
}
looks=UIManager.getInstalledLookAndFeels();
}
privateclassActionHandlerimplementsActionListener
{
publicvoidactionPerformed(ActionEvente)
{
if(e.getSource()==menuItem[0])
{
owner.resetGame();
ConfigMenu.this.setVisible(false);
}
elseif(e.getSource()==menuItem[1])
{
owner.stopGame();
ConfigMenu.this.setVisible(true);
ConfigMenu.this.setMenuEnable(true);
}
elseif(e.getSource()==menuItem[2])
{
System.exit(0);
}
elseif(e.getSource()==menuItem[3])
{
ConfigDialogtemp=newConfigDialog(owner);
temp.setVisible(true);
}
elseif(e.getSource()==menuItem[4])
{
JOptionPane.showMessageDialog(null,"SankeGame2.0Version!
"+
"Author:FinalCore
");
}
}
}
privateclassItemHandlerimplementsItemListener
{
publicvoiditemStateChanged(ItemEvente)
{
for(inti=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);
}
elseif(e.getSource()==standardItem[1])
{
owner.setGrid(45,30,10);
}
elseif(e.getSource()==standardItem[2])
{
owner.setGrid(30,20,15);
}
for(inti=0;i<modelItem.length;i++)
{
if(e.getSource()==modelItem[i])
{
try
{
UIManager.setLookAndFeel(looks[i].getClassName());
}catch(Exceptionex){}
}
}
}
}
publicvoidsetMenuEnable(booleantemp)
{
menu[1].setEnabled(temp);
}
}
ConfigDialog.java
packageSnakeGame;
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
publicclassConfigDialogextendsJDialog
{
privateContainerc;
privateJFrameowner;
privateOwnPanel[]panel=newOwnPanel[4];
Boxbox1,box2;
privateJButtoncommitButton,cancelButton;
Color[]color=newColor[4];
publicConfigDialog(Frameowner)
{
this.owner=(JFrame)owner;
this.setSize(400,200);
this.setResizable(false);
this.setTitle("ConfigYourGame");
this.c=this.getContentPane();
this.c.setBackground(Color.WHITE);
this.c.setLayout(newFlowLayout());
this.box1=Box.createVerticalBox();
for(inti=0;i<panel.length;i++)
{
panel[i]=newOwnPanel();
panel[i].addActionListener(newActionHandler());
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=newJButton("断定");
this.commitButton.setFont(Font.getFont("Fixedsys"));
this.commitButton.addActionListener(newActionHandler());
this.cancelButton=newJButton("作废");
this.cancelButton.setFont(Font.getFont("Fixedsys"));
this.cancelButton.addActionListener(newActionHandler());
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);
}
publicvoidsetOwnerColor(Colortemp)
{
this.owner.getContentPane().setBackground(temp);
}
privateclassActionHandlerimplementsActionListener
{
publicvoidactionPerformed(ActionEvente)
{
for(inti=0;i<color.length;i++)
{
if(e.getSource()==panel[i].reButton())
{
color[i]=JColorChooser.showDialog(ConfigDialog.this,
"ChooseBackGroundColor",
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();
}
elseif(e.getSource()==cancelButton)
{
ConfigDialog.this.setVisible(false);
ConfigDialog.this.dispose();
}
}
}
}
classOwnPanelextendsJPanel
{
privateJLabeltemp1;
privateJTextFieldtemp2;
privateJButtontemp3;
OwnPanel()
{
temp1=newJLabel();
temp1.setFont(Font.getFont("Fixedsys"));
temp2=newJTextField();
temp3=newJButton("Change");
temp3.setFont(Font.getFont("Fixedsys"));
temp2.setEditable(false);
temp2.setColumns(10);
this.add(temp1);
this.add(temp2);
this.add(temp3);
this.setLayout(newGridLayout(1,3));
}
publicvoidsetButtonName(Stringtemp)
{
temp3.setName(temp);
}
publicvoidsetBack(Colortemp)
{
temp2.setBackground(temp);
}
publicvoidsetText(Stringtemp)
{
temp1.setText(temp);
}
publicObjectreButton()
{
returntemp3;
}
publicvoidaddActionListener(ActionListenerac)
{
temp3.addActionListener(ac);
}
}
Tools.java
packageSnakeGame;
importjava.awt.*;
importjavax.swing.*;
classColorGroup
{
staticColorCOLOR_BACK=Color.WHITE;
staticColorCOLOR_SNAKE=newColor(43,110,187);
staticColorCOLOR_BEAN=Color.BLUE;
staticColorCOLOR_EATEDBEAN=Color.CYAN;
staticvoidsetCOLOR_BACK(Colortemp)
{
COLOR_BACK=temp;
}
staticvoidsetCOLOR_SNAKE(Colortemp)
{
COLOR_SNAKE=temp;
}
staticvoidsetCOLOR_BEAN(Colortemp)
{
COLOR_BEAN=temp;
}
staticvoidsetCOLOR_EATEDBEAN(Colortemp)
{
COLOR_EATEDBEAN=temp;
}
}
finalclassMenuGet
{
publicstaticJMenugetMenu(Stringtemp)
{
JMenureMenu=newJMenu(temp);
reMenu.setFont(Font.getFont("Fixedsys"));
returnreMenu;
}
}
finalclassMenuItemGet
{
publicstaticJMenuItemgetMenuItem(Stringtemp)
{
JMenuItemreMenuItem=newJMenuItem(temp);
reMenuItem.setFont(Font.getFont("Fixedsys"));
returnreMenuItem;
}
}
因为能用到多少功能就用多少,不能用就不用!总的来说:要简单要性能好,可以不用框架。你说java复杂,就是因为你把java(j2ee)与这些框架混在了一起。 |
|