I've been learning C# along with XNA from multiple resources online for a few weeks now. I kinda feel dumb asking this because its so simple and probably very obvious. I'm making my first game "Pong". I have a menu system with a Main menu, and an in game pop up menu that pauses the game using simple enums. The game play works fine. The player in possession of the ball, launches the ball and it bounces around fine colliding with the paddles and the walls. When the ball goes of the screen left or right It adds to the score and resets the ball position. The issue I am having is with the score. It is incrementing by 2 every time a player scores. I can literally see it go 1..2, really quick then 3..4 and so on. I am at the office right now, so I do not have my code to look at so I will post what I can of my logic from memory and later I will update it if necessary.
The player class has a Boolean value scored initialized to false and is set to true when the player scores then set back to false when score is updated.
Player.cs
bool scored;
I pass the players names and set the scores to 0 when a new instance of Hud is called.
Hud.cs
string p1;
string p2;
int p1Score;
int p2Score;
public Hud(sting p1, string p2)
{
this.p1 = p1;
this.p2 = p2;
p1Score = 0;
p2Score = 0;
}
public Update(bool p1, bool p2)
{
if (p1)
{
p1Score++;
}else if(p2)
{
p2Score++;
}
}
public Draw(SpriteBatch spriteBatch)
{
//Draw Player name and score
}
Here is my play screen where everything comes together.
pScreen.cs
Player PlayerOne;
Player PlayerTwo;
Ball ball;
Hud hud;
public void Load()
{
PlayerOne = new Player(1);
PlayerTwo = new Player(2);
ball = new Ball();
hud = new Hud("PlayerOne", "PlayerTwo")
}
public void Update(GameTime gametime, KeyboardState currentKey)
{
//Bunch of update logic... blah blah blah
CheckBallCollision();
//If either player scores update the Hud
if (PlayerOne.scored || PlayerTwo.scored)
{
hud.Update(PlayeOne.scored, PlayerTwo.scored);
PlayerOne.scored = false;
PlayerTwo.scored = false;
}
PlayerOne.Update(gametime, currentKey);
PlayerTwo.Update(gametime, currentKey);
ball.Update();
}
checkBallCollision()
{
//Check for collision with paddles & top & bottom
//.....
//Check if ball is off the screen
if (ball.position.X <= 0) // Off the Left side
{
PlayerTwo.Scored = true;
//Give PlayerTwo possession of the ball
ball.WhosBall = 2;
}else if (ball.position.X >= screenWidth) // Off the Right side
{
PlayerOne.Scored = true;
//Give PlayerOne possession of the ball
ball.WhosBall = 1;
}else { PlayerOne.Scored = false; PlayerTwo.Scored = false; }
}
Of course there is a lot of code omitted. For one I don't have it all memorized and two I don't see the rest of it relative to updating the score. Thanks in advance to any help provided in finding the flaw in my logic.