| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4915 人关注过本帖
标题:[原创]贪吃蛇
只看楼主 加入收藏
hellboy
Rank: 1
等 级:新手上路
威 望:1
帖 子:245
专家分:0
注 册:2006-6-24
收藏
 问题点数:0 回复次数:22 
[原创]贪吃蛇
//做的不太好 有点白痴 没有什么扩展功能 请多多批评
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class TestSneak extends JPanel implements KeyListener,Runnable{

final int width=30;
final int height=20;
int x=0,y=0;
int a[][]=new int[21][31];
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;
SpecialThread st;
int count;

boolean pause=true;
public TestSneak(){
this.setBackground(Color.black);
//this.setSize(400,320);
this.addKeyListener(this);
this.setLayout(new BorderLayout());
this.setOpaque(true);

}

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

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

}
for(int j=0;j<=30;j++){
a[0][j]=3;
a[20][j]=3;
}
for(int i=0;i<=20;i++){
a[i][0]=3;
a[i][30]=3;
}



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]==3);
a[food.randomx][food.randomy]=2;
count++;
///label.setText(String.valueOf(count));
}




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

public boolean go()
{
Node node1=(Node)linklist.getFirst();

x=node1.x;
y=node1.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)||(a[x][y]==3))
{

//System.out.println(x+" "+y);
JOptionPane.showMessageDialog(null,"a 啊 不行了 ");
init();
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(100);
go();

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

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

}


}

public void creatSpecial()
{


}

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

if(a[i][j]==3)
{
g.setColor(Color.magenta);
g.fillRect(10*j,10*i,9,9);
}

if(a[i][j]==4)
{
g.setColor(Color.green);
g.fillRect(10*j,10*i,9,9);
}
}
}
}

public static void main(String[] arg){

TestSneak sm=new TestSneak();
JFrame frame=new JFrame();
frame.add(sm,"Center");
frame.setUndecorated(true);

frame.setBounds(300,300,310,210);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
sm.setFocusable(true);
try
{
Thread thread =new Thread(sm);
thread.start();
}catch(Exception e){}



}




}

[此贴子已经被作者于2006-10-31 23:30:52编辑过]

搜索更多相关主题的帖子: 贪吃 
2006-10-31 23:28
无理取闹
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:53
帖 子:4264
专家分:0
注 册:2006-7-26
收藏
得分:0 
up

win32汇编
病毒 加密
目前兴趣所在
2006-11-01 07:02
WestNet
Rank: 1
等 级:新手上路
帖 子:174
专家分:0
注 册:2006-10-31
收藏
得分:0 

怎么不能编译?
Node node;
Food food=new Food();....在哪里定义了?


不灰心等待,痛苦也忍耐。
2006-11-01 09:28
hellboy
Rank: 1
等 级:新手上路
威 望:1
帖 子:245
专家分:0
注 册:2006-6-24
收藏
得分:0 
哦 忘记了 晚上回来再发

努力 努力 !!!!!!
2006-11-01 17:56
hellboy
Rank: 1
等 级:新手上路
威 望:1
帖 子:245
专家分:0
注 册:2006-6-24
收藏
得分:0 
public class Node{
int x;
int y;
public Node(int x,int y)
{
this.x=x;
this.y=y;
}
}//这个是忘记发的那个类

[此贴子已经被作者于2006-11-1 22:52:28编辑过]


努力 努力 !!!!!!
2006-11-01 22:52
zhhaitao
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2006-10-23
收藏
得分:0 

Food类呢?

2006-11-02 17:26
hellboy
Rank: 1
等 级:新手上路
威 望:1
帖 子:245
专家分:0
注 册:2006-6-24
收藏
得分:0 
晕 有忘记了
public class Food{

Node node;
int randomx;
int randomy;

public void creatFood()
{
randomx=(int)(Math.random()*20);
randomy=(int)(Math.random()*30);
node=new Node(randomx,randomy);
}

}
//这次好了

努力 努力 !!!!!!
2006-11-02 22:50
qsrock
Rank: 1
等 级:新手上路
帖 子:255
专家分:0
注 册:2005-12-29
收藏
得分:0 
不错不错!~支持的说!~
有待研究!
2006-11-05 01:33
shen_success
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2007-3-19
收藏
得分:0 
SpecialThread类呢
2007-03-19 13:13
shen_success
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2007-3-19
收藏
得分:0 

麻烦了

2007-03-19 13:16
快速回复:[原创]贪吃蛇
数据加载中...
 
   



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

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