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.
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.