首页 > 代码库 > java贪吃蛇

java贪吃蛇

自己写了个贪吃蛇Demo,只有简单控制功能大神勿喷,谢谢

方向键控制

蛇:

 1 public class Snack {
 2     private int x;
 3     
 4     private int y;
 5     
 6     public Snack(int x , int y){
 7         this.x = x;
 8         this.y = y;
 9         
10     }
11 
12     public int getX() {
13         return x;
14     }
15 
16     public void setX(int x) {
17         this.x = x;
18     }
19 
20     public int getY() {
21         return y;
22     }
23 
24     public void setY(int y) {
25         this.y = y;
26     }
27     
28     
29 }

显示面板:

  1 /*
  2  * 啓動類
  3  * auth:Aronn
  4  * 2017-4-23
  5  * */
  6 
  7 import java.awt.Color;
  8 import java.awt.Graphics;
  9 import java.awt.Insets;
 10 import java.awt.Rectangle;
 11 import java.awt.event.KeyAdapter;
 12 import java.awt.event.KeyEvent;
 13 import java.awt.event.KeyListener;
 14 import java.util.ArrayList;
 15 import java.util.List;
 16 
 17 import javax.swing.JOptionPane;
 18 import javax.swing.JPanel;
 19 import javax.swing.border.Border;
 20 
 21 public class SnackPanl extends JPanel implements Runnable {
 22 
 23     int dirx = 20, diry = 0;// 修改后方向
 24     int cdirx = 20, cdiry = 0;// 当前移动方向
 25     int fx = 0, fy = 0;
 26     boolean RIGHT = true, LEFT = false, UP = false, DOWN = false, DIE = false;
 27     // 蛇身
 28     List<Snack> snack = new ArrayList<Snack>();
 29 
 30     Thread t;
 31 
 32     public SnackPanl() {
 33         init();
 34         createFood();
 35         addKeyListener(new KeyAdapter() {
 36             Snack sn = snack.get(0);
 37 
 38             public void keyPressed(KeyEvent e) {
 39                 // 转向点
 40                 switch (e.getKeyCode()) {
 41                 case KeyEvent.VK_DOWN:
 42                     if (UP) {
 43                         DOWN = false;
 44                         LEFT = false;
 45                         RIGHT = false;
 46                     } else {
 47                         dirx = 0;
 48                         diry = 20;
 49                         DOWN = true;
 50                         RIGHT = false;
 51                         LEFT = false;
 52                         UP = false;
 53                     }
 54                     break;
 55                 case KeyEvent.VK_UP:
 56                     if (DOWN) {
 57                         UP = false;
 58                         LEFT = false;
 59                         RIGHT = false;
 60                     } else {
 61                         dirx = 0;
 62                         diry = -20;
 63                         DOWN = false;
 64                         RIGHT = false;
 65                         LEFT = false;
 66                         UP = true;
 67 
 68                     }
 69                     break;
 70                 case KeyEvent.VK_RIGHT:
 71                     if (LEFT) {
 72 
 73                         UP = false;
 74                         RIGHT = false;
 75                         DOWN = false;
 76                     } else {
 77                         dirx = 20;
 78                         diry = 0;
 79                         DOWN = false;
 80                         RIGHT = true;
 81                         LEFT = false;
 82                         UP = false;
 83 
 84                     }
 85 
 86                     break;
 87                 case KeyEvent.VK_LEFT:
 88                     if (RIGHT) {
 89                         UP = false;
 90                         LEFT = false;
 91                         DOWN = false;
 92                     } else {
 93                         dirx = -20;
 94                         diry = 0;
 95                         DOWN = false;
 96                         RIGHT = false;
 97                         LEFT = true;
 98                         UP = false;
 99 
100                     }
101                     break;
102                 default:
103                     break;
104                 }
105 
106             }
107 
108         });
109 
110         this.setVisible(true);
111 
112         t = new Thread(this);
113          t.start();
114     }
115 
116     @Override
117     public void paint(Graphics g) {
118 
119         super.paint(g);
120 
121         // 画网格
122         paintColRow(g);
123 
124         // 如果碰壁死亡
125         if (DIE) {
126             int result = JOptionPane.showConfirmDialog(this, "Game over",
127                     "message", 2, 2);
128             System.exit(0);
129 
130         } else {
131             // 移动核心代码
132             // 蛇身
133             Snack s = snack.get(0);
134             g.setColor(Color.red);
135             g.fillRect(s.getX() + cdirx, s.getY() + cdiry, 20, 20);// 蛇头按照当前控制方向向前移动
136             // 第二个移动到蛇头的位置
137             // 第三个移动到第二个的位置
138             // 依次类推
139             for (int i = 1; i <= snack.size(); i++) {
140                 g.setColor(Color.green);
141                 g.fillRect(snack.get(i - 1).getX(), snack.get(i - 1).getY(),
142                         20, 20);
143             }
144             // 移动全部蛇身后
145             // 重新设置蛇的当前位置
146             s.setX(s.getX() + cdirx);
147             s.setY(s.getY() + cdiry);
148             for (int i = snack.size() - 1; i >= 1; i--) {
149                 s = snack.get(i);
150                 s.setX(snack.get(i - 1).getX());
151                 s.setY(snack.get(i - 1).getY());
152             }
153 
154         }
155         // 食物
156         g.setColor(Color.PINK);
157         g.fillRect(this.fx, this.fy, 20, 20);
158 
159     }
160 
161     // 构建网格代码
162     // 22*22
163     // 最外一层是墙壁
164     private void paintColRow(Graphics g) {
165 
166         for (int i = 0; i <= 20; i++) {
167             g.setColor(Color.black);
168             g.drawRect(0, i * 20, 20, 20);
169         }
170         for (int i = 0; i <= 21; i++) {
171             g.setColor(Color.black);
172             g.drawRect(420, i * 20, 20, 20);
173         }
174         for (int i = 0; i <= 20; i++) {
175             g.setColor(Color.black);
176             g.drawRect(i * 20, 0, 20, 20);
177         }
178         for (int i = 0; i <= 20; i++) {
179             g.setColor(Color.black);
180             g.drawRect(i * 20, 420, 20, 20);
181         }
182         g.setColor(Color.white);
183         for (int i = 1; i <= 20; i++) {
184             for (int j = 1; j <= 20; j++) {
185                 g.drawRect(i * 20, j * 20, 20, 20);
186             }
187         }
188     }
189 
190     @Override
191     public void run() {
192         while (true) {
193             try {
194                 // 控制速度
195                 Thread.sleep(200);
196 
197                 // 判断是否吃食物
198                 if (this.eatFood()) {
199                     // 吃到食物蛇长度加1,并产生新的食物
200                     Snack ss = snack.get(snack.size() - 1);
201                     Snack s = new Snack(ss.getX(), ss.getY());
202                     snack.add(s);
203                     createFood();
204                 }
205                 cdirx = dirx;
206                 cdiry = diry;
207                 // 判断是否死亡碰壁
208                 if (snack.get(0).getX() <= 0 || snack.get(0).getX() >= 420
209                         || snack.get(0).getY() <= 0
210                         || snack.get(0).getY() >= 420) {
211                     DIE = true;
212                 }
213                 repaint();
214 
215             } catch (InterruptedException e) {
216 
217                 e.printStackTrace();
218             }
219         }
220     }
221 
222     // 產生食物
223     void createFood() {
224         fx = ((int) (Math.random() * 20) + 1) * 20;
225         fy = ((int) (Math.random() * 20) + 1) * 20;
226 
227     }
228 
229     boolean eatFood() {
230         boolean ff = false;
231         Snack s = snack.get(0);
232         Rectangle r1 = new Rectangle(s.getX(), s.getY(), 20, 20);
233         Rectangle r2 = new Rectangle(this.fx, this.fy, 20, 20);
234         if (r1.intersects(r2)) {
235             ff = true;
236         }
237         return ff;
238     }
239 
240     // 初始化
241     void init() {
242         Snack s1 = new Snack(180, 60);
243 
244         Snack s2 = new Snack(160, 60);
245 
246         Snack s3 = new Snack(140, 60);
247 
248         snack.add(s1);
249         snack.add(s2);
250         snack.add(s3);
251 
252     }
253  
254 }

