This article uses examples to describe the method of using Delphi to achieve the impact of a ball, similar to the pinball effect. Click the "Start" button, and a green ball in the form will continue to hit and move left and right, and its size will keep changing. Click the "Stop" button to stop the ball. Use the mouse to drag the adjustment control on the lower right to adjust the speed of the ball.
Here we mainly use Timer control, Panel control, TrackBar control, SpinEdit control, Shape control and Button control to achieve it. The focus is on Delphi's Timer control. While the program is running, the control is not visible and cannot be manipulated directly. The Timer control automatically triggers the OnTimer() event at regular intervals. This example uses it to trigger the ball movement event.
Design idea: The main problem to be solved by this program is how to control the movement of the ball and the speed and size of the ball during the movement. Use the OnTimer() event of the Timer control to control the movement of the ball; use the TrackBar control and SpinEdit control to adjust the speed of the ball movement. The ball is generated by the Shape control.
one. New construction:
(1) Start the Delphi 6.0 program. Select the "New" command under the "File" menu, select the Application option from the pop-up submenu, and the Form1 window of the new project will appear.
(2) Select the "Save Project As" command under the "File" menu to pop up the Save Unit1 As dialog box. In the "Save in" list box, select the directory name where the window file needs to be stored; fill in the file name in the "File name" edit box, fill in "Unit1" here, and click the "Save" button to save the above options.
(3) After executing the previous step, the "Save Project As" dialog box will pop up. In the "Save in" list box, select the directory name where the project needs to be stored. It is best to select the same directory as in step (2); in the "File name" edit box, fill in the file name to save the source code of the project. Fill in "Project1" here and click the "Save" button to save the above options.
two. Some parameter settings:
(1) Activate the Unit1 window, set the Caption property of the form to "Moving Sphere" and the Color property to "clMoneyGreen" in the "Properties" panel.
(2) Select the Panel option on the "Standard" page, add the Panel control in the form, adjust its appropriate size and position, and set the Name property of the control in the "Properties" panel to "Panel1" and the BevelInner to "bvLowered" ", BevelOuter is "blNone", BorderStyle is set to "bsSingle", and Color property is set to "clSkyBlue".
(3) Select the Timer option on the "System" page, add a Timer control to the form, and set its Interval property to 1 and Name property to "Timer1" in the Object Inspector panel.
(4) Select the Shape option on the "Additional" page, add a Shape control on Panel1 in the form, set its Show Hint property to "True" and the Hint property to "Sports Sphere" in the "Properties" panel. The Brush.Color property is ClLime and its Shape property is set to "slCircle".
(5) Select the Button option on the "Standard" page, add three Button controls to the form, and adjust their appropriate size and position. Set its Caption properties in the "Properties" panel to "Start", "Stop" and "Exit" respectively.
(6) Select the TrackBar option on the Win 32 page, add the TrackBar control to the form, and adjust its appropriate size and position. Set its appropriate properties on the Properties panel.
(7) Select the SpinEdit option on Samples, add the SpinEdit control to the form, and adjust its appropriate size and position. Set its appropriate properties on the Properties panel.
3. Code analysis:
(1) In order to realize the function of the ball moving back and forth, a global variable needs to be set, that is, the integer I. The initial value of I is 1. When I=1, the ball moves to the left, and when it hits the left wall of the panel, the I value becomes 2. At this time, the ball moves to the right, and when it hits the right wall of the panel, the I value becomes 1 again. In this way, through the change of I value, the back and forth movement of the ball is realized. This part of the code is in the OnTimer event:
procedure TForm1.Timer1Timer(Sender: TObject);beginTimer1.interval:=trackbar1.position; //Set the trigger time interval of the Timer control if i=1 then //The ball moves to the left beginif shape1.left>0 thenbeginshape1.left: =shape1.left-10; //Change the position of the ball shape1.Width:=(shape1.Width+1)mod 70; //Change the width of the rectangle where the ball is located shape1.Height:=(shape1.Height+1)mod 70; //Change the height of the rectangle where the ball is located endelsei:=2; //Move the ball to the rightend;if i=2 thenbeginif shape1.left<(panel1.Width-shape1.Width-5) thenbeginshape1.left:=shape1.left+10; //Change the position of the ball shape1.Width:=(shape1.Width+1)mod 70;//Change the width of the rectangle where the ball is located shape1.Height:=(shape1.Height+1)mod 70;/ /Change the height of the rectangle where the ball is locatedendelsei:=1;end;end;
(2) The "Start" and "Stop" buttons control whether the ball moves. In fact, they control whether the Timer works:
procedure TForm1.Button1Click(Sender: TObject);beginTimer1.interval:=trackbar1.position; //Set the triggering interval of the Timer control Timer1.Enabled:=true; //The ball starts to moveend;procedure TForm1.Button2Click(Sender: TObject);beginTimer1.Enabled:=False; //The ball stops movingend;
(3) The two controls TrackBar and SpinEdit control the speed of the ball movement. The corresponding codes are as follows:
procedure TForm1.SpinEdit1Change(Sender: TObject);begintrackbar1.position:=spinEdit1.Value; //Change the movement speedend;procedure TForm1.TrackBar1Change(Sender: TObject);beginspinEdit1.value:=trackbar1.Position; //Change the Trackbar value Notify the SpinEdit control of changesend;
4. The complete code of Delphi pinball is as follows:
unit Unit1;interfaceusesWindows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,ExtCtrls, ComCtrls, StdCtrls, Spin;typeTForm1 = class(TForm)Panel1: TPanel;Button1: TButton;Button2: TButton;TrackBar1: TTrackBar;Timer1 : TTimer;Shape1: TShape;SpinEdit1: TSpinEdit;Button3: TButton;procedure Timer1Timer(Sender: TObject);procedure Button1Click(Sender: TObject);procedure Button2Click(Sender: TObject);procedure SpinEdit1Change(Sender: TObject);procedure TrackBar1Change(Sender: TObject); procedure Button3Click(Sender: TObject);private{ Private declarations }public{ Public declarations }end;varForm1: TForm1;i:integer;implementation{$R *.DFM}procedure TForm1.Timer1Timer(Sender: TObject);beginTimer1.interval:=trackbar1.position; //Set the trigger time interval of the Timer control if i=1 then //The ball moves to the left beginif shape1.left>0 thenbeginshape1.left:=shape1.left-10; //Change the position of the ball shape1.Width:=(shape1.Width+1)mod 70;//Change the width of the rectangle where the ball is located shape1.Height:= (shape1.Height+1)mod 70;//Change the height of the rectangle where the ball is locatedendelsei:=2; //The ball moves to the rightend;if i=2 thenbeginif shape1.left<(panel1.Width-shape1.Width-5) thenbeginshape1.left:=shape1.left+10; //Change the position of the ball shape1.Width:=(shape1.Width+1)mod 70; //Change the width of the rectangle where the ball is located shape1.Height:=(shape1.Height+1)mod 70;//Change the height of the rectangle where the ball is locatedendelsei:=1;end;end;procedure TForm1.Button1Click(Sender: TObject);beginTimer1.interval:=trackbar1.position; //Set the triggering time interval of the Timer control Timer1 .Enabled:=true; //The ball starts to moveend;procedure TForm1.Button2Click(Sender: TObject);beginTimer1.Enabled:=False; //The ball stops movingend;procedure TForm1.SpinEdit1Change(Sender: TObject);begintrackbar1.position:=spinEdit1.Value; //Change the movement speedend;procedure TForm1.TrackBar1Change(Sender : TObject);beginspinEdit1.value:=trackbar1.Position; //Notify the SpinEdit control of changes in Trackbar valueend;procedure TForm1.Button3Click(Sender: TObject);beginclose; //Exitend;initializationi:=1;end.