Brayden Williams

Greenhorn
+ Follow
since Jan 21, 2019
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Brayden Williams

Hey, I am a beginner programmer, and I was watching a tutorial on how to code a game. Just wanted to ask how can I separate this code into two different classes (The tutorial didn't explain about breaking it into two different classes, so I am just curious)? One being Snake.java and Board.java. Thank you in advanced!

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Snake extends JFrame {

   
   public final int BOARD_WIDTH = 800;
   public final int BOARD_HEIGHT = 800;
   public final int TILE_SIZE = 20;
   public final int ALL_TILES = 1200;
   public final int DELAY = 105;

   
   private int[] X = new int[ALL_TILES];
   private int[] Y = new int[ALL_TILES];

 
   private int apple_x, apple_y;

   
   int KEYSTROKES = KeyEvent.VK_DOWN;
   private int snakeSize = 3;
   private boolean inGame = true;
   
     public Snake(){
   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   setSize(BOARD_WIDTH, BOARD_HEIGHT);
   setResizable(false);
   setLocation(650, 120);

   Board b = new Board();
   addKeyListener(b);
   add(b);
   setVisible(true);

   Timer t = new Timer(DELAY, b);
   t.start();
 }


   public class Board extends JPanel implements ActionListener, KeyListener {
   Board(){
       setBackground(Color.blue);

       // Set snake starting coordinates.
       for(int i = 0; i < snakeSize; i++){
         Y[i] = 140 - (i * 30);
         X[i] = 140;
       }

       spawnApple();
   }

   @Override
   public void paintComponent(Graphics g) {
       super.paintComponent(g);

       if (inGame) {

       /* Draw apple. */
       g.setColor(Color.red);
       g.fillRect(apple_x, apple_y, TILE_SIZE, TILE_SIZE);

       /* Draw snake. */
       for (int i = 0; i < snakeSize; i++) {

           if (i == 0) {
             g.setColor(Color.yellow); // Snakes head yellow
           } else {
             g.setColor(Color.green);}

           g.fillRect(X[i], Y[i], TILE_SIZE, TILE_SIZE);
       }

       /* Draw score */
       g.setFont(new Font("Sans serif", Font.BOLD, 20));
       g.drawString(getScore(), 550, 30);

       } else {
         gameOver(g);
       }
   }

   @Override
   public void actionPerformed(ActionEvent e) {

       checkTile();
       snakeMovement();
       repaint();

   }

   
   @Override
   public void keyPressed(KeyEvent e) {
       KEYSTROKES = e.getKeyCode();
   }

   @Override
   public void keyReleased(KeyEvent e){}
   @Override
   public void keyTyped(KeyEvent e){}

   private void checkTile(){
       /* Check if outside of wall. */
       if ( X[0] > BOARD_WIDTH || X[0] < 0 || Y[0] > BOARD_HEIGHT || Y[0] < 0 ) {
            inGame = false;
          }

       /* Check for collisions. */
       for(int i = 1; i < X.length; i++){
         if (X[0] == X[i] && Y[0] == Y[i]){
             inGame = false;
         }
       }

       
       if ((X[0] == apple_x) && (Y[0] == apple_y)) {
         snakeSize++;
         spawnApple();
       }
   }

   
   private void spawnApple() {
       int apple = (int) (Math.random() * Math.sqrt(ALL_TILES) - 1);
       apple_x = ((apple * TILE_SIZE));

       apple = (int) (Math.random() * Math.sqrt(ALL_TILES) - 1);
       apple_y = ((apple * TILE_SIZE));
   }

   
   private void gameOver(Graphics g) {
       g.setColor(Color.white);
       g.setFont(new Font("Sans serif", Font.BOLD, 20));
       g.drawString(("Game Over! You ate " + (getScore()) + " apples!"),
            BOARD_WIDTH / 4, BOARD_HEIGHT / 2);
       g.drawString("Press space to restart",
            BOARD_WIDTH / 4 + 20, BOARD_HEIGHT / 2 + 30);

       
       if (KEYSTROKES == KeyEvent.VK_SPACE){
         inGame = true;
         KEYSTROKES = KeyEvent.VK_DOWN;
         setVisible(false);
         dispose();
         Snake s = new Snake();
       }
   }

   private void snakeMovement(){

     
       for (int i = snakeSize; i > 0; i--) {
         X[i] = X[(i - 1)];
         Y[i] = Y[(i - 1)];
       }

       
       switch (KEYSTROKES) {
       case KeyEvent.VK_DOWN:
         Y[0] += TILE_SIZE;
         break;
       case KeyEvent.VK_UP:
         Y[0] -= TILE_SIZE;
         break;
       case KeyEvent.VK_LEFT:
         X[0] -= TILE_SIZE;
         break;
       case KeyEvent.VK_RIGHT:
         X[0] += TILE_SIZE;
         break;
       }
   }

   private String getScore(){
       return "" + (snakeSize - 3);
   }
 }



 public static void main (String[] args) {
   Snake s = new Snake();
 }      
}
5 years ago