PDA

View Full Version : Java program Runtime problem


Mink
12-10-2006, 07:51 PM
OK so awhile ago I made a Snake game, and then I tried implementing a Retry fucntion after I died...but I never was able to get it to work. When the game first starts up it is fine, but when I call my retry function...which just reloads everything, the game lags...i believe it is because my program is caught in the playgame loop. But it doesnt make any sense to me. I load all the GUIs before entering the loop, which leads me to believe the GUI takes too long to load which is why it jumps to the loop to fast. I know this because if I comment out the line where it runs the loop the retry function loads the gui fine.

I was hoping someone out there would know how to optimize this code to allow my retry fucntion to work.

This is my retry class. It is a menu item:

public class ListenMenuRetry implements ActionListener {
public void actionPerformed(ActionEvent e){

playGame(); //resets all the GUI components back to initial settings
start(); //starts the game

}

}

This is the function that resets the variables:
public void playGame(){
//initialize members
row = snakeLevel.length / 2;
col = snakeLevel[row].length / 2;
rowMod = 0;
colMod = 0;
points = 0;
pointsInc = 10;
lvl = 250;
head = new SnakePart(row,col,null,null);
part1 = new SnakePart(row,col-1,null,head);
part2 = new SnakePart(row,col-2,null,part1);
tail = part2;
alive = true;
headSymbol = headRight;

pointsLbl.setText("Score: 0");

//clear the field
for(int i = 0; i < snakeLevel.length; i++){
for(int j = 0; j < snakeLevel[i].length; j++){
snakeLevel[i][j].setIcon(blank);
}
}

//Add the snake
snakeLevel[head.row][head.col].setIcon(headSymbol);
snakeLevel[part1.row][part1.col].setIcon(part);
snakeLevel[part2.row][part2.col].setIcon(part);

addFood();
head.lastRow = row; head.lastCol = col-1;
part1.lastRow = row; part1.lastCol = col-2;
part2.lastRow = row; part2.lastCol = col-3;
head.next = part1; part1.next = part2;

}

This is the function that runs the game. It is called from start():

public void playSnake()
{

while(true){

if(rowMod == 0 && colMod == 0)
continue;
else if(inBounds(head.row+rowMod,head.col+colMod))
updateSnake();
else break;

try{
Thread.sleep(lvl);
}catch(InterruptedException ie){
break;
}
}

}


Like I said...I believe it is because the GUI takes to long to load before it jumps into the game loop...but maybe it is something else I am overlooking.

If something is unclear, or if you need mroe code just ask. Other than that, if you see a way i can get the retry to work, I would greatly appreciate it. I have tried everything I can think of to fix it, but nothing works.

ess
12-10-2006, 09:13 PM
Hi Mink,

I am not sure if this is going to be very helpful....as you have included the code that performs the drawing etc.

Therefore, I will just ask few questions and hopefully I could help you that way.

1- How do you re-paint the elements/objects on the screen? are you calling repaint() in order to re-draw objects on the screen?

2- I am not sure if it is a good idea to use a while loop with an argument as "true" so that code execution will continue. In order words, you should consider using a variable which would terminate the loop if it is equal to false. then set it to true...so that the code block in the while loop will execute continuously...once you have reset the elements in the screen to their default values.

Please include the code that paints the elements in the screen..so that we can help you better...instead of making vague questions.

Cheers,
Ess

Mink
12-11-2006, 03:05 AM
The problem has been solved. For those interested, the problem arose here:


/**
* Method runs the game
*/
public void playSnake()
{
while(true){

if(rowMod == 0 && colMod == 0)
continue;
else if(inBounds(head.row+rowMod,head.col+colMod))
updateSnake();
else break;

try{
Thread.sleep(lvl);
}catch(InterruptedException ie){
break;
}
}

}

Upon each iteration I put what I thought was the only Thread to sleep. In reality, there are 2 Threads at this point in the application: the main Thread, and the GUI Thread. I am putting the GUI Thread to sleep while the main thread continues along. Unfortunately, in order to solve this problem I will have to restructure the entire code.

I hope this may help anyone who is or had similar problems.