执行:

 1 /*
 2  * 啓動類
 3  * auth:Aronn
 4  * 2017-4-23
 5  * */
 6 
 7 import java.awt.BorderLayout;
 8 import java.awt.Color;
 9 import java.awt.Container;
10 import java.awt.FlowLayout;
11 import java.awt.Graphics;
12 import java.awt.event.ActionEvent;
13 import java.awt.event.ActionListener;
14 import java.awt.event.KeyEvent;
15 import java.awt.event.KeyListener;
16 import java.awt.event.MouseEvent;
17 import java.awt.event.MouseListener;
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.Random;
21 
22 import javax.swing.JButton;
23 import javax.swing.JFrame;
24 import javax.swing.JLabel;
25 import javax.swing.JPanel;
26 import javax.swing.plaf.SliderUI;
27 
28 public class Run extends JFrame {
29 
30     SnackPanl snackpanl;
31     Thread t;
32 
33     public Run() {
34         // TODO Auto-generated constructor stub
35 
36         Container con = this.getContentPane();
37         con.setLayout(new BorderLayout());
38         snackpanl = new SnackPanl();
39 
40         snackpanl.setSize(20 * 23, 20 * 23);
41         snackpanl.setFocusable(true);
42 
43         con.add(snackpanl, BorderLayout.CENTER);
44 
45         this.setSize(460, 480);
46         this.setLocation(500, 50);
47         this.setBackground(Color.blue);
48         this.setVisible(true);
49         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
50 
51     }
52 
53     public static void main(String[] args) {
54         new Run();
55     }
56 
57 }

 

 运行结果:

技术分享

java贪吃蛇