Java版贪食蛇程序,请看看 (50分)

  • 主题发起人 主题发起人 renyi
  • 开始时间 开始时间
R

renyi

Unregistered / Unconfirmed
GUEST, unregistred user!
我写的程序如下(还没有写完),本想用键盘控制方向,但按键盘没有反应,是什么原因
下面是完整的代码:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.util.*;

public class Snake extends Applet
implements KeyListener {
Label star;
Timer timer;
myTimerTask timerTask;
int starX = 200 , starY = 150;
int starNumber = 10;
public int[][] mystar = new int[100][2];;
public void init() {

setBackground(Color.pink);
addKeyListener(this);
requestFocus();
setLayout(null);

for(int i = starNumber-1 ;
i >= 0 ;
i--) {
mystar[0] = 200 ;
mystar[1] = (starNumber-1-i)*8;
}
star = new Label("*");
star.setBounds(198,151,8,8);
this.add(star);

timer = new Timer();
timerTask = new myTimerTask();
timer.schedule(timerTask,500,500);
}
class myTimerTask extends TimerTask {
//direction 为移动的方向,1向上,2向下,3向左,4向右
private int direction = 3;
public void run() {
for(int i = starNumber-1 ;
i >=1 ;
i--) {
mystar[0] = mystar[i-1][0];
mystar[1] = mystar[i-1][1];
}
switch(direction) {
case 1:
mystar[0][1] -= 8;
break;
case 2:
mystar[0][1] += 8;
break;
case 3:
mystar[0][0] -= 8;
break;
case 4:
mystar[0][0] += 8;
break;
}
repaint();
}
public void setdirection(int direction) {
this.direction = direction;
}
}
public void keyPressed(KeyEvent ke) {
int key = ke.getKeyCode();
switch(key) {
case KeyEvent.VK_UP:
timerTask.setdirection(1);
break;
case KeyEvent.VK_DOWN:
timerTask.setdirection(2);
break;
case KeyEvent.VK_LEFT:
timerTask.setdirection(3);
break;
case KeyEvent.VK_RIGHT:
timerTask.setdirection(4);
break;
}
}

public void keyReleased(KeyEvent ke) {
}

public void keyTyped(KeyEvent ke) {
}

public void paint(Graphics g) {
int i = 0;
while(i < starNumber) {
g.drawString("*",mystar[0],mystar[1]);
i++;
}
}

}
 
windowevent没有设置按扭啊
 
要學習學習
 
學習中。。。。。
 
这是我收集的程序 作者不是我
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&amp;&amp;randomY==0)
continue;
else
if (randomX==0&amp;&amp;randomY==SQUARE_LENGTH)
continue;
else
if (randomX==0&amp;&amp;randomY==2*SQUARE_LENGTH)
continue;
else
if (randomX==0&amp;&amp;randomY==3*SQUARE_LENGTH)
continue;
else
if (randomX==0&amp;&amp;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&amp;&amp;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))
&amp;&amp;(!isStarted)&amp;&amp;(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())&amp;&amp;((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)
&amp;&amp;(keyCode!=MainPanel.UP)&amp;&amp;(keyCode!=MainPanel.DOWN))
{
keyBufferList.add(new Integer(keyCode));

}
else
if ((formerDirection==MainPanel.DOWN)
&amp;&amp;(keyCode!=MainPanel.UP)&amp;&amp;(keyCode!=MainPanel.DOWN))
{
keyBufferList.add(new Integer(keyCode));

}
else
if ((formerDirection==MainPanel.LEFT)
&amp;&amp;(keyCode!=MainPanel.LEFT)&amp;&amp;(keyCode!=MainPanel.RIGHT))
{
keyBufferList.add(new Integer(keyCode));
}
else
if ((formerDirection==MainPanel.RIGHT)
&amp;&amp;(keyCode!=MainPanel.RIGHT)&amp;&amp;(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);
}
}

 
上面的代码怎么这么长,我写的才200行,基本做完了.
 
...朋友!!!!傻子才用JAVA做贪吃蛇哪!!!!!^^
编译速度又慢,打开速度更慢,不如考虑换成DELPHI开发ACTIVEX控件,速度又快,看的又HAPPY
不过,你们得精神还是值得我要学习得!
 
多人接受答案了。
 
后退
顶部