├── 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 | 15 | 16 | 17 | 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 | 58 | 59 | 60 | 63 | 64 | 65 | 71 | 72 | 73 | 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 | 111 | 112 | 113 | 114 | 117 | 118 | 121 | 122 | 123 | 124 | 127 | 128 | 131 | 132 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 166 | 167 | 168 | 172 | 173 | 174 | 191 | 192 | 193 | 209 | 210 | 217 | 218 | 219 | 232 | 233 | 234 | 241 | 244 | 246 | 247 | 248 | 249 | 250 | 251 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 311 | 312 | 313 | 324 | 325 | 326 | 336 | 337 | 344 | 345 | 346 | 347 | 365 | 372 | 373 | 374 | 375 | 393 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 412 | 413 | 414 | 415 | 1484481008722 416 | 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 | 455 | 456 | 457 | 458 | 459 | 460 | 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 | --------------------------------------------------------------------------------