You've got an ice cold Tab sitting next to you as you hack away on your Pong clone while blasting Wham! on your Walkman. You finally got a ball bouncing back and forth across your terminal window (see my previous post) and you're wondering "what's next?"
Let's split our Pong game into two parts: the playing field and the score display. We'll plan for the playing field to be at the top portion of the screen, and the player's current score will be displayed at the bottom. This makes sense, but how do we do it?
A simple implementation might be to just move the current drawing position to a location on the bottom of the screen and print out the current score value (using
mvwprintw), but that would require you to bounce the ball before it overlaps the score display. While that would work, it's less than ideal for a couple of reasons:
You can't update the score display independently from the main playing field. Each time the playing field needs to be redrawn you need to also redraw the score display, even if it hasn't changed.
More importantly, your code is now littered with confusing collision detection logic. No longer are you checking if
y >= max_y, you now need to see if
(y + score_height) >= max_y.
Fortunately, ncurses provides the ability to split these two concerns into separate windows, each updated independently. It might be a little more work to manage multiple windows in your program, but it's the right thing to do. In the end, you envision splitting the windows into something like this:
Let's see how we would implement that.