这是我收集的程序 作者不是我
import javax.swing.border.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.awt.geom.*;
import java.awt.*;
public class SnakeGame extends JFrame
{
public SnakeGame()
{
BarPanel barPanel=new BarPanel();
MainPanel mainPanel=new MainPanel(barPanel);
Container contentPane=getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(mainPanel,BorderLayout.CENTER);
contentPane.add(barPanel,BorderLayout.SOUTH);
setSize(416,395);
setLocation(300,150);
show();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
SnakeGame aGame=new SnakeGame();
}
}
//a snake moves on it...
class MainPanel extends JPanel implements Runnable
{
public MainPanel(BarPanel bPanel)
{
addKeyListener(new StartListener());
//add startlistener
addKeyListener(new SaveSnake());
//add save listener
directionControler=new DirectionControler();
directionControler.keyDirection(this);
//add keylistener to mainpanel
keyList=directionControler.returnKeyBufferList();
setBorder(BorderFactory.createEtchedBorder());
//set border
aBarPanel=bPanel;
snakeBody=new ArrayList();
for (int i=4;i>=0;i--)
{
snakeBody.add(new Rectangle2D.Double(i*SQUARE_LENGTH,0
,SQUARE_LENGTH,SQUARE_LENGTH));
}
//make a random square for first time
for (;
{
randomSquare();
if (randomX==0&&randomY==0)
continue;
else
if (randomX==0&&randomY==SQUARE_LENGTH)
continue;
else
if (randomX==0&&randomY==2*SQUARE_LENGTH)
continue;
else
if (randomX==0&&randomY==3*SQUARE_LENGTH)
continue;
else
if (randomX==0&&randomY==4*SQUARE_LENGTH)
continue;
else
break;
}
//aRandom=new Random();
setBackground(Color.cyan);
}
//let panel accept focus
public boolean isFocusTraversable()
{
return true;
}
public void paintComponent(Graphics g)
{
Rectangle2D tempSquare;
super.paintComponent(g);
Graphics2D g2=(Graphics2D)g;
g2.setPaint(Color.gray);
for (int i=0;i<snakeBody.size();i++)
{
tempSquare=(Rectangle2D)snakeBody.get(i);
g2.draw(tempSquare);
g2.fill(tempSquare);
}
makeSmallSnakeBody(snakeBody);
g2.setPaint(Color.blue);
for (int i=0;i<smallSnakeBody.size();i++)
{
tempSquare=(Rectangle2D)smallSnakeBody.get(i);
g2.draw(tempSquare);
g2.fill(tempSquare);
}
g2.draw(new Rectangle2D.Double(randomX
,randomY,SQUARE_LENGTH,SQUARE_LENGTH));
}
public void makeSmallSnakeBody(ArrayList aList)
{
int newX;
int newY;
smallSnakeBody=new ArrayList();
for (int i=aList.size()-1;i>=0;i--)
{
currentSquare=(Rectangle2D)aList.get(i);
newX=(int)currentSquare.getX()+BORDER_SPACE;
newY=(int)currentSquare.getY()+BORDER_SPACE;
smallSnakeBody.add(new Rectangle2D.Double(newX,newY
,SQUARE_LENGTH-2*BORDER_SPACE,SQUARE_LENGTH-2*BORDER_SPACE));
}
}
public void randomSquare() /////////////
{
Random aRandom=new Random();
randomX=aRandom.nextInt(20)*SQUARE_LENGTH;
randomY=aRandom.nextInt(20)*SQUARE_LENGTH;
//return new Rectangle2D.Double(i*10,j*10,SQUARE_LENGTH,SQUARE_LENGTH);
}
public boolean isIncluded(ArrayList aList,int x,int y,int flag)
{
int sX;int sY;
int i=flag;
for (;i<aList.size();i++)
{
currentSquare=(Rectangle2D)aList.get(i);
sX=(int)currentSquare.getX();
sY=(int)currentSquare.getY();
if (sX==x&&sY==y)
return true;
}
return false;
}
public void run()
{
while(isRunnable==true)
{
int keyListLength=keyList.size();
if (keyList.size()>1)
{
direction=((Integer)keyList.get(1)).intValue();
keyList.remove(1);
keyList.set(0,new Integer(direction));
}
directionControler.whileDirection(direction,snakeBody);
//
{
currentSquare=(Rectangle2D)snakeBody.get(0);
int aX=(int)currentSquare.getX();
int aY=(int)currentSquare.getY();
if(isIncluded(snakeBody,aX,aY,3)||aX<0||aY<0||aX>400||aY>300)
{
canAddSquare=false;
isSaveKeyAvailable=true;
try {
Thread.sleep(SAVE_TIME);
}
catch (InterruptedException e) {}
isSaveKeyAvailable=false;
canAddSquare=true;
currentSquare=(Rectangle2D)snakeBody.get(0);
aX=(int)currentSquare.getX();
aY=(int)currentSquare.getY();
if(isIncluded(snakeBody,aX,aY,3)
||aX<0||aY<0||aX>400||aY>300)
{
isRunnable=false;
return;
}
}
if (aX!=randomX||aY!=randomY)
snakeBody.remove(snakeBody.size()-1);
else
//make a random square
for (;
{
randomSquare();
if (!isIncluded(snakeBody,randomX,randomY,0))
{
aBarPanel.setScore(score);
///here set the score
score+=100;
break;
}
}
}
repaint();
try {Thread.sleep(MOVE_DELAY);}
catch(InterruptedException e){}
}
}
//the thread will be run here
class StartListener extends KeyAdapter
{
public void keyPressed(KeyEvent e)
{
int keyCode=e.getKeyCode();
if (((keyCode==DOWN)||(keyCode==RIGHT))
&&(!isStarted)&&(runThread==null))
{
runThread=new Thread(MainPanel.this);
runThread.start();
isStarted=true;
}
}
}
class SaveSnake extends KeyAdapter
{
public void keyPressed(KeyEvent e)
{
if (isSaveKeyAvailable)
{
int x;
int y;
snakeBody.remove(0);
currentSquare=(Rectangle2D)snakeBody.get(0);
x=(int)currentSquare.getX();
y=(int)currentSquare.getY();
int keyCode=e.getKeyCode();
if (keyCode==UP)
{
y-=SQUARE_LENGTH;
direction=UP;
}
else
if (keyCode==DOWN)
{
y+=SQUARE_LENGTH;
direction=DOWN;
}
else
if (keyCode==LEFT)
{
x-=SQUARE_LENGTH;
direction=LEFT;
}
else
if (keyCode==RIGHT)
{
x+=SQUARE_LENGTH;
direction=RIGHT;
}
keyList.add(new Integer(direction));
snakeBody.add(1,new Rectangle2D.Double(x,y
,SQUARE_LENGTH,SQUARE_LENGTH));
isSaveKeyAvailable=false;
}
}
}
public static boolean returnIsStarted()
{
return isStarted;
}
private Thread runThread;
public static final int UP=KeyEvent.VK_UP;
public static final intdo
WN=KeyEvent.VK_DOWN;
public static final int LEFT=KeyEvent.VK_LEFT;
public static final int RIGHT=KeyEvent.VK_RIGHT;
public static final int SQUARE_LENGTH=15;
public static final int BORDER_SPACE=2;
public static final int MOVE_DELAY=80;
public static final int SAVE_TIME=100;
private int direction=RIGHT;
private int randomX=-1;
private int randomY=-1;
private int score=100;
private boolean isRunnable=true;
private boolean isSaveKeyAvailable=false;
public static boolean canAddSquare=true;
private static boolean isStarted=false;
private BarPanel aBarPanel;
private ArrayList snakeBody;
private ArrayList smallSnakeBody;
private ArrayList keyList;
private Rectangle2D currentSquare;
private DirectionControler directionControler;
}
class DirectionControler
{
public DirectionControler()
{
keyBufferList=new ArrayList();
keyBufferList.add(new Integer(MainPanel.RIGHT));
}
public void whileDirection(int runDirection,ArrayList aList)
{
int size=aList.size();
Rectangle2D tempSquare=(Rectangle2D)aList.get(0);
int firstX=(int)tempSquare.getX();
int firstY=(int)tempSquare.getY();
if (runDirection==MainPanel.UP)
{
firstY-=MainPanel.SQUARE_LENGTH;
}
else
if (runDirection==MainPanel.DOWN)
{
firstY+=MainPanel.SQUARE_LENGTH;
}
else
if (runDirection==MainPanel.LEFT)
{
firstX-=MainPanel.SQUARE_LENGTH;
}
else
if (runDirection==MainPanel.RIGHT)
{
firstX+=MainPanel.SQUARE_LENGTH;
}
aList.add(0,new Rectangle2D.Double(firstX,firstY
,MainPanel.SQUARE_LENGTH,MainPanel.SQUARE_LENGTH));
}
public void keyDirection(Container aContainer)
{
aContainer.addKeyListener(new ActionKeyDirection());
}
///as a direction key is pressed,the direction will be added to a keylist
class ActionKeyDirection extends KeyAdapter
{
public void keyPressed(KeyEvent e)
{
if (MainPanel.canAddSquare)
{
int keyCode=e.getKeyCode();
if ((MainPanel.returnIsStarted())&&((keyCode==MainPanel.UP) //to see if thred is started
||(keyCode==MainPanel.DOWN)||(keyCode==MainPanel.LEFT)
||(keyCode==MainPanel.RIGHT)))
{
int formerDirection=((Integer)
keyBufferList.get(keyBufferList.size()-1)).intValue();
if ((formerDirection==MainPanel.UP)
&&(keyCode!=MainPanel.UP)&&(keyCode!=MainPanel.DOWN))
{
keyBufferList.add(new Integer(keyCode));
}
else
if ((formerDirection==MainPanel.DOWN)
&&(keyCode!=MainPanel.UP)&&(keyCode!=MainPanel.DOWN))
{
keyBufferList.add(new Integer(keyCode));
}
else
if ((formerDirection==MainPanel.LEFT)
&&(keyCode!=MainPanel.LEFT)&&(keyCode!=MainPanel.RIGHT))
{
keyBufferList.add(new Integer(keyCode));
}
else
if ((formerDirection==MainPanel.RIGHT)
&&(keyCode!=MainPanel.RIGHT)&&(keyCode!=MainPanel.LEFT))
{
keyBufferList.add(new Integer(keyCode));
}
}
}
}
}
public ArrayList returnKeyBufferList()
{
return keyBufferList;
}
private MainPanel ownMainPanel;
private ArrayList keyBufferList;
private Thread runThread;
}
class BarPanel extends JPanel
{
public BarPanel()
{
//make two buttons and add actionlistener for them
restartButton=new JButton("重新开始");
restartButton.addActionListener(new ActionRestartButton());
add(restartButton);
continueButton=new JButton("继续");
continueButton.addActionListener(new ActionContinueButton());
add(continueButton);
//made a panel with combox
classPanel=new JPanel();
classPanel.add(new JLabel("级别:"));
classBox();
//make a combox and add actonlitener.
classPanel.add(barBox);
//make a panel with score field
scorePanel=new JPanel();
scorePanel.add(new JLabel("得分:"));
scoreField=new ScoreField("0",6);
scorePanel.add(scoreField);
//add classPanel and scorePanel to barpanel
add(classPanel);
add(scorePanel);
//to make a border and set color
//setBackground(Color.cyan);
//setForeground(Color.blue);
setBorder(BorderFactory.createEtchedBorder());
}
public void setScore(int s)
{
scoreField.setText(""+s);
}
public void classBox()
{
barBox=new JComboBox();
barBox.addItem("1");
barBox.addItem("2");
barBox.addItem("3");
barBox.addItem("4");
barBox.addItem("5");
barBox.addActionListener(new ActionBox());
}
private JButton restartButton;
private JButton continueButton;
private JPanel classPanel;
private JPanel scorePanel;
private JComboBox barBox;
private ScoreField scoreField;
class ActionBox implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
}
}
class ActionRestartButton implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.out.println("restart");
}
}
class ActionContinueButton implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.out.println("continue");
}
}
}
//override method isFocusTraversable so that it cannot get focus
class ScoreField extends JTextField
{
public ScoreField(String score,int scoreLength)
{
super(score,scoreLength);
}
public boolean isFocusTraversable()
{
return(false);
}
}