( Index )
Month

Brief Information about the September '12 CSIG Meeting

One Balloon - A Simple Graphics Program/Game/Practice-Aid

by Bruce Arnold
Catch Me!   States

Welcome to the CSIG, a Special Interest Group of the ACGNJ. This is an exciting time for the C Language programming since Microsoft now has 4 different language compilers: C++, C++ Express, C-Sharp, and C-Sharp Express. These are all capable of creating Windows (tm) programs. (Some are FREE!)

Here's a brief synopsis of this month's meeting:

The object of this program is to create a Teaching Aid for students of Computer Literacy who have no mouse experience.  That's right: NO-MOUSE-EXPERIENCE. A few years ago I wrote a program called "Balloons" for that same purpose. About 75 percent of new computer users can work with that program and then move on to programs like "Solitare" in order to develop the proper HAND-EYE coordination necessary to productively use a computer. The other 25 percent are left behind. This application is for them.

 As can be seen in the screen shot above, the program displays a colored circle on a colored background. Additionally, the circle or balloon has some text printed inside. When the user moves the mouse over to the balloon, it "runs away" from the mouse. The goal of the game is to click the mouse inside the balloon before it runs away.  (1 second delay.) If the user is successful, the balloon "explodes" and the counter is incremented. A new balloon then appears in another location.

 This program demonstrates several of the DOT NET graphics functions. The algorithm is created around a STATE MACHINE in order to control sequence.  Trigger events into the code include: A. The mouse has a new position. B. The mouse has been clicked. C. Timer elapsed. The States of the program State Machine include: Outside_Balloon, Inside_Balloon, Balloon_Exploding, Balloon_Imploding.

The program uses a number of DOT NET Library functions including the following:

DrawString
MeasureString
Font()
Point
SolidBrush
Random
DoubleBuffered
ClientRectangle
DrawEllipse
FillEllipse


There are a number of ways to refer to Microsoft's latest compilers and code. Here's what Wikipedia says: The Common Language Infrastructure (CLI) is an open specification developed by Microsoft that describes the executable code and runtime environment that form the core of the Microsoft .NET Framework. The specification defines an environment that allows multiple high-level languages to be used on different computer platforms without being rewritten for specific architectures.

Microsoft .Net Framework 3.5
C++ 9.0
.Net 3.5
CLI
Common Language Infrastructure
Managed

Sample Code

// // Paint Message Handler // private: System::Void Form1_Paint(System::Object^ sender, System::Windows::Forms::PaintEventArgs^ e) { e; sender; // avoid unreferenced parameter error message during compilation. Rectangle windowRect = ClientRectangle; // Get size of window. Graphics ^g = e->Graphics; g->FillEllipse(BalloonBrush, // Fill the whole balloon with color BalloonCenter.X - BalloonRadius, BalloonCenter.Y - BalloonRadius, 2 * BalloonRadius, 2 * BalloonRadius); g->DrawEllipse(Pens::Black, // Draw a black line around it. BalloonCenter.X - BalloonRadius, BalloonCenter.Y - BalloonRadius, 2 * BalloonRadius, 2 * BalloonRadius); g->DrawEllipse(Pens::White, // Draw a white line around it. BalloonCenter.X - BalloonRadius - 1, BalloonCenter.Y - BalloonRadius - 1, 2 * BalloonRadius + 2, 2 * BalloonRadius + 2); Drawing::Font ^mFont = gcnew Drawing::Font("Arial", 24, Drawing::FontStyle::Bold); String ^mString = gcnew String("Catch Me!"); Drawing::SizeF mFontSize = g->MeasureString(mString,mFont); g->DrawString(mString, mFont, Brushes::Yellow, (float)BalloonCenter.X-(mFontSize.Width/2), (float)BalloonCenter.Y-(mFontSize.Height/2) ); } // // Randomize the center of the balloon // Point Form1_GetRandPoint(Rectangle rr) { Point pp; if ((rr.Right - rr.Left) < 1 || (rr.Bottom - rr.Top) < 1) { pp.X; pp.Y = 0; return pp; // trap potential divide by zero } pp.X = rr.Left + pRand->Next(rr.Right - rr.Left); pp.Y = rr.Top + pRand->Next(rr.Bottom - rr.Top); return pp; } // // Randomize the color of the Balloon to descrete values. // Color Form1_GetRandColor() { int arg[3]; arg[0] = 0; arg[1] = 128; arg[2] = 192; int alpha[3]; alpha[0] = 0xEF; alpha[1] = 0xF0; alpha[2] = 0xFF; return Color::FromArgb( alpha[2], // leave alpha solid. arg[pRand->Next(3)], arg[pRand->Next(3)], arg[pRand->Next(3)] ); }

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