unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls;
type
TForm1 =
class(TForm)
Timer1: TTimer;
procedure FormCreate(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure FormPaint(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
Ball : TRect;
BallSpeedX : integer = 5;
BallSpeedY : integer = 5;
dx : integer = 1;
dy : integer = 1;
BallHeight : integer = 30;
BallWidth : integer = 30;
// offscreen, flickefrei
Offscreen : TBitmap;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
Caption := '
B_Ball';
Width := 600;
Height := 300;
Ball := Rect(0,0,BallWidth,BallHeight);
Timer1.Interval := 1;
Offscreen := TBitmap.Create();
with Offscreen
do
begin
PixelFormat := pf32bit;
Width := Screen.Width;
Height := Screen.Height;
Canvas.Brush.Color := clOlive;
Canvas.Pen.Color := clBlack;
end;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var old : integer;
begin//
FormPaint(Self);
OffsetRect(Ball,dx * BallSpeedX,dy * BallSpeedY);
// Korrektur bei Bandenkollision
if (Ball.Left < 0)
then // links
begin
dx := 1;
end
else if (Ball.Left + BallWidth > ClientRect.Right)
then// rechts
begin
dx := -1;
end;
if (Ball.Top < 0)
then // oben
begin
dy := 1;
end
else if (Ball.Top + BallHeight > ClientRect.Bottom)
then// unten
begin
dy := -1;
end;
FormPaint(Self);
end;
procedure TForm1.FormPaint(Sender: TObject);
begin//
with Offscreen
do
begin
Canvas.Brush.Style := bsSolid;
Canvas.Brush.Color := clOlive;
Canvas.FillRect(ClientRect);
Canvas.Brush.Style := bsClear;
Canvas.Ellipse(Ball);
end;
BitBlt(Canvas.Handle,0,0,Width,Height,
Offscreen.Canvas.Handle,0,0,SRCCOPY);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin//
Offscreen.Free();
end;
end.