Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 22 of 22

Thread: more help with snake game

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    28
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default more help with snake game

    Hello all, this is godlynom again, thanks to those who helped me last time. Everything is working, now I just need the body parts to be added after I collide with the food. Essentially everytime you run into a food, you need to grow a body part, or another snake object behind the current one. I have been working on this for quite a bit now and have run into a roadblock, I feel like I need to create an array of SnakeBody objects, but I am confused as to how to go about that. We have not done too many arrays in class, if someone could just at least point me in the right way, that would be wonderful. Here is my code so far, there is some extraneous methods from me trying to figure out how to get it to work.
    Food Class:
    import java.awt.*;

    public class Food
    {
    	double x = 0, y = 0;
    	private double myX;   // x and y coordinates of center
          private double myY;
          private double myDiameter;
          private Color myColor; 
          private double myRadius;
    		private double myHeight;
          private static final int FRAME = 495;
     
     
    	public Food()
    	{
    		myX = 0;
          myY = 200;
          myHeight = 15;
          myColor = Color.RED;
          myRadius = myDiameter/2;
    	}
    	public Food(double x, double y, double d, Color c)
    	{
    		myX = x;
          myY = y;
          myHeight = d;
          myColor = c;
          myRadius = d/2; 
    	}
    	 public double getX() 
          { 
             return myX;
          }
           public double getY()      
          { 
             return myY;
          }
           public double getDiameter() 
          { 
             return myDiameter;
          }
           public Color getColor() 
          { 
             return myColor;
          }
           public double getRadius() 
          { 
             return myRadius;
          }
    		public double getHeight()
           {
        	   return myHeight;
           }
     
       // modifier methods
           public void setX(double x)
          {
             myX = x;
          } 
           public void setY(double y)
          {
             myY = y;
          } 
           public void setColor(Color c)
          {
             myColor = c;
          }
           public void setDiameter(double d)
          {
             myDiameter = d;
             myRadius = d/2;
          }
           public void setRadius(double r)
          {
             myRadius = r;
             myDiameter = 2*r;
          }
        //	 instance methods
           public void jump(int rightEdge, int bottomEdge)
          {
             // moves location to random (x, y) within the edges
            	 myX = rightEdge;
                 myY = bottomEdge; 
          }
     
    	public void draw(Graphics myBuffer) 
          {
              myBuffer.setColor(myColor);
             myBuffer.fillRect((int)(myX), (int)(myY), (int)myHeight, (int)myHeight);
          }
    }

    SnakeBody Class:
       import java.awt.*;
     
        public class SnakeBody
       {
          private double myX;   // x and y coordinates of center
          private double myY;
          private double myDiameter;
          private Color myColor; 
          private double myRadius;
          private double myHeight;
    		private Graphics myBuffer;
     
          double x =350, y = 350, velx = 0, vely = 0, x1 = 0, y1 = 0;
     
         // constructors
           public SnakeBody()     //default constructor
          {
             myX = 0;
             myY = 0;
             myHeight = 15;
             myColor = Color.RED;
             myRadius = myDiameter/2;
     
          }
           public SnakeBody(double x, double y, double d, Color c)
          {
             myX = x;
             myY = y;
             myHeight = d;
             myColor = c;
             myRadius = d/2;
          }
        // accessor methods
           public int getX() 
          { 
             return (int) myX;
          }
           public int getY()      
          { 
             return (int) myY;
          }
           public double getDiameter() 
          { 
             return myDiameter;
          }
           public Color getColor() 
          { 
             return myColor;
          }
           public double getRadius() 
          { 
             return myRadius;
          }
           public double getHeight()
           {
        	   return myHeight;
           }
       // modifier methods
           public void setX(double x)
          {
             myX = x;
          } 
           public void setY(double y)
          {
             myY = y;
          } 
           public void setColor(Color c)
          {
             myColor = c;
          }
           public void setDiameter(double d)
          {
             myDiameter = d;
             myRadius = d/2;
          }
           public void setRadius(double r)
          {
             myRadius = r;
             myDiameter = 2*r;
          }  
        //	 instance methods
           public void draw(Graphics myBuffer) 
          {
             myBuffer.setColor(myColor);
             myBuffer.fillRect((int)(myX), (int)(myY), (int)myHeight, (int)myHeight);
          }
    }

    Snake Class(Panel):
       import javax.swing.*;
     
       import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
     
        public class Snake extends JPanel implements ActionListener, KeyListener
       {
          Timer t = new Timer(75, this);
          double x =0, y = 0, velx = 0, vely = 0, x1 = 0, y1 = 0;
          private static final int FRAME = 480;
          private Food food;
          private SnakeBody snake, snake1;
     
          int count = 0;
    		int distance = 15;
     
           public Snake()
          {
    			t.start();
    			addKeyListener(this);
             setFocusable(true);
    			setFocusTraversalKeysEnabled(false);
     
             x = 240;
             y = 240;
     
    	      x1 = 15 * (int)(Math.random()* 32);
    	      y1 = 15 * (int)(Math.random()* 32);
    	      food = new Food(x1,y1,15,Color.BLUE);
             snake = new SnakeBody(x,y,15, Color.RED);
    			SnakeBody[] snake1 = new SnakeBody[100];
          }
    	public void paintComponent(Graphics g)
          {
        	 super.paintComponent(g);
             food.draw(g);
             snake.draw(g);
            	collide(snake, food);
    			for(int i = 0; i < 480; i+=15)
    			{
    				g.setColor(Color.BLACK);
    				g.drawLine(i,FRAME,i,0);
    				g.drawLine(FRAME,i,0,i);
    			}
    			g.drawLine(FRAME,0,FRAME,FRAME);
    			g.drawLine(0,FRAME,FRAME,FRAME);
     
             g.setColor(Color.BLACK);
             g.setFont(new Font("Monospaced",Font.BOLD,24));
             g.drawString("Count:" + count, FRAME - 300, 25);
          }
           public void actionPerformed(ActionEvent e)
          {
            	x += velx;
             y += vely;
    			if(x<0)
    			{
    				x = 465;
    			}
    			if(x>465)
    			{
    				x = 0;
    			}
    			if(y<0)
    			{
    				y = 465;
    			}
    			if(y>465)
    			{
    				y = 0;
    			}
    			snake.setX(x);
    			snake.setY(y);
     
    			repaint();
          }
    	private void collide(SnakeBody snake, Food food)
          {
             double d = distance(snake.getX(),snake.getY(),food.getX(),food.getY());
     
             if(d < snake.getHeight()/2 + food.getHeight()/2)
             {
    				int x3 = 15 * (int)(Math.random()* 32);
    	      	int y3 = 15 * (int)(Math.random()* 32);
                food.jump(x3,y3);
    				snake.newBody(snake);
                count += 250;
             }
     
          }
           private double distance(double x1, double y1, double x2, double y2)
          {
             return Math.sqrt(Math.pow(x2-x1,2)+Math.pow(y2-y1,2));
          }
           public void up()
          {
             vely = -15;
             velx = 0;
          }
           public void down()
          {
             vely = 15;
             velx = 0;
          }
           public void left()
          {
             velx = -15;
             vely = 0;
          }
           public void right()
          {
             velx = 15;
             vely = 0;
          }
           public void keyPressed(KeyEvent e)
          {
             int code = e.getKeyCode();
             if(code == KeyEvent.VK_LEFT)
             {
    	            if(velx == 15)
    	            {
    	               up();
    	            }
    	            else if(velx == -15)
    	            {
    	               down();
    	            }
    	            else if(vely == 15)
    	            {
    	               right();
    	            }
    	            else if(vely == -15)
    	            {	
    	               left();
    	            }
    	            else
    	            {
    	               left();
    	            }
             }
             if(code == KeyEvent.VK_RIGHT)
             {
     
    	            if(velx == 15)
    	            {
    	               down();
    	            }
    	            else if(velx == -15)
    	            {
    	               up();
    	            }
    	            else if(vely == 15)
    	            {
    	               left();
    	            }
    	            else if(vely == -15)
    	            {	
    	               right();
    	            }
     
    	            else
    	            {
    	               right();
    	            }
             }
          }
           public void keyTyped(KeyEvent e){}
           public void keyReleased(KeyEvent e){}
       }

    Main Class:
    import javax.swing.JFrame;
     
    public class SnakeMain
    {
    	public static void main(String[] args)
    	{
    		JFrame f = new JFrame("Snake Game");
    		Snake s = new Snake();
    		f.add(s);
    		f.setVisible(true);
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		f.setSize(489,515);
    	}
    }

    thanks
    Last edited by godlynom; October 2nd, 2012 at 10:30 AM.


  2. #2
    Junior Member
    Join Date
    Sep 2012
    Posts
    28
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: more help with snake game

    The food class did not copy all over, here is the code again
    import java.awt.*;
     
    public class Food
    {
    	double x = 0, y = 0;
    	private double myX;   // x and y coordinates of center
          private double myY;
          private double myDiameter;
          private Color myColor; 
          private double myRadius;
    		private double myHeight;
          private static final int FRAME = 495;
     
     
    	public Food()
    	{
    		myX = 0;
          myY = 200;
          myHeight = 15;
          myColor = Color.RED;
          myRadius = myDiameter/2;
    	}
    	public Food(double x, double y, double d, Color c)
    	{
    		myX = x;
          myY = y;
          myHeight = d;
          myColor = c;
          myRadius = d/2; 
    	}
    	 public double getX() 
          { 
             return myX;
          }
           public double getY()      
          { 
             return myY;
          }
           public double getDiameter() 
          { 
             return myDiameter;
          }
           public Color getColor() 
          { 
             return myColor;
          }
           public double getRadius() 
          { 
             return myRadius;
          }
    		public double getHeight()
           {
        	   return myHeight;
           }
     
       // modifier methods
           public void setX(double x)
          {
             myX = x;
          } 
           public void setY(double y)
          {
             myY = y;
          } 
           public void setColor(Color c)
          {
             myColor = c;
          }
           public void setDiameter(double d)
          {
             myDiameter = d;
             myRadius = d/2;
          }
           public void setRadius(double r)
          {
             myRadius = r;
             myDiameter = 2*r;
          }
        //	 instance methods
           public void jump(int rightEdge, int bottomEdge)
          {
             // moves location to random (x, y) within the edges
            	 myX = rightEdge;
                 myY = bottomEdge; 
          }
     
    	public void draw(Graphics myBuffer) 
          {
              myBuffer.setColor(myColor);
             myBuffer.fillRect((int)(myX), (int)(myY), (int)myHeight, (int)myHeight);
          }
    }

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: more help with snake game

    I would recommend using a List, more specifically a ArrayList. It's created like any other object and you don't have to worry too much about the low-level details required with arrays (specifically you don't have to manually resize ArrayLists).

    Lists are regular objects so you use them as you would any other object:

    // create a new list
    ArrayList<String> names = new ArrayList<String>();
     
    // add items to the list
    names.add("Andrew");
    names.add("Bill");
    names.add("Charles");
     
    // print out all items in the list
    for(int i = 0; i < names.size(); ++i)
    {
        System.out.println(names.get(i));
    }

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: more help with snake game

    That's way too much code for us to wade through, and we can't do your homework for you. I suggest breaking your problem down into an SSCCE and focusing on one thing at a time.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Sep 2012
    Posts
    28
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: more help with snake game

    So if I were to make many snake objects, would i just create an:

    ArrayList <SnakeBody> snake = new ArrayList <SnakeBody>();

    and then call them one by one in a for loop based on that?

    @KevinWorkman I was not asking you to do this for me, and this is not homework, doing this for fun and practice, just wanted some advice on a direction to go, like helloworld922 did, thought posting the code would help some folks

  6. #6
    Junior Member
    Join Date
    Sep 2012
    Posts
    28
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: more help with snake game

    as an update, i worked on my class and now the snake body parts get created and drawn, but only for a second, so you see the body part and then it disappears, any help?
    Snake class:
       import javax.swing.*;
     	import java.util.ArrayList;
       import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
     
        public class Snake extends JPanel implements ActionListener, KeyListener
       {
          Timer t = new Timer(75, this);
          double x =0, y = 0, velx = 0, vely = 0, x1 = 0, y1 = 0;
          private static final int FRAME = 480;
    		private boolean dup,ddown,dright,dleft, go;
          private Food food;
    		private SnakeBody snakeH;
          private ArrayList<SnakeBody> snake = new ArrayList<SnakeBody>();
     
          int count = 0;
    		int distance = 15;
     
     
           public Snake()
          {
    			addKeyListener(this);
             setFocusable(true);
    			setFocusTraversalKeysEnabled(false);
    			go = true;
     
             x = 240;
             y = 240;
     
    	      x1 = 15 * (int)(Math.random()* 32);
    	      y1 = 15 * (int)(Math.random()* 32);
    	      food = new Food(x1,y1,15,Color.BLUE);
             snake.add(new SnakeBody(x,y,15, Color.RED));
    			SnakeBody temp = new SnakeBody(0,0,0,Color.red);
    			snakeH=new SnakeBody(0,0,0,Color.RED);
    			t.start();
    	}
    	public void paintComponent(Graphics g)
          {
        	 super.paintComponent(g);
             food.draw(g);
    			for(SnakeBody s: snake)
    			{
    				s.draw(g);
    			}
    			for(int i = 0; i < 480; i+=15)
    			{
    				g.setColor(Color.BLACK);
    				g.drawLine(i,FRAME,i,0);
    				g.drawLine(FRAME,i,0,i);
    			}
    			g.drawLine(FRAME,0,FRAME,FRAME);
    			g.drawLine(0,FRAME,FRAME,FRAME);
     
             g.setColor(Color.BLACK);
             g.setFont(new Font("Monospaced",Font.BOLD,24));
             g.drawString("Count:" + count, FRAME - 300, 25);
          }
           public void actionPerformed(ActionEvent e)
          {
            	x += velx;
             y += vely;
    			if(x<0)
    			{
    				x = 465;
    			}
    			if(x>465)
    			{
    				x = 0;
    			}
    			if(y<0)
    			{
    				y = 465;
    			}
    			if(y>465)
    			{
    				y = 0;
    			}
     			snakeH.setX(x);
     			snakeH.setY(y);
    			SnakeBody temp=new SnakeBody(0,0,0,Color.red);
    			SnakeBody temp1=new SnakeBody(0,0,0,Color.red);
    			go=true;
    			collide(snakeH, food);
    			for(SnakeBody s: snake)
    			{
    				if(snake.indexOf(s)==0)
    				{
    					snakeH=new SnakeBody(0,0,0,Color.red);
    					snakeH=s;
    				}
    			}
    			if(go)
    			{
    				int i=0;
    				for(SnakeBody s: snake)
    				{
    					if(snake.indexOf(s)>=1)
    					{
    						temp1=s;
    						s.follow(snake.get(i));
    						temp=temp1;
    					}
    					else
    					{
    						snakeH = s;
    						temp=s;
    					}
    					i=snake.indexOf(s);
    				}
    			}
    			go=true;
     
    			repaint();
          }
    	private void collide(SnakeBody s, Food food)
          {
    			int dx=0,dy=0;
    			if(dup)
    			{
    				dx=0;
    				dy=-15;
    			}
    			else if(ddown)
    			{
    				dx=0;
    				dy=15;
    			}
    			else if(dleft)
    			{
    				dx = -15;
    				dy = 0;
    			}
    			else if(dright)
    			{
    				dx = 15;
    				dy = 0;
    			}
             double d = distance(s.getX()+dx,s.getY()+dy,food.getX(),food.getY());
     
             if(d < s.getHeight()/2 + food.getHeight()/2)
             {
    				snake.add(0,new SnakeBody(food.getX(),food.getY(),15,Color.red));
    				int x3 = 15 * (int)(Math.random()* 32);
    	      	int y3 = 15 * (int)(Math.random()* 32);
                food.jump(x3,y3);
    				go=false;
                count += 250;
             }
     
          }
           private double distance(double x1, double y1, double x2, double y2)
          {
             return Math.sqrt(Math.pow(x2-x1,2)+Math.pow(y2-y1,2));
          }
           public void up()
          {
             vely = -15;
             velx = 0;
    			dup=true;
    			ddown=false;
    			dleft = false;
    			dright = false;
          }
           public void down()
          {
             vely = 15;
             velx = 0;
    			dup=false;
    			ddown=true;
    			dleft = false;
    			dright = false;
          }
           public void left()
          {
             velx = -15;
             vely = 0;
    			dup=false;
    			ddown=false;
    			dleft = true;
    			dright = false;
          }
           public void right()
          {
             velx = 15;
             vely = 0;
    			dup=false;
    			ddown=false;
    			dleft = false;
    			dright = true;
          }
           public void keyPressed(KeyEvent e)
          {
             int code = e.getKeyCode();
             if(code == KeyEvent.VK_LEFT)
             {
    	            if(velx == 15)
    	            {
    	               up();
    	            }
    	            else if(velx == -15)
    	            {
    	               down();
    	            }
    	            else if(vely == 15)
    	            {
    	               right();
    	            }
    	            else if(vely == -15)
    	            {	
    	               left();
    	            }
    	            else
    	            {
    	               left();
    	            }
             }
             if(code == KeyEvent.VK_RIGHT)
             {
     
    	            if(velx == 15)
    	            {
    	               down();
    	            }
    	            else if(velx == -15)
    	            {
    	               up();
    	            }
    	            else if(vely == 15)
    	            {
    	               left();
    	            }
    	            else if(vely == -15)
    	            {	
    	               right();
    	            }
     
    	            else
    	            {
    	               right();
    	            }
             }
          }
           public void keyTyped(KeyEvent e){}
           public void keyReleased(KeyEvent e){}
       }

    and here is the follow method in the SnakeBody class:
    public void follow(SnakeBody snake)
    		{
    			myX=snake.getX();
    			myY=snake.getY();
    			myHeight = snake.getHeight();
    		}

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: more help with snake game

    body part and then it disappears, any help?
    Try debugging the code by printing out the values of the variables that control the drawing of the body. Are their values changing and causing the code to stop drawing the body?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Sep 2012
    Posts
    28
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: more help with snake game

    well i debugged the paintComponent, and whenever i collide with a food, a new SnakeBody is created and read every single tick, however, it is not being drawn

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: more help with snake game

    it is not being drawn
    Why isn't it being drawn? Does the println statement in its draw() method print out?

    new SnakeBody is created and read every single tick,
    When you create a new SnakeBody object, is it added to the GUI so it will be seen when its drawn?
    Are you calling a method in an obJect that has not been added to the GUI?
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Sep 2012
    Posts
    28
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: more help with snake game

    well i put the debugger here
     for(SnakeBody s: snake)
    			{
    				s.draw(g);
    				System.out.println(s);
    			}
    and each time i got a food it created a new SnakeBody, and then also was drawing them all every single tick

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: more help with snake game

    drawing them all every single tick
    Is it working now?
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Junior Member
    Join Date
    Sep 2012
    Posts
    28
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: more help with snake game

    no, they still disappear, are the coordinates getting changed to the first snake's, or is the SnakeBody not being actually drawn, it says it is, trying to figure this out now

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: more help with snake game

    are the coordinates getting changed
    Time for more debugging: add the x,y values to the printlns in the SnakeBody draw() method so you can see where the draw method is putting the rectangles. Also print out the width and height values.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Junior Member
    Join Date
    Sep 2012
    Posts
    28
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: more help with snake game

    okay, yea, my assumption was correct, they do draw, but then their x and y are changed to that of the original snake, thus merging back into one snake

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: more help with snake game

    Are you saying that all the drawing is done on top of each other?
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Junior Member
    Join Date
    Sep 2012
    Posts
    28
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: more help with snake game

    they are drawn separately, with different x's and y's, but then the next time they move they all become the original snake's x and y and then are drawn upon each other, so it is all the bodyparts in one box basically

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: more help with snake game

    Where in the code are the values changed so that they are on top of each other?
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Junior Member
    Join Date
    Sep 2012
    Posts
    28
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: more help with snake game

    I think it is this code
    if(go)
    			{
    				int i=0;
    				for(SnakeBody s: snake)
    				{
    					if(snake.indexOf(s)>=1)
    					{
    						temp1=s;
    						s.follow(snake.get(i));
    						temp=temp1;
    					}
    					else
    					{
    						snakeH = s;
    						temp=s;
    					}
    					i=snake.indexOf(s);
    				}
    			}

    here is the follow method:

    public void follow(SnakeBody snake)
    		{
    			myX=snake.getX();
    			myY=snake.getY();
    			myHeight = snake.getHeight();
    		}

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: more help with snake game

    Sorry, its your code. You should be able to see where it changes the x & y values for the SnakeBody objects.
    What are the x and y values set to in the follow() method?
    If you don't understand my answer, don't ignore it, ask a question.

  20. #20
    Junior Member
    Join Date
    Sep 2012
    Posts
    28
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: more help with snake game

    yea, i tested it out, the problem is in my follow method, what i do is i set the x and y of the bodyparts to the x and y of the snake, so now i need to make it so that it appears behind the snake, but travels at the same velocity and direction

  21. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: more help with snake game

    Now you see the problem you should be able to fix it.

    Done for tonight. Back tomorrow.
    If you don't understand my answer, don't ignore it, ask a question.

  22. #22
    Junior Member
    Join Date
    Sep 2012
    Posts
    28
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: more help with snake game

    I have figured it out, the snake game is up and running, everything works, all the collides and all, now to customize it how i want, thanks so much for your help both times Norm and anyone else who also helped

Similar Threads

  1. [SOLVED] Help with the Snake in a Snake Game
    By godlynom in forum What's Wrong With My Code?
    Replies: 20
    Last Post: September 27th, 2012, 06:41 PM
  2. [Snake]Help
    By Totel in forum Object Oriented Programming
    Replies: 23
    Last Post: September 26th, 2012, 09:17 AM
  3. Snake Game
    By Cuju in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 19th, 2011, 08:31 PM
  4. Snake Game In Java
    By Shyamz1 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 10th, 2011, 10:00 AM
  5. Snake game in java
    By freaky in forum Object Oriented Programming
    Replies: 0
    Last Post: April 19th, 2010, 11:04 AM

Tags for this Thread