标题:[原创][开源]贪吃蛇
取消只看楼主
aiyuheng
Rank: 1
等 级:新手上路
威 望:1
帖 子:656
专家分:0
注 册:2006-1-12
 问题点数:0 回复次数:9 
[原创][开源]贪吃蛇
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编辑过]

搜索更多相关主题的帖子: import awt 贪吃 java SnakeFrame 
2006-05-29 16:06
aiyuheng
Rank: 1
等 级:新手上路
威 望:1
帖 子:656
专家分:0
注 册:2006-1-12
得分:0 
初学一个月,也总想写点东西,就写了个贪吃蛇.写的有点粗糙.
在细节方面有许多要改进的地方.
有时菜单需要点两下才反应,不知为什么.这个没有解决.
还有就是没有很好的写个面向对象的程序出来.
本来想把蛇本身写个类来着,可不知道怎么下手,正在思考中...
写这个程序,是我第一个JAVA的程序.希望大家不要见笑.也希望大家给提提意见,指点一二.
谢谢了.
程序正在改进中......

when i want to ask anyone,i will ask myself first.
2006-05-29 16:12
aiyuheng
Rank: 1
等 级:新手上路
威 望:1
帖 子:656
专家分:0
注 册:2006-1-12
得分:0 

提提意见呗


when i want to ask anyone,i will ask myself first.
2006-05-29 17:29
aiyuheng
Rank: 1
等 级:新手上路
威 望:1
帖 子:656
专家分:0
注 册:2006-1-12
得分:0 
我用的是1.4 因为没有空找1.5
这个1.4也是JCreator带的 
由于图形这块刚看 所以做的挺粗糙的
还有 我现在在看JAVA核心技术(第六版)这本书是从学校借的 里面讲的就是1.4
至于1.5 应该说我还不知道是怎么回事 手里的资料太少了!
等到放假再好好看看
现在就只能是用些过时的了 
你们说的那些不安全我还不懂呢
继续学习
谢谢上面两位高手

when i want to ask anyone,i will ask myself first.
2006-05-30 15:52
aiyuheng
Rank: 1
等 级:新手上路
威 望:1
帖 子:656
专家分:0
注 册:2006-1-12
得分:0 
用了泛型处理数据,将过时的方法去掉了,加了速度(效果不明显)
界面还是很粗糙
程序正在改进中....

when i want to ask anyone,i will ask myself first.
2006-05-31 15:11
aiyuheng
Rank: 1
等 级:新手上路
威 望:1
帖 子:656
专家分:0
注 册:2006-1-12
得分:0 

把蛇提了出来
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
class NewSnakeMain
{
public static void main(String [] args)
{
NewSnakeFrame f=new NewSnakeFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.setResizable(false);
}
}
class NewSnakeFrame extends JFrame
{
public NewSnakeFrame()
{
NewSnakePanel p=new NewSnakePanel();
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 NewSnakePanel extends JPanel implements ActionListener,Runnable
{
public NewSnakePanel()
{
start=new JMenuItem("开始");
hang=new JMenuItem("暂停");
restart=new JMenuItem("重新开始");
start.addActionListener(this);
hang.addActionListener(this);
restart.addActionListener(this);
snake=new Snake(this);
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(!snake.getMove()&&startOrNot==false)
{
th.start();
snake.setMove(true);
startOrNot=true;
}
else
{
resumeThread();
snake.setMove(true);
snake.setDead(false);
}

}catch(Exception e1){}
}
else if(e.getSource()==hang)
{
snake.setMove(false);
}
else if(e.getSource()==restart)
{
snake.reStart();
snake.setDead(false);
resumeThread();
snake.setMove(true);
}
}
public synchronized void hangThread() throws InterruptedException
{
wait();
}
public synchronized void resumeThread()
{
notifyAll();
}
public void run()
{
while(th!=null)
{
while(!snake.getMove())
{
try
{
hangThread();
}catch(InterruptedException e){}
}

try
{
Thread.sleep(5);
}catch(InterruptedException e){}
snake.move();
repaint();
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
snake.paint(g);
}
private JMenuItem start;
private JMenuItem hang;
private JMenuItem restart;
private boolean startOrNot;
private Thread th;
private Snake snake;
}
class Snake
{
public Snake(NewSnakePanel p)
{
rects=new LinkedList<Rectangle2D>();
leftX=100;
topY=100;
itleft=20;
ittop=20;
canUp=false;
canDown=true;
canLeft=true;
canRight=true;
howlong=15;
move=false;
where=RIGHT;
creatOne();
KeyHandler listener=new KeyHandler();
p.addKeyListener(listener);
p.setFocusable(true);
}
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;
}
}
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;
}
}
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 paint(Graphics 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);
}

}
}
public boolean getDead()
{
return dead;
}
public void setDead(boolean b)
{
dead=b;
}
public boolean getMove()
{
return move;
}
public void setMove(boolean b)
{
move=b;
}
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;
public static final double WIDTH=6;
public static final double HEIGHT=6;
private Rectangle2D rect,rectP;
private LinkedList<Rectangle2D> rects;
private int howlong;
private boolean move;
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){}

}
}

when i want to ask anyone,i will ask myself first.
2006-06-06 10:25
aiyuheng
Rank: 1
等 级:新手上路
威 望:1
帖 子:656
专家分:0
注 册:2006-1-12
得分:0 
升级到1.5就行了啊!

when i want to ask anyone,i will ask myself first.
2006-06-24 12:52
aiyuheng
Rank: 1
等 级:新手上路
威 望:1
帖 子:656
专家分:0
注 册:2006-1-12
得分:0 
楼上斑竹加我QQ85127658

when i want to ask anyone,i will ask myself first.
2006-06-24 13:29
aiyuheng
Rank: 1
等 级:新手上路
威 望:1
帖 子:656
专家分:0
注 册:2006-1-12
得分:0 
恩 这样啊 没关系 其实我的QQ也没别的人 
只是些同学 和一些在网上认识的聊学习的朋友

when i want to ask anyone,i will ask myself first.
2006-06-24 13:49
aiyuheng
Rank: 1
等 级:新手上路
威 望:1
帖 子:656
专家分:0
注 册:2006-1-12
得分:0 
为什么不想升到1.5呢
1.4有什么好的东西在1.5中删掉了吗??

when i want to ask anyone,i will ask myself first.
2006-06-24 16:18



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-68284-1-1.html




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

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