Advertisement
Guest User

Untitled

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