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:
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 >=6) or (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)