BigCoolJesus
Banned
OK, so im making a crude version of Battleship in java, and it works 100%, the AI is working, the graphics all work. Everything works that needs to work.
But i was doing some side stuff to make the game a little more enjoyable/elegant, and i came across a roadblock.
I have a mouselistener (mouse clicked) that i use for almost the whole game. Its used to take shots, place ships, start the game over, etc.
I also have a wait method i created so i can pause the game at certain points (when a shot is taken, when a ship is sunk, etc)
But i noticed that with my mouselistener, when the wait method is being called, the game waits for the specified amount of time (pegs dont show up, hits dont show up, etc), but if during that waiting time i click on a square to take a shot, even though at the time nothing shows up, after the waiting is over it immedialty renders the shot i took during the wait.
What i want to know is is there an easy way to tell the mouselistener to become inactive, or stop listening during the wait time, but then as soon as the wait is over start listening again?
(so that it wont render any actions while the game is waiting)??
Heres the code for the mouselistener and the wait method
(ignore the variables, ill highlight where the wait methods are called in the code)
--------------------------------------------------------------------------
/**
* The main worker method of the game. Uses the mouse button (when its clicked)
* to set certain things in motion, such as setting ships, taking shots, restarting the
* game, or error checking
*/
public void mousePressed(MouseEvent event)
{
if (gameState == GAME_INTRO) {
gameState = GAME_SETUP;
blueBoard.placeComputerShips();
repaint();
setup();
}
else if (gameState == GAME_SETUP) {
int row = convertY(event.getY());
int col = convertX(event.getX());
if ((event.getY() >= 430) && (event.getY() <= 730) && (event.getX() >= 310) && (event.getX() <= 610)){
if (redBoard.didHitShip(row, col) == false){
if (posClick) {
setMessage("Click any of the green boxes to place the end of the ship. The box selected will determine the position of the ship (ie. up, down, sideways.)", true);
redBoard.setFirst(row, col);
posClick = redBoard.setFirst(row, col);
repaint();
}
else {
setMessage("Click any of the green boxes to place the end of the ship. The box selected will determine the position of the ship (ie. up, down, sideways.)", true);
posClick = redBoard.setSecond(row, col);
repaint();
if (redBoard.getShipCount() == 5) {
gameState = BattleshipApp.GAME_PLAYING;
setMessage("PLAY GAME!", true);
}
}
}
else{
setMessage("Please click on an empty grid location.", true);
}
}
else{
setMessage("Please click on a square inside the red grid.", true);
}
}
else if (gameState == GAME_PLAYING) {
int row = convertYEnemy(event.getY());
int col = convertXEnemy(event.getX());
if (targetValid(event.getX(), event.getY())) {
if (blueBoard.didHitShip(row, col)) {
setMessage(playName + "'s shot.........HIT!", true);
Ship hitShip = blueBoard.applyDamage(row, col);
blueBoard.putPegInSquare(row, col, true);
if (hitShip.getHits() == hitShip.getSize()){
if (hitShip.getSize() == 5){
size = "Aircraft Carrier";
}
else if (hitShip.getSize() == 4){
size = "Battleship";
}
else if (hitShip.getSize() == 3){
if (hitShip.getType() == 2){
size = "Cruiser";
}
else{
size = "Submarine";
}
}
else if (hitShip.getSize() == 2){
size = "PT Boat";
}
String message = "HIT! .....you sank my " + size + "!";
setMessage(message, true);
paint(this.getGraphics());
wait(2000);
}
paint(this.getGraphics());
wait(1000);
}
else{
setMessage(playName + "'s shot.........MISS!", true);
blueBoard.putPegInSquare(row, col, false);
paint(this.getGraphics());
wait(1000);
}
if (!blueBoard.anyShipLeft()) {
setMessage("YOU WIN!!!!!!!", true);
won = true;
playAgain();
gameState = GAME_GAMEOVER;
}
else{
enemyPlayer.selectTarget();
row = enemyPlayer.getTargetRow();
col = enemyPlayer.getTargetColumn();
if (redBoard.didHitShip(row, col)) {
setMessage("Computers shot.........HIT!", true);
Ship hitShip = redBoard.applyDamage(row, col);
redBoard.putPegInSquare(row, col, true);
if (hitShip.getHits() == hitShip.getSize()){
if (hitShip.getSize() == 5){
size = "Aircraft Carrier";
}
else if (hitShip.getSize() == 4){
size = "Battleship";
}
else if (hitShip.getSize() == 3){
if (hitShip.getType() == 2){
size = "Cruiser";
}
else{
size = "Submarine";
}
}
else if (hitShip.getSize() == 2){
size = "PT Boat";
}
String message = "HIT! .....you sank " + playName + "'s " + size + "!";
setMessage(message, true);
paint(this.getGraphics());
wait(2000);
}
if (hitShip.getHits() == hitShip.getSize()){
enemyPlayer.undoWeights(hitShip);
}
else{
enemyPlayer.hitWeights(row, col, false);
}
paint(this.getGraphics());
wait(1000);
}
else{
setMessage("Computers shot.........MISS!", true);
redBoard.putPegInSquare(row, col, false);
enemyPlayer.missWeights(row, col);
paint(this.getGraphics());
wait(1000);
}
if (!redBoard.anyShipLeft()) {
setMessage("YOU LOSE!!!!!!!!!!", true);
playAgain();
gameState = GAME_GAMEOVER;
}
}
}
else {
setMessage("Please click on a blue square.", true);
}
}
else if (gameState == GAME_GAMEOVER){
setMessage("Please use the buttons to play again or quit the game.", true);
}
}
/**
* Used to allow for a small pause when shots have been taken
*/
public void wait(int milliseconds)
{
try
{
Thread.sleep(milliseconds);
}
catch (Exception e)
{
}
}
So, again, i want the mouselistener to become inactive whenever the wait method is called in the code above............
But i was doing some side stuff to make the game a little more enjoyable/elegant, and i came across a roadblock.
I have a mouselistener (mouse clicked) that i use for almost the whole game. Its used to take shots, place ships, start the game over, etc.
I also have a wait method i created so i can pause the game at certain points (when a shot is taken, when a ship is sunk, etc)
But i noticed that with my mouselistener, when the wait method is being called, the game waits for the specified amount of time (pegs dont show up, hits dont show up, etc), but if during that waiting time i click on a square to take a shot, even though at the time nothing shows up, after the waiting is over it immedialty renders the shot i took during the wait.
What i want to know is is there an easy way to tell the mouselistener to become inactive, or stop listening during the wait time, but then as soon as the wait is over start listening again?
(so that it wont render any actions while the game is waiting)??
Heres the code for the mouselistener and the wait method
(ignore the variables, ill highlight where the wait methods are called in the code)
--------------------------------------------------------------------------
/**
* The main worker method of the game. Uses the mouse button (when its clicked)
* to set certain things in motion, such as setting ships, taking shots, restarting the
* game, or error checking
*/
public void mousePressed(MouseEvent event)
{
if (gameState == GAME_INTRO) {
gameState = GAME_SETUP;
blueBoard.placeComputerShips();
repaint();
setup();
}
else if (gameState == GAME_SETUP) {
int row = convertY(event.getY());
int col = convertX(event.getX());
if ((event.getY() >= 430) && (event.getY() <= 730) && (event.getX() >= 310) && (event.getX() <= 610)){
if (redBoard.didHitShip(row, col) == false){
if (posClick) {
setMessage("Click any of the green boxes to place the end of the ship. The box selected will determine the position of the ship (ie. up, down, sideways.)", true);
redBoard.setFirst(row, col);
posClick = redBoard.setFirst(row, col);
repaint();
}
else {
setMessage("Click any of the green boxes to place the end of the ship. The box selected will determine the position of the ship (ie. up, down, sideways.)", true);
posClick = redBoard.setSecond(row, col);
repaint();
if (redBoard.getShipCount() == 5) {
gameState = BattleshipApp.GAME_PLAYING;
setMessage("PLAY GAME!", true);
}
}
}
else{
setMessage("Please click on an empty grid location.", true);
}
}
else{
setMessage("Please click on a square inside the red grid.", true);
}
}
else if (gameState == GAME_PLAYING) {
int row = convertYEnemy(event.getY());
int col = convertXEnemy(event.getX());
if (targetValid(event.getX(), event.getY())) {
if (blueBoard.didHitShip(row, col)) {
setMessage(playName + "'s shot.........HIT!", true);
Ship hitShip = blueBoard.applyDamage(row, col);
blueBoard.putPegInSquare(row, col, true);
if (hitShip.getHits() == hitShip.getSize()){
if (hitShip.getSize() == 5){
size = "Aircraft Carrier";
}
else if (hitShip.getSize() == 4){
size = "Battleship";
}
else if (hitShip.getSize() == 3){
if (hitShip.getType() == 2){
size = "Cruiser";
}
else{
size = "Submarine";
}
}
else if (hitShip.getSize() == 2){
size = "PT Boat";
}
String message = "HIT! .....you sank my " + size + "!";
setMessage(message, true);
paint(this.getGraphics());
wait(2000);
}
paint(this.getGraphics());
wait(1000);
}
else{
setMessage(playName + "'s shot.........MISS!", true);
blueBoard.putPegInSquare(row, col, false);
paint(this.getGraphics());
wait(1000);
}
if (!blueBoard.anyShipLeft()) {
setMessage("YOU WIN!!!!!!!", true);
won = true;
playAgain();
gameState = GAME_GAMEOVER;
}
else{
enemyPlayer.selectTarget();
row = enemyPlayer.getTargetRow();
col = enemyPlayer.getTargetColumn();
if (redBoard.didHitShip(row, col)) {
setMessage("Computers shot.........HIT!", true);
Ship hitShip = redBoard.applyDamage(row, col);
redBoard.putPegInSquare(row, col, true);
if (hitShip.getHits() == hitShip.getSize()){
if (hitShip.getSize() == 5){
size = "Aircraft Carrier";
}
else if (hitShip.getSize() == 4){
size = "Battleship";
}
else if (hitShip.getSize() == 3){
if (hitShip.getType() == 2){
size = "Cruiser";
}
else{
size = "Submarine";
}
}
else if (hitShip.getSize() == 2){
size = "PT Boat";
}
String message = "HIT! .....you sank " + playName + "'s " + size + "!";
setMessage(message, true);
paint(this.getGraphics());
wait(2000);
}
if (hitShip.getHits() == hitShip.getSize()){
enemyPlayer.undoWeights(hitShip);
}
else{
enemyPlayer.hitWeights(row, col, false);
}
paint(this.getGraphics());
wait(1000);
}
else{
setMessage("Computers shot.........MISS!", true);
redBoard.putPegInSquare(row, col, false);
enemyPlayer.missWeights(row, col);
paint(this.getGraphics());
wait(1000);
}
if (!redBoard.anyShipLeft()) {
setMessage("YOU LOSE!!!!!!!!!!", true);
playAgain();
gameState = GAME_GAMEOVER;
}
}
}
else {
setMessage("Please click on a blue square.", true);
}
}
else if (gameState == GAME_GAMEOVER){
setMessage("Please use the buttons to play again or quit the game.", true);
}
}
/**
* Used to allow for a small pause when shots have been taken
*/
public void wait(int milliseconds)
{
try
{
Thread.sleep(milliseconds);
}
catch (Exception e)
{
}
}
So, again, i want the mouselistener to become inactive whenever the wait method is called in the code above............