Thema: Delphi Labyrinth

Einzelnen Beitrag anzeigen

Sandro
(Gast)

n/a Beiträge
 
#1

Labyrinth

  Alt 30. Mär 2008, 20:18
Guten Abend.

Ich habe hier ein Delphi Programm das leider nicht genau das macht was es eigl. soll.
Ziel ist es dass die Maus durch ein Labyrith zum Käse läuft.
Delphi-Quellcode:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls;

type
  TSpielfeld = class(TForm)
    Timer1: TTimer;
    Bild: TImage;
    procedure FormCreate(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);

  end;
     type tBall=class x,y,vx,vy,r: Single;
   farbe : tColor;
   procedure Init(fNeu: tColor;
   xNeu,yNeu,vxNeu,vyNeu,rNeu: Single);
   procedure ZeigeDich;
   procedure BewegeDich;
   end;


var
  Spielfeld: TSpielfeld;
  ball1 : tBall;
  ball2 : tBall;
  bild : tImage;


implementation

{$R *.dfm}
 procedure tBall.Init(fNeu: tColor;
xNeu,yNeu,vxNeu,vyNeu,rNeu: Single);
begin
farbe:= fNeu;
r := rNeu;
x := xNeu;
y := yNeu;
vx := vxNeu;
vy := vyNeu;
end;



procedure tBall.ZeigeDich;
begin
with Spielfeld.Bild.Canvas do
begin
Brush.color := Farbe;
ellipse(Round(x-r),Round(y-r),Round(x+r),Round(y+r))
end;
end;

procedure tBall.BewegeDich;
begin
x := x + vx; y := y + vy;
with Spielfeld.Bild do
begin
if (x >Width-r-50) then
begin x := Width-r-50; vx := 0 end;
if x < r + 50 then
begin x := r + 50; vx := 0 end;
if (y >Height-r - 50) then
begin y := Height-r - 50; vy := 0 end;
if y < r + 120 then
begin y := r + 120; vy := 0 end;
end
end;

procedure TSpielfeld.FormCreate(Sender: TObject);
var x,y,vx,vy: single;
begin

with Bild.Canvas do
begin
pen.width := 2;
Brush.Color := clwhite;
Rectangle(0,0,Bild.Width,Bild.Height);
pen.width := 2;
brush.Style := bsSolid;
pen.Mode := pmNOTXOR;
end;

Ball1.init(clred,210,
                30,
                5,0,4);
Ball1.ZeigeDich;
repeat x:= Random(Spielfeld.Bild.width-50)+25;
       y:=Random(Spielfeld.Bild.Height-50)+25;
       until sqrt(sqr(x-Ball1.x)+sqr(y-Ball1.y))>=8;
       Ball2.Init(clyellow,260,270,0,0,4);
       Ball2.ZeigeDich;
end;



procedure nachlinks;
begin
if ball1.vx=0
then begin
if Bild.Canvas.Pixels[Round(ball1.x),Round(ball1.y+ball1.r+1)]=clblack
then begin
if (ball1.vx=0)and (ball1.vy>0)
then begin
ball1.vx:=5;
ball1.vy:=0;
end
else if (ball1.vx=0)and (ball1.vy<0)
then begin
ball1.vx:=-5;
ball1.vy:=0;
end
else if (ball1.vx<0)and (ball1.vy=0)
then begin
ball1.vx:=0;
ball1.vy:=5;
end
else if (ball1.vx>0)and (ball1.vy=0)
then begin
ball1.vx:=0;
ball1.vy:=-5;
end
end
end;
if ball1.vy=0
then begin
if Bild.Canvas.Pixels[Round(ball1.x+ball1.r+1),Round(ball1.y)]=clblack
then begin
if (ball1.vx=0)and (ball1.vy>0)
then begin
ball1.vx:=5;
ball1.vy:=0;
end
else if (ball1.vx=0)and (ball1.vy<0)
then begin
ball1.vx:=-5;
ball1.vy:=0;
end
else if (ball1.vx<0)and (ball1.vy=0)
then begin
ball1.vx:=0;
ball1.vy:=5;
end
else if (ball1.vx>0)and (ball1.vy=0)
then begin
ball1.vx:=0;
ball1.vy:=-5;
end
end
end;
end;

procedure TSpielfeld.Timer1Timer(Sender: TObject);
begin
Ball1.ZeigeDich; Ball2.ZeigeDich;
nachlinks;
Ball1.BewegeDich; Ball2.BewegeDich;
Ball1.ZeigeDich; Ball2.ZeigeDich;
end;
Initialization
Randomize;
Ball1 := tBall.Create;
Ball2 := tBall.Create;
Finalization
Ball1.Destroy;
Ball2.Destroy;



end.
Bin für jede Hilfe dankbar.

Gruß
Alida
  Mit Zitat antworten Zitat