Advertisement
Guest User

Board.java

a guest
Nov 15th, 2018
113
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.codef0x.snake;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.KeyEvent;
  6. import java.awt.event.KeyListener;
  7. import java.util.ArrayList;
  8. import java.util.Timer;
  9. import java.util.TimerTask;
  10.  
  11.  
  12. public class Board extends JPanel implements KeyListener {
  13.     Snake player;
  14.     Food food;
  15.     ArrayList<SnakePart> snakeCoordinates;
  16.  
  17.     public Board() {
  18.  
  19.         this.snakeCoordinates = new ArrayList<>();
  20.  
  21.         snakeCoordinates.add(new SnakePart(150, 150));
  22.         snakeCoordinates.add(new SnakePart(140, 150));
  23.         snakeCoordinates.add(new SnakePart(130, 150));
  24.         snakeCoordinates.add(new SnakePart(120, 150));
  25.  
  26.         this.player = new Snake(snakeCoordinates);
  27.  
  28.         this.food = new Food();
  29.     }
  30.  
  31.     public static void main(String[] args) {
  32.  
  33.         JFrame frame = new JFrame("Snake");
  34.         frame.setDefaultCloseOperation(3);
  35.  
  36.         Board board = new Board();
  37.         frame.add(board);
  38.         frame.setSize(500, 500);
  39.         frame.addKeyListener(new Board());
  40.         frame.setVisible(true);
  41.  
  42.         board.run(board);
  43.     }
  44.  
  45.     @Override
  46.     public void paintComponent(Graphics g) {
  47.         clear(g);
  48.  
  49.         this.player.coordinates.forEach(snakePart -> {
  50.             g.setColor(Color.BLUE);
  51.             g.fillRect(snakePart.x, snakePart.y, 10, 10);
  52.         });
  53.  
  54.         g.setColor(Color.RED);
  55.         g.fillRect(this.food.foodX, this.food.foodY, 10, 10);
  56.  
  57.         if (ateFood(this.food, g)) {
  58.             this.food.spawn(getHeight(), getWidth());
  59.             //this.player.grow();
  60.         }
  61.     }
  62.  
  63.     public void clear(Graphics g) {
  64.         g.clearRect(0, 0, getHeight(), getWidth());
  65.     }
  66.  
  67.     @Override
  68.     public void update(Graphics g) {
  69.         paintComponent(g);
  70.     }
  71.  
  72.     public boolean ateFood(Food food, Graphics g) {
  73.  
  74.         if (this.player.coordinates.get(0).x == food.foodX && this.player.coordinates.get(0).y == food.foodY) {
  75.             g.clearRect(food.foodX, food.foodY, 10, 10);
  76.             return true;
  77.         }
  78.         return false;
  79.     }
  80.  
  81.     public boolean hitSomething() {
  82.         boolean hitLeftWall = this.player.coordinates.get(0).x < 10;
  83.         boolean hitUpperWall = this.player.coordinates.get(0).y < 0;
  84.         boolean hitRightWall = this.player.coordinates.get(0).x > getWidth() - 10;
  85.         boolean hitLowerWall = this.player.coordinates.get(0).y > getHeight() - 10;
  86.  
  87.         for (int i = 1; i < 3; i++) {
  88.             if (this.player.coordinates.get(i).x == this.player.coordinates.get(0).x && this.player.coordinates.get(i).y == this.player.coordinates.get(0).y) {
  89.                 return true;
  90.             }
  91.         }
  92.  
  93.         if (hitLeftWall || hitUpperWall || hitRightWall || hitLowerWall) {
  94.             return true;
  95.         }
  96.  
  97.         return false;
  98.     }
  99.  
  100.     public void gameOver(Graphics g) {
  101.         clear(g);
  102.  
  103.         g.setColor(Color.BLACK);
  104.         g.setFont(new Font("TimesRoman", Font.PLAIN, 32));
  105.         g.drawString("Game over!", getWidth() / 2 - 70, getHeight() / 2);
  106.     }
  107.  
  108.     @Override
  109.     public void keyPressed(KeyEvent event) {
  110.         int keyCode = event.getKeyCode();
  111.  
  112.         System.out.println("Before altering:");
  113.         System.out.println(player.dx + "  " + player.dy);
  114.  
  115.         if (keyCode == 37) {
  116.             this.player.dx = -10;
  117.             this.player.dy = 0;
  118.         } else if (keyCode == 38) {
  119.             this.player.dx = 0;
  120.             this.player.dy = -10;
  121.         } else if (keyCode == 39) {
  122.             this.player.dx = 10;
  123.             this.player.dy = 0;
  124.         } else if (keyCode == 40) {
  125.             this.player.dx = 0;
  126.             this.player.dy = 10;
  127.         }
  128.  
  129.         System.out.println("After altering:");
  130.         System.out.println(player.dx + " "  + player.dy);
  131.     }
  132.  
  133.     @Override
  134.     public void keyTyped(KeyEvent event) {
  135.         return;
  136.     }
  137.  
  138.     @Override
  139.     public void keyReleased(KeyEvent event) {
  140.         return;
  141.     }
  142.  
  143.     public void run(Board board) {
  144.         Timer game = new Timer();
  145.         game.schedule(new TimerTask() {
  146.             boolean initiallySpawned = false;
  147.             @Override
  148.             public void run() {
  149.                 Graphics g = board.getGraphics();
  150.  
  151.                 if (hitSomething()) {
  152.                     game.cancel();
  153.                     gameOver(g);
  154.                     return;
  155.                 }
  156.  
  157.                 if (!initiallySpawned) {
  158.                     board.food.spawn(getHeight(), getWidth());
  159.                     initiallySpawned = true;
  160.                 }
  161.  
  162.                 player.move();
  163.  
  164.                 if (ateFood(food, g)) {
  165.                     food.spawn(getHeight(), getWidth());
  166.                 }
  167.  
  168.                 update(g);
  169.             }
  170.         }, 0, 500);
  171.     }
  172. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement