26 thoughts on “How to make Snake in Java – Part 3: Movement and Apples!

  1. If anyone is still looking at this, i need some help.
    I did everything the same but my snake does not react to the keys that i press
    I can't find what's wrong

  2. For those of you that are wondering on how to change the color enter this code under

    public void paint(Graphics g){
    g.clearRect(0, 0, WIDTH, HEIGHT);

    —BACKGROUND CODE-

    g.setColor(new Color(18, 40, 11)); //replace the 3 numbers with the color you like
    g.fillRect(0, 0, WIDTH, HEIGHT);

  3. Incase this helps anyone still going through this, I think its better to replace the tick int in the tick method in the screen class which counts up to 250,000 and replacing it with sleeping the thread (i.e. Thread.sleep(60);).
    So instead of running through each of those for loops and if statements in the tick method 250,000 times the thread waits 60 milliseconds and only runs that code once it needs to.
    This should remove any speed issues and allows you to set how long the user gets to make a move and makes the game less processor dependent I would think.

  4. I'm at about  3:15 and I have some errors I think they are pretty simple but I can't seem to figure them out Bymyself (there are stars where the errors are)package com.lol.game.graphics;import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.util.ArrayList;import javax.swing.JPanel;import com.lol.game.entities.BodyPart;public class Screen extends JPanel implements Runnable { private static final long serialVersionUID = 1L;
     
     public static final int WIDTH = 800, HEIGHT = 800;
     private Thread thread;
     private boolean running = false;
     
     private BodyPart b;
     private ArrayList<BodyPart> snake;
     
     private int xCoor = 10, yCoor = 10;
     private int size = 5;
     
     private boolean right = false, left = false, up = false, down = true;
     
     private int ticks = 0;
     
     *private Key key;
     
     public Screen() {
      setFocusable(true);
     * key = new Key();
     * addKeyListener(key);
      setPreferredSize(new Dimension(WIDTH, HEIGHT));
      
      snake = new ArrayList<BodyPart>();
     
      start();
     }
     
     public void tick() {
      if(snake.size() == 0 ) {
       b = new BodyPart(xCoor, yCoor, 10);
       snake.add(b);
       
       if(snake.size() > size) {
        snake.remove(0);
       }
      }
      
      
      ticks++;
      
      if(ticks > 250000) {
       if(right) xCoor++;
       if(left) xCoor–;
       if(up) yCoor–;
       if(down) yCoor++;
       
       ticks = 0;
       
       b = new BodyPart(xCoor, yCoor, 10);
       snake.add(b);
       
       if(snake.size() > size) {
        snake.remove(0);
       }
      }
     }
     
     public void paint(Graphics g) {
      g.clearRect(0, 0, WIDTH, HEIGHT);
      
      g.setColor(Color.BLACK);
      for(int i = 0; i < WIDTH / 10; i++) {
       g.drawLine(i * 10, 0, i * 10, HEIGHT);
      }
      
      for(int i = 0; i < HEIGHT / 10; i++) {
       g.drawLine(0, i * 10, WIDTH, i * 10);
      }
      
      for(int i = 0; i < snake.size(); i++) {
       snake.get(i).draw(g);
      }
     }
     
     public void start () {
      running = true;
      thread = new Thread(this, "Game Loop");
      thread.start();
      
     }
     
     public void stop () {
      
     }
     
     public void run() {
      while(running) {
       tick();
       repaint();
      }
     }
     
     private class key implements KeyListener {  public void keyPressed(KeyEvent e) {
       int key = e.getKeyCode();
       
       if(key == KeyEvent.VK_RIGHT && !left) {
        up = false;
        down = false;
        right = true;
        
       }
       
       if(key == KeyEvent.VK_LEFT && !right) {
        up = false;
        down = false;
        left = true;
        
       }
       
       if(key == KeyEvent.VK_UP && !down) {
        left = false;
        right = false;
        up = true;
       }
       
       if(key == KeyEvent.VK_DOWN && !up) {
        left = false;
        right = false;
        down = true;
       }
      }  public void keyReleased(KeyEvent arg0) {
       
      }  public void keyTyped(KeyEvent arg0) {
       
      }
      
     }}

  5. Is there any kind of potential memory problem with constantly creating and removing BodyParts? Will garbage collect always do its job? I was thinking about trying something like this but having each BodyPart hold a reference to the body part in front of it and moving toward it when it's time to tick.

  6. Instead of stopping the game when it goes out of bounds replace that piece of code with:
     if(xCoor < 0) xCoor = 79;
    if(xCoor > 79) xCoor = 0;
    if(yCoor < 0) yCoor = 79;
    if(yCoor > 79) yCoor = 0;

  7. Thanks for the video, I've been working for quite a few hours but can't seem to figure out how to restart the program when prompted (i.e with an if statement) i just keep getting errors, or the snake starts off dead

Leave a Reply

Your email address will not be published. Required fields are marked *