Autor Beitrag
mimi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3458

Ubuntu, Win XP
Lazarus
BeitragVerfasst: So 27.03.05 14:07 
Hallo,
ich möchte gerne Raumschiffe per zufall in den raum setzen(im moment brauche ich nur den x wert aber später auch den y wert)
dazu habe ich folgende function geschrieben:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
function TShipGame.RandomShipPos:TPoint;
var
  i,x,y,z:Integer;
  isOk,isOk1:Boolean;
begin
  z:=0;
  repeat
    isOk:=False;
    isOk1:=False;
    x:=Random(640);
    y:=Random(280);
    inc(z);

    for i:=0 to HIGH(Ships) do begin
      if ((x >= Ships[i].x) and (y >= Ships[i].y)) and ((x <= Ships[i].x+Ships[i].w) and (y <= Ships[i].y+Ships[i].h)) then begin
        isOk1:=True;
        break;
      end;
    end;

    if isOk1 = False then
      isOK:=True;
  until  (z >=6or (isOK = true);
  result.x:=x;
  result.y:=y;
end;

die auch manchmal funktioniert aber warum nicht immer ?
was habe ich falsch gemacht ?
mein ziel ist es die raumschiffe so per zufall zu verteieln das sie sich nicht gegenseitig berüheren kännen am besten noch mit einem abstand dawischen von 4 pixeln(x richtung)

_________________
MFG
Michael Springwald, "kann kein englisch...."
F34r0fTh3D4rk
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5284
Erhaltene Danke: 27

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: So 27.03.05 14:16 
mache doch eine while schleife, solange schiff 1 in der nähe von schiff 2 do x:= random(bla) y:= random(bla) und dann lässt du das schiff zeichen, bzw dann erst startest du kollisionsabfragen und dergleichen :)
mimi Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3458

Ubuntu, Win XP
Lazarus
BeitragVerfasst: So 27.03.05 14:22 
ich weiß was du meinst den abstand könnte ich auch nehmen, währe auch letzendlich pratkischer werde ich testen.....

_________________
MFG
Michael Springwald, "kann kein englisch...."
mimi Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3458

Ubuntu, Win XP
Lazarus
BeitragVerfasst: So 27.03.05 14:45 
jetzt habe ich es so gemacht:
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
function TShipGame.RandomShipPos:TPoint;
var
  i,x,y,z:Integer;
  isOk,isOk1:Boolean;
begin
  z:=0;
  repeat
    isOk:=False;
    isOk1:=False;
    x:=Random(540);
    y:=Random(280);
    inc(z);

    for i:=0 to HIGH(Ships) do begin
      if x-Ships[i].x > 0 then // Für die Linkge seite
        if x-Ships[i].x > 6 then
          isOK1:=False
        else
          isOK1:=True
      else begin
        if Ships[i].x-x > 6 then
          isOK1:=False
        else
          isOK1:=True;
      end;

      if x-(Ships[i].x+Ships[i].w) > 0 then // Für die Rechte seite
        if x-(Ships[i].x+Ships[i].w) > 6 then
          isOK1:=False
        else
          isOK1:=True

      else begin
        if (Ships[i].x+Ships[i].w)-x > 6 then
          isOK1:=False
        else
          isOK1:=True;
      end;

    end;

    if isOk1 = False then
      isOK:=True;
  until  (isOK = true);
  result.x:=x;
  result.y:=y;
end;

das gleiche ergbnis:
einige schiffe überscheiden sich trozdem, obwohl sie mind. 6 pixl abstand haben sollten, was mache ich falsch ???

_________________
MFG
Michael Springwald, "kann kein englisch...."