11 Super Basic Game Structure

Video


Table of Contents

  1. Introduction
  2. Initializing Allegro
  3. Keyboard Input
  4. Drawing Graphics
  5. Playing Sound
  6. Drawing Text
  7. Regulating FPS
  8. Enumerations and Random Numbers
  9. Bounding Box Collision Detection
  10. Planning the Game
  11. Super Basic Game Structure
  12. Wrapping up

Downloads

Download entire guide as ODT


 

Part 11 – Super Basic Game Structure

In this guide, a lot of the example code is bare-bones. If you write your program with the example code as-is, it will not be very object oriented or neat. I will outline some example classes for things like Input and Timers here.

The Game Loop

Your game’s logic will run something like this:

  1. Initialize library and game
  2. While player hasn’t quit…
    1. Check for and handle input
    2. Check for and handle collisions
    3. Draw everything to the buffer
    4. Draw everything to the screen
    5. Clear buffer
    6. Repeat
  3. Once the player has quit, clean up the resources
  4. return 0

The main objects

These should definitely be classes, and not just loose variables sitting in main. If you notice any similarities, you might use some inheritance.

Player

  • A rectangle holding the (x, y) coordinates and width/height
  • An enumeration of the current direction the player is facing
  • Current frame index
  • Walk speed
  • Score
  • Move function
  • Draw function
  • Increment frame function
  • Increment score function
  • Get/Set functions as needed

Stick

  • A rectangle holding the (x, y) coordinates and width/height
  • Generate coordinates function
  • Get/Set functions as needed

The ”Helper” objects

These keep your code cleaner if you put them in classes, but they’re not necessary for such a basic game. You can put all your image and sound files in main.cpp, but it’s not something you want to get into the practice of.These keep your code cleaner if you put them in classes, but they’re not necessary for such a basic game. You can put all your image and sound files in main.cpp, but it’s not something you want to get into the practice of.These keep your code cleaner if you put them in classes, but they’re not necessary for such a basic game. You can put all your image and sound files in main.cpp, but it’s not something you want to get into the practice of.

ImageManager

  • All the different images in the game
    • Player
    • Stick
    • Grass
    • Buffer
  • Setup function to load in all the images when the game begins
  • Destructor to destroy all files when program ends
  • Function to draw bitmaps to the buffer
  • Function to draw buffer to the screen and then clear it

SoundManager

  • All the different sounds in the game
    • Stick picked up happy victory noise
    • Background sound
    • Screen collision noise
  • Setup function to load in all the sounds when the game begins
  • Destructor to destroy all fiels when program ends
  • Function to play a sound
  • Function to stop a (or all) sounds

System

  • The screen width and height
  • Function that calls Allegro init functions
    • allegro_init()
    • install_keyboard()
    • install_timer()
    • install_sound(…)
    • set_color_depth(…)
    • set_gfx_mode(…)

© Rachel J. Morris, 2009

Print Friendly