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

Thread: Snake Game

  1. #1
    Member
    Join Date
    Sep 2013
    Posts
    41
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Snake Game

    Hi I am attempting to develop a Snake game, I am a complete beginner to 2D graphics, and I was wondering how to get my snake ( a rectangle) to move. I have tried Key event but it does not work. What did I do wrong and where can I improve. Thank you for your time.

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
     
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.Timer;
    import javax.swing.WindowConstants;
     
    public class Snake extends JComponent implements ActionListener, KeyListener {
     
     
    	public static void main(String[] args) {
     
    		JFrame window = new JFrame ("Snake Game By Sakon");
    		Snake game = new Snake();
    		window.add(game);
    		window.pack();
    		window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    		window.setVisible(true);
     
    	    Timer t= new Timer (100, game);
    		t.start();
     
    	}
     
     
    	private int snakeX=400;
    	private int snakeY=400;
     
     
     
    	public Dimension getPreferredSize() {
    		return new Dimension (800,600);
    	}
     
     
    	public void paint(Graphics g) {
    		//Set Back Round black
    		g.setColor(Color.BLACK);
    		g.fillRect(0, 0, 800, 600);
     
    		//Draw dot.
    		g.setColor(Color.WHITE);
    		g.fillOval(0, 0, 10, 10);
     
    		//Draw Snake
    		g.setColor(Color.LIGHT_GRAY);
    		g.fillRect(snakeX, snakeY, 5, 100);
     
    	}
     
     
    	@Override
    	public void keyPressed(KeyEvent e) {
     
    	switch(e.getKeyCode()){
     
    		case KeyEvent.VK_UP:{
     
    			if(snakeY>600)
    			snakeY=1;
     
    			snakeY=snakeY+1;
     
    			repaint();
    		}
     
    		case KeyEvent.VK_DOWN:{
     
    			if(snakeY<0)
    			snakeY=600;
     
    			snakeY=snakeY-1;
     
    			repaint();
     
    		}
     
    		case KeyEvent.VK_LEFT:{
     
    			if(snakeX<0)
    			snakeY=800;
     
    			snakeY=snakeY-1;
     
    			repaint();
     
    		}
    		case KeyEvent.VK_RIGHT:{
     
    			if(snakeX>800)
    			snakeY=1;
     
    			snakeY=snakeY+1;
     
    			repaint();
     
    		}
     
     
     
    		}
     
    	}
     
     
    	@Override
    	public void keyReleased(KeyEvent e) {
    		// TODO Auto-generated method stub
     
    	}
     
     
    	@Override
    	public void keyTyped(KeyEvent e) {
    		// TODO Auto-generated method stub
     
    	}
     
     
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		// TODO Auto-generated method stub
     
    	}


  2. #2
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Snake Game

    Hello.
    Your code is incomplete.
    You will need to learn about KeyListener, ActionListener, and Timer classes in the swing package.
    1. How to Write an Action Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)
    2. How to Write a Key Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)
    3. How to Use Swing Timers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)

    Try to follow some examples. You should be able to write your code better.

    Syed.

  3. The Following User Says Thank You to syedbhai For This Useful Post:

    sakonpure6 (September 28th, 2013)

Similar Threads

  1. Help with Snake Game Java Code: It's Impossible to Lose the Game
    By haruspex_icis in forum What's Wrong With My Code?
    Replies: 20
    Last Post: December 17th, 2012, 11:21 AM
  2. [SOLVED] more help with snake game
    By godlynom in forum What's Wrong With My Code?
    Replies: 21
    Last Post: October 4th, 2012, 03:32 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. Snake Game
    By Cuju in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 19th, 2011, 07:31 PM
  5. Snake game in java
    By freaky in forum Object Oriented Programming
    Replies: 0
    Last Post: April 19th, 2010, 11:04 AM