( Index )

Brief Information about the Jan '03 CSIG Meetings

Pick Up Sticks -- A Graphical Game

Normal Game Debug Screen

The subject for this month is a graphical game program written in Microsoft Visual C++ using Microsoft Foundation Classes, MFC. This game is suitable for small children ages 3 to 6 years. When the game starts, 25 colored sticks are presented on the screen. A mouse click "picks up" a stick and it disappears. When all 25 are picked up, a smiley face appears.

The program uses the Windows and MFC Graphical routines. It demonstrates a number of concepts including device contexts, drawing lines of various colors, brushes, graphical searching and 2 dimensional calculations. One of the biggest challenges of the program was to determining exactly which stick was selected with the mouse. Several algorithms will be discussed at the meeting.

Sample Code

void CPDlg::OnLButtonDown(UINT nFlags, CPoint point) 
  CRect rect; CDC *pDC = GetDC(); COLORREF mouse; int i;
  mouse = pDC->GetPixel(point);

  for(i=MAXSTICKS-1; i>=0; i--)
    if (StickStart[i].x < 0) continue;

    // Set up "Point In Rectangle" area
    rect.SetRect(StickStart[i], StickEnd[i]);
    ShowTarget(&rect); // Rect Box for debug only.

    if (
       ColorsMatch(mouse, StickColor[i])   &&
       rect.PtInRect(point)                  && 
       HitTest(&StickStart[i], &StickEnd[i], &point))
         StickStart[i].x = -1;
  CDialog::OnLButtonDown(nFlags, point);
  for(i=0; i<MAXSTICKS; i++)
    if (StickStart[i].x > -1) break;
  if (i==MAXSTICKS)

void CPDlg::FixLocalColors(CDC *pDC)
  for(int i=0; i<MAXCOLORS; i++)
    x = pDC->GetNearestColor(colors[i]);
    colors[i] = x;

"Random Access" questions start at 7:30 Tuesday night.


Source Code Files

For help, email me at b a r n o l d @ i e e e . o r g
Back to C++ Main Page