[原创][开源]贪吃蛇
import javax.swing.*;import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
class SnakeMain
{
public static void main(String [] args)
{
SnakeFrame f=new SnakeFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.setResizable(false);
}
}
class SnakeFrame extends JFrame
{
public SnakeFrame()
{
SnakePanel p=new SnakePanel();
Container con=getContentPane();
con.add(p);
JMenuBar menubar=new JMenuBar();
JMenu game=new JMenu("game");
menubar.add(game);
game.add(p.getStart());
game.addSeparator();
game.add(p.getHang());
game.addSeparator();
game.add(p.getRestart());
setTitle("贪吃蛇");
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
setJMenuBar(menubar);
}
public static final int DEFAULT_WIDTH=400;
public static final int DEFAULT_HEIGHT=300;
}
class SnakePanel extends JPanel implements ActionListener,Runnable
{
public SnakePanel()
{
rects=new LinkedList<Rectangle2D>();
leftX=100;
topY=100;
itleft=20;
ittop=20;
canUp=false;
canDown=true;
canLeft=true;
canRight=true;
howlong=15;
speed=10;
move=false;
where=RIGHT;
creatOne();
start=new JMenuItem("开始");
hang=new JMenuItem("暂停");
restart=new JMenuItem("重新开始");
start.addActionListener(this);
hang.addActionListener(this);
restart.addActionListener(this);
KeyHandler listener=new KeyHandler();
addKeyListener(listener);
setFocusable(true);
th=new Thread(this);
}
public JMenuItem getStart()
{
return start;
}
public JMenuItem getHang()
{
return hang;
}
public JMenuItem getRestart()
{
return restart;
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==start)
{
try
{
if(!move&&startOrNot==false)
{
th.start();
move=true;
startOrNot=true;
}
else
{
resumeThread();
move=true;
dead=false;
}
}catch(Exception e1){}
}
else if(e.getSource()==hang)
{
move=false;
}
else if(e.getSource()==restart)
{
reStart();
dead=false;
resumeThread();
move=true;
}
}
public synchronized void hangThread() throws InterruptedException
{
wait();
}
public synchronized void resumeThread()
{
notifyAll();
}
public void run()
{
while(th!=null)
{
while(!move)
{
try
{
hangThread();
}catch(InterruptedException e){}
}
try
{
Thread.sleep(speed);
}catch(InterruptedException e){}
move();
}
}
public void creatOne()
{
itleft=(int)(Math.random()*350);
ittop=(int)(Math.random()*235);
}
public void move()
{
deadOrNot();
eatOrNot();
if(where==UP)
{
canLeft=true;
canRight=true;
topY-=1;
}
else if(where==DOWN)
{
canLeft=true;
canRight=true;
topY+=1;
}
else if(where==LEFT)
{
canUp=true;
canDown=true;
leftX-=1;
}
else if(where==RIGHT)
{
canUp=true;
canDown=true;
leftX+=1;
}
repaint();
}
public void eatOrNot()
{
Point2D p1=new Point2D.Double(itleft,ittop);
Point2D p2=new Point2D.Double(itleft+WIDTH,ittop+HEIGHT);
if(rect.contains(p1)||rect.contains(p2))
{
setLong();
alreadyEat=true;
if(speed>1)
speed--;
}
}
public void deadOrNot()
{
if(topY<0||topY>235||leftX<0||leftX>385)
{
dead=true;
}
for(int j=rects.size()-8;j>0;j--)
{
Rectangle2D r=rects.get(j);
if(r.getX()==leftX && r.getY()==topY)
{
dead=true;
}
}
}
public void setLong()
{
howlong+=10;
}
public void reStart()
{
for(int i=0;i<rects.size();i++)
{
rects.remove(i);
}
score=0;
leftX=100;
topY=100;
howlong=15;
where=RIGHT;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2=(Graphics2D)g;
String message="Game Over!";
String counter="";
g2.drawString("Score:"+counter.valueOf(score),10,10);
Font f=new Font("Serif",Font.BOLD,36);
g2.setFont(f);
g2.setPaint(Color.black);
rect=new Rectangle2D.Double(leftX,topY,WIDTH,HEIGHT);
rectP=new Rectangle2D.Double(itleft,ittop,WIDTH,HEIGHT);
rects.add(rect);
rects.add(0,rectP);
if(alreadyEat)
{
++score;
rects.remove(0);
creatOne();
alreadyEat=false;
}
if(dead)
{
g2.drawString(message,130,180);
move=false;
}
for(int i=0;i<rects.size();i++)
{
g2.draw((Rectangle2D)rects.get(i));
if(i>howlong)
{
rects.remove(i-howlong);
rects.remove(0);
}
}
}
private double leftX;
private double topY;
private int itleft;
private int ittop;
private boolean canUp;
private boolean canDown;
private boolean canLeft;
private boolean canRight;
private boolean dead;
private long speed;
public static final double WIDTH=6;
public static final double HEIGHT=6;
private JMenuItem start;
private JMenuItem hang;
private JMenuItem restart;
private Thread th;
private Rectangle2D rect,rectP;
private LinkedList<Rectangle2D> rects;
private int howlong;
private boolean move;
private boolean startOrNot;
private boolean alreadyEat;
private int score;
private int where;
public static final int UP=1;
public static final int DOWN=2;
public static final int LEFT=3;
public static final int RIGHT=4;
private class KeyHandler implements KeyListener
{
public void keyPressed(KeyEvent e)
{
int keyCode=e.getKeyCode();
if(keyCode==KeyEvent.VK_LEFT&&canLeft)
{
where=LEFT;
canRight=false;
}
else if(keyCode==KeyEvent.VK_RIGHT&&canRight)
{
where=RIGHT;
canLeft=false;
}
else if(keyCode==KeyEvent.VK_UP&&canUp)
{
where=UP;
canDown=false;
}
else if(keyCode==KeyEvent.VK_DOWN&&canDown)
{
where=DOWN;
canUp=false;
}
}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
}
}
[此贴子已经被作者于2006-5-31 15:08:33编辑过]