Einzelnen Beitrag anzeigen

Benutzerbild von jfheins
jfheins

Registriert seit: 10. Jun 2004
Ort: Garching (TUM)
4.579 Beiträge
 
#4

Re: Problem mit Ballbewegung (Anfänger)

  Alt 6. Sep 2008, 12:46
Ok, nochmal eine kleine Skizze in Pseudocode:

Basisklasse:
Delphi-Quellcode:
TGameObject = class(TPersistent)
protected
x, y, vx, vx: Single;
Engine: TEngine;
public
procedure PaintTo(Dest: Bitmap); abstract;
procedure Step(deltaT: Cardinal); abstract;
end;

TGameCircle = class(TGameObject)
protected
radius: Integer;
Color: TColor;
public
procedure PaintTo(Dest: Bitmap); overrride;
procedure Step(deltaT: Cardinal); override;
end;

implementation

TGameCircle.PaintTo(Dest: Bitmap)
begin
  Bitmap.Canvas.Ellipse(x-radius, x+radius, y-radius, y+radius);
end;

TGameCircle.Step(deltaT: Cardinal)
begin
  x = x + vx * deltaT;
  y = y + vy * deltaT;

  //CheckForCollision(); // ggf. Kollisionserkennung ==> Änderung von vx & vy
end;
So ... du definierst also ein Gamebject und leitest davon deine Objekte ab.

Und jetzt kommt die Engine:

Delphi-Quellcode:
TEngine = class(TThread)
protected
  Dest: HDC; // Für bitblt()
  Objects: array of TGameObject;
  Bitmap: TBitmap;

  procedure OutputFrame();
  procedure Execute(); override;
public
  constructor Create(destination: HDC); override;
end;

implementation

constructor TEngine.Create(destination: HDC)
begin
  dest = destination;

  Bitmap = TBitmmap.Create();
  // Objeke erstellen
end;

procedure TEngine.Execute()
var
  lastt, dt: Cardinal;
begin
  lastt = Gettickcount() // ggf. queryperformancecounter benutzen
  dt = 0;
  
  while (not Terminated)
  begin
    Bitmap.Clear(); // Füllen mit schwarz

    for each TGameObject in Objects do
      Object.step(dt);

    for each TGameObject in Objects do
      Object.PaintTo(dt);

    synchronize(OutputFrame);

    dt = Gettickcount() - lastt;
    lastt = lastt + dt;
  end;
end;

procedure TEngine.OutputFrame()
begin // synchronized!
  BitBlt({von Bitmap nach HDC von der paintbox kopieren});
  // evtl. ein form1.Paintbox.Refresh();
end;
So, das sollte aber verständlich sein

Ist jetzt im Browser hinggetippt, werden also noch fehler drin sein - aber ich glaube, man erkennt wie ich das meine ^^
  Mit Zitat antworten Zitat