( Index )
Month

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]);
    rect.NormalizeRect();
    rect.InflateRect(10,10);
    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;
         Invalidate(TRUE);
         break;
         }
     }
  CDialog::OnLButtonDown(nFlags, point);
  for(i=0; i<MAXSTICKS; i++)
    if (StickStart[i].x > -1) break;
  if (i==MAXSTICKS)
    m_smiley.ShowWindow(SW_SHOW);
  }

void CPDlg::FixLocalColors(CDC *pDC)
  {
  COLORREF x;
  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

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