Adding Movement to Pong Part 2
April 15, 2008 — JesseI’m making steady progress towards learning SDL, C++, and programming Pong. Recently, I’ve implemented a sprite class and started using dynamic memory allocation. The code for handling the game sprites has therefore improved somewhat but it still functions as before. The biggest difference is that all the basic functions of the sprite are handled locally by the class rather than by main(). This allows me to focus on the code locally without worrying about how the code will function globally. When using classes, data and methods will be encapsulated inside the class. The advantage of this is that it prevents run time errors because any code that attempts to change the class data will have to go through the class interface first. This makes it easy to track down bugs when the data gets corrupted at run time.
Instead of having a crowded main function, I can make calls to class methods using the dot operator (.) or the right arrow - > depending if I instantiate the object at compile time or at run time. For a game like Pong, it won’t matter too much since it isn’t graphic intensive but it’s a good habit to start learning how to instantiate objects dynamically to avoid overflowing the stack. Here, all I need to do is initialize a class pointer using the keyword new to request system memory. Before my program ends, I will need to restore the memory using the keyword delete. After the object is created on the heap, I can access its methods using the right arrow - > syntax.
To implement movement for the paddles, I use an array of 323 boolean types to emulate the keyboard. If an element is false, then the key is not depressed. If an element is true, then the key is depressed and I need to redraw the sprite to reflect the new position. Each key has a unique name in SDL. For example, SDLK_UP is the up arrow key. These names can be figured out by reading the SDL header files or the SDL documentation. I’m only using the W, X, up arrow, and down arrow keys.
To implement bounds for the vertical movement, I check the highest possible position by verifying that the position never becomes negative. This is because the top of the screen is actually the line . Anything with a negative y-value will be off the screen. For the lowest position, I calculate the screen height minus the paddle height. This allows the paddles to reach the edge without disappearing if they reach the screen height. As long as these bounds hold true, I can call the sprite move method to update the new sprite position.
After calculating the new positions I need to update the screen. I blank the entire screen using SDL_FillRect() and pass the parameter for black which is the color RGB 0,0,0. I do this to erase the sprites from the previous frame. If I didn’t do this, the sprites would be doubled up in the new frame.
I then tell the sprites to reposition themselves by using the draw() method. Before calling SDL Flip (), I cap the frame rate by calling SDL Delay() and delaying the game by 10ms. This allows the game to play smoothly at a particular rate: . This prevents a jagged frame rate. Finally, I call SDL Flip to update the screen and the game loop repeats again. C++ source file download.







