├── dot.png ├── apple.png ├── .idea ├── copyright │ └── profiles_settings.xml ├── modules.xml ├── compiler.xml ├── misc.xml └── workspace.xml ├── out └── production │ └── SnakeGame │ ├── GameField.class │ ├── MainWindow.class │ └── GameField$FieldKeyListener.class ├── SnakeGame.iml └── src ├── MainWindow.java └── GameField.java /dot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/irinamore/snake_part3-final-/HEAD/dot.png -------------------------------------------------------------------------------- /apple.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/irinamore/snake_part3-final-/HEAD/apple.png -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /out/production/SnakeGame/GameField.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/irinamore/snake_part3-final-/HEAD/out/production/SnakeGame/GameField.class -------------------------------------------------------------------------------- /out/production/SnakeGame/MainWindow.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/irinamore/snake_part3-final-/HEAD/out/production/SnakeGame/MainWindow.class -------------------------------------------------------------------------------- /out/production/SnakeGame/GameField$FieldKeyListener.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/irinamore/snake_part3-final-/HEAD/out/production/SnakeGame/GameField$FieldKeyListener.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.awt.event.KeyAdapter; 6 | import java.awt.event.KeyEvent; 7 | import java.util.Random; 8 | 9 | /** 10 | * Created by infuntis on 15/01/17. 11 | */ 12 | public class GameField extends JPanel implements ActionListener{ 13 | private final int SIZE = 320; 14 | private final int DOT_SIZE = 16; 15 | private final int ALL_DOTS = 400; 16 | private Image dot; 17 | private Image apple; 18 | private int appleX; 19 | private int appleY; 20 | private int[] x = new int[ALL_DOTS]; 21 | private int[] y = new int[ALL_DOTS]; 22 | private int dots; 23 | private Timer timer; 24 | private boolean left = false; 25 | private boolean right = true; 26 | private boolean up = false; 27 | private boolean down = false; 28 | private boolean inGame = true; 29 | 30 | 31 | public GameField(){ 32 | setBackground(Color.black); 33 | loadImages(); 34 | initGame(); 35 | addKeyListener(new FieldKeyListener()); 36 | setFocusable(true); 37 | 38 | } 39 | 40 | public void initGame(){ 41 | dots = 3; 42 | for (int i = 0; i < dots; i++) { 43 | x[i] = 48 - i*DOT_SIZE; 44 | y[i] = 48; 45 | } 46 | timer = new Timer(250,this); 47 | timer.start(); 48 | createApple(); 49 | } 50 | 51 | public void createApple(){ 52 | appleX = new Random().nextInt(20)*DOT_SIZE; 53 | appleY = new Random().nextInt(20)*DOT_SIZE; 54 | } 55 | 56 | public void loadImages(){ 57 | ImageIcon iia = new ImageIcon("apple.png"); 58 | apple = iia.getImage(); 59 | ImageIcon iid = new ImageIcon("dot.png"); 60 | dot = iid.getImage(); 61 | } 62 | 63 | @Override 64 | protected void paintComponent(Graphics g) { 65 | super.paintComponent(g); 66 | if(inGame){ 67 | g.drawImage(apple,appleX,appleY,this); 68 | for (int i = 0; i < dots; i++) { 69 | g.drawImage(dot,x[i],y[i],this); 70 | } 71 | } else{ 72 | String str = "Game Over"; 73 | //Font f = new Font("Arial",14,Font.BOLD); 74 | g.setColor(Color.white); 75 | // g.setFont(f); 76 | g.drawString(str,125,SIZE/2); 77 | } 78 | } 79 | 80 | public void move(){ 81 | for (int i = dots; i > 0; i--) { 82 | x[i] = x[i-1]; 83 | y[i] = y[i-1]; 84 | } 85 | if(left){ 86 | x[0] -= DOT_SIZE; 87 | } 88 | if(right){ 89 | x[0] += DOT_SIZE; 90 | } if(up){ 91 | y[0] -= DOT_SIZE; 92 | } if(down){ 93 | y[0] += DOT_SIZE; 94 | } 95 | } 96 | 97 | public void checkApple(){ 98 | if(x[0] == appleX && y[0] == appleY){ 99 | dots++; 100 | createApple(); 101 | } 102 | } 103 | 104 | public void checkCollisions(){ 105 | for (int i = dots; i >0 ; i--) { 106 | if(i>4 && x[0] == x[i] && y[0] == y[i]){ 107 | inGame = false; 108 | } 109 | } 110 | 111 | if(x[0]>SIZE){ 112 | inGame = false; 113 | } 114 | if(x[0]<0){ 115 | inGame = false; 116 | } 117 | if(y[0]>SIZE){ 118 | inGame = false; 119 | } 120 | if(y[0]<0){ 121 | inGame = false; 122 | } 123 | } 124 | 125 | @Override 126 | public void actionPerformed(ActionEvent e) { 127 | if(inGame){ 128 | checkApple(); 129 | checkCollisions(); 130 | move(); 131 | 132 | } 133 | repaint(); 134 | } 135 | 136 | class FieldKeyListener extends KeyAdapter{ 137 | @Override 138 | public void keyPressed(KeyEvent e) { 139 | super.keyPressed(e); 140 | int key = e.getKeyCode(); 141 | if(key == KeyEvent.VK_LEFT && !right){ 142 | left = true; 143 | up = false; 144 | down = false; 145 | } 146 | if(key == KeyEvent.VK_RIGHT && !left){ 147 | right = true; 148 | up = false; 149 | down = false; 150 | } 151 | 152 | if(key == KeyEvent.VK_UP && !down){ 153 | right = false; 154 | up = true; 155 | left = false; 156 | } 157 | if(key == KeyEvent.VK_DOWN && !up){ 158 | right = false; 159 | down = true; 160 | left = false; 161 | } 162 | } 163 | } 164 | 165 | 166 | } 167 | -------------------------------------------------------------------------------- /.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 | 109 | 110 | 111 | 112 | 115 | 116 | 119 | 120 | 121 | 122 | 125 | 126 | 129 | 130 | 133 | 134 | 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 | 508 | 509 | 510 | 511 | 512 | 513 | 514 | 515 | 516 | 517 | 518 | 519 | 520 | 521 | 522 | 523 | 524 | 525 | 526 | 527 | 528 | 529 | 530 | 531 | 532 | 533 | 534 | 535 | 536 | 537 | 538 | 539 | 540 | 541 | 542 | 543 | 544 | 545 | 546 | 547 | 548 | 549 | --------------------------------------------------------------------------------