贪吃蛇的小程序

  1 import java.awt.*;
  2 import javax.swing.*;
  3 import java.util.*;
  4 import java.awt.event.*;
  5 class  Demo
  6 {
  7  public static void main(String[] args) 
  8  {
  9   Worm wo=new Worm();
 10  }
 11 }
 12 class Worm extends JFrame
 13 {
 14  JPanel panel;
 15  Point food=null;
 16  private int speed=200;
 17  private int dir=1;//方向
 18  JButton contro,bu,bu2;
 19  ArrayList<Point> worm=new ArrayList<Point>();
 20  HashMap<Point,JButton> map=new HashMap<Point,JButton>();
 21  Dialog dia;
 22  Label la;
 23  boolean isContinue=true;
 24  Worm()
 25  {
 26   init();
 27   start();
 28  }
 29  public void init()
 30  {
 31   this.setBounds(250,100,500,500);
 32   this.add(setPanel());
 33   this.setResizable(false);
 34   this.myEvent();
 35   this.setVisible(true);
 36  }
 37  private void setdia()//设置弹出窗口
 38  {
 39   dia=new Dialog(this,"game over",true);
 40   bu=new JButton("重新开始");
 41   la=new Label("game over");
 42   bu2=new JButton("退出");
 43   dia.add(la);
 44   dia.add(bu);
 45   dia.add(bu2);
 46   dia.setBounds(350,200,200,100);
 47   dia.setLayout(new FlowLayout());
 48   
 49  }
 50  public JPanel setPanel()
 51  {
 52   if(panel==null)
 53   {
 54    panel=new JPanel();
 55    if(contro==null)
 56     getcontro();
 57    setdia();
 58    panel.add(contro);
 59    panel.setLayout(null);
 60    panel.setBackground(new Color(0,0,0));
 61    for(int i=0;i<500;i+=10)
 62    {
 63     for(int j=0;j<470;j+=10)
 64     {
 65      Point p=new Point(i,j);
 66      JButton jb=new JButton();
 67      jb.setBounds(i,j,10,10);
 68      jb.setBackground(new Color(255,255,255));
 69      jb.setEnabled(false);
 70      panel.add(jb,null);
 71      map.put(p,jb);
 72     }
 73    }
 74    getFood();
 75    addworm();
 76   }
 77   return panel;
 78  }
 79  public void getFood()
 80  {
 81   if(food==null)
 82   {
 83    Random ran=new Random();
 84    int x=ran.nextInt(49)*10;
 85    int y=ran.nextInt(46)*10;
 86    food=new Point(x,y);
 87    while(worm.contains(food))
 88    {
 89     x=ran.nextInt(49)*10;
 90     y=ran.nextInt(46)*10;
 91     food=new Point(x,y);
 92    }
 93    JButton jb1=map.get(food);
 94    jb1.setBackground(Color.black);
 95   }
 96  }
 97  public void start()//游戏开始
 98  {
 99   while(true)
100   {
101    System.out.println("");
102    while(isContinue)
103    {
104     Point p=worm.get(0);
105     int x=0,y=0;
106     if(dir==-1)
107     {
108      x=(int)p.getX()-10;
109      y=(int)p.getY();
110     }
111     if(dir==1)
112     {
113      x=(int)p.getX()+10;
114      y=(int)p.getY();
115     }
116     if(dir==-2)
117     {
118      x=(int)p.getX();
119      y=(int)p.getY()-10;
120     }
121     if(dir==2)
122     {
123      x=(int)p.getX();
124      y=(int)p.getY()+10;
125     }
126     Point nextp=new Point(x,y);
127     if(food.equals(nextp))
128     {
129      worm.add(0,nextp);
130      map.get(nextp).setBackground(Color.red);
131      food=null;
132      getFood();
133      if(speed!=80)
134       speed-=10;
135      continue;
136     }
137     if(worm.contains(nextp)||map.get(nextp)==null)
138     {
139      dia.setVisible(true);
140      continue;
141     }
142     worm.add(0,nextp);
143     map.get(nextp).setBackground(Color.red);
144     map.get(worm.remove(worm.size()-1)).setBackground(Color.white);
145     try{Thread.sleep(speed);}
146     catch(Exception e)
147     {
148      throw new RuntimeException("发生错误");
149     }
150    }
151   }
152  }
153  private void addworm()//构造一条贪吃蛇
154  {
155   worm=new ArrayList<Point>();
156   for(int i=200;i<230;i+=10)
157   {
158    Point p=new Point(i,200);
159    worm.add(0,p);
160    map.get(p).setBackground(Color.red);
161   }
162  }
163  public void getcontro()
164  {
165   contro=new JButton();
166  }
167  private void compare(int num)
168  {
169   if((num+dir)!=0)
170    dir=num;
171  }
172  private void resum()//重构游戏
173  {
174   ListIterator<Point> it=worm.listIterator();
175   while(it.hasNext())
176   {
177    map.get(it.next()).setBackground(Color.white);
178   }
179   map.get(food).setBackground(Color.white);
180   food=null;
181   getFood();
182   addworm();
183   dia.setVisible(false);
184   speed=200;
185   if(dir==-1)
186    dir=1;
187  }
188  private void myEvent()//所有监听器
189  {
190   this.addWindowListener(new WindowAdapter()
191   {
192    public void windowClosing(WindowEvent e)
193    {
194     System.exit(0);
195    }
196   });
197   bu.addActionListener(new ActionListener()
198   {
199    public void actionPerformed(ActionEvent e)
200    {
201     resum();
202    }
203   });
204   bu2.addActionListener(new ActionListener()
205   {
206    public void actionPerformed(ActionEvent e)
207    {
208     System.exit(0);
209    }
210   });
211   contro.addKeyListener(new KeyAdapter()
212   {
213    public void keyPressed(KeyEvent l)
214    {
215     int numb=l.getKeyCode();
216     switch(numb)
217     {
218      case KeyEvent.VK_LEFT :compare(-1);break;
219                     case KeyEvent.VK_RIGHT :compare(1);break;
220                     case KeyEvent.VK_UP :compare(-2);break;
221                     case KeyEvent.VK_DOWN :compare(2);break;
222      case KeyEvent.VK_ENTER:isContinue=true;break;
223                     case KeyEvent.VK_SPACE:isContinue=false;break;
224     }
225    }
226   });
227  }
228 }


自制贪吃蛇小游戏  

posted @ 2016-03-18 18:23  小寐一觉  阅读(520)  评论(0编辑  收藏  举报