Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Multimedia (https://www.delphipraxis.net/16-multimedia/)
-   -   Delphi Nicht einfallswinkel = ausfallswinkel bei Pong (https://www.delphipraxis.net/62231-nicht-einfallswinkel-%3D-ausfallswinkel-bei-pong.html)

NMR 1. Feb 2006 16:12


Nicht einfallswinkel = ausfallswinkel bei Pong
 
Hi
ich habe mal ne frage
wie schaff ich das bei dem dem Spiel pong das der einfallswinkel nicht gleich ausfallswinkel ist?
Vll. mit random nur wie?
ein teil meinses Delphi Quelltextextes..
SCHON MAL DANKE!!!

Delphi-Quellcode:
procedure TForm1.Timer1Timer(Sender: TObject);

begin

(...)
      if (Ball.Left < 0) then begin
        scorep2 := scorep2 + 1;
        Ball.Left := 320 ;
        xspeed := 1;
        yspeed := 2;
         exit;

  end;

      if (Ball.Left > 800) then begin
        scorep1 := scorep1 + 1;
        Ball.Left := 320 ;
        xspeed := -1;
        yspeed := 2;
         exit;
end;

   if yspeed = 0 then yspeed := 2;
   if xspeed = 0 then xspeed := 5;
      Ball.Top := Ball.Top + yspeed;
      Ball.Left := Ball.Left + xspeed;
   if (Ball.Top > 500)
     or (Ball.Top <= 0)
       then yspeed := -yspeed;

   if (Ball.Left < shape1.Left + 22)
     and (Ball.Top > shape1.Top)
     and (Ball.Top < shape1.Top + 90)
      then xspeed := + speed;

   if (Ball.Left > shape2.Left - 22)
      and (Ball.Top > shape2.Top)
      and (Ball.Top < shape2.Top + 90)
        then xspeed := - speed;

   if (shape2.Top + 28 > Ball.Top)
       and (shape2.Top > 0)
         then shape2.Top := shape2.Top - dif;

   if (shape2.Top + 28 < Ball.Top)
       and (shape2.Top < 444)
         then shape2.Top := shape2.Top + dif;

(...)
     
end;

JamesTKirk 1. Feb 2006 16:55

Re: Nicht einfallswinkel = ausfallswinkel bei Pong
 
Ich an deiner Stelle würde die Verschiebung des Balles mit Sinus und Kosinus erledigen... Braucht man allerdings an kleinen Umweg:

Delphi-Quellcode:
var x,y,angle:Single; //globale Variablen
//Der Winkel ist im Bogenmaß anzugeben...

procedure TForm1.Timer1OnTimer(...);
(...)
begin
(...)
  x:=x+cos(angle);
  y:=y+sin(angle);

  Ball.Top:=Trunc(y); //Es geht auch Round(y)
  Ball.Left:=Trunc(x);
(...)
end;
Und die Sache mit dem Abprallwinkel machst du abhängig davon, wie weit der Ball von der Mitte des Schlägers entfernt aufkommt... zB:

Delphi-Quellcode:
//wir sind in der Timer Prozedur
//Als Beispiel: der Ball kommt von links an den rechten Schläger
  if Ball.Left+Ball.Width=Shape2.Left then begin //Ball trifft Schläger
   if (Ball.Top+Ball.Height>=Shape2.Top) and (Ball.Top<=Shape2.Top+Shape2.Height) then begin
    //Ball ist auf Höhe des Schlägers
   
    //Berechnung des neuen Winkels
    angle:=angle+pi/2*((Shape.Top+Shape.Height/2)-(Ball.Top+Ball.Height/2))/10;
    //Ist jetzt nur ein Beispiel...
   end;
  end;

  //Hier dann die Positionsberechnung von oben
So müsste es in groben Zügen funktionieren... Habs jetzt grad net ausgetestet, du musst aber vermutlich ein bisschen mit den Zahlen rumspielen ;)
Falls du Sin(x) und Cos(x) nicht finden kannst, binde die Math Unit ein... :-D

NMR 1. Feb 2006 21:03

Re: Nicht einfallswinkel = ausfallswinkel bei Pong
 
jo danke schön!!!

würd am liebsten in dem stück vll was ändern mit random(..)

Delphi-Quellcode:
if yspeed = 0 then yspeed := 2;
   if xspeed = 0 then xspeed := 5;
      Ball.Top := Ball.Top + yspeed;
      Ball.Left := Ball.Left + xspeed;
   if (Ball.Top > 500)
     or (Ball.Top <= 0)
       then yspeed := -yspeed;

NMR 2. Feb 2006 19:33

Re: Nicht einfallswinkel = ausfallswinkel bei Pong
 
Jp besten dank nochmal hat geklappt wie ich es wollte

JamesTKirk 2. Feb 2006 20:12

Re: Nicht einfallswinkel = ausfallswinkel bei Pong
 
Dann ist ja alles bestens :mrgreen: Viel Erfolg noch bei dem Klassiker :zwinker:


Alle Zeitangaben in WEZ +1. Es ist jetzt 04:34 Uhr.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz