I have a project due in class Tuesday, and I've kinda hit a wall. I'm making the classic game Snake, and I've gotten most of the basic game functions working aside from making the snake grow when it eats an apple. I keep getting an out of bounds exception for every approach I've tried. The code compiles and everything, but when I eat the first apple it freezes and gives me the exception.
Here's my grow method and my main method. If it would be helpful for me to post the rest of my code just let me know. And the DrawingPanel and Graphics are classes written by the author of the textbook my class uses.
public static void main(String[] args) {
DrawingPanel panel = new DrawingPanel(SCREENW, SCREENH);
Graphics g = panel.getGraphics();
//The main game loop variable
boolean play = true;
Point pApple = new Point();
Point[] pSnake;
//Length of sides of the square apple
int appleSide = 10;
//Length of sides of the square snake head
int snakeSide = 10;
//Number of pixels the snake head moves each time
int velocity = 10;
//Variable to slow down the snake to a playable speed
int sleepTime = 50;
//These points are just to test the functionality. Once I finish I'll go back and make the initial points random
Point p1 = new Point(70, 30);
Point p2 = new Point(60, 30);
Point p3 = new Point(50, 30);
Point p4 = new Point(40, 30);
Point p5 = new Point(30, 30);
Point p6 = new Point(20, 30);
Point p7 = new Point(10, 30);
//Sets the initial point for the apple
pApple = appleEaten(appleSide);
//Set the initial points for the snake
pSnake = new Point[]{p1, p2, p3, p4, p5, p6, p7};
//Main game loop
while (play) {
//Draws the background of the game screen
g.setColor(Color.BLACK);
g.fillRect(0, 0, SCREENW, SCREENH);
//Draws the apple to the screen
drawApple(panel, g, pApple, appleSide);
//Sets the direction of the snake to move depending on which arrow key is hit (I know the method name is kinda misleading. I just haven't changed it from my initial idea
moveSnake(panel);
//Updates the snakes coordinates depending on which flag is true and draws it to the screen
drawSnake(panel, g, pSnake, snakeSide, velocity, sleepTime);
//Checks to see if snake head hits the snake body or a screen boundary
checkLose(pSnake);
//If the snake head is at the same point as the apple
if (pSnake[0].equals(pApple)) {
//A new apple is created
pApple = appleEaten(appleSide);
//The snake grows by one segment
pSnake = grow(pSnake);
//The snake moves faster
sleepTime -= 5;
}
panel.copyGraphicsToScreen();
}
}
public static Point[] grow(Point[] pSnake) {
//Create a new snake one index longer than the original snake
Point[] newPSnake = new Point[pSnake.length + 1];
//Sets the new snake equal to the old snake
for(int i = 0; i < pSnake.length; i++)
{
newPSnake[i] = pSnake[i];
}
//Sets the last index of the new snake depending on which direction the snake is moving
if (right) {
newPSnake[4].setLocation(newPSnake[newPSnake.length - 2].getX() - 10, newPSnake[newPSnake.length - 2].getY());
}
if (left) {
newPSnake[newPSnake.length - 1].setLocation(pSnake[pSnake.length - 1].getX() + 10, pSnake[pSnake.length - 1].getY());
}
if (up) {
newPSnake[newPSnake.length - 1].setLocation(pSnake[pSnake.length - 1].getX(), pSnake[pSnake.length - 1].getY() + 10);
}
if (down) {
newPSnake[newPSnake.length - 1].setLocation(pSnake[pSnake.length - 1].getX(), pSnake[pSnake.length - 1].getY() - 10);
}
return newPSnake;
}
Thanks for any help you guys can give!!
-M@

New Topic/Question
Reply




MultiQuote







|