Removing Installed Controls

 

Project BattingPractice

To view this project, open the IDE and open the BattingPractice project in Visual Studio 2005\Projects\DemosSourceCode. Compile and run the project. The main window looks like this:

image from book
Figure 27-2: Batting Practice window

When the Start button is picked, this window opens:

image from book
Figure 27-3: Batting Practice Field window

For ease of baseball diamond construction, we used panels for each of the bases and the pitcher s mound. The blue stick in the lower-right corner of the window is the bat. The ball is a red letter o and appears only after the Pitch The Ball button is selected.

All the action in this project occurs on Form 2.

Pitching the Ball

Lines BP118-BP119 are 31 sets of X and Y coordinates that dictate the baseball s trajectory. If you do not like this method of prescribing the trajectory, then start the baseball at coordinates (290, 260) and increment the trajectory by 8, 10, or 12 after each timer1_Tick (lines BP140-BP150).

The timer that controls the baseball is located in lines BP128-BP131. This timer runs continuously, and its timer- ticks are either used or unused in lines BP140-BP150, based on whether the boolean variable BallStart is true or false. Note that these same lines of code are shown in the button1_Click method (lines BP202-BP205), and they are commented out. If the timer in lines BP128-BP131 is used, then it is turned on once (line BP131) and never turned off between pitches.

If the timer in button1_Click is used, then the timer must be turned on for each pitch and turned off when the pitch is complete. The worst thing a programmer can do in this situation is to turn the timer on several times but forget to ever turn it off. This causes the timer to emit ticks in an erratic manner and spoils the game.

Click on the Pitch The Ball button and the baseball will proceed from the pitcher s mound to the home plate. The speed of the pitch is controlled by line BP113, intBallTimer1Interval = 200;. To increase the baseball velocity, decrease the number 200. Later in this section we suggest that you place a button on the batting practice field to increase or decrease the baseball speed as batting practice progresses, but this means that the timer must be moved from BP128-BP131 to BP202-BP205. Moving the timer means that it must be turned on and off after each pitch. This second timer configuration is demonstrated in the Rolling Block project later in this chapter.

Swinging the Bat

Lines BP120-BP121 are 16 sets of cosines and sines that dictate the position of the bat swing. Each position is 10 degrees beyond its predecessor (10 positions for the first 90 degrees). The timer that controls the bat swing is located in lines BP132-BP135. This timer runs continuously, and its timer-ticks are either used or unused in lines BP160-BP177, based on whether the boolean variable BatStart is true or false. Note that these same lines of code are shown in the button2_ Click method (lines BP222-BP225), and they are commented out. If the timer in lines BP132-BP135 is used, then it is turned on once (line BP135) and never turned off.

If the timer in button2_Click is used, then the timer must be turned on for each pitch and turned off when the pitch is complete.

Click on the Swing! button and the bat will proceed from the starting position to a position 150 degrees later (a right-handed batter). The speed of the bat swing is controlled by line BP114, intBatTimer2Interval = 100;. To increase the bat velocity, decrease the number 100. Later in this section we suggest that you place a button on the batting practice field to increase or decrease the bat speed as the batting practice progresses. This means that the timer must be moved from BP132-BP135 to BP222-BP225.

Determining if It s a Hit!

This is the real bell ringer in this project. All of this logic is located in method timer2_Tick(), and it is not very good. In lines BP169-BP173 we attempt to determine if the ball and the bat are located at the same place at the same time, and conclude that this is a hit.

               // Check for a 'hit'. BP169:        if ((intBallLoc == 24) || (intBallLoc == 25) /*||(intBallLoc==26)*/) BP170:        { BP171:          if ((intBatLoc == 4) || (intBatLoc == 5) /*||(intBatLoc==6)*/ ) BP172:          { BP173:            label1.Text = "It's a HIT !!!"; BP174:          } 

