Thema: Delphi 2d game:ping-pong

Einzelnen Beitrag anzeigen

Benutzerbild von sniper_w
sniper_w

Registriert seit: 12. Dez 2004
Ort: Wien, Österriech
893 Beiträge
 
Delphi 6 Enterprise
 
#19

Re: 2d game:ping-pong

  Alt 9. Apr 2005, 13:23
Delphi-Quellcode:
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;
  BallHeight : integer = 30;
  BallWidth : integer = 30;

// offscreen, flickefrei
  Offscreen : TBitmap;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
 Caption := 'B_Ball';
 Width := 300;
 Height := 300;
 Ball := Rect(0,0,BallWidth,BallHeight);
 Timer1.Interval := 10;

 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,BallSpeedX,BallSpeedY);

      // Korrektur bei Bandenkollision
      if (Ball.Left < 0) then // links
      begin
        BallSpeedX := -BallSpeedX;
        Ball.Left := - Ball.Left;
      end
      else if (Ball.Left + BallWidth > ClientRect.Right) then// rechts
      begin
        BallSpeedX := -BallSpeedX;
        Ball.Left := 2 * (ClientRect.Right - BallWidth ) - Ball.Left;
      end;

      if (Ball.Top < 0) then // oben
      begin
        BallSpeedY := -BallSpeedY;
        Ball.Top := - Ball.Top;
      end
      else if (Ball.Top + BallHeight > ClientRect.Bottom) then// unten
      begin
        BallSpeedY := -BallSpeedY;
        Ball.Top := 2 * (ClientRect.Bottom - BallHeight) - Ball.Top;
      end;

 FormPaint(Self);
end;

procedure TForm1.FormPaint(Sender: TObject);
begin//
 with Offscreen do
 begin
  Canvas.FillRect(ClientRect);
  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.
Und ich weiss nicht warum sich Ball sooo verhält .
Katura Haris
Es (ein gutes Wort) ist wie ein guter Baum, dessen Wurzel fest ist und dessen Zweige in den Himmel reichen.
  Mit Zitat antworten Zitat