pause game

does anybody hav ideas on how to pause a game. i cant figure it out. thx.
[80 byte] By [stephensk8sa] at [2007-11-27 2:45:11]
# 1
Thread.sleep(insertTimeAmountHere); //you need to catch an InterruptedException here
CaptainMorgan08a at 2007-7-12 3:12:31 > top of Java-index,Other Topics,Java Game Development...
# 2
i need a way to get input. as in, if you type p it pauses the game and typing p again starts it again.
stephensk8sa at 2007-7-12 3:12:31 > top of Java-index,Other Topics,Java Game Development...
# 3
but i will try that
stephensk8sa at 2007-7-12 3:12:31 > top of Java-index,Other Topics,Java Game Development...
# 4
> i need a way to get input. as in, if you type p it> pauses the game and typing p again starts it again.It's hard to say how to do that without seeing how you designed your game.
CaptainMorgan08a at 2007-7-12 3:12:31 > top of Java-index,Other Topics,Java Game Development...
# 5

i can post the code here. if u want to run it, then u will need the png files so just tell me if u do.

import java.applet.*;

import java.awt.*;

import java.util.*;

import java.net.*;

//import javax.swing.JOptionPane;

public class TopGun6 extends Applet implements Runnable

{

private Image backImage;

private int backx;

private int backy;

private int backxspeed;

private int backyspeed;

private Image backImage2;

private int backgroundx;

private int backgroundy;

private int backgroundxspeed;

private int backgroundyspeed;

private boolean firing;

private int i;

private int i2;

private long lastTime;

//private int current;

private int y_speed;

private int x_speed;

private int x_pos;

private int y_pos;

private int[] enemyOnexspeed = new int[1];

private int[] enemyOnexpos = new int[1];

private int[] enemyOneypos = new int[1];

private int[] enemyTwoxspeed = new int[1];

private int[] enemyTwoxpos = new int[1];

private int[] enemyTwoypos = new int[1];

private boolean active;

private int width; //= 1000;//getSize().height;

private int height;

//private boolean track = true;

//private int bulletx;

//private int bullety;

//private int byspeed;

//private int bxspeed;

// = 1100;//getSize().width;

private Random rnd;

private Image [] jets;

private Image [] classics;

private Image [] display;

private Image [] bullets;

private Image bank;

private Image dbImage;

private Graphics dbg;

private Image Paddle;

//private Graphics padd;

private final int maxbullets = 200;

private final int bulletSpacing = 150;

private Image bullet;

private Image plane;

private AudioClip blastSound;

private AudioClip debrisSound;

private AudioClip explodeSound;

private AudioClip fireSound;

private AudioClip passingSound;

private AudioClip targetedSound;

private int countForArrays = 0;

private Image[] orangeBullet = new Image[maxbullets];

private int[] bxspeed = new int[maxbullets];

private int[] byspeed = new int[maxbullets];

private int[] bulletx = new int[maxbullets];

private int[] bullety = new int[maxbullets];

private int keepTrackOfLastCountForArrays = 0;

Image[] enemies = new Image[2];

public TopGun6()

{

i = 0;

i2 = 0;

active = true;

//bulletx = x_pos + 60;// = 100;

//bullety = y_pos + 50;// = 100;

rnd = new Random ();

jets = new Image[6];

//bullets = new Image[500];

backx = 0;

backy = -2600;

backgroundx = 0;

backgroundy = -6008;

backxspeed = 0;

backyspeed = 2;

backgroundxspeed = 0;

backgroundyspeed = 2;

}

public void init()

{

backImage = getImage (getCodeBase(), "map2.png");

backImage2 = getImage (getCodeBase(), "map2.png");

setBackground(Color.black);

for (int count = 0; count < maxbullets; count++)

{

orangeBullet[count] = getImage (getCodeBase(), "bullet.png");

bullety[count] = -100;

}

plane = getImage (getCodeBase(), "f-16.png");

jets[0] = getImage (getCodeBase(), "f-16.png");

jets[1] = getImage (getCodeBase(), "su-27.png");

jets[2] = getImage (getCodeBase(), "f-22.png");

jets[3] = getImage (getCodeBase(), "f-35.png");

jets[4] = getImage (getCodeBase(), "f-117.png");

jets[5] = getImage (getCodeBase(), "mb339.png");

enemies[0] = getImage (getCodeBase(), "go-229.png");

enemies[1] = getImage (getCodeBase(), "mig-19.png");

height = getSize().height;

width = getSize().width;

x_pos = 350;

y_pos = 600;

setEnemySpeedsAndCoordinates();

}

public void loadSounds()

{

// Load all sound clips by playing and immediately stopping them.

try {

blastSound= getAudioClip(new URL("fire.au"));

debrisSound= getAudioClip(new URL("debris.au"));

explodeSound = getAudioClip(new URL( "explode.au"));

fireSound= getAudioClip(new URL("fire.au"));

passingSound = getAudioClip(new URL("passing.au"));

targetedSound = getAudioClip(new URL("targeted.au"));

}

catch (MalformedURLException e) {}

blastSound.play();blastSound.stop();

debrisSound.play();debrisSound.stop();

explodeSound.play(); explodeSound.stop();

fireSound.play();fireSound.stop();

passingSound.play(); passingSound.stop();

targetedSound.play(); targetedSound.stop();

}

public void start()

{

Thread th = new Thread (this);

th.start ();

}

public boolean keyDown (Event e, int key)

{

if (key == 120)

{

if (i > 5)

i = 0;

plane = jets[i];

i++;

i2 = i - 1;

if (i > 5)

i = 0;

}

if (key == 122)

{

if (i2 < 0)

i2 = 6;

i2--;

plane = jets[i2];

i = i2 + 1;

if (i2 < 0)

i2 = 6;

}

if (key == 32)

{

//boolean checked in the run method in FireABullet(). if false it doesnt fire, if true it does.

firing=true;

keepTrackOfLastCountForArrays = countForArrays;

}

if (key == 109)

{

blastSound.play();

}

if (key == Event.UP)

{

y_speed = -5;

}

if (key == Event.DOWN)

{

y_speed = +5;

}

if (key == Event.RIGHT)

{

x_speed = +5;

}

if (key == Event.LEFT)

{

x_speed = -5;

}

return true;

}

public void FireABullet()

{

int i;

if (System.currentTimeMillis() - lastTime > bulletSpacing)

{

for (i=0;i<maxbullets;i++)

{

if (bullety[i] ><= 0)

{

bullety[i] = y_pos;

bulletx[i] = x_pos+(plane.getWidth(this)/2 - orangeBullet[0].getWidth(this)/2);

byspeed[i] = -10;

lastTime = System.currentTimeMillis();

break;

}

}

}

}

public boolean keyUp(Event e, int key)

{

if (key==32) {firing=false;}

if (key == Event.UP)

{

y_speed = 0;

}

if (key == Event.DOWN)

{

y_speed = 0;

}

if (key == Event.RIGHT)

{

x_speed = 0;

}

if (key == Event.LEFT)

{

x_speed = 0;

}

return true;

}

public void run ()

{

Thread.currentThread().setPriority(Thread.MIN_PRIORITY);

while (true)

{

if (backy >= height)

{

backy = -6008;

}

else if (backgroundy >= height)

{

backgroundy = -6008;

}

backx += backxspeed;

backy += backyspeed;

backgroundx += backgroundxspeed;

backgroundy += backgroundyspeed;

if (firing) {FireABullet();}

checkSpeed();

keepPlanesInBounds();

repaint();

try

{

Thread.sleep (15);

}

catch (InterruptedException ex)

{

}

Thread.currentThread().setPriority(Thread.MAX_PRIORITY);

}

}

public void setEnemySpeedsAndCoordinates()

{

enemyOnexspeed[0] = +5;

enemyOnexpos[0] = 400;

enemyOneypos[0] = 50;

enemyTwoxspeed[0] = -5;

enemyTwoxpos[0] = 300;

enemyTwoypos[0] = 50;

}

public void checkSpeed()

{

for (int count = 0; count < maxbullets; count++)

{

if (bullety[count] > -10)

{

bulletx[count] += bxspeed[count];

bullety[count] += byspeed[count];

}

}

x_pos += x_speed;

y_pos += y_speed;

enemyOnexpos[0] += enemyOnexspeed[0];

enemyTwoxpos[0] += enemyTwoxspeed[0];

}

public void keepPlanesInBounds()

{

if (x_pos < 0)

{

x_pos = +0;

}

else if (x_pos + 125 > width)

{

x_pos = width - 125;

}

else if (y_pos < 0)

{

y_pos = +0;

}

else if (y_pos + 250 > height)

{

y_pos = height - 250;

}

//first enemy plane

if (enemyOnexpos[0] < 10)

{

enemyOnexpos[0] = 10;

enemyOnexspeed[0] = +5;

}

else if (enemyOnexpos[0] > (300 - enemies[0].getWidth(this)))

{

enemyOnexpos[0] = (300 - enemies[0].getWidth(this));

enemyOnexspeed[0] = -5;

}

//second enemy plane

if (enemyTwoxpos[0] < 500)

{

enemyTwoxpos[0] = 500;

enemyTwoxspeed[0] = +5;

}

else if (enemyTwoxpos[0] > (790 - enemies[1].getWidth(this)))

{

enemyTwoxpos[0] = (790 - enemies[1].getWidth(this));

enemyTwoxspeed[0] = -5;

}

}

public void update(Graphics g)

{

if (dbImage == null)

{

dbImage = createImage (this.getSize().width, this.getSize().height);

dbg = dbImage.getGraphics ();

}

dbg.setColor (getBackground ());

dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);

dbg.setColor (getForeground());

paint (dbg);

g.drawImage (dbImage, 0, 0, this);

}

