Advertisement
Guest User

ajkfajskdfjkanta

a guest
May 22nd, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.Toolkit;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import java.awt.event.KeyAdapter;
  10. import java.awt.event.KeyEvent;
  11. import javax.swing.JPanel;
  12. import javax.swing.Timer;
  13.  
  14. @SuppressWarnings("serial")
  15. public class Board extends JPanel implements ActionListener {
  16.  
  17.  
  18.  
  19. // größe board
  20. private final static int BOARDWIDTH = 1000;
  21. private final static int BOARDHEIGHT = 980;
  22.  
  23. //joint / food größe
  24. private final static int PIXELSIZE = 25;
  25.  
  26.  
  27. private final static int TOTALPIXELS = (BOARDWIDTH * BOARDHEIGHT)
  28.         / (PIXELSIZE * PIXELSIZE);
  29.  
  30. // Check if running
  31. private boolean inGame = true;
  32.  
  33. // Timer für ticks
  34. private Timer timer;
  35.  
  36. //schwierigkeitsgrad, höher, gleich leichter
  37. private static int speed = 100;
  38.  
  39. // Instances of our snake & food so we can use their methods
  40. private Snake snake = new Snake();
  41. private Food food = new Food();
  42.  
  43.  
  44. public Board() {
  45.  
  46.     addKeyListener(new Keys());
  47.     setBackground(Color.BLACK);
  48.     setFocusable(true);
  49.  
  50.     setPreferredSize(new Dimension(BOARDWIDTH, BOARDHEIGHT));
  51.  
  52.     initializeGame();
  53.     //sound
  54.     Sound.BACK.play();
  55. }
  56.  
  57.  
  58. // Zeichnen
  59. @Override
  60. protected void paintComponent(Graphics g) {
  61.     super.paintComponent(g);
  62.  
  63.     draw(g);
  64. }
  65.  
  66.  
  67.  
  68.  
  69. // Draw our Snake & Food (Called on repaint()).
  70. void draw(Graphics g) {
  71.     // Only draw if the game is running / the snake is alive
  72.     if (inGame == true) {
  73.  
  74.         g.setColor(Color.white);
  75.         g.drawString("Length: " + snake.getJoints(), 500, 500);
  76.  
  77.         g.setColor(Color.green);
  78.         g.fillRect(food.getFoodX(), food.getFoodY(), PIXELSIZE, PIXELSIZE); // food
  79.  
  80.         // Draw our snake.
  81.         for (int i = 0; i < snake.getJoints(); i++) {
  82.             // Snake's head
  83.             if (i == 0) {
  84.                 g.setColor(Color.RED);
  85.                 g.fillRect(snake.getSnakeX(i), snake.getSnakeY(i),
  86.                         PIXELSIZE, PIXELSIZE);
  87.                 // Body of snake
  88.            
  89.             } else {
  90.                 g.fillRect(snake.getSnakeX(i), snake.getSnakeY(i),
  91.                         PIXELSIZE, PIXELSIZE);
  92.             }
  93.         }
  94.  
  95.         // Sync our graphics together
  96.         Toolkit.getDefaultToolkit().sync();
  97.     } else {
  98.         // If we're not alive, then we end our game
  99.         endGame(g);
  100.     }
  101. }
  102.  
  103.    
  104. void initializeGame() {
  105.     snake.setJoints(3); // anfang
  106.  
  107.    
  108.     for (int i = 0; i < snake.getJoints(); i++) {
  109.         snake.setSnakeX(BOARDWIDTH / 2);
  110.         snake.setSnakeY(BOARDHEIGHT / 2);
  111.     }
  112.    
  113.     snake.setMovingRight(true);
  114.  
  115.  
  116.     food.createFood();
  117.  
  118.     // set the timer to record our game's speed / make the game move
  119.     timer = new Timer(speed, this);
  120.     timer.start();
  121. }
  122.  
  123.  
  124. // nähe food
  125. void checkFoodCollisions() {
  126.  
  127.     if ((proximity(snake.getSnakeX(0), food.getFoodX(), 20))
  128.             && (proximity(snake.getSnakeY(0), food.getFoodY(), 20))) {
  129.         //sound
  130.        
  131.         Sound.FC.play();
  132.         System.out.println("intersection");
  133.        
  134.         snake.setJoints(snake.getJoints() + 1);
  135.        
  136.         food.createFood();
  137.     }
  138. }
  139.  
  140. //collision mit snake
  141. void checkCollisions() {
  142.    
  143.    
  144.     for (int i = snake.getJoints(); i > 0; i--) {
  145.  
  146.        
  147.         if ((i > 5)
  148.                 && (snake.getSnakeX(0) == snake.getSnakeX(i) && (snake
  149.                         .getSnakeY(0) == snake.getSnakeY(i)))) {
  150.             inGame = false;
  151.         }
  152.     }
  153.  
  154.     // Intersect board
  155.     if (snake.getSnakeY(0) >= BOARDHEIGHT) {
  156.         inGame = false;
  157.     }
  158.  
  159.     if (snake.getSnakeY(0) < 0) {
  160.         inGame = false;
  161.     }
  162.  
  163.     if (snake.getSnakeX(0) >= BOARDWIDTH) {
  164.         inGame = false;
  165.     }
  166.  
  167.     if (snake.getSnakeX(0) < 0) {
  168.         inGame = false;
  169.     }
  170.  
  171.     // If the game has ended, then we can stop our timer
  172.     if (!inGame) {
  173.         timer.stop();
  174.     }
  175. }
  176.  
  177. void endGame(Graphics g) {
  178.     //sound
  179.     Sound.BACK.stop();
  180.     Sound.END.play();
  181.    
  182.    
  183.     String message = "Game over";
  184.  
  185.     // Create a new font instance
  186.     Font font = new Font("Times New Roman", Font.BOLD, 80);
  187.     FontMetrics metrics = getFontMetrics(font);
  188.  
  189.     // Set the color of the text to red, and set the font
  190.     g.setColor(Color.red);
  191.     g.setFont(font);
  192.  
  193.     // Draw the message to the board
  194.     g.drawString(message, (BOARDWIDTH - metrics.stringWidth(message)) / 2,
  195.             BOARDHEIGHT / 2);
  196.  
  197.     System.out.println("Game Ended");
  198.    
  199.     String message1 = "Press Enter for restart";
  200.     Font font1 = new Font("Times New Roman", Font.BOLD, 80);
  201.     FontMetrics metrics1 = getFontMetrics(font1);
  202.     g.setColor(Color.red);
  203.     g.setFont(font1);
  204.     g.drawString(message1, (BOARDWIDTH - metrics1.stringWidth(message1)) / 2,
  205.             BOARDHEIGHT -100);
  206. }
  207.  
  208. // Run constantly as long as we're in game.
  209. @Override
  210. public void actionPerformed(ActionEvent e) {
  211.     if (inGame == true) {
  212.  
  213.         checkFoodCollisions();
  214.         checkCollisions();
  215.         snake.move();
  216.  
  217.         System.out.println(snake.getSnakeX(0) + " " + snake.getSnakeY(0)
  218.                 + " " + food.getFoodX() + ", " + food.getFoodY());
  219.     }
  220.     // Repaint or 'render' our screen
  221.     repaint();
  222. }
  223.  
  224. private class Keys extends KeyAdapter {
  225.  
  226.     @Override
  227.     public void keyPressed(KeyEvent e) {
  228.  
  229.         int key = e.getKeyCode();
  230.  
  231.         if ((key == KeyEvent.VK_LEFT) && (!snake.isMovingRight())) {
  232.             snake.setMovingLeft(true);
  233.             snake.setMovingUp(false);
  234.             snake.setMovingDown(false);
  235.         }
  236.  
  237.         if ((key == KeyEvent.VK_RIGHT) && (!snake.isMovingLeft())) {
  238.             snake.setMovingRight(true);
  239.             snake.setMovingUp(false);
  240.             snake.setMovingDown(false);
  241.         }
  242.  
  243.         if ((key == KeyEvent.VK_UP) && (!snake.isMovingDown())) {
  244.             snake.setMovingUp(true);
  245.             snake.setMovingRight(false);
  246.             snake.setMovingLeft(false);
  247.         }
  248.  
  249.         if ((key == KeyEvent.VK_DOWN) && (!snake.isMovingUp())) {
  250.             snake.setMovingDown(true);
  251.             snake.setMovingRight(false);
  252.             snake.setMovingLeft(false);
  253.         }
  254.         if ((key == KeyEvent.VK_NUMPAD1)){
  255.             tM = 100;
  256.             timer.stop();
  257.             timer.setDelay( tM );
  258.             timer.start();
  259.         }
  260.         if ((key == KeyEvent.VK_NUMPAD2)){
  261.             tM = 1000;
  262.             timer.stop();
  263.             timer.setDelay( tM );
  264.             timer.start();
  265.         }
  266.        
  267.         if (key == KeyEvent.VK_SPACE) {
  268.             try {
  269.                 Thread.sleep(1500);
  270.             } catch (InterruptedException e1) {
  271.                 // TODO Auto-generated catch block
  272.                 e1.printStackTrace();
  273.             }
  274.             //pause text
  275.            
  276.            
  277.         }
  278.  
  279.         if ((key == KeyEvent.VK_ENTER) && (inGame == false)) {
  280.  
  281.             inGame = true;
  282.             snake.setMovingDown(false);
  283.             snake.setMovingRight(false);
  284.             snake.setMovingLeft(false);
  285.             snake.setMovingUp(false);
  286.  
  287.             initializeGame();
  288.            
  289.             Sound.BACK.play();
  290.         }
  291.     }
  292. }
  293.  
  294. private boolean proximity(int a, int b, int closeness) {
  295.     return Math.abs((long) a - b) <= closeness;
  296. }
  297.  
  298. public static int getAllDots() {
  299.     return TOTALPIXELS;
  300. }
  301.  
  302. public static int getDotSize() {
  303.     return PIXELSIZE;
  304. }
  305. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement