Hi,
I'm currently creating a pool game which is nearly complete but I want to have it as a 2 player (human vs human) game. All that I want the program to perform is that when the game begins then the two players are asked to enter their names (I have got this working) and when the game begins the first players name is displayed on the screen (this is also working, but needs adjusting). The issue I am having though is making the names alternate after a shot is missed. I am not worried at this point about assigning a specific colour to a player or awarding two shots when the white is potted. I just want the names to change when a ball is not potted or the cue ball is potted.
I have declared a first and second player variable at the start of my applet class:
String player1, player2;
Within my start() method I have added the code for the players to enter their names:
player1 = JOptionPane.showInputDialog("Please enter the name of Player 1: ");
player2 = JOptionPane.showInputDialog("Please enter the name of Player 2: ");
For displaying the first players name to the screen I have written a method called playersTurn() in which I have the following code:
public void playerTurn(Ball b[], Graphics g)
{
for(int i = 0; i < b.length; i++)
if(b[i].isPotted() != true)
{
String firstPlayer = player1;
Font font = new Font("Serif", Font.BOLD, 18);
g.setFont(font);
g.setColor(Color.BLACK);
g.drawString(firstPlayer, getWidth() - 101, 26);
g.setColor(Color.BLUE);
g.drawString(firstPlayer, getWidth() - 100, 25);
}
else{
String secondPlayer = player2;
Font font = new Font("Serif", Font.BOLD, 18);
g.setFont(font);
g.setColor(Color.BLACK);
g.drawString(secondPlayer, getWidth() - 101, 26);
g.setColor(Color.BLUE);
g.drawString(secondPlayer, getWidth() - 100, 25);
}
}
I understand that this is probably a way off the mark so any help I can get would be much appreciated. I have a boolean variable in my Ball class called isPotted() initially set to false this is changed to true in my ballPotted() method in my Vector class as follows:
private void ballPotted(Ball b[], Ball p[])
{
for(int i = 0; i < b.length; i++)
{
for(int j = 0; j < p.length; j ++)
{
double yPosDifference = p[j].getY() - b[i].getY();
double xPosDifference = p[j].getX() - b[i].getX();
double pot = Ball.getRadius()*1.5 + Ball.getRadius()*1.5;
double centres = Math.sqrt((yPosDifference * yPosDifference) + (xPosDifference * xPosDifference));
if(centres <= pot && (b[i].getVelocity()).magnitude() > 0)
{
System.out.println("Pot has occurred for ball at position (" + b[i].getX() + ", " +
b[i].getY() + ") and pocket position (" + p[j].getX() + ", " + p[j].getY() + ")");
performRemoval(b[i], p[j]);
b[i].setPotted(true);
if(b[0].isPotted())
{
b[0].setDeltaX(0);
b[0].setDeltaY(0);
b[0].setX(200);
b[0].setY(425);
}
}
}
}
blackPotted(b);
}
I am at a bit of a loss at the moment so any help or advice as to how I should go about this would be greatly welcomed. Thanks in advance and if you have any questions or would like to see more code please let me know.