Recent Posts
Selamat datang di Coding Delphi Land Weblog kumpulan source code pemogram delphi
(Bukan maksud untuk menggurui tetapi marilah kita berbagi ilmu tuk perkembangan kemajuan teknologi kita
Minggu, 15 November 2009
Bouncing Ball
unit BallU;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls;
type
TForm1 = class(TForm)
Timer1: TTimer;
procedure FormCreate(Sender: TObject);
procedure FormResize(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure FormPaint(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
screen1: tbitmap; // a bitmap used to buffer the drawing of the ball
ballPos: tpoint; // the coordinates of the top, left corner of the ball
ballMotion: tpoint; // the change in position each interval of the timer
const BallDiameter=20; // this is obvious
implementation
{$R *.DFM}
procedure DrawScreen; // output the ball drawing to the screen
begin
with screen1.canvas do // in the followind block it is assumed that screen1.canvas is typed before each line
begin
brush.color:=clBlack; // background colour
fillrect(form1.clientrect); // fill a rectangle over the entire background
brush.color:=clwhite; // set the ball's colour
ellipse(ballpos.x,ballpos.y,ballpos.x+BallDiameter,ballpos.y+BallDiameter);
// draw the ball as a circle with a top left corner of ballpos and a bottom right corner of
// the ball's position plus the diameter of the ball
end;
form1.canvas.draw(0,0,screen1);
// draw the screen bitmap on the form
end;
procedure TForm1.FormCreate(Sender: TObject);
begin // called once when the program is first run
screen1:=tbitmap.create; // create a bitmap to use for buffering and prevent flicker
BallPos:=Point(100,100); // the top, left point of the ball
BallMotion:=Point(10,10); // initial motion of the ball
end;
procedure TForm1.FormResize(Sender: TObject);
begin // this is called whenever the window is resized
screen1.height:=clientheight;
screen1.width:=clientwidth;
// adjust the dimensions of the bitmap to the dimensions of the form
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin // this is called once when the program is closing
screen1.free; // free the bitmap so it doesn't continue occupying memory after the program closes
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin // this is called every 1/30 seconds or so
BallPos.x:=BallPos.x+BallMotion.x;
BallPos.y:=BallPos.y+BallMotion.y;
// update the position of the ball
if (BallPos.x>ClientWidth-BallDiameter) then // the ball is going off the left side of the window
BallMotion.x:=-Abs(BallMotion.x)
// ensure that the next moving is going to move the ball left
else if BallPos.x<0 then // the ball is going off the right side of the window
BallMotion.x:=Abs(BallMotion.x);
if (BallPos.y>ClientHeight-BallDiameter) then // ... off the bottom of the screen
BallMotion.y:=-Abs(BallMotion.y)
// ensure that the next moving is going to move the ball left
else if BallPos.y<0 then
BallMotion.y:=Abs(BallMotion.y); // ... off the bottom of the screen
// The abs function always returns a positive value.
// if the sign is negative, it returns positive.
// if the sign is positive, nothing changes.
// The rest of the number is not changed.
DrawScreen; // output the changes in ball position to the screen
end;
procedure TForm1.FormPaint(Sender: TObject);
begin // This is called when the form has to be repainted.
// ie. a window was moved infront of it and this window must be painted
DrawScreen; // update the screen
end;
end.
Download Source Code
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls;
type
TForm1 = class(TForm)
Timer1: TTimer;
procedure FormCreate(Sender: TObject);
procedure FormResize(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure FormPaint(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
screen1: tbitmap; // a bitmap used to buffer the drawing of the ball
ballPos: tpoint; // the coordinates of the top, left corner of the ball
ballMotion: tpoint; // the change in position each interval of the timer
const BallDiameter=20; // this is obvious
implementation
{$R *.DFM}
procedure DrawScreen; // output the ball drawing to the screen
begin
with screen1.canvas do // in the followind block it is assumed that screen1.canvas is typed before each line
begin
brush.color:=clBlack; // background colour
fillrect(form1.clientrect); // fill a rectangle over the entire background
brush.color:=clwhite; // set the ball's colour
ellipse(ballpos.x,ballpos.y,ballpos.x+BallDiameter,ballpos.y+BallDiameter);
// draw the ball as a circle with a top left corner of ballpos and a bottom right corner of
// the ball's position plus the diameter of the ball
end;
form1.canvas.draw(0,0,screen1);
// draw the screen bitmap on the form
end;
procedure TForm1.FormCreate(Sender: TObject);
begin // called once when the program is first run
screen1:=tbitmap.create; // create a bitmap to use for buffering and prevent flicker
BallPos:=Point(100,100); // the top, left point of the ball
BallMotion:=Point(10,10); // initial motion of the ball
end;
procedure TForm1.FormResize(Sender: TObject);
begin // this is called whenever the window is resized
screen1.height:=clientheight;
screen1.width:=clientwidth;
// adjust the dimensions of the bitmap to the dimensions of the form
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin // this is called once when the program is closing
screen1.free; // free the bitmap so it doesn't continue occupying memory after the program closes
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin // this is called every 1/30 seconds or so
BallPos.x:=BallPos.x+BallMotion.x;
BallPos.y:=BallPos.y+BallMotion.y;
// update the position of the ball
if (BallPos.x>ClientWidth-BallDiameter) then // the ball is going off the left side of the window
BallMotion.x:=-Abs(BallMotion.x)
// ensure that the next moving is going to move the ball left
else if BallPos.x<0 then // the ball is going off the right side of the window
BallMotion.x:=Abs(BallMotion.x);
if (BallPos.y>ClientHeight-BallDiameter) then // ... off the bottom of the screen
BallMotion.y:=-Abs(BallMotion.y)
// ensure that the next moving is going to move the ball left
else if BallPos.y<0 then
BallMotion.y:=Abs(BallMotion.y); // ... off the bottom of the screen
// The abs function always returns a positive value.
// if the sign is negative, it returns positive.
// if the sign is positive, nothing changes.
// The rest of the number is not changed.
DrawScreen; // output the changes in ball position to the screen
end;
procedure TForm1.FormPaint(Sender: TObject);
begin // This is called when the form has to be repainted.
// ie. a window was moved infront of it and this window must be painted
DrawScreen; // update the screen
end;
end.
Download Source Code
0 komentar:
Posting Komentar