Sample Application: Falling Apples Game


Introduction

Rules of the Game: If you lose 8 red apples and 8 yellow apples, then the game is over. If you take 30 life graphics, then you win.

Example usage

We need 4 structures: men, red apple, yellow apple and life. 

// use 4 structures

struct Apple

{

     int  Xpos;

     int  Ypos;

     BOOL Falling;

}
Apple[1000];

struct snow

{

     int  Xpos;

     int  Ypos;

     BOOL Falling;

}
snow[1000];

struct life

{

     int  Xpos;

     int  Ypos;

     BOOL Falling;

}
life[1000];

struct men

{

     int  Xpos;

     int  Ypos;

     int  Frame;

     BOOL FacingLeft;

     BOOL FacingRight;

}
men;

Now we load the bitmaps for men, apples and life.
   

HBITMAP fillbmp;

CClientDC pdc(this);

HDC MemoryDC;

fillbmp = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_1));

MemoryDC = CreateCompatibleDC(pdc);

SelectObject(MemoryDC, fillbmp);

if(men.FacingRight)

{

    switch(men.Frame)    // go men left ,right

    {

        case 0:

        BitBlt(pdc, men.Xpos, men.Ypos , 32, 32, MemoryDC, 1, 34, SRCAND);

        BitBlt(pdc, men.Xpos, men.Ypos , 32, 32, MemoryDC, 1, 1, SRCPAINT);

        break;

        case 1:

        BitBlt(pdc,men.Xpos, men.Ypos  , 32, 32, MemoryDC, 34, 34, SRCAND);

        BitBlt(pdc, men.Xpos, men.Ypos , 32, 32, MemoryDC, 34, 1, SRCPAINT);

        break;

    }

}

if(men.FacingLeft)

{

    switch(men.Frame)

   {

       case 0:

       BitBlt(pdc, men.Xpos, men.Ypos, 32, 32, MemoryDC, 1, 100, SRCAND);

       BitBlt(pdc, men.Xpos, men.Ypos, 32, 32, MemoryDC, 1, 67, SRCPAINT);

       break;

       case 1:

       BitBlt(pdc, men.Xpos, men.Ypos, 32, 32, MemoryDC, 34, 100, SRCAND);

       BitBlt(pdc, men.Xpos, men.Ypos, 32, 32, MemoryDC, 34, 67, SRCPAINT);

       break;

   }

}

for(int i = 0; i < 500; i ++)    // falling apples

{

    if(Apple[i].Falling)

    {

        BitBlt(pdc, Apple[i].Xpos, Apple[i].Ypos, 32, 32, MemoryDC, 67, 34, SRCAND);

        BitBlt(pdc, Apple[i].Xpos, Apple[i].Ypos, 32, 32, MemoryDC, 67, 1, SRCPAINT);

    }

}

}


Load the background bitmap in the resource project and draw on the dialog.

// calling background bitmap for drawing the workspace.

HINSTANCE hi=NULL;

HBITMAP hbmp;

BITMAP bmp;

CClientDC pdc(this);

HDC hdc2 = CreateCompatibleDC(pdc);

hbmp = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_2));

GetObject(hbmp, sizeof(bmp),&bmp);

SelectObject(hdc2, hbmp);

BitBlt(pdc, 0, 0, bmp.bmWidth, bmp.bmHeight, hdc2, 50, 150, SRCCOPY); //draws the image

DeleteObject(hbmp);

DeleteDC(pdc);

DeleteDC(hdc2); 

// free DC


To increment the 4 structures, we use the function Timer().

The Code

Collapse

if(losr>=8&&losy>=8){KillTimer(1);

{
    MessageBox("Oh Sorry ! You Lose " );
    exit(0);
}

GetWindowText(checklife);

CString str[]={"3","6","9","12","15","18","21","24","27","30"};

for(int q=0;q<=10;q++)
{

    if(checklife==str[q])
    {

         losy=-1;

         losr=-1;

         strtakelife="";SetWindowText(strtakelife);}

    }
if(checklife=="30")
{
    KillTimer(1);
    MessageBox("BRAVO !!!  You Win ");
    exit(0);
}

for(int j = 0; j < 500; j ++)
{
    InvalidateRect(0);
    if(snow[j].Falling)
    {

         snow[j].Ypos+=6;    //Checking yellow apples for falling in this Timer

         if(snow[j].Ypos >= 310)
         {
              losy++;

              char ah[10];

              itoa(losy,ah,10);
              strlosy=ah;

              snow[j].Falling = FALSE;

              snow[(j+ 1)].Falling = TRUE;
              InvalidateRect(0);

         }
    RECT RMen;

    RECT Rsnow;

    RECT RCalc1;

    SetRect(&RMen, men.Xpos, men.Ypos, men.Xpos + 32,men.Ypos + 32);

    SetRect(&Rsnow, snow[j].Xpos, snow[j].Ypos, snow[j].Xpos + 30,snow[j].Ypos+30); 

    IntersectRect(&RCalc1, &RMen, &Rsnow);

    if((RCalc1.right - RCalc1.left) >= 10 &&(RCalc1.right - RCalc1.left) <= 32)
    {

         takey++;

         char vh[10];

         itoa(takey,vh,10);strtakey=vh;

         snow[j].Falling = FALSE;

         InvalidateRect(0);

         snow[(j + 1)].Falling = TRUE;

     }
}

for(int k= 0; k < 500; k ++)
{
    InvalidateRect(0);
    if(life[k].Falling)
    {

         life[k].Ypos+=6;    // checking life

         if(life[k].Ypos >= 310)
         {
              loselife++;

              char kh[10];

              itoa(loselife,kh,10);
              strloslife=kh;

              life[k].Falling = FALSE;

              InvalidateRect(0);

              life[(k+ 1)].Falling = TRUE;
              InvalidateRect(0);

         }
     RECT RMen;

     RECT Rlife;

     RECT RCalc2;

     SetRect(&RMen, men.Xpos, men.Ypos, men.Xpos + 32,men.Ypos + 32); 

     SetRect(&Rlife, life[k].Xpos, life[k].Ypos, life[k].Xpos + 30,life[k].Ypos + 30);

     IntersectRect(&RCalc2, &RMen, &Rlife);
}

Congratulations! We have created an simple game with this example.

Up Next
    Ebook Download
    View all
    Learn
    View all