public void paint (Graphics g)

{

g.drawImage (backImage, backx, backy, this);

g.drawImage(backImage2, backgroundx, backgroundy, this);

g.setColor (Color.blue);

for (int count = 0; count < maxbullets; count++)

{

g.drawImage(orangeBullet[count], bulletx[count], bullety[count], this);

}

g.drawImage (enemies[0], enemyOnexpos[0], enemyOneypos[0], this);

g.drawImage (enemies[1], enemyTwoxpos[0], enemyTwoypos[0], this);

g.drawImage (plane, x_pos, y_pos, this);

}

}

stephensk8sa at 2007-7-12 3:12:31 > top of Java-index,Other Topics,Java Game Development...
# 6

You could have some sort of flag that lets the game know when it's paused.

private boolean isPaused = false;

Then your run() method could look like this.

public void run()

{

while(isRunning)

{

if(!isPaused)

{

// do game stuff

}

Thread.sleep(someAmount); //catch the exception here

}

}

You would set/unset the isPaused flag with your KeyListener. Get it?

CaptainMorgan08a at 2007-7-12 3:12:31 > top of Java-index,Other Topics,Java Game Development...
# 7
yes i think so. but y would i still need to use the sleep method? isnt that pointless?
stephensk8sa at 2007-7-12 3:12:31 > top of Java-index,Other Topics,Java Game Development...
# 8
> yes i think so. but y would i still need to use the> sleep method? isnt that pointless?No, the computer enjoys the 15 ms rest. ;-)
CaptainMorgan08a at 2007-7-12 3:12:31 > top of Java-index,Other Topics,Java Game Development...
# 9
hahaha. ya i dont think im gonna let it enjoy that time cuz last time i used the sleep method to try to pause it the bullets jumped about a hundred pixels after it was done.
stephensk8sa at 2007-7-12 3:12:31 > top of Java-index,Other Topics,Java Game Development...
# 10
actually nvm im retarded. i didnt even notice i had the try catch already in ther. haha
stephensk8sa at 2007-7-12 3:12:31 > top of Java-index,Other Topics,Java Game Development...
# 11

omg thank u so much captainmorgan. those java tutorials were really confusing. and ur code worked! i cant believe the little errors i was making when i had something similar to that. i was changing the boolean in the while statement instead of the if statement and my keyListener method was a little messed up.

thanks a lot again

stephensk8sa at 2007-7-12 3:12:31 > top of Java-index,Other Topics,Java Game Development...