Delphi realizes the complete instance code of the collision sphere _delphi

Source: Internet
Author: User
Tags time interval

In this paper, an example is given to illustrate the effect of impact Ball with Delphi, which is similar to pinball effect. Click on the "Start" button, a green ball in the form will constantly impact movement, and its size constantly changing. Click the "Stop" button and the ball stops moving. Drag the control on the lower right side of the mouse to adjust the speed of the ball movement.

Here we use the Timer control, the Panel control, the TrackBar control, the Spinedit control, the shape control, and the button control to implement the key. The key is the Delphi Timer control. When you run a program, the control is not visible, and you cannot manipulate it directly. The Timer control automatically triggers the OnTimer () event every once in a while. This example is used to trigger the event of a small ball movement.

Design idea: The main problem that the program solves is how to control the ball movement and the speed and size of the ball during the movement. The OnTimer () event of the timer control is used to control the movement of the ball, and the trackbar control and the Spinedit control are used to adjust the speed of the ball movement. The ball is generated by the shape control.

A New project:

(1) Start Delphi 6.0 program. The Form1 window for the new project appears when you select the new command under the File menu and select the Application option from the Pop-up submenu.
(2) Select the "Project Save as" command under the File menu to eject the Save Unit1 as dialog box. In the Save in list box, select the name of the directory where you want the window file to reside, fill in the filename in the File name box, fill in "Unit1" here, and click the "Save" button to save the options above.
(3) After the previous step, the Project Save As dialog box pops up. In the Save in list box, select the name of the directory where you want the project to reside. It is best to select the same directory as Step (2), to fill in the file name of the source code for the project in the File name edit box, to fill in the "Project1" and click the "Save" button to save the above options.

Two Partial parameter settings:

(1) Activates the Unit1 window and sets the Caption property of the form to "moving sphere" in the properties panel, with the Color property set to "Clmoneygreen".
(2) Select the Panel option on the standard page, add a Panel control to the form, adjust its appropriate size and position, set the control's Name property to "Panel1" in the properties panel, Bevelinner to "bvlowered", and Bevelouter to "Blnone", BorderStyle is set to "Bssingle" and the 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 the 1,name property to "Timer1" in the object Inspecter panel.
(4) Select the Shape option on the Attach page, add a shape control to the Panel1 on the form, set its show Hint property to "True" in the Properties panel, Hint the property to "motion sphere", and set its Brush.color property to Cllime. Set its Shape property to "Slcircle".
(5) Select the button option on the standard page, add three button controls to the form, and adjust the appropriate size and position. Set its Caption property to start, stop, and exit, respectively, in the Properties panel.
(6) Select the TrackBar option on the Win 32 page and add the TrackBar control to the form to adjust its appropriate size and position. Set the appropriate properties on the Properties panel.
(7) Select the Spinedit option on the samples, add the Spinedit control to the form, and adjust its appropriate size and position. Set the appropriate properties on the Properties panel.

Three. Code Analysis:

(1) In order to realize the function of the ball moving back and forth, we need to set a global variable, that is, the initial value of integer i,i is 1. When I=1, the ball moved to the left, hit the wall of the panel, I changed to 2, when the ball to the right, hit the right wall of the panel, I also changed to 1. In this way, the movement of the small ball is realized by the change of the I value. This part of the code is in the OnTimer event:

Procedure Tform1.timer1timer (sender:tobject);
Begin
Timer1.interval:=trackbar1.position//Set trigger interval for timer control
if I=1 then//ball left motion
begin
If Shape1.left>0 then
begin
shape1.left:=shape1.left-10//change the position of the ball
Shape1. Width:= (Shape1. width+1) mod 70; The width of the rectangular sphere is changed
shape1. Height:= (Shape1. height+1) mod 70; The height of the rectangular ball is changed end
else
i:=2;//Ball right motion end
;
If i=2 then
begin
If shape1.left< (Panel1. WIDTH-SHAPE1.WIDTH-5) then
begin
shape1.left:=shape1.left+10//change the position of the ball
Shape1. Width:= (Shape1. width+1) mod 70;//changes shape1 the width of the rectangle in which the ball is located
. Height:= (Shape1. height+1) mod 70;//changes the height of the rectangular ball to the end
else
i:=1;
End;
End

(2) "Start" and "Stop" button to control whether the ball is moving, in fact, is to control whether the timer is working:

Procedure Tform1.button1click (sender:tobject);
Begin
Timer1.interval:=trackbar1.position//Set trigger interval for timer control
timer1.enabled:=true;//ball start motion end
;
Procedure Tform1.button2click (sender:tobject);
Begin
Timer1.enabled:=false;//Ball stop motion end
;

(3) TrackBar and Spinedit two controls control the speed of the ball movement, the corresponding code is as follows:

Procedure Tform1.spinedit1change (sender:tobject);
Begin
Trackbar1.position:=spinedit1.value//change motion speed end
;
Procedure Tform1.trackbar1change (sender:tobject);
Begin
Spinedit1.value:=trackbar1. Position; Notifies the Spinedit control end of changes to the TrackBar value
;

Four. The complete code for Delphi Pinball is as follows:

Unit Unit1; Interface uses Windows, Messages, Sysutils, Classes, Graphics, Controls, Forms, Dialogs, Extctrls, Comctrls, Stdctrls, Spi
N
Type TFORM1 = 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;
var Form1:tform1;
I:integer; Implementation {$R *.
DFM} procedure Tform1.timer1timer (Sender:tobject); Begin Timer1.interval:=trackbar1.position; Sets the trigger interval for the timer control if I=1 then//ball left motion begin if shape1.left>0 then BEGIN shape1.left:=shape1.left-10; Change the position of the ball shape1. Width:= (Shape1. width+1) mod 70;//changes shape1 the width of the rectangle in which the ball is located. Height:= (Shape1. height+1) mod 70;//The height of the rectangular ballChanged end else i:=2;
Ball right motion end; If i=2 then BEGIN if shape1.left< (Panel1. WIDTH-SHAPE1.WIDTH-5) THEN begin shape1.left:=shape1.left+10; Change the position of the ball shape1. Width:= (Shape1. width+1) mod 70; The width of the rectangular sphere is changed shape1. Height:= (Shape1.
height+1) mod 70;//changes the height of the rectangular ball to the end else i:=1;
End
End
Procedure Tform1.button1click (Sender:tobject); Begin Timer1.interval:=trackbar1.position; Sets the trigger time interval for the timer control timer1.enabled:=true;
The ball starts the motion end;
Procedure Tform1.button2click (Sender:tobject); Begin Timer1.enabled:=false;
Ball stop motion end;
Procedure Tform1.spinedit1change (Sender:tobject); Begin Trackbar1.position:=spinedit1.value;
Change motion speed end;
Procedure Tform1.trackbar1change (Sender:tobject); Begin SPINEDIT1.VALUE:=TRACKBAR1. Position;
Notifies the Spinedit control end of changes to the TrackBar value;
Procedure Tform1.button3click (Sender:tobject); Begin close;
Exit end;
Initialization i:=1;
 End.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.