您的位置:首页 > 移动开发 > 微信开发

贪吃蛇的小程序

2016-03-18 18:23 441 查看
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
class  Demo
{
public static void main(String[] args)
{
Worm wo=new Worm();
}
}
class Worm extends JFrame
{
JPanel panel;
Point food=null;
private int speed=200;
private int dir=1;//方向
JButton contro,bu,bu2;
ArrayList<Point> worm=new ArrayList<Point>();
HashMap<Point,JButton> map=new HashMap<Point,JButton>();
Dialog dia;
Label la;
boolean isContinue=true;
Worm()
{
init();
start();
}
public void init()
{
this.setBounds(250,100,500,500);
this.add(setPanel());
this.setResizable(false);
this.myEvent();
this.setVisible(true);
}
private void setdia()//设置弹出窗口
{
dia=new Dialog(this,"game over",true);
bu=new JButton("重新开始");
la=new Label("game over");
bu2=new JButton("退出");
dia.add(la);
dia.add(bu);
dia.add(bu2);
dia.setBounds(350,200,200,100);
dia.setLayout(new FlowLayout());

}
public JPanel setPanel()
{
if(panel==null)
{
panel=new JPanel();
if(contro==null)
getcontro();
setdia();
panel.add(contro);
panel.setLayout(null);
panel.setBackground(new Color(0,0,0));
for(int i=0;i<500;i+=10)
{
for(int j=0;j<470;j+=10)
{
Point p=new Point(i,j);
JButton jb=new JButton();
jb.setBounds(i,j,10,10);
jb.setBackground(new Color(255,255,255));
jb.setEnabled(false);
panel.add(jb,null);
map.put(p,jb);
}
}
getFood();
addworm();
}
return panel;
}
public void getFood()
{
if(food==null)
{
Random ran=new Random();
int x=ran.nextInt(49)*10;
int y=ran.nextInt(46)*10;
food=new Point(x,y);
while(worm.contains(food))
{
x=ran.nextInt(49)*10;
y=ran.nextInt(46)*10;
food=new Point(x,y);
}
JButton jb1=map.get(food);
jb1.setBackground(Color.black);
}
}
public void start()//游戏开始
{
while(true)
{
System.out.println("");
while(isContinue)
{
Point p=worm.get(0);
int x=0,y=0;
if(dir==-1)
{
x=(int)p.getX()-10;
y=(int)p.getY();
}
if(dir==1)
{
x=(int)p.getX()+10;
y=(int)p.getY();
}
if(dir==-2)
{
x=(int)p.getX();
y=(int)p.getY()-10;
}
if(dir==2)
{
x=(int)p.getX();
y=(int)p.getY()+10;
}
Point nextp=new Point(x,y);
if(food.equals(nextp))
{
worm.add(0,nextp);
map.get(nextp).setBackground(Color.red);
food=null;
getFood();
if(speed!=80)
speed-=10;
continue;
}
if(worm.contains(nextp)||map.get(nextp)==null)
{
dia.setVisible(true);
continue;
}
worm.add(0,nextp);
map.get(nextp).setBackground(Color.red);
map.get(worm.remove(worm.size()-1)).setBackground(Color.white);
try{Thread.sleep(speed);}
catch(Exception e)
{
throw new RuntimeException("发生错误");
}
}
}
}
private void addworm()//构造一条贪吃蛇
{
worm=new ArrayList<Point>();
for(int i=200;i<230;i+=10)
{
Point p=new Point(i,200);
worm.add(0,p);
map.get(p).setBackground(Color.red);
}
}
public void getcontro()
{
contro=new JButton();
}
private void compare(int num)
{
if((num+dir)!=0)
dir=num;
}
private void resum()//重构游戏
{
ListIterator<Point> it=worm.listIterator();
while(it.hasNext())
{
map.get(it.next()).setBackground(Color.white);
}
map.get(food).setBackground(Color.white);
food=null;
getFood();
addworm();
dia.setVisible(false);
speed=200;
if(dir==-1)
dir=1;
}
private void myEvent()//所有监听器
{
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
bu.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
resum();
}
});
bu2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
contro.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent l)
{
int numb=l.getKeyCode();
switch(numb)
{
case KeyEvent.VK_LEFT :compare(-1);break;
case KeyEvent.VK_RIGHT :compare(1);break;
case KeyEvent.VK_UP :compare(-2);break;
case KeyEvent.VK_DOWN :compare(2);break;
case KeyEvent.VK_ENTER:isContinue=true;break;
case KeyEvent.VK_SPACE:isContinue=false;break;
}
}
});
}
}


自制贪吃蛇小游戏
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: