├── dot.png
├── apple.png
├── .idea
├── copyright
│ └── profiles_settings.xml
├── modules.xml
├── compiler.xml
├── misc.xml
└── workspace.xml
├── out
└── production
│ └── SnakeGame
│ ├── GameField.class
│ └── MainWindow.class
├── SnakeGame.iml
└── src
├── MainWindow.java
└── GameField.java
/dot.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/irinamore/snake_part2/HEAD/dot.png
--------------------------------------------------------------------------------
/apple.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/irinamore/snake_part2/HEAD/apple.png
--------------------------------------------------------------------------------
/.idea/copyright/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/out/production/SnakeGame/GameField.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/irinamore/snake_part2/HEAD/out/production/SnakeGame/GameField.class
--------------------------------------------------------------------------------
/out/production/SnakeGame/MainWindow.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/irinamore/snake_part2/HEAD/out/production/SnakeGame/MainWindow.class
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/SnakeGame.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/src/MainWindow.java:
--------------------------------------------------------------------------------
1 | import javax.swing.*;
2 |
3 | /**
4 | * Created by infuntis on 15/01/17.
5 | */
6 | public class MainWindow extends JFrame {
7 |
8 | public MainWindow(){
9 | setTitle("Змейка");
10 | setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
11 | setSize(320,345);
12 | setLocation(400,400);
13 | add(new GameField());
14 | setVisible(true);
15 | }
16 |
17 | public static void main(String[] args) {
18 | MainWindow mw = new MainWindow();
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
--------------------------------------------------------------------------------
/src/GameField.java:
--------------------------------------------------------------------------------
1 | import javax.swing.*;
2 | import java.awt.*;
3 | import java.awt.event.ActionEvent;
4 | import java.awt.event.ActionListener;
5 | import java.util.Random;
6 |
7 | /**
8 | * Created by infuntis on 15/01/17.
9 | */
10 | public class GameField extends JPanel implements ActionListener{
11 | private final int SIZE = 320;
12 | private final int DOT_SIZE = 16;
13 | private final int ALL_DOTS = 400;
14 | private Image dot;
15 | private Image apple;
16 | private int appleX;
17 | private int appleY;
18 | private int[] x = new int[ALL_DOTS];
19 | private int[] y = new int[ALL_DOTS];
20 | private int dots;
21 | private Timer timer;
22 | private boolean left = false;
23 | private boolean right = true;
24 | private boolean up = false;
25 | private boolean down = false;
26 | private boolean inGame = true;
27 |
28 |
29 | public GameField(){
30 | setBackground(Color.black);
31 | loadImages();
32 | initGame();
33 |
34 | }
35 |
36 | public void initGame(){
37 | dots = 3;
38 | for (int i = 0; i < dots; i++) {
39 | x[i] = 48 - i*DOT_SIZE;
40 | y[i] = 48;
41 | }
42 | timer = new Timer(250,this);
43 | timer.start();
44 | createApple();
45 | }
46 |
47 | public void createApple(){
48 | appleX = new Random().nextInt(20)*DOT_SIZE;
49 | appleY = new Random().nextInt(20)*DOT_SIZE;
50 | }
51 |
52 | public void loadImages(){
53 | ImageIcon iia = new ImageIcon("apple.png");
54 | apple = iia.getImage();
55 | ImageIcon iid = new ImageIcon("dot.png");
56 | dot = iid.getImage();
57 | }
58 |
59 | @Override
60 | protected void paintComponent(Graphics g) {
61 | super.paintComponent(g);
62 | if(inGame){
63 | g.drawImage(apple,appleX,appleY,this);
64 | for (int i = 0; i < dots; i++) {
65 | g.drawImage(dot,x[i],y[i],this);
66 | }
67 | }
68 | }
69 |
70 | public void move(){
71 | for (int i = dots; i > 0; i--) {
72 | x[i] = x[i-1];
73 | y[i] = y[i-1];
74 | }
75 | if(left){
76 | x[0] -= DOT_SIZE;
77 | }
78 | if(right){
79 | x[0] += DOT_SIZE;
80 | } if(up){
81 | y[0] -= DOT_SIZE;
82 | } if(down){
83 | y[0] += DOT_SIZE;
84 | }
85 | }
86 |
87 | public void checkApple(){
88 | if(x[0] == appleX && y[0] == appleY){
89 | dots++;
90 | createApple();
91 | }
92 | }
93 |
94 | public void checkCollisions(){
95 | for (int i = dots; i >0 ; i--) {
96 | if(i>4 && x[0] == x[i] && y[0] == y[i]){
97 | inGame = false;
98 | }
99 | }
100 |
101 | if(x[0]>SIZE){
102 | inGame = false;
103 | }
104 | if(x[0]<0){
105 | inGame = false;
106 | }
107 | if(y[0]>SIZE){
108 | inGame = false;
109 | }
110 | if(y[0]<0){
111 | inGame = false;
112 | }
113 | }
114 |
115 | @Override
116 | public void actionPerformed(ActionEvent e) {
117 | if(inGame){
118 | checkApple();
119 | checkCollisions();
120 | move();
121 |
122 | }
123 | repaint();
124 | }
125 | }
126 |
--------------------------------------------------------------------------------
/.idea/workspace.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 |
163 |
164 |
165 |
166 |
167 |
168 |
169 |
170 |
171 |
172 |
173 |
174 |
175 |
176 |
177 |
178 |
179 |
180 |
181 |
182 |
183 |
184 |
185 |
186 |
187 |
188 |
189 |
190 |
191 |
192 |
193 |
194 |
195 |
196 |
197 |
198 |
199 |
200 |
201 |
202 |
203 |
204 |
205 |
206 |
207 |
208 |
209 |
210 |
211 |
212 |
213 |
214 |
215 |
216 |
217 |
218 |
219 |
220 |
221 |
222 |
223 |
224 |
225 |
226 |
227 |
228 |
229 |
230 |
231 |
232 |
233 |
234 |
235 |
236 |
237 |
238 |
239 |
240 |
241 |
242 |
243 |
244 |
245 |
246 |
247 |
248 |
249 |
250 |
251 |
252 |
253 |
254 |
255 |
256 |
257 |
258 |
259 |
260 |
261 |
262 |
263 |
264 |
265 |
266 |
267 |
268 |
269 |
270 |
271 |
272 |
273 |
274 |
275 |
276 |
277 |
278 |
279 |
280 |
281 |
282 |
283 |
284 |
285 |
286 |
287 |
288 |
289 |
290 |
291 |
292 |
293 |
294 |
295 |
296 |
297 |
298 |
299 |
300 |
301 |
302 |
303 |
304 |
305 |
306 |
307 |
308 |
309 |
310 |
311 |
312 |
313 |
314 |
315 |
316 |
317 |
318 |
319 |
320 |
321 |
322 |
323 |
324 |
325 |
326 |
327 |
328 |
329 |
330 |
331 |
332 |
333 |
334 |
335 |
336 |
337 |
338 |
339 |
340 |
341 |
342 |
343 |
344 |
345 |
346 |
347 |
348 |
349 |
350 |
351 |
352 |
353 |
354 |
355 |
356 |
357 |
358 |
359 |
360 |
361 |
362 |
363 |
364 |
365 |
366 |
367 |
368 |
369 |
370 |
371 |
372 |
373 |
374 |
375 |
376 |
377 |
378 |
379 |
380 |
381 |
382 |
383 |
384 |
385 |
386 |
387 |
388 |
389 |
390 |
391 |
392 |
393 |
394 |
395 |
396 |
397 |
398 |
399 |
400 |
401 |
402 |
403 |
404 |
405 |
406 |
407 |
408 |
409 |
410 |
411 |
412 |
413 |
414 |
415 | 1484481008722
416 |
417 |
418 | 1484481008722
419 |
420 |
421 |
422 |
423 |
424 |
425 |
426 |
427 |
428 |
429 |
430 |
431 |
432 |
433 |
434 |
435 |
436 |
437 |
438 |
439 |
440 |
441 |
442 |
443 |
444 |
445 |
446 |
447 |
448 |
449 |
450 |
451 |
452 |
453 |
454 |
455 |
456 |
457 |
458 |
459 |
460 |
461 |
462 |
463 |
464 |
465 |
466 |
467 |
468 |
469 |
470 |
471 |
472 |
473 |
474 |
475 |
476 |
477 |
478 |
479 |
480 |
481 |
482 |
483 |
484 |
485 |
486 |
487 |
488 |
489 |
490 |
491 |
492 |
493 |
494 |
495 |
496 |
497 |
498 |
499 |
500 |
501 |
502 |
503 |
504 |
505 |
506 |
507 |
--------------------------------------------------------------------------------