These three ball locations, (24, 25, and 26) and three bat locations (4, 5, and 6) cover the area of the home plate, so this scheme should work (to determine if the ball is hit). Sometimes this works, and sometimes it doesn t.

A Project in Progress

At this point you ll probably realize that project BattingPractice is really a work in progress. There are many things you could add to this project to make it better:

  • Provide buttons to increase and decrease the ball speed on Form2. The variable you want to decrease or increase is intBallTimerInterval.

  • Provide buttons to increase and decrease the bat speed on Form2. The variable you want to decrease or increase is intBatTimer2Interval.

  • Provide a scoreboard on Form2 that will keep track of the number of pitches thrown and the number of hits. To initialize the scoreboard data, we suggest that you add an event handler named Form2_Load() to Form2. To add this event handler, highlight the Form2 template (to bring up the window properties in the Properties window), click on the Events icon, move to Behavior | Load, and double-click on Load. Within this event handler you must initialize the scoreboard data (see project TicTacToe, lines TT0120-TT0125).

  • If you name the datakeepers intNoOfPitches and intNoOfHits, then these variables must be declared near line BP125 and initialized in Form2_Load(), which does not exist at this time. Within these event handlers you must also initialize the scoreboard data.

  • Provide a more accurate means of determining if the bat swing causes a hit. Perhaps one way would be to increase the number of locations of the ball between the pitcher s mound and home plate (from 31 locations to 62, for example). If you do this, you must decrease the intBallTimer1Interval from 200 to 100, since there are twice as many images of the baseball to be shown in the same length of time.

    Good luck!

    BattingPractice (BP) Listing

     Form1.cs: BP002:        using System; BP003:        using System.Collections.Generic; BP004:        using System.ComponentModel; BP005:        using System.Data; BP006:        using System.Drawing; BP007:        using System.Windows.Forms; BP009:        namespace BattingPractice BP010:        { BP011:          partial class Form1 : Form BP012:          { BP013:            public Form1() BP014:            {InitializeComponent(); } //-----------------------------------------------------------------------------------------// BP020:            private void button1_Click(object sender, EventArgs e) BP021:            { // Start. BP022:              Form2 frm2 = new Form2(); BP023:              frm2.ShowDialog(); BP024:              frm2.Close(); BP025:            } //-----------------------------------------------------------------------------------------// BP030:            private void button2_Click(object sender, EventArgs e) BP031:            { // Quit. BP032:              Close(); BP033:            } BP034:          } BP035:        } //=========================================================================================// Form1.Designer.cs: BP040:        namespace BattingPractice BP041:        { BP042:          partial class Form1 BP043:          {                   // Required designer variable. BP044:            private System.ComponentModel.IContainer components = null;                   // Clean up any resources being used. BP045:            protected override void Dispose(bool disposing) BP046:            { BP047:              if (disposing && (components != null)) components.Dispose(); BP048:              base.Dispose(disposing); BP049:            } BP050:            #region Windows Form Designer generated code                   // Required method for Designer support. BP051:            private void InitializeComponent() BP052:            { BP053:              this.label1 = new System.Windows.Forms.Label(); BP054:              this.button1 = new System.Windows.Forms.Button(); BP055:              this.button2 = new System.Windows.Forms.Button(); BP056:              this.SuspendLayout();                     //                     // label1                     // BP057:              this.label1.AutoSize = true; BP058:              this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 14F,                       System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point,                       ((byte)(0))); BP059:              this.label1.Location = new System.Drawing.Point(77, 8); BP060:              this.label1.Name = "label1"; BP061:              this.label1.Size = new System.Drawing.Size(141, 24); BP062:              this.label1.TabIndex = 0; BP063:              this.label1.Text = "Batting Practice";                     //                     // button1                     // BP064:              this.button1.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F,                       System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,                       ((byte)(0))); BP065:              this.button1.Location = new System.Drawing.Point(75, 48); BP066:              this.button1.Name = "button1"; BP067:              this.button1.Size = new System.Drawing.Size(150, 25); BP068:              this.button1.TabIndex = 1; BP069:              this.button1.Text = "Start"; BP070:              this.button1.Click += new System.EventHandler(this.button1_Click);                     //                     // button2                     // BP071:              this.button2.Location = new System.Drawing.Point(8, 93); BP072:              this.button2.Name = "button2"; BP073:              this.button2.Size = new System.Drawing.Size(75, 25); BP074:              this.button2.TabIndex = 2; BP075:              this.button2.Text = "Quit"; BP076:              this.button2.Click += new System.EventHandler(this.button2_Click);                     //                     // Form1                     // BP077:              this.AutoScaleDimensions = new System.Drawing.SizeF(5F, 13F); BP078:              this.ClientSize = new System.Drawing.Size(292, 128); BP079:              this.Controls.Add(this.button2); BP080:              this.Controls.Add(this.button1); BP081:              this.Controls.Add(this.label1); BP082:              this.Name = "Form1"; BP083:              this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; BP084:              this.Text = "Batting Practice"; BP085:              this.ResumeLayout(false); BP086:              this.PerformLayout(); BP087:            } BP088:            #endregion BP089:            private System.Windows.Forms.Label label1; BP090:            private System.Windows.Forms.Button button1; BP091:            private System.Windows.Forms.Button button2; BP092:          } BP093:        } //=========================================================================================// Form2.cs: BP100:        #region Using directives BP101:        using System; BP102:        using System.Collections.Generic; BP103:        using System.ComponentModel; BP104:        using System.Data; BP105:        using System.Drawing; BP106:        using System.Text; BP107:        using System.Windows.Forms; BP108:        #endregion BP109:        namespace BattingPractice BP110:        { BP111:          partial class Form2 : Form BP112:          {                   // Programmer-added variables.                   // Set ball speed. The lower the number, the FASTER the ball moves. BP113:            public int intBallTimer1Interval = 200;                   // Set bat speed. The lower the number, the FASTER the bat moves. BP114:            public int intBatTimer2Interval = 100;                   // BBPX = BatPivotPointX. BP115:            public int iBPPX = 525;                   // BPPY = BatPivotPointY. BP116:            public int iBPPY = 525; BP117:            public double dBL = 40.0; BP118:            public int[] BallX = new int[31]{290,300,310,320,330,340,350,360,370,                     380,390,400,410,420,430,440,450,460,470,480,490,500,510,520,530,540,                     550,560,570,580,590}; BP119:            public int[] BallY = new int[31]{260,270,280,290,300,310,320,330,340,                     350,360,370,380,390,400,410,420,430,440,450,460,470,480,490,500,510,                     520,530,540,550,560};                   // -20,-10,0,10,20,30,40,50,60,70,80,90,100,110,120 degrees. BP120:            public double[] BatXCos = new double[16] {0.93969,0.98481,1.0,0.98481,                     0.93969,0.86603,0.76604,0.64279,0.5,0.34202,0.17365,0.0,-0.17365,                     -0.34202,-.5,0.93969}; BP121:            public double[] BatYSin = new double[16] {-0.34202,-0.17365,0.0,0.17365,                     0.34202,0.5,0.64279,0.76604,0.86603,0.93969,0.98481,1.0,0.98481,                     0.93969,0.86603,-0.34202}; BP122:            public int intBallLoc;  // Positions '0' through '30'. BP123:            public int intBatLoc;   // Positions '0' through 15'. BP124:            public bool BallStart = false; BP125:            public bool BatStart = false; BP126:            public Form2() BP127:            {                     // 'Pitch the Ball' timer. BP128:              Timer timer1 = new Timer(); BP129:              timer1.Tick += new EventHandler(timer1_Tick); BP130:              timer1.Interval = intBallTimer1Interval; BP131:              timer1.Start();                     // 'Swing the Bat' timer. BP132:              Timer timer2 = new Timer(); BP133:              timer2.Tick += new EventHandler(timer2_Tick); BP134:              timer2.Interval = intBatTimer2Interval; BP135:              timer2.Start(); BP136:              InitializeComponent(); BP137:            } //-----------------------------------------------------------------------------------------// BP140:            private void timer1_Tick(object sender, EventArgs e) BP141:            { // 'Pitch The Ball' timer. BP142:              if (BallStart) BP143:              { BP144:                 intBallLoc++; BP145:                 if (intBallLoc <= 26) BP146:                   Invalidate(); BP147:                 else BP148:                   BallStart = false; BP149:               } BP150:             } //-----------------------------------------------------------------------------------------// BP160:             private void timer2_Tick(object sender, EventArgs e) BP161:             { // 'Batter Swing' timer. BP162:               if (BatStart) BP163:               { BP164:                 intBatLoc++; BP165:                 if (intBatLoc <= 15) BP166:                   Invalidate(); BP167:                 else BP168:                   BatStart = false;                        // Check for a 'hit'. BP169:                 if ((intBallLoc == 24) || (intBallLoc == 25) /*||(intBallLoc==26)*/) BP170:                 { BP171:                   if ((intBatLoc == 4) || (intBatLoc == 5) /*||(intBatLoc==6)*/) BP172:                   {    BP173:                     label1.Text = "It's a HIT !!!"; BP174:                   } BP175:                 } BP176:               } BP177:             } //-----------------------------------------------------------------------------------------// BP180:             private void Form2_Paint(object sender, PaintEventArgs e) BP181:             { // Paint the window. BP182:               Graphics grfx = e.Graphics; BP183:               Font ballfont = new Font("Arial", 8); // This is the baseball. BP184:               Pen bluePen = new Pen(Color.Blue, 4); // This is the bat. BP185:               if (BallStart) BP186:               { BP187:                 grfx.DrawString("O", ballfont, Brushes.Red, BP188:                 BallX[intBallLoc], BallY[intBallLoc]); BP189:               } BP190:               if (BatStart) // This is the bat. BP191:               { BP192:                 grfx.DrawLine(bluePen, iBPPX, iBPPY, (iBPPX + (int)(dBL *                          BatXCos[intBatLoc])), (iBPPY - (int)(dBL * BatYSin[intBatLoc]))); BP193:               } BP194:               else BP195:               {                        // Show the blue bat.                        grfx.DrawLine(bluePen, iBPPX, iBPPY, (iBPPX + (int)(dBL * BatXCos[0])),                          (iBPPY - (int)(dBL * BatYSin[0]))); BP196:               } BP197:             } //-----------------------------------------------------------------------------------------// BP200:             private void button1_Click(object sender, EventArgs e) BP201:             {// Pitch the ball.                      /* All of this has been moved to lines BP039 - 0142. BP202:               Timer timer1 = new Timer(); BP203:               timer1.Tick += new EventHandler(timer1_Tick); BP204:               timer1.Interval = 1000; BP205:               timer1.Start(); */ BP206:               intBallLoc = 0; BP207:               BallStart = true; BP208:               label1.Text = ""; BP209:             } //-----------------------------------------------------------------------------------------// BP220:             private void button2_Click(object sender, EventArgs e) BP221:             { // Batter Swing !                      /* All of this has been moved from lines BP0132 - BP135. BP222:               Timer timer2 = new Timer(); BP223:               timer2.Tick += new EventHandler(timer2_Tick); BP224:               timer2.Interval = 1000; BP225:               timer2.Start(); */ BP226:               intBatLoc = 0; BP227:               BatStart = true; BP228:             } //-----------------------------------------------------------------------------------------// BP230:             private void button3_Click(object sender, EventArgs e) BP231:             { // Quit. BP232:               Close(); BP233:             } BP234:           } BP235:        } //=========================================================================================// Form2.Designer.cs: BP300:        namespace BattingPractice BP301:        { BP302:          partial class Form2 BP303:          {                   // Required designer variable. BP304:            private System.ComponentModel.IContainer components = null;                   // Clean up any resources being used. BP305:            protected override void Dispose(bool disposing) BP306:            { BP307:              if (disposing && (components != null)) components.Dispose(); BP308:              base.Dispose(disposing); BP309:            } BP310:            #region Windows Form Designer generated code                   // Required method for Designer support. BP311:            private void InitializeComponent() BP312:            { BP313:              this.components = new System.ComponentModel.Container(); BP314:              this.panel1 = new System.Windows.Forms.Panel(); BP315:              this.panel2 = new System.Windows.Forms.Panel(); BP316:              this.panel3 = new System.Windows.Forms.Panel(); BP317:              this.panel4 = new System.Windows.Forms.Panel(); BP318:              this.panel5 = new System.Windows.Forms.Panel(); BP319:              this.panel6 = new System.Windows.Forms.Panel(); BP320:              this.panel7 = new System.Windows.Forms.Panel(); BP321:              this.timer1 = new System.Windows.Forms.Timer(this.components); BP322:              this.timer2 = new System.Windows.Forms.Timer(this.components); BP323:              this.button1 = new System.Windows.Forms.Button(); BP324:              this.button2 = new System.Windows.Forms.Button(); BP325:              this.button3 = new System.Windows.Forms.Button(); BP326:              this.label1 = new System.Windows.Forms.Label(); BP327:              this.SuspendLayout();                     //                     // panel1                     // BP328:              this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; BP329:              this.panel1.Location = new System.Drawing.Point(20, 520); BP330:              this.panel1.Name = "panel1"; BP331:              this.panel1.Size = new System.Drawing.Size(531, 2); BP332:              this.panel1.TabIndex = 0;                     //                     // panel2                     // BP333:              this.panel2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; BP334:              this.panel2.Location = new System.Drawing.Point(550, 20); BP335:              this.panel2.Name = "panel2"; BP336:              this.panel2.Size = new System.Drawing.Size(2, 501); BP337:              this.panel2.TabIndex = 1;                     //                     // panel3                     // BP338:              this.panel3.BackColor = System.Drawing.Color.Transparent; BP339:              this.panel3.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; BP340:              this.panel3.Location = new System.Drawing.Point(531, 501); BP341:              this.panel3.Name = "panel3"; BP342:              this.panel3.Size = new System.Drawing.Size(20, 20); BP343:              this.panel3.TabIndex = 2;                     //                     // panel4                      // BP344:              this.panel4.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; BP345:              this.panel4.Location = new System.Drawing.Point(531, 58); BP346:              this.panel4.Name = "panel4"; BP347:              this.panel4.Size = new System.Drawing.Size(20, 20); BP348:              this.panel4.TabIndex = 3;                     //                     // panel5                     // BP349:              this.panel5.BackColor = System.Drawing.Color.Transparent; BP350:              this.panel5.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; BP351:              this.panel5.Location = new System.Drawing.Point(280, 250); BP352:              this.panel5.Name = "panel5"; BP353:              this.panel5.Size = new System.Drawing.Size(20, 20); BP354:              this.panel5.TabIndex = 4;                     //                     // panel6                     // BP355:              this.panel6.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; BP356:              this.panel6.Location = new System.Drawing.Point(88, 501); BP357:              this.panel6.Name = "panel6"; BP358:              this.panel6.Size = new System.Drawing.Size(20, 20); BP359:              this.panel6.TabIndex = 5;                     //                     // panel7                     // BP360:              this.panel7.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; BP361:              this.panel7.Location = new System.Drawing.Point(88, 58); BP362:              this.panel7.Name = "panel7"; BP363:              this.panel7.Size = new System.Drawing.Size(20, 20); BP364:              this.panel7.TabIndex = 6;                     //                     // timer1                     // BP365:              this.timer1.Tick += new System.EventHandler(this.timer1_Tick);                     //                     // timer2                     // BP366:              this.timer2.Tick += new System.EventHandler(this.timer2_Tick);                     //                     // button1                     // BP367:              this.button1.Location = new System.Drawing.Point(136, 534); BP368:              this.button1.Name = "button1"; BP369:              this.button1.Size = new System.Drawing.Size(125, 25); BP370:              this.button1.TabIndex = 7; BP371:              this.button1.Text = "Pitch the Ball"; BP372:              this.button1.Click += new System.EventHandler(this.button1_Click);                     //                     // button2                     // BP373:              this.button2.Location = new System.Drawing.Point(352, 534); BP374:              this.button2.Name = "button2"; BP375:              this.button2.Size = new System.Drawing.Size(125, 25); BP376:              this.button2.TabIndex = 8; BP377:              this.button2.Text = "Swing !"; BP378:              this.button2.Click += new System.EventHandler(this.button2_Click);                     //                     // button3                     // BP379:              this.button3.Location = new System.Drawing.Point(440, 12); BP380:              this.button3.Name = "button3"; BP381:              this.button3.Size = new System.Drawing.Size(75, 25); BP382:              this.button3.TabIndex = 9; BP383:              this.button3.Text = "Quit"; BP384:              this.button3.Click += new System.EventHandler(this.button3_Click);                     //                     // label1                     // BP385:              this.label1.AutoSize = true; BP386:              this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 36F,                       System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point,                       ((byte)(0))); BP387:              this.label1.Location = new System.Drawing.Point(128, 16); BP388:              this.label1.Name = "label1"; BP389:              this.label1.Size = new System.Drawing.Size(0, 0); BP390:              this.label1.TabIndex = 10;                     //                     // Form2                     // BP391:              this.AutoScaleDimensions = new System.Drawing.SizeF(5F, 13F); BP392:              this.ClientSize = new System.Drawing.Size(584, 562); BP393:              this.Controls.Add(this.label1); BP394:              this.Controls.Add(this.button3); BP395:              this.Controls.Add(this.button2); BP396:              this.Controls.Add(this.button1); BP397:              this.Controls.Add(this.panel7); BP398:              this.Controls.Add(this.panel6); BP399:              this.Controls.Add(this.panel5); BP400:              this.Controls.Add(this.panel4); BP401:              this.Controls.Add(this.panel3); BP402:              this.Controls.Add(this.panel2); BP403:              this.Controls.Add(this.panel1); BP404:              this.Name = "Form2"; BP405:              this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; BP406:              this.Text = "Batting Practice Field"; BP407:              this.Paint += new System.Windows.Forms.PaintEventHandler                       (this.Form2_Paint); BP408:              this.ResumeLayout(false); BP409:              this.PerformLayout(); BP410:            } BP411:            #endregion BP412:            private System.Windows.Forms.Panel panel1; BP413:            private System.Windows.Forms.Panel panel2; BP414:            private System.Windows.Forms.Panel panel3; BP415:            private System.Windows.Forms.Panel panel4; BP416:            private System.Windows.Forms.Panel panel5; BP417:            private System.Windows.Forms.Panel panel6; BP418:            private System.Windows.Forms.Panel panel7; BP419:            private System.Windows.Forms.Timer timer1; BP420:            private System.Windows.Forms.Timer timer2; BP421:            private System.Windows.Forms.Button button1; BP422:            private System.Windows.Forms.Button button2; BP423:            private System.Windows.Forms.Button button3; BP424:            private System.Windows.Forms.Label label1; BP425:          } BP426:        } 
 


Unlocking Microsoft C# V 2.0 Programming Secrets
Unlocking Microsoft C# V 2.0 Programming Secrets (Wordware Applications Library)
ISBN: 1556220979
EAN: 2147483647
Year: 2005
Pages: 129

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net