I am having trouble creating the snake, let alone making the animation. I need someone to assist me in creating such things, as well as helping me complete the program. Thanks
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* @(#)SnakeGame.java
*
*
* @author
* @version 1.00 2009/4/30
*/
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 = 1;
// 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 += 4 ;
snakeY += 4;
}
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());
/**
* 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
{
}*/
}