Alex Russell's Dos Game Programming in C for Beginners

Introduction . Chapter 1 . Chapter 2 . Chapter 2.1 . Chapter 3 . Chapter 4 . Chapter 5 . Chapter 6

Chapter 6

Break Out

In this chapter we are going to develop a simple game of Break Out. There will be two sections of bricks at the top of the screen, a paddle at the bottom, and the score and number of balls remaining at the very top. We will support the mouse, and joystick to move the paddle, and allow the user to select the joystick at the start of the game. A plain background will be used. The bricks will be rectangular bitmaps, the ball a small sprite. The ball will speed up every time it hits 5 bricks. The bricks in each row will be worth one point more than bricks in the previous row. We will not be supporting any fancy options like laser paddles. The ball will bounce at different angles depending on where it hits the paddle.

Code we will need:

  • Load sprites into memory
  • Draw paddle
  • Draw ball
  • Draw initial screen
  • Draw score
  • Erase hit brick
  • Move ball
  • Move paddle
  • Detect collision with brick
  • Bounce off paddle with varying angles.
  • Time everything
  • Art we will need:

  • Paddle
  • Ball
  • Bricks
  • Background (?)
  • Utilities we will need:

  • Drawing program
  • Convert art to m13 and rle sprites
  • Get palette from art
  • How to proceed

    When doing a large game it is common to get started with simple `place holder' artwork for testing. Replacing art is easy. Sound and music, which we do not cover, is almost always done with a sound library. Then the actual game programming is done. Utilities, and small test programs are done first. You want to get the various parts working well on their own before adding them to the game. This simplifies debugging immensely. If you try to program the whole thing at once and it doesn't work (I can guarantee it won't work the first time) it is very difficult to tell which part is failing.

    There is no one right way to write a game, but here is an idea of one way to start.

  • Get basic sprite primitives working.
  • Get user input (mouse, keyboard, joystick) working.
  • Get art ready, and plan screen layout.
  • Get paddle control working, including joystick calibration.
  • Get drawing and erasing bricks working.
  • Get bouncing the ball off the paddle on an empty screen working.
  • Get hitting bricks working.
  • Get scoring working.
  • Always put a fair amount of effort into designing an easy to use interface for your game. If the player cannot use the controls effortlessly the game will fail.

    This is the complete source code for a simple break out game

    Click here for chap6.zip (31 KB)

    Chapter 6 Exercises

    1. What are two ways to change the speed that a sprite moves across the screen.

    2. In the Chap6.c source code an array holds information on which bricks have been hit, and their value. Why does this array include information for the part of the screen in between the sections of bricks?

    3. Change chap6.c so that all the global variables are contained in a structure. Why would you want to do this?

    4. Make a title screen appear at the start of the game. It stays on screen until a button or key is pressed, or 15 seconds elapses.

    5. Load up a dark coloured patterned background at game startup.

    6. What code do you change to make the joystick more responsive?

    7. Make the game drop a second ball if 20 bricks are hit in a row. You only lose a `life/ball' if there are no active balls.

    8. Change chap6.c so that the ball is moved by changing its direction, as an angle, instead of using dx, dy. This will require using trigonometry. Use a double to store the angle. Is this faster or slower? Are there any advantages to this method?

    9. How could you speed up calculating sine and cosine in a game?

    10. Change the game so that the paddle can be moved up a few paddle widths, as well as back and forth.

    Click here for chap6.zip (31 KB)

    Introduction . Chapter 1 . Chapter 2 . Chapter 2.1 . Chapter 3 . Chapter 4 . Chapter 5 . Chapter 6

    Copyright 1998 (c), Alex Russell, All rights reserved