Creating "Snake" Game in Java. Assistance?

I am trying to create a Snake game, but all I have so far is to move t

Page 1 of 1

0 Replies - 2606 Views - Last Post: 02 May 2009 - 02:38 PM Rate Topic: -----

#1 DraDallas2009  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 02-May 09

Creating "Snake" Game in Java. Assistance?

Posted 02 May 2009 - 02:38 PM

I have been trying to figure out how to create the Snake and how to animate it where it grows with each piece of "food" that is eaten. And i would like to know if the program would be easier if i imported the Snake and food icons instead of creating them with the Graphics class. Thanks

 

public class SnakeGame extends JFrame
{
	// the starting position for the food
		private int foodX = 180;
   		private int foodY = 120;
   	
   		// the starting position for the snake
   		private int snakeX = 240;
   		private int snakeY = 168;
	
	// the time delay for the snake
	private final int TIME_DELAY = 40;
	
	// Timer for the Snake
	private Timer timeSnake;
	
	// Make sure the moving is working	
	private boolean movingOn = true;
	
	// the minimum range for the snake
	private final int MINIMUM_Y = 0;
	
	// the maximum range for the snake
	private final int MAXIMUM_Y = 240;
	
	// the minimum domain for the snake
	private final int MINIMUM_X = 0;
	
	// the maximum domain for the snake
	private final int MAXIMUM_X = 360;
	
	
	
	// Creates the keyboard panel for the computer to read the keyboard.
	private KeyboardPanel keyboardPanel = new KeyboardPanel();

		
		
	// TimerListener for the animation
	private class TimerListener implements ActionListener
   	{
   		public void actionPerformed(ActionEvent e)
   		{
   			if(movingOn)
   			{
   				if(snakeY < MINIMUM_Y && snakeX < MINIMUM_X)
   				{
   				 	System.exit(0);
   				}	
   				 else
   				 	movingOn = false;		
   			}
   			else
   			{
   				if (snakeY < MAXIMUM_Y && snakeX < MAXIMUM_X)
   				{
   					snakeX += 8;
   					snakeY += 8;
   				}
   				else
   					movingOn = true;	
   			}
   			repaint();
   		}
   	}	
	
	// class for the Snake Game
	public SnakeGame()
	{
	
	  /*add(new Food());*/
	  
	  add(keyboardPanel);
	  
	  keyboardPanel.setFocusable(true);
	 
	  timeSnake = new Timer(TIME_DELAY, new TimerListener());
	  timeSnake.start();
	  
	  
	/**
	 * the KeyboardPanel class is for the receiving the 
	 * key input.
	 */  
	}
	
	 class KeyboardPanel extends JPanel
	{
		
   	
   	// Controls the snakes movements	
	public KeyboardPanel()
	{
	addKeyListener(new KeyAdapter()
	  {
		  public void keyPressed(KeyEvent e)
		  {
			  switch(e.getKeyCode())
			  {
				  case KeyEvent.VK_DOWN: snakeY+= 8; break;
				  case KeyEvent.VK_UP:  snakeY-= 8; break;
				  case KeyEvent.VK_RIGHT: snakeX += 8; break;
				  case KeyEvent.VK_LEFT: snakeX -= 8; break;
				  default: e.getKeyChar();
			  }
			  
			  repaint();
		  }
	  });
	}
	
		public void paintComponent(Graphics g)
   		{
   		
   			super.paintComponent(g);
   			g.setColor(Color.red);
   			g.fillRect(foodX, foodY, 8, 8);
   		
   			g.setColor(Color.green);
   			g.fillOval(snakeX, snakeY, 8, 8);
   		}	
	}
	// Main class
	public static void main(String [] args)
	{
		SnakeGame snake = new SnakeGame();
		
		snake.setSize(360, 240);
		
		snake.setTitle("SNAKE");
		
		// centers the frame
		snake.setLocationRelativeTo(null);
		
		snake.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		snake.setVisible(true);
		
	}
	
   /* creates the components for the food and snake
   class Food extends JPanel
   {
   	
   		
   	
   	}*/
   	
   	
   	}	
   
   

This post has been edited by DraDallas2009: 03 May 2009 - 07:33 AM


Is This A Good Question/Topic? 0
  • +

Page 1 of 1