Autor Beitrag
Symbol91
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 18



BeitragVerfasst: Fr 07.11.08 12:14 
Hi ich hab langeweile weil ich bei nem anderen projekt nicht weiterkomme und wollte jetzt mal ein kleines spiel programmieren .
Und zwar man hat ein kleines auto das durch einen timer gerade aus fährt und mit den pfeiltasten nach oben und unten steuerbar ist.
jetzt habe ich an verschiedenen stellen des birlschirms hindernisse (Images ) platziert und möchte, dass wenn das auto so ein hinderniss rammt es 10 pixel zurückgesetzt wird .
Mein problem besteht in letzteren.
Wie bekomme ich es hin das wenn mein auto ein hinderniss rammt es 10 pixel zurückgesetzt wird
Danke schonmal
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Fr 07.11.08 12:17 
Das kommt ja ganz darauf an wie du das programmiert hast... :nixweiss:
Grundsätzlich müsstest du bei jeder Bewegung eine entsprechende Prüfung einbauen, das könntest du ja zentral in eine Methode verpacken und diese bei jeder Bewegung aufrufen.
Symbol91 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 18



BeitragVerfasst: Fr 07.11.08 12:25 
Hier hab ich mal den quelltext :

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:
48:
49:
50:
51:
type
  TForm1 = class(TForm)
    ImAuto: TImage;
    Timer1: TTimer;
    ImBlitz1: TImage;
    ImBlitz3: TImage;
    ImBlitz2: TImage;

    procedure Timer1Timer(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);


  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}



procedure TForm1.Timer1Timer(Sender: TObject);
begin
  ImAuto.Left := imAuto.Left + 1;
end;

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  case key of
    vk_Down: ImAuto.Top := ImAuto.Top + 2;
    vk_up: ImAuto.Top := ImAuto.Top - 2;
  end;
end;








end.


Moderiert von user profile iconAXMD: Delphi-Tags hinzugefügt
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Fr 07.11.08 12:33 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
function CheckCollision(uCar, uObstacle: TImage): Boolean;
begin
  Result := (uCar.Left + uCar.Width >= uObstacle.Left)
    and (uCar.Left < uObstacle.Left + uObstacle.Width);
end;
Damit wird geprüft ob die rechte Seite des Autos weiter rechts ist als die linke Seite des Hindernisses. Aufurfen kannst du das dann mit
ausblenden Delphi-Quelltext
1:
2:
  if CheckCollision(ImAuto, ImBlitz1) then
    // mach was bei Kollision
Nicht überprüft wird, ob das Auto vertikal überhaupt auf der Höhe des Hindernisses ist, das musst du noch einbauen.
Symbol91 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 18



BeitragVerfasst: Fr 07.11.08 12:54 
Ok aber was soll result sein =? soll ich dort mein ziel eintragen also was er tun soll wenn dies passiert , allerdings schreibe ich das doch erst beim aufruf hin oder etwas nicht
aber danke hat mir schonmal was geholfen
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Fr 07.11.08 12:57 
Result ist das Ergebnis, das die Funktion CheckCollision zurücklifert, wenn du es wie im zweiten Quelltext gezeigt aufrufst. An der Stelle wo du dann bei einer Kollision etwas machen musst steht ja der entsprechende Kommentar.

Die Frage zeigt mir, dass du diesen Link sicherlich brauchen kannst:
crashkurs.christian-stelzmann.de
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Fr 07.11.08 13:11 
Es ist sinnvoller die Diskussion direkt hier weiterzuführen, erstens kann dann auch jemand anderes antworten ggf. und zweitens nutzt es vielleicht einem späteren Leser ;-).
user profile iconSymbol91 (PN) hat folgendes geschrieben:
ja also arbeite erst 1 monat mit delphi im informartik untericht bin deshalb noch nit so vertraut mit delphi hatten vorher immer logo gemacht und des is jez schon ne umstellung xDD
des is mien quelltext bis jez das result fehlt aber noch und der aufruf :


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:
procedure Zusammenstoss  ;

  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}



procedure TForm1.Timer1Timer(Sender: TObject);
begin
  ImAuto.Left := imAuto.Left + 1;

end;

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  case key of
    vk_Down: ImAuto.Top := ImAuto.Top + 2;
    vk_up: ImAuto.Top := ImAuto.Top - 2;
  end;
end;


procedure TForm1.Zusammenstoss;
begin
    ImAuto.left + ImAuto.width >= ImBlitz1.left
    and ImAuto.left < ImBlitz1.left + ImBlitz1.width ;
end;





end.


könntest du mir vil sagen was ich jeztz bei dem "result" hinschreiben muss habe den crashkurs zwar gelesen aba bin darauf nit so schlau geworden am beispiel selbst würde ich des vil besser verstehen
Der von mir gepostete Quelltext war ein vollständiges Beispiel, du musst daran erstmal gar nix ändern...
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
function CheckCollision(uCar, uObstacle: TImage): Boolean;
begin
  Result := (uCar.Left + uCar.Width >= uObstacle.Left)
    and (uCar.Left < uObstacle.Left + uObstacle.Width);
end;

procedure TForm1.Timer1Timer(Sender: TObject); 
begin 
  ImAuto.Left := imAuto.Left + 1; 
  if CheckCollision(ImAuto, ImBlitz1) then
    // mach was bei Kollision
end;
Symbol91 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 18



BeitragVerfasst: Fr 07.11.08 13:20 
ok jetzt schreibt er mir beim timer check collision undefinierter bezeichner



public
{ Public-Deklarationen }


end;

var
Form1: TForm1;

implementation

{$R *.DFM}



procedure TForm1.Timer1Timer(Sender: TObject);
begin
ImAuto.Left := imAuto.Left + 1;
if CheckCollision(ImAuto, ImBlitz1) then //fehler: Undefinierter bezeichner
ImAuto.left := ImAuto.left -10
end;

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
case key of
vk_Down: ImAuto.Top := ImAuto.Top + 2;
vk_up: ImAuto.Top := ImAuto.Top - 2;
end;
end;

function CheckCollision(uCar, uObstacle: TImage): Boolean;
begin
Result := (uCar.Left + uCar.Width >= uObstacle.Left)
and (uCar.Left < uObstacle.Left + uObstacle.Width);
end;


end.
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Fr 07.11.08 14:22 
Weil du CheckCollision unterhalb von Timer1Timer geschrieben hast. Woher soll Delphi wissen, dass das noch irgendwo kommt? ;-)

Btw. solltest du Quelltext hier im Forum so schreiben: <span class="inlineSyntax"><span class="codecomment">{PROTECTTAG802a60dea5ec013f1452fd709feba65b}</span></span>
Dann sieht der so aus wie du es weiter oben siehst ;-).
Symbol91 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 18



BeitragVerfasst: Fr 07.11.08 14:27 
AHHHHHHHHHHh KKKKKK DANKE !!!!
Hast mir echt super geholfen nun klappt es danke !!!!!
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Fr 07.11.08 14:32 
Aber wie gesagt: Es wird so noch nicht getestet, ob das Auto auf gleicher Höhe ist! Wenn du das nicht noch einbaust kann das Auto auch nicht oben oder unten vorbeifahren.
Dafür müsstest du nicht nur Left / Width und damit die linke und rechte Kante testen sondern auch noch Top / Height, also die obere und untere Kante. (Entweder die obere oder die untere Kante des Autos müssen ja für eine Kollision zwischen der oberen und unteren Kante des Hindernisses sein.)
Symbol91 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 18



BeitragVerfasst: Fr 07.11.08 14:34 
jojo aber mein erstes problem ist schonmal gelöst xDDDD
jetzt muss ich das gleiche bei function noch mit .top mahcn oder
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Fr 07.11.08 14:37 
Ja genau, so ähnlich, nur dass dort dann eben (Top vom Auto größer als Top vom Hindernis) und (Top vom Auto kleiner als Top + Height vom Hindernis) sein muss ODER das selbe mit Top + Height vom Auto ;-).
Symbol91 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 18



BeitragVerfasst: Fr 07.11.08 14:44 
jojo hab ich aber irgendwie fährt der beim start direkt zurück der sieht irgendwie den kompletten bildschirm als dieses image xDD

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
function CheckCollision(uCar, uObstacle: TImage): Boolean;
begin
  Result := (uCar.Left + uCar.Width >= uObstacle.Left)
    and (uCar.Left < uObstacle.Left + uObstacle.Width);
  Result := (uCar.top + uCar.height >= uObstacle.top)
    and (uCar.top < uObstacle.top + uObstacle.height);
end;
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Fr 07.11.08 16:23 
Du musst das mit dem ersten verknüpfen, dein Quelltext ignoriert das zuerst zugewiesene Result, und das steht auch als Compilerwarnung da ;-):
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
function CheckCollision(uCar, uObstacle: TImage): Boolean;
begin
  Result := (uCar.Left + uCar.Width >= uObstacle.Left)
    and (uCar.Left < uObstacle.Left + uObstacle.Width)
    and (((uCar.top + uCar.height >= uObstacle.top)
        and (uCar.top < uObstacle.top + uObstacle.height))
      or (()
        and ()));
end;
Da fehlt noch die Prüfung der Unterkante des Fahrzeugs ;-). Es reicht ja wenn Ober- ODER Unterkante auf Höhe des Hindernisses sind. Möglich ist auch
ausblenden Delphi-Quelltext
1:
2:
  Result := X;
  Result := Result and XY;
Also du kannst das auch nacheinander machen und musst nicht alles in einem zusammenbasteln.
Symbol91 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 18



BeitragVerfasst: Fr 07.11.08 16:58 
jo ok danke dir spiel ist nun fertig xDDDD
THX !!!