logo.jpg (7978 bytes)

OO Game Programming tutorial - Part 11



Scoring goals

Checking if a goal is scored should be easy if you've been following. The question is what do we do when a goal is scored?

First the player who scored needs to gain a point, we just add 1 to their score attribute.

player.score++

Secondly the ball needs to be reset and the player's should be given a little time to get ready. This is where our gameState variable will come in handy. We'll set it to GETREADY that was defined earlier for this purpose.

So we can test when to change from GETREADY back to PLAYING we need to use a timer which will be handled with a new global name gameStateTimer. The system function timeGetTime() is used to set it to the current time in milliseconds and add a delay for the time when we change states. 2000 milliseconds (2 seconds) for the delay should be fine.

When the game state is GETREADY I've displayed some text so the player's know what's going on.

In Game_Main() we check for the game state being GETREADY and test whether it is time to change back to PLAYING by comparing the timer with the current time.

One more thing is make sure the ball only moves when the game state is PLAYING.

Wow, that was a big section!

Project: Pong11.zip

 

Next page

Back to main page