Results 1 to 8 of 8
  1. #1
    bobbybdole is offline Member
    Join Date
    Jun 2014
    Posts
    3
    Rep Power
    0

    Default Snake Game Movement Algorithm

    I am working on replicating this project: http://www.java-forums.org/new-java/...ame-print.html

    Can someone help me figure out what is wrong with the moveSnake method and the for loop within it?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,403
    Rep Power
    25

    Default Re: Snake Game Movement Algorithm

    Please post the code here that you have questions about. Be sure to wrap the code in code tags.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    bobbybdole is offline Member
    Join Date
    Jun 2014
    Posts
    3
    Rep Power
    0

    Default Re: Snake Game Movement Algorithm

    Java Code:
    import java.util.*;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.FontMetrics;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import java.util.ArrayList;
    
    import javax.swing.ImageIcon;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.Timer;
    
    /**
     * The main game class that reads user input,
     * maintains a snake and updates game board
     * 
     *
     **/
    public class MainGame implements ActionListener {
    
            // size of the game board (30 * 30 by default)
            private final int WIDTH = 30;
            private final int HEIGHT = 30;
            
            private final int Init_Snake_Length = 5;
            
            // delay of detecting user input ( in milliseconds )
            private final int DELAY = 100;
            
            // grow the snake every GROWTH_INTERVAL milliseconds
            private final int GROWTH_INTERVAL = 5000;
            // move the snake every MOVE_INTERVAL milliseconds
            private final int MOVE_INTERVAL = 500;
            
            // The snake (as a list of coordinates in the 2D board array)
            private ArrayList<Coordinate> snake;
            
            // The game board object - read GameBoard.java
            private GameBoard board; 
            
            // Flag variables indicating which direction is chosen
            // by user. Only one of the four can be true at any moment
            private boolean left = false;
            private boolean right = true;
            private boolean up = false;
            private boolean down = false;
    
            private Timer timer;
            private long growCounter;
            private long moveCounter;
            
            public MainGame() {
                    board = new GameBoard(WIDTH, HEIGHT);
                    initGame();
                    board.addKeyListener(new TAdapter());
            }
            
            public static void main(String[] argv) {
                    MainGame mg = new MainGame();
            }
            
            public void initGame() {
                    snake = new ArrayList<Coordinate>();
                    snake.add(new Coordinate (15,15)); // 0
                    snake.add(new Coordinate (14,15)); // 1 
                    snake.add(new Coordinate (13,15)); // 2
                    snake.add(new Coordinate (12,15)); // 3
                    snake.add(new Coordinate (11,15)); // 4
                    
                    board.updateSnake(snake);
                    
                    timer = new Timer(DELAY, this);
                    timer.start();
                    growCounter = 0;
                    moveCounter = 0;
                    
                    left = false;
                    right = true;
                    up = false;
                    down = false;                
            }
            
            [B]private void moveSnake() {
                    
                    for (int i = snake.size() - 1; i > 0; i--){
                            snake.set(i, snake.get(i-1));
                    }
                    
                    if (right == true) {
                            snake.get(0).x = snake.get(0).x + 1;
                            
                    }
                    
                    if (left == true) {
                            snake.get(0).x = snake.get(0).x-1;
                    }
                            
                    if (up == true) {
                            snake.get(0).y = snake.get(0).y-1;
                    }
                    
                    if (down == true) {
                            snake.get(0).y = snake.get(0).y+1;
                    }[/B]
                    
            }

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,403
    Rep Power
    25

    Default Re: Snake Game Movement Algorithm

    what is wrong with the moveSnake method and the for loop within it?
    Can you explain what the problem is? What is "wrong"?


    NOTE: The posted code does not compile!!! It is not possible to test a program that does not compile.
    Last edited by Norm; 06-16-2014 at 04:45 PM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,004
    Rep Power
    6

    Default Re: Snake Game Movement Algorithm

    That's the idea friendly friend Norm; he's trying to copy someone else's code and is now asking you to help him make it work.

    IMO copying the idea and writing the code from scratch is going to take -way- less time and effort and will provide a warm glowing feeling of personal progress.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    bobbybdole is offline Member
    Join Date
    Jun 2014
    Posts
    3
    Rep Power
    0

    Default Re: Snake Game Movement Algorithm

    the snake's tail disappears.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: Snake Game Movement Algorithm

    Quote Originally Posted by gimbal2 View Post
    T
    IMO copying the idea and writing the code from scratch is going to take -way- less time and effort and will provide a warm glowing feeling of personal progress.
    Especially since it is apparent that the code isn't written all that well. So when folks new to Java do this they tend to learn bad habits and don't learn how to think thru a design.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,403
    Rep Power
    25

    Default Re: Snake Game Movement Algorithm

    Please post code that compiles, executes and shows the problem.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Snake game
    By surbhijain93 in forum New To Java
    Replies: 18
    Last Post: 11-07-2013, 02:13 PM
  2. Snake Game
    By FrankElliot9 in forum New To Java
    Replies: 5
    Last Post: 12-03-2012, 05:01 AM
  3. Snake Game
    By LuluMM in forum New To Java
    Replies: 0
    Last Post: 03-08-2012, 06:48 AM
  4. Snake Game
    By mustachMan in forum New To Java
    Replies: 2
    Last Post: 12-10-2009, 10:35 PM
  5. Snake game movement
    By BeerMonkey in forum New To Java
    Replies: 9
    Last Post: 11-27-2008, 12:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •