| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 663 人关注过本帖
标题:贪吃蛇不能动??
取消只看楼主 加入收藏
hellboy
Rank: 1
等 级:新手上路
威 望:1
帖 子:245
专家分:0
注 册:2006-6-24
收藏
 问题点数:0 回复次数:0 
贪吃蛇不能动??
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class Sneak extends JFrame implements ActionListener,KeyListener,Runnable{

final int width=30;
final int height=20;
int x=0,y=0;
int a[][]=new int[20][30];
int direction=4;
final int up=1;
final int down=3;
final int left=2;
final int right=4;
final int length=8;//初始化长度
final int initx=8;
final int inity=5;
Node node;
Thread thread;
Food food=new Food();
LinkedList linklist;
JPanel mainPanel,operatePanel;
JButton startBt,quitBt;
JLabel label,score;
boolean pause=false;
public Sneak(){

this.setSize(400,320);
this.addKeyListener(this);
this.setLayout(new BorderLayout());
this.setBackground(Color.white);
startBt=new JButton("Start");
quitBt=new JButton("quit");
//restart=new JButton("Restart")
label=new JLabel("your score:");
score=new JLabel("");
mainPanel=new JPanel();
//mainPanel.setFocusable(true);
mainPanel.setSize(310,210);
// mainPanel.addKeyListener(mainPanel);
mainPanel.setBackground(Color.white);
//mainPanel.addKeyListener(this);
this.getContentPane().add(mainPanel,"Center");
//this.addKeyListener(this);
// this.getcontentp
operatePanel=new JPanel(new GridLayout(4,1,6,6));
operatePanel.add(label);
operatePanel.add(score);
operatePanel.add(startBt);
operatePanel.add(quitBt);
this.getContentPane().add(operatePanel,"East");
startBt.addActionListener(this);
quitBt.addActionListener(this);
// restart.addActionListener(this);

}

public void init()
{
arrayIni();
direction=4;
linklist=new LinkedList();

for(int i=10;i>=inity;i--)
{
node=new Node(initx,i);
a[initx][i]=1;
linklist.add(node);

}

this.creatFood();
}
public void arrayIni()
{
for(int i=0;i<20;i++)
{
for(int j=0;j<30;j++)
{
a[i][j]=0;
}
}
}
public void creatFood()
{
do
food.creatFood();
while(a[food.randomx][food.randomy]==1);
a[food.randomx][food.randomy]=2;
}

public void actionPerformed(ActionEvent e)
{
if(e.getSource()==startBt)
{
//init();
pause=true;
// this.setFocusable(true);
// repaint();
/* try
{
thread=new Thread(this);
thread.start();
}catch(Exception err){System.out.println(err.toString());}


}*/
}
if(e.getSource()==quitBt)
{
System.exit(0);
}

}


public void changeDirection(int i)
{
if((i%2)!=(direction%2))
{
direction=i;
}
}

public boolean go()
{
Node node1=(Node)linklist.getFirst();
//Node node2=(Node)linklist.getLast();
//System.out.println(node1.x+" "+node1.y);
//System.out.println(food.randomx+" "+food.randomy);
x=node1.x;
y=node1.y;
// System.out.println(x+" yes "+y+" "+a[x][y]);
switch(direction)
{

case up:
//x=node1.x;
x--;break;
case down:
//x=node1.x;
x++;break;
case right:
//y=node1.y;
y++;break;
case left:
//y=node1.y;
y--;break;

}

if((x>=20)||(y>=30)||(x<0)||(y<0)||(a[x][y]==1))
{

//System.out.println(x+" "+y);
return false;
}

else
{

if(a[x][y]==2)
{
linklist.addFirst(new Node(x,y));
a[x][y]=1;
creatFood();


}

else if(a[x][y]==0)
{
// System.out.println("asdf");
linklist.addFirst(new Node(x,y));
a[x][y]=1;
Node node=(Node)linklist.removeLast();
a[node.x][node.y]=0;
//System.out.println(node.x+"ok"+node.y+" "+a[node.x][node.y]);

}

repaint();
return true;
}

}

public void keyPressed(KeyEvent e)
{
System.out.println("change");
if(e.getKeyCode()==KeyEvent.VK_UP)
{

changeDirection(up);
}
if(e.getKeyCode()==KeyEvent.VK_DOWN)
{
changeDirection(down);
}
if(e.getKeyCode()==KeyEvent.VK_LEFT)
{
changeDirection(left);
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
{
changeDirection(right);
}
}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
public void run()
{
init();

while(true)
{
try
{
if(pause)
{
Thread.sleep(500);

go();
//System.out.println("is go");
}

}catch(Exception e){System.out.println(e.getMessage());}

}
}

public void paint(Graphics g)
{
// g.setColor(Color.white);
// g.fillRect(0,0,150,100);
// System.out.println(direction);
super.paint(g);
for(int i=0;i<20;i++)
{
for(int j=0;j<30;j++)
{
if(a[i][j]==1)
{
g.setColor(Color.BLACK);
g.fillRect(10*j,10*i,9,9);
}
if(a[i][j]==2)
{
g.setColor(Color.red);
g.fillRect(10*j,10*i,9,9);
}
}
}
}

public static void main(String[] arg){
Sneak sm=new Sneak();

sm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
sm.setVisible(true);

try
{
Thread thread =new Thread(sm);
thread.start();
}catch(Exception e){}



}




}先在不能动 我把继承改到了 JPanel 就可以动的 为什么啊

[此贴子已经被作者于2006-10-24 23:23:02编辑过]

搜索更多相关主题的帖子: int final import 贪吃 java 
2006-10-24 23:20
快速回复:贪吃蛇不能动??
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025458 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved