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 8 of 8

Thread: Help with the body/tail of Snake

  1. #1
    Junior Member
    Join Date
    Oct 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Help with the body/tail of Snake

    Hi, I'm trying to create a snake game, but I can't make the body/tail to follow correctly at the back of the snake's head.
    Honestly, I don't know what to do here..


    here's my code
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.event.ActionEvent;
    import java.util.Random;
    public class snake extends JPanel implements ActionListener,KeyListener{
    		static JFrame frame = new  JFrame();
    		static int x = 150,y=150,x1=x-10,y1=y,eposx=200,eposy=120,n=0;
    		static int slength = 3;
    		boolean right = true,left=true,up=true,down=true,rmove=true,lmove=false,umove=false,dmove=false;
    		Timer tmr = new Timer(50,this);
    		Random rdm = new Random();
     
    	public snake(){
    		setLayout(null);		
    		this.requestFocus();
    		addKeyListener(this);
            setFocusable(true);
            setFocusTraversalKeysEnabled(false);
    	}
     
    	public void actionPerformed(ActionEvent e){
    	if(e.getSource()==tmr){
     
    		if(right==true&&lmove==false){
    		x=x+5;
     
    		System.out.println("X = " + x + " Y = " + y);
    		}
    		if(left==true&&rmove==false){
    		x=x-5;
     
    			System.out.println("X = " + x + " Y = " + y);
    		}
    		if(up==true&&dmove==false){
    		y=y-5;
     
    		System.out.println("X = " + x + " Y = " + y);
     
    		}
    		if(down==true&&umove==false){
    		y=y+5;
    		System.out.println("X = " + x + " Y = " + y);
    		}
    	}
    	if(x>=280){
    		x=0;
     
    	}
    	else if(x<=00){
    		x=280;
    	}
    	else if(y<=00){
    		y=260;
     
    	}
    	else if(y>=260){
    		y=0;
     
    	}
    	if(x==eposx&&y==eposy){
    		eposx=(rdm.nextInt(20)*10+10);
    		eposy=(rdm.nextInt(20)*10+10);
    	}
    		repaint();
    	}
    	public void paintComponent(Graphics g){
    		g.setColor(Color.WHITE);
    		g.fillRect(0,0,100000,10000000);
    		g.setColor(Color.BLACK);
    		g.fillOval(x,y,10,10);
    		g.setColor(Color.GREEN);
    		g.fillOval(x1,y1,10,10);
    		g.setColor(Color.RED);
    		g.fillRect(eposx,eposy,10,10);
     
    	}
    	public void keyPressed(KeyEvent e){
    		tmr.start();
    		int c = e.getKeyCode();
    		if(c==KeyEvent.VK_RIGHT&&rmove==true){
    			right=true;
    			left=false;
    			up=false;
    			down=false;
    			umove=true;
    			dmove=true;
    			lmove=false;
    			rmove=false;
     
    		}
    		if(c==KeyEvent.VK_LEFT&&lmove==true){
    			right=false;
    			left=true;
    			up=false;
    			down=false;
    			rmove=false;
    			lmove=true;
    			umove=true;
    			dmove=true;
     
    		}
    		if(c==KeyEvent.VK_UP&&umove==true){
    			right=false;
    			left=false;
    			up=true;
    			down=false;
    			umove=true;
    			dmove=false;
    			lmove=true;
    			rmove=true;
     
    		}
    		if(c==KeyEvent.VK_DOWN&&dmove==true){
    			right=false;
    			left=false;
    			up=false;
    			down=true;
    			umove=false;
    			dmove=true;
    			lmove=true;
    			rmove=true;
     
    		}
    		repaint();
     
    	}
    	public void keyTyped(KeyEvent e){}
    	public void keyReleased(KeyEvent e){}
     
    	public static void main(String[]args){
    		snake obj = new snake();	
     
    	frame.setSize(300,300);
     
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    	frame.setVisible(true);
     
    	frame.add(obj);	
     
    	}
    }

    What should I add?


  2. #2
    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: Help with the body/tail of Snake

    What should I add?
    What are the rules for playing the game?
    What is supposed to happen?
    What does happen?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with the body/tail of Snake

    Quote Originally Posted by Norm View Post
    What are the rules for playing the game?
    What is supposed to happen?
    What does happen?
    My problem is the body of the snake, I need it to follow the snakes head, once the head of the snake moves on the position of the body, game-over. The body goes to every previous position of the head of the snake.. I don't know what to do about the body part..

  4. #4
    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: Help with the body/tail of Snake

    the body of the snake, I need it to follow the snakes head,
    Where is the code for the snake's body?

    The shapes have width and height that the code should consider when detecting a collision and not just a single point.

    The variables in the code are poorly named. There are three shapes drawn on the screen.
    There should be a name for each shape and the x,y variables for those shapes should include that name.
    For example the snake's x,y variables' names could be: snakeX and snakeY.
    And the same for the other two shapes.

    --- Update ---

    You missed my questions:
    What are the rules for playing the game?
    What is supposed to happen?
    What does happen?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Oct 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with the body/tail of Snake

    Here's how it works the Black oval is the head of the snake, it's followed by one green oval which is followed by one green oval. These green oval's are the body/tail of the snake. The black oval(head) keeps moving on the direction pressed, but will not move on the opposite direction if its going up it cant go down.

    For example the current position of the black oval is sHeadx=150,sHeady150 , the user pressed right so it goes to the right adding(continuous) 5 to the sHeadx which makes it 155, the green oval must go to the previous location of the black oval(head) which was 150,150. The same goes for the 2nd green oval, but it follows the location of the 1st green oval.

    My problem is, I don't know how to make the green ovals follow the previous position of the black oval or the head.


    here's the updated.. named it properly
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.event.ActionEvent;
    import java.util.Random;
    import java.util.ArrayList;
    public class snake extends JPanel implements ActionListener,KeyListener{
     
    		static JFrame frame = new  JFrame();
    		int sHeadx = 150,sHeady=150,eposx=200,eposy=120,score=0;
    		int []sBodyx = new int[]{sHeadx-10,sHeadx-20};
    		int []sBodyy = new int[] {sHeady,sHeady};
    		static int slength = 3;
    		boolean right = true,left=true,up=true,down=true,rmove=true,lmove=false,umove=false,dmove=false;
    		Timer tmr = new Timer(50,this);
    		Random rdm = new Random();
     
    	public snake(){
    		setLayout(null);		
    		this.requestFocus();
    		addKeyListener(this);
            setFocusable(true);
            setFocusTraversalKeysEnabled(false);
    	}
     
     
    	public void actionPerformed(ActionEvent e){
    	if(e.getSource()==tmr){
     
    		if(right==true&&lmove==false){
    		sHeadx=sHeadx+5;
    		sBodyx[0]=sHeadx-10;sBodyy[0]=sHeady;
    		sBodyx[1]=sHeadx-20;sBodyy[1]=sHeady;
    		System.out.println("X = " + sHeadx + " Y = " + sHeady);
    		}
    		if(left==true&&rmove==false){
    		sHeadx=sHeadx-5;
    		sBodyx[0]=sHeadx+10;sBodyy[0]=sHeady;
    		sBodyx[1]=sHeadx+20;sBodyy[1]=sHeady;
    			System.out.println("X = " + sHeadx + " Y = " + sHeady);
    		}
    		if(up==true&&dmove==false){
    		sHeady=sHeady-5;
    		sBodyx[0]=sHeadx;sBodyy[0]=sHeady+10;
    		sBodyx[1]=sHeadx;sBodyy[1]=sHeady+20;
    		System.out.println("X = " + sHeadx + " Y = " + sHeady);
     
    		}
    		if(down==true&&umove==false){
    		sHeady=sHeady+5;
    		sBodyx[0]=sHeadx;sBodyy[0]=sHeady-10;
    		sBodyx[1]=sHeadx;sBodyy[1]=sHeady-20;
    		System.out.println("X = " + sHeadx + " Y = " + sHeady);
    		}
    	}
    	if(sHeadx>=280){
    		sHeadx=0;
     
    	}
    	else if(sHeadx<=00){
    		sHeadx=280;
    	}
    	else if(sHeady<=00){
    		sHeady=260;
     
    	}
    	else if(sHeady>=260){
    		sHeady=0;
     
    	}
     
    	if(sHeadx==eposx&&sHeady==eposy){
    		eposx=(rdm.nextInt(20)*10+10);
    		eposy=(rdm.nextInt(20)*10+10);
    		score=score+10;
    		System.out.println("CURRENT SCORE : "+score);
    	}
    		repaint();
    	}
    	public void paintComponent(Graphics g){
    		g.setColor(Color.WHITE);
    		g.fillRect(0,0,100000,10000000);
    		g.setColor(Color.BLACK);
    		g.fillOval(sHeadx,sHeady,10,10);
    		g.setColor(Color.GREEN);
    		g.fillOval(sBodyx[0],sBodyy[0],10,10);//1st green dot
    		g.fillOval(sBodyx[1],sBodyy[1],10,10);//2nd green dot
    		g.setColor(Color.RED);//Food 
    		g.fillRect(eposx,eposy,10,10);//Food Position
     
    	}
    	public void keyPressed(KeyEvent e){
    		tmr.start();
    		int c = e.getKeyCode();
    		if(c==KeyEvent.VK_RIGHT&&rmove==true){
    			right=true;
    			left=false;
    			up=false;
    			down=false;
    			umove=true;
    			dmove=true;
    			lmove=false;
    			rmove=false;
     
    		}
    		if(c==KeyEvent.VK_LEFT&&lmove==true){
    			right=false;
    			left=true;
    			up=false;
    			down=false;
    			rmove=false;
    			lmove=true;
    			umove=true;
    			dmove=true;
     
    		}
    		if(c==KeyEvent.VK_UP&&umove==true){
    			right=false;
    			left=false;
    			up=true;
    			down=false;
    			umove=true;
    			dmove=false;
    			lmove=true;
    			rmove=true;
     
    		}
    		if(c==KeyEvent.VK_DOWN&&dmove==true){
    			right=false;
    			left=false;
    			up=false;
    			down=true;
    			umove=false;
    			dmove=true;
    			lmove=true;
    			rmove=true;
     
    		}
    		repaint();
     
    	}
    	public void keyTyped(KeyEvent e){}
    	public void keyReleased(KeyEvent e){}
     
    	public static void main(String[]args){
    		snake obj = new snake();	
     
    	frame.setSize(300,300);
     
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    	frame.setVisible(true);
     
    	frame.add(obj);	
     
    	}
    }

  6. #6
    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: Help with the body/tail of Snake

    What value is in the eposx variable?

    Is this too restrictive, requiring the points to be equal?
    if(sHeadx==eposx&&sHeady==eposy){
    Should the width of the shapes be considered?

    how to make the green ovals follow the previous position of the black oval
    Should the body shapes move through the same locations that the head moved through? And the body shapes need to move to where the shape of the one in front of it moved from.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Oct 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with the body/tail of Snake

    Quote Originally Posted by Norm View Post
    What value is in the eposx variable?

    Is this too restrictive, requiring the points to be equal?
    if(sHeadx==eposx&&sHeady==eposy){
    Should the width of the shapes be considered?
    moving to the exact position of eposx and eposy will give you a score
    g.setColor(Color.RED);//Food 
    g.fillRect(eposx,eposy,10,10);//Food Position

    if the head of the snake goes to the exact position, you gain a score, then it changes position.
     
    if(sHeadx==eposx&&sHeady==eposy){
    		eposx=(rdm.nextInt(20)*10+10);
    		eposy=(rdm.nextInt(20)*10+10);
    		score=score+10;
    		System.out.println("CURRENT SCORE : "+score);
    	}

    My real problem is the green ovals going to the previous positions of the black oval..

  8. #8
    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: Help with the body/tail of Snake

    the exact position
    That's confusing visually when you see the shapes are overlapping but there isn't a score.

    problem is the green ovals going to the previous positions of the black oval..
    What ideas do you have for the green shapes to move in-chain following the black shape?
    One design consideration is to make objects from a class for each of the shapes that hold information about where they are located and their relationship to the other shapes in the snakes body.
    To get some ideas, work with a paper and pencil drawing out a position and then move the snake one unit and draw out the new position. Look at what data items were changed and where the new values came from for each shape in the body.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Hello Every Body!
    By zahidhasan in forum Member Introductions
    Replies: 1
    Last Post: March 30th, 2013, 04:22 PM
  2. please any body help me
    By lanya in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 1st, 2013, 01:11 PM
  3. [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
  4. trying to pass default params to tail recursion function
    By johnmerlino in forum Algorithms & Recursion
    Replies: 1
    Last Post: July 16th, 2012, 03:21 AM
  5. hi every body!!!!!!!!!........
    By kranthi in forum Member Introductions
    Replies: 2
    Last Post: January 10th, 2012, 06:03 AM