Click here to Skip to main content
15,885,216 members
Articles / Multimedia / DirectX
Article

Invasion - A computer game using DirectDraw

Rate me:
Please Sign up or sign in to vote.
4.92/5 (30 votes)
4 Jun 20022 min read 379.6K   7.6K   76   86
This "Space Invaders"-like game was originally writen in DirectX 6, but I've changed some things so that you can use it with the latest version of the DirecX SDK. It uses just the Windows API and DirectX library (no MFC here). Have fun!

Sample Image - Invasion1.jpg Sample Image - Invasion2.jpg

This was my first attempt writing a computer game using the DirectX library. It was originally written for DirectX 6 library, but I've made some changes that made it work with the latest SDK. The code is based on the Windows API, without any reference to the MFC library. The only thing you need to compile this is VC++ and DirectX SDK, which can be found at http://www.microsoft.com/directx.

All the processing is done when no messages are available in the application message queue, by calling the UpdateFrame() function. As you see in the code, I haven't used many classes, and almost all the code is based on function calls. The only classes that are available are:

Alpha: this class is used as a reference to draw characters to the screen. This allow us to use a bitmap to draw the letters to the screen (so that the user doesn't need to install extra fonts in the system).

Extra: Defines an "Extra" element in the screen, that can be an Ammo box, an Bonus Box, a Weapon Advance Box or a Shield Charge.

Ovni: This class represents the UFOs that are going to be shot! Just to explain the name of the class... Ovni means UFO in Portuguese. Since I'm a Brazilian and a Portuguese speaker, there are some things in the code that are with my local language (don't worry, the comments are all English :o) )

Bullet: This represents the bullets that fly around the galactic battlefield.

Each one of the classes has a 'built-in' linked list that will be used a lot in the code. Each one of the classes have a common Draw function the is used to draw the specific object on the screen in its current state.

Almost everything in the code is commented, but if you have any trouble just mail me.

Have fun!

Updates

4 June 2002: I´ve removed a memory leak (directdraw interface was not being released) and have improved the surface creation function (winmain.cpp, line 3190). Now I try to create every surface on VIDEOMEMORY. If the function returns a DDERR_OUTOFMEMORY, I try to recreate it on SYSTEMMEMORY (winmain.cpp, line 3242).

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Brazil Brazil
Mauricio Ritter lives in Brazil, in the city of Porto Alegre. He is working with software development for about 8 years, and most of his work was done at a bank, within a home and office banking system.
Mauricio also holds MCSD, MCSE, MCDBA, MCAD and MCT Microsoft certifications and work as a trainer/consultant in some MS CTEC in his city.
Mauricio also works in his own programming site, aimed to Brazilian Developers: http://www.dotnetmaniacs.com.br

In his spare time he studys korean language...

Comments and Discussions

 
GeneralRe: I wish more people put full games on here.. Pin
Mauricio Ritter10-Mar-02 23:32
Mauricio Ritter10-Mar-02 23:32 
GeneralI was wondering.. Pin
LiquidKnight26-Mar-02 11:01
LiquidKnight26-Mar-02 11:01 
GeneralRe: I was wondering.. Pin
Mauricio Ritter26-Mar-02 16:01
Mauricio Ritter26-Mar-02 16:01 
GeneralRe: I was wondering.. Pin
Selevercin25-May-02 17:16
Selevercin25-May-02 17:16 
GeneralRe: I was wondering.. Pin
Mauricio Ritter26-May-02 1:52
Mauricio Ritter26-May-02 1:52 
GeneralRe: I was wondering.. Pin
Selevercin1-Jun-02 12:23
Selevercin1-Jun-02 12:23 
GeneralRe: I wish more people put full games on here.. Pin
Anonymous1-Sep-02 7:42
Anonymous1-Sep-02 7:42 
GeneralMore Fun with Invasion Pin
Matt Philmon21-Feb-02 3:34
Matt Philmon21-Feb-02 3:34 
First of all, Mauricio, thank you SO SO much. This is a WONDERFUL example and exactly the kind of thing I was looking for to start learning DirectX.

Second of all, I've been having way too much fun with the code. Roll eyes | :rolleyes:

I'm working on some new screens, effects, weapons, configuration options, etc to help me better learn how the game works and how a game in general is put together. As I've gone through the code I've found quite a few things you can play with for more interesting effects. For instance, I've currently got a list of defines at the top of winmain.cpp:
#define STARTUP_WEAPON_MAX      0
#define STARTUP_WEAPON          0
#define STARTUP_SHIELD          50
#define STARTUP_LEVEL           0
#define STARTUP_SHIPSTATE       0
#define STARTUP_LASERAMMO       100
#define STARTUP_PHOTONAMMO      0
#define STARTUP_SCORE           0
#define SHIP_DAMAGE_DECREMENT           20
#define AMMO_LASER_INCREMENT            40
#define AMMO_PHOTON_INCREMENT           25
#define SHIELD_INCREMENT        10
#define MAX_SHIELD          50
#define SCORE_INCREMENT         100
#define LASER_DELAY         400
#define PHOTON_DELAY            680


The descriptions are easy enough to understand what they adjust. Still, all you really need to play with (in winmain.cpp) are the following:

In MainWndproc go to "case VK_RETURN:" and a little further down the comment: "// Reset the score counter, level, ship state and ammo" There's your starting variables.

In DrawShip check out:
if (iShipState == SHIP_OK &&
    pExtra != NULL)
{
    int iType;

    // check if we hit an extra
    // if so, select the kinf of extra
    iType = CheckHitExtra();
    switch (iType)
    {
    case 1:
        iPhotonAmmo += AMMO_PHOTON_INCREMENT;
        if (iPhotonAmmo > 999)
            iPhotonAmmo = 999;
        SndObjStop(hsoGetExtra);
        SndObjPlay(hsoGetExtra, NULL);
        break;
    case 2:
        iMaxWeapon++;
        if (iMaxWeapon > MAX_WEAPONS)
            iMaxWeapon = MAX_WEAPONS;
        else
            iWeapon = iMaxWeapon;
        SndObjStop(hsoGetExtra);
        SndObjPlay(hsoGetExtra, NULL);
        break;
    case 3:
        score += SCORE_INCREMENT;
        SndObjStop(hsoGetExtra);
        SndObjPlay(hsoGetExtra, NULL);
        break;
    case 4:
        iLaserAmmo += AMMO_LASER_INCREMENT;
        if (iLaserAmmo > 999)
            iLaserAmmo = 999;
        SndObjStop(hsoGetExtra);
        SndObjPlay(hsoGetExtra, NULL);
        break;
    case 5:
        iShield += SHIELD_INCREMENT;
        if (iShield > MAX_SHIELD)
            iShield = MAX_SHIELD;
        SndObjStop(hsoGetExtra);
        SndObjPlay(hsoGetExtra, NULL);
        break;

    }

}


Also, back in MainWndproc, look for "case VK_SPACE:" and move to the bottom of that case and you'll see:
if (iWeapon == 0)
    SetTimer(hWnd,1,400,NULL);
else
    SetTimer(hWnd,1,680,NULL);


Mine looks like:
if (iWeapon == 0)
    SetTimer(hWnd,1,LASER_DELAY,NULL);
else
    SetTimer(hWnd,1,PHOTON_DELAY,NULL);


which will let you tweak the firing delays.

Have fun and great game!

Matt
GeneralRe: More Fun with Invasion Pin
Mauricio Ritter21-Feb-02 4:18
Mauricio Ritter21-Feb-02 4:18 
GeneralSuggestion Pin
Nish Nishant12-Jan-02 19:29
sitebuilderNish Nishant12-Jan-02 19:29 
GeneralRe: Suggestion Pin
Mauricio Ritter13-Jan-02 1:11
Mauricio Ritter13-Jan-02 1:11 
GeneralRe: Suggestion Pin
Nish Nishant13-Jan-02 4:19
sitebuilderNish Nishant13-Jan-02 4:19 
GeneralRe: Suggestion Pin
Nish Nishant20-Jan-02 18:22
sitebuilderNish Nishant20-Jan-02 18:22 
GeneralPretty cool ! Pin
Braulio Dez9-Jan-02 21:52
Braulio Dez9-Jan-02 21:52 
GeneralThanks! Pin
Cathy7-Jan-02 9:50
Cathy7-Jan-02 9:50 
GeneralRe: Thanks! Pin
Mauricio Ritter7-Jan-02 10:37
Mauricio Ritter7-Jan-02 10:37 
GeneralRe: Thanks! Pin
Christian Graus9-Jan-02 15:55
protectorChristian Graus9-Jan-02 15:55 
GeneralRe: Thanks! Pin
Mauricio Ritter9-Jan-02 23:03
Mauricio Ritter9-Jan-02 23:03 
GeneralRe: Thanks! Pin
Michael P Butler9-Jan-02 23:36
Michael P Butler9-Jan-02 23:36 
GeneralRe: Thanks! Pin
Paul Watson8-Jan-02 6:34
sitebuilderPaul Watson8-Jan-02 6:34 
GeneralRe: Thanks! Pin
Cathy8-Jan-02 7:42
Cathy8-Jan-02 7:42 
GeneralRe: Thanks! Pin
Nish Nishant12-Jan-02 19:34
sitebuilderNish Nishant12-Jan-02 19:34 
GeneralRe: Thanks! Pin
Cathy14-Jan-02 6:23
Cathy14-Jan-02 6:23 
GeneralRe: Thanks! Pin
24-Jan-02 10:14
suss24-Jan-02 10:14 
GeneralRe: Thanks! Pin
Zac Howland11-Jun-02 6:15
Zac Howland11-Jun-02 6:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.