Sign in to follow this  
Oblivion Jedi

Need a little Help with My first Game(Pong)

Recommended Posts

I joined here years ago wanting to learn game development, done a lot of trolling. Took a few programming classes in college but never really got around to learn game development. I guess life happened. I'm actually finishing up a degree in Computer Networking Technology right now. I've always loved programming and just creating things and now I've gotten the game bug again. This time around I feel I'm actually learning and making progress.

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
[code]bool scored;[/code]

I pass the players names and set the scores to 0 when a new instance of Hud is called.

Hud.cs
[code]

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
}
[/code]


Here is my play screen where everything comes together.

pScreen.cs

[code]

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; }
}
[/code]


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.

Share this post


Link to post
Share on other sites
I think your trouble lies here

In checkBallCollision() ball.position.X is checked but not reset.

I am guessing the next pass it resets the scoring mechanism and gives the player an additional point.

Without the full code I am just guessing. if you like, I can debug quite well. Ask my Email and you can send over the project Will not take more than 10 min to track down a bug in such small program.

Share this post


Link to post
Share on other sites
Inside your method:
public void Update(GameTime gametime, KeyboardState currentKey)

[font="Arial"]there are 2 calls to the same Player.Update() method. This method checks between two boolean if they are true and adds the score to the player, however you are checking this condition twice : one for Player1 and the other one for Player2. Both add score to the player with boolean set to true.

To fix this, I would use classes as so (just an idea):

[code]
class Player
{
private:
int score;
public:
void Update(bool scored)
{
if(scored == true)
this.score++;
}
}
[/code]
[/font]

Share this post


Link to post
Share on other sites
[quote name='Napivo' timestamp='1317316676' post='4867261']I think your trouble lies here

In checkBallCollision() ball.position.X is checked but not reset.

I am guessing the next pass it resets the scoring mechanism and gives the player an additional point.

Without the full code I am just guessing. if you like, I can debug quite well. Ask my Email and you can send over the project Will not take more than 10 min to track down a bug in such small program.
[/quote]

Thank you!. You are correct. I also learned how to add a break in the debugger is VS. First pass the value of ball.position.X was < 0 and the second pass was even less so an additional point was added. I added a StopBall() Method to the ball class and call it after checking if the ball is off the left and right sides of the screen and reset the ball before it gets back around to the Update() method of pScreen.cs. Glad I got to learn a little more about debugging :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this