Advertisement
daniv1

Untitled

May 18th, 2018
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.85 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Dimension;
  3. import java.awt.Font;
  4. import java.awt.FontMetrics;
  5. import java.awt.Graphics;
  6. import java.awt.Image;
  7. import java.awt.Toolkit;
  8. import java.awt.event.ActionEvent;
  9. import java.awt.event.ActionListener;
  10. import java.awt.event.KeyAdapter;
  11. import java.awt.event.KeyEvent;
  12. import java.awt.Component;
  13. import javax.swing.*;
  14. import javafx.embed.swing.*;
  15. public class Board extends JPanel implements ActionListener {
  16.  
  17.     private final int B_WIDTH = 500;
  18.     private final int B_HEIGHT = 500;
  19.     private final int DOT_SIZE = 10;
  20.     private final int ALL_DOTS = 25000;
  21.     private final int RAND_POS = 48;
  22.     private final int DELAY = 100;
  23.  
  24.     private final int x[] = new int[ALL_DOTS];
  25.     private final int y[] = new int[ALL_DOTS];
  26.  
  27.     private int dots;
  28.     private int dw = 1;
  29.    
  30.     private int bseCount;
  31.     private int[] bse_x = new int[15];
  32.     private int bs_x;
  33.     private int bs_y;
  34.     private int bsDir = 1;
  35.    
  36.     private int cnt = 0;
  37.     private int cnt_h = 0;
  38.     private int cnt_w = 0;
  39.     private int apple_x;
  40.     private int apple_y;
  41.    
  42.     private int heart_x;
  43.     private int heart_y;
  44.    
  45.     private final int obstacle_x [] = new int[10];
  46.     private final int obstacle_y[] = new int[10];
  47.  
  48.     private int r1 = (int)(Math.random() * RAND_POS);
  49.     private int r2 = (int)(Math.random() * RAND_POS);
  50.    
  51.     private  final int worm_x[] = new int [10];
  52.     private  final int worm_y[] = new int [10];
  53.    
  54.     private boolean leftDirection = false;
  55.     private boolean rightDirection = true;
  56.     private boolean upDirection = false;
  57.     private boolean downDirection = false;
  58.     public boolean inGame = true;
  59.  
  60.     private Timer timer;
  61.     private Image ball;
  62.     private Image apple;
  63.     private Image head;
  64.     private Image obs;
  65.     private Image snake;
  66.     private Image heart;
  67.  
  68.  
  69.    
  70.     public Board() {
  71.  
  72.         addKeyListener(new TAdapter());
  73.         setBackground(Color.gray);
  74.         setFocusable(true);
  75.  
  76.         setPreferredSize(new Dimension(B_WIDTH, B_HEIGHT));
  77.         loadImages();
  78.         initGame();
  79.     }
  80.  
  81.     private void loadImages() {
  82.  
  83.         ImageIcon iid = new ImageIcon("dot.png");
  84.         ball = iid.getImage();
  85.  
  86.         ImageIcon iia = new ImageIcon("apple.png");
  87.         apple = iia.getImage();
  88.  
  89.         ImageIcon iih = new ImageIcon("head.png");
  90.         head = iih.getImage();
  91.        
  92.         ImageIcon iiobs = new ImageIcon("obs.phg");
  93.         obs = iiobs.getImage();
  94.        
  95.         ImageIcon iiw = new ImageIcon("worm.png");
  96.         snake = iiw.getImage();
  97.        
  98.         ImageIcon iiheart = new ImageIcon("heart.png");
  99.         heart = iiheart.getImage();
  100.     }
  101.     private void initGame() {
  102.  
  103.        
  104.         leftDirection = false;
  105.         rightDirection = true;
  106.         upDirection = false;
  107.         downDirection = false;
  108.         dots = 3;
  109.         for (int z = 0; z < dots; z++) {
  110.             x[z] = 250 - z * 10;
  111.             y[z] = 250;
  112.         }
  113.  
  114.         locateApple();
  115.         localeHeart();
  116.         locateObstacle();
  117.         locateBlockSnake();;
  118.        
  119.         timer = new Timer(DELAY, this);
  120.         timer.start();
  121.     }
  122.  
  123.    
  124.     public void paintComponent(Graphics g) {
  125.         super.paintComponent(g);
  126.  
  127.         doDrawing(g);
  128.     }
  129.    
  130.     private void doDrawing(Graphics g) {
  131.        
  132.         if (inGame) {
  133.  
  134.                
  135.            
  136.             g.drawImage(apple, apple_x, apple_y, this);
  137.             if(cnt%10 == 0) {
  138.             g.drawImage(heart, heart_x, heart_y, this);
  139.             }
  140.             for(int i = 0; i < 10; ++i) {
  141.                 g.drawImage(this.snake, this.bse_x[i], this.bs_y, this);
  142.             }
  143.             for( int i = 0; i < 10 ; i++ ) {
  144.                 //g.drawImage(worm, worm_x[i] , worm_y[i] , this);
  145.                 g.drawImage(ball, obstacle_x[i], obstacle_y[i] , this);
  146.        
  147.             }
  148.             for (int z = 0; z < dots; z++) {
  149.                 if (z == 0) {
  150.                     g.drawImage(head, x[z], y[z], this);
  151.                 } else {
  152.                     g.drawImage(ball, x[z], y[z], this);
  153.                 }
  154.             }
  155.  
  156.             Toolkit.getDefaultToolkit().sync();
  157.  
  158.         } else {
  159.  
  160.             gameOver(g);
  161.         }        
  162.     }
  163.  
  164.     private void gameOver(Graphics g) {
  165.         String msgcnt = "eaten an apple  - "+ (cnt - cnt_h);
  166.         String msgheart = "eaten hearts  - " + cnt_h;
  167.         String msg = "Game Over";
  168.         Font small = new Font("Helvetica", Font.BOLD, 14);
  169.         FontMetrics metr = getFontMetrics(small);
  170.  
  171.         g.setColor(Color.white);
  172.         g.setFont(small);
  173.         g.drawString(msg, (B_WIDTH - metr.stringWidth(msg)) / 2, B_HEIGHT / 2);
  174.         g.drawString(msgcnt, 200,300 );
  175.         g.drawString(msgheart, 200, 320);
  176.         cnt = 0;
  177.         cnt_h = 0;
  178.        
  179.    
  180.     }
  181.  
  182.     private void checkApple() {
  183.  
  184.         if ((x[0] == apple_x) && (y[0] == apple_y)) {
  185.  
  186.            
  187.             dots++;
  188.             cnt++;
  189.            
  190.             locateApple();
  191.             locateObstacle();
  192.         }
  193.     }
  194.    
  195.     private void checkHeart() {
  196.        if ((x[0] == heart_x) && (y[0] == heart_y)) {
  197.  
  198.        
  199.             dots+=3;
  200.             cnt_h++;
  201.             cnt++;
  202.          
  203.            if(cnt% 10== 0)localeHeart();
  204.            locateObstacle();
  205.         }
  206.     }
  207.  
  208.  
  209.     private void move() {
  210.  
  211.    
  212.         for (int z = dots; z > 0; z--) {
  213.             x[z] = x[(z - 1)];
  214.             y[z] = y[(z - 1)];
  215.         }
  216.  
  217.         if (leftDirection) {
  218.             x[0] -= DOT_SIZE;
  219.         }
  220.  
  221.         if (rightDirection) {
  222.             x[0] += DOT_SIZE;
  223.         }
  224.  
  225.         if (upDirection) {
  226.             y[0] -= DOT_SIZE;
  227.         }
  228.  
  229.         if (downDirection) {
  230.             y[0] += DOT_SIZE;
  231.         }
  232.         this.bs_x += 10 * this.bsDir;
  233.  
  234.         for(int i = 0; i < 15; ++i) {
  235.             if (bs_x >= 0 && bs_x <= 500) {
  236.                 this.bse_x[i] = this.bs_x + i * 10;
  237.             }
  238.  
  239.             if (this.bs_x + 15 * 10 == 600) {
  240.                 this.bsDir = -1;
  241.             }
  242.  
  243.             if (this.bs_x == 0) {
  244.                 this.bsDir = 1;
  245.             }
  246.         }
  247.     }
  248.  
  249.     private void checkObstacle() {
  250.        
  251.         for(int i = 0 ; i < 10 ; i++) {
  252.            
  253.         if(x[0] == obstacle_x[i] && y[0] == obstacle_y[i]) {  
  254.             inGame = false;
  255.             timer.stop();
  256.      
  257.         }
  258.         }
  259.  
  260.    
  261.     }
  262.     private void checkCollision() {
  263.  
  264.         for (int z = dots; z > 0; z--) {
  265.  
  266.             if ((z > 4) && (x[0] == x[z]) && (y[0] == y[z])) {
  267.                 inGame = false;
  268.             }
  269.         }
  270.  
  271.         if (y[0] >= B_HEIGHT) {
  272.             inGame = false;
  273.         }
  274.  
  275.         if (y[0] < 0) {
  276.             inGame = false;
  277.         }
  278.  
  279.         if (x[0] >= B_WIDTH) {
  280.             inGame = false;
  281.         }
  282.  
  283.         if (x[0] < 0) {
  284.             inGame = false;
  285.         }
  286.        
  287.         if(!inGame) {
  288.             timer.stop();
  289.         }
  290.     }
  291.  
  292.     private void locateApple() {
  293.  
  294.         int r = (int) (Math.random() * RAND_POS);
  295.         apple_x = ((r * DOT_SIZE));
  296.  
  297.         r = (int) (Math.random() * RAND_POS);
  298.         apple_y = ((r * DOT_SIZE));
  299.     }
  300.  
  301.  
  302.     private void locateObstacle() {
  303.         for(int i = 0 ; i < 10 ; i++) {
  304.             int r = (int)(Math.random() * RAND_POS);
  305.             obstacle_x[i] = r * DOT_SIZE;
  306.            
  307.          r = (int)(Math.random() * RAND_POS);
  308.             obstacle_y[i] = r* DOT_SIZE;
  309.            
  310.         }
  311.         }
  312.  
  313.     private void checkBlockWorm() {
  314.         for(int i = 0; i < 15; ++i) {
  315.             for(int j = 0; j <100; ++j) {
  316.                  if (this.x[j] == this.bse_x[i] && this.y[j] == this.bs_y) {
  317.                      if (j > 1) {
  318.                          this.dots = j;
  319.                      } else {
  320.                          this.inGame = false;
  321.                          this.timer.stop();
  322.                      }
  323.                 }
  324.             }
  325.         }
  326.  
  327.     }
  328.     private void locateBlockSnake() {
  329.  
  330.  
  331.         bs_y = (int)(Math.random() * 600.0D / 10.0D) - 2;
  332.         bs_y *= 10;
  333.         bs_x = 0;
  334.  
  335.         for(int i = 0; i < 15; ++i) {
  336.             this.bse_x[i] = this.bs_x + i * 10;
  337.         }
  338.  
  339.     }
  340.  
  341.        
  342.    
  343.     private void localeHeart() {
  344.         if(cnt % 10 == 0) {
  345.         int r = (int) (Math.random() * RAND_POS);
  346.         heart_x = ((r * DOT_SIZE));
  347.  
  348.         r = (int) (Math.random() * RAND_POS);
  349.         heart_y = ((r * DOT_SIZE));
  350.         }
  351.        
  352.     }
  353.     public void actionPerformed(ActionEvent e) {
  354.  
  355.         if (inGame) {
  356.  
  357.             checkApple();
  358.             checkCollision();
  359.             checkHeart();
  360.             checkObstacle();
  361.             checkBlockWorm();
  362.             move();
  363.         }
  364.  
  365.         repaint();
  366.     }
  367.  
  368.     private class TAdapter extends KeyAdapter {
  369.  
  370.  
  371.         public void keyPressed(KeyEvent e) {
  372.  
  373.             int key = e.getKeyCode();
  374.  
  375.             if ((key == KeyEvent.VK_LEFT || key == KeyEvent.VK_A) && (!rightDirection)) {
  376.                 leftDirection = true;
  377.                 upDirection = false;
  378.                 downDirection = false;
  379.             }
  380.  
  381.             if ((key == KeyEvent.VK_RIGHT || key == KeyEvent.VK_D) && (!leftDirection)) {
  382.                 rightDirection = true;
  383.                 upDirection = false;
  384.                 downDirection = false;
  385.             }
  386.  
  387.             if ((key == KeyEvent.VK_UP ||key == KeyEvent.VK_W) && (!downDirection)) {
  388.                 upDirection = true;
  389.                 rightDirection = false;
  390.                 leftDirection = false;
  391.             }
  392.  
  393.             if ((key == KeyEvent.VK_DOWN|| key == KeyEvent.VK_S) && (!upDirection)) {
  394.                 downDirection = true;
  395.                 rightDirection = false;
  396.                 leftDirection = false;
  397.             }
  398.             if((key == KeyEvent.VK_ENTER)&&(!inGame)) {
  399.                 inGame = true; 
  400.                 initGame();
  401.             }
  402.            
  403.         }
  404.     }
  405. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement