Game Design

Now is a good time to start thinking about the nuts and bolts of our game and how we’re going to make it all work. We can think of our game as a series of things with “is a” and “has a” relationships, we’ll probably want something like this:

  • A Game class that has:
    • A game state enum
    • A method for changing game states
    • A method for restarting the game
    • A Ball class
    • A Player class
    • A playing field or level
    • A win condition that ends the game
  • Ball class that has:
    • An X,Y position
    • An X,Y velocity
    • An image
    • A method for Collision Detection
  • A Player class that has:
    • A score
    • An SDL_Rect
    • A method for getting points

We can start building out our classes and adding functionality as we go. We’ll ignore the state machine that we implemented earlier so that we can focus on getting stuff on the screen, once we have stuff on the screen we can worry about states and what they will do.

The Player class is a good place to start since it’s very simple and will allow us to get something on the screen. An SDL_Rect has a height and width as well as an x,y position, so adding the SDL_Rect means we don’t need a separate set of position variables, we can just use those. So far our player class looks like this:

[pastacode lang=”cpp” manual=”%23ifndef%20PLAYER_H%0A%23define%20PLAYER_H%0A%0A%23include%20%3CSDL2%2FSDL.h%3E%0A%0Aclass%20Player%0A%7B%0A%20%20%20%20public%3A%0A%20%20%20%20%20%20%20%20Player()%3B%0A%0A%20%20%20%20%20%20%20%20void%20AddScore(int)%3B%0A%20%20%20%20%20%20%20%20int%20GetScore()%7Breturn%20score%3B%7D%3B%0A%20%20%20%20%20%20%20%20void%20Update()%3B%0A%20%20%20%20%20%20%20%20SDL_Rect*%20GetPaddle()%7Breturn%20%26paddle%3B%7D%3B%0A%0A%20%20%20%20private%3A%0A%20%20%20%20%20%20%20%20int%20score%3B%0A%20%20%20%20%20%20%20%20SDL_Rect%20paddle%3B%0A%0A%7D%3B%0A%0A%23endif%20%2F%2F%20PLAYER_H%0A” message=”Player.h” highlight=”” provider=”manual”/]

[pastacode lang=”cpp” manual=”%23include%20%22player.h%22%0A%0APlayer%3A%3APlayer()%0A%7B%0A%20%20%20%20%20%20%20%20score%20%3D%200%3B%0A%20%20%20%20%20%20%20%20paddle.x%20%3D%200%3B%0A%20%20%20%20%20%20%20%20paddle.y%20%3D%200%3B%0A%20%20%20%20%20%20%20%20paddle.h%20%3D%20100%3B%0A%20%20%20%20%20%20%20%20paddle.w%20%3D%2015%3B%0A%7D%0A%0Avoid%20Player%3A%3AAddScore(int%20s)%0A%7B%0A%20%20%20%20score%20%2B%3D%20s%3B%0A%7D%0A%0Avoid%20Player%3A%3AUpdate()%0A%7B%0A%0A%7D%0A” message=”” highlight=”” provider=”manual”/]

We can make the player paddle object show up by instantiating a Player object called P1 and adding the following to the render sequence:

[pastacode lang=”cpp” manual=”SDL_SetRenderDrawColor(renderer%2C%200%2C%200%2C%200%2C%20255)%3B%0ASDL_RenderClear(renderer)%3B%0ASDL_SetRenderDrawColor(renderer%2C%20255%2C%200%2C%200%2C%20255)%3B%0ASDL_RenderFillRect(renderer%2C%20P1.GetPaddle())%3B%0ASDL_RenderPresent(renderer)%3B” message=”” highlight=”” provider=”manual”/]

And with that we get this:

Not terribly exciting yet, but this is a start! It’s a simple matter now to instantiate another player and add the ball class, after that we are into game logic and we’re basically done! We’ll get more done on the next entry!

Leave a Reply

Your email address will not be published. Required fields are marked *