Einzelnen Beitrag anzeigen

maddom

Registriert seit: 7. Apr 2009
3 Beiträge
 
#1

pong-bug! suche hilfe

  Alt 23. Jun 2009, 13:45
hallo leute,
habe mal ein pong spiel programmiert, doch wenn der ball an die schmale seite des paddels kommt, dopst der imm so komisch hin und her...
das spiel ist auch sonst noch nicht ganz fertig, zum beispiel bin ich mir noch nicht sicher, wie ich die KI machen soll(im moment noch unbesiegbar ).
habe mal das ganze ding gepackt und dazugeladen, wär nett, wenn sich das jemand mal anschauen würde(ziemlich übersichtlich, da viele kommentare).
danke schonmal im vorraus!

falls ihr es nicht runterladen woillt wegen virengefahr, hier auch noch der quellcode:

Delphi-Quellcode:
unit mPong;

interface

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

type
  TForm1 = class(TForm)
    Timer1: TTimer;
    Ball: TShape;
    PaddelL: TShape;
    PaddelR: TShape;
    labPunkteL: TLabel;
    labPunkteR: TLabel;
    butStart: TButton;
    labZeit: TLabel;
    labGewVer: TLabel;
    labBeruehrungL: TLabel;
    labBeruehrungR: TLabel;
    procedure Timer1Timer(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure butStartClick(Sender: TObject);
  private
    { Private declarations }
    StartZeit : TDateTime;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  PunkteL : integer;
  PunkteR : integer;
  beruehrung : integer;
  beruehrungL : integer;


implementation
  var x,y : Integer;
{$R *.dfm}

procedure TForm1.Timer1Timer(Sender: TObject);
begin
 //punktelabeleinstellungen
 labPunkteL.Caption := inttostr(PunkteL);
 labPunkteR.Caption := inttostr(PunkteR);
 //Bewegung
 ball.Left := ball.Left + x ;
 ball.Top := ball.Top + y ;

 //abdopsen vom Rand
 if Ball.Top >= 440
  then y := -Y ;
 if Ball.Top <= 0
  then y := -Y ;

 //abdopsen von paddel links
 if (Ball.Left <= 49) and (ball.Top +9 > paddelL.Top) and (ball.Top +9 < paddelL.Height + paddelL.top)
  Then begin
  x := -X ;
  beruehrungl := beruehrungL+1;
  end ;
              //abdopsen an den schmalen rändern L
 if ((ball.left +9)<(paddell.Left +25)) and ((ball.Left+9)>paddell.Left) and ((ball.Top+17)>=paddell.Top)
  then y := -y ;
 if ((ball.left +9)<(paddell.Left +25)) and ((ball.Left+9)>paddell.Left) and ((ball.Top)<=(paddell.Top+113))
  then y := -y ;

 //abdopsen von paddel rechts
 if (ball.Left >= 599) and (Ball.Top > paddelR.Top) and (ball.Top < (paddelR.Height +paddelR.top))
  then begin
  x:= -x;
  Beruehrung := Beruehrung +1;
  end
               //abdopsen an den schmalen rändern R
 if ((ball.left+9)>paddelr.Left) and ((ball.left +9)<(paddelr.Left +25)) and ((ball.Top +17)>=paddelr.Top)
 then y := -y;
 if ((ball.left+9)>paddelr.Left) and ((ball.left +9)<(paddelr.Left +25)) and (ball.Top<=paddelr.Top+113)
 then y := -y;
 



 //ball ins aus rechts
 if ball.Left >= form1.Width
  then begin
  ball.Left :=form1.Width div 2 ;
  ball.Top := form1.Height div 2 ;
  x := -x;
  PunkteL := punktel+1;
  beruehrung := 0;
  end ;

 //ball ins aus links
 if ball.left <= 0
  then begin
  ball.Left :=form1.Width div 2 ;
  ball.Top := form1.Height div 2 ;
  x := -x;
  PunkteR:= PunkteR +1 ;
  beruehrung := 0;
  end ;

 //Zeitanzeige
 labZeit.Caption := TimeToStr (now-StartZeit);
 //gewonnen!
 if PunkteL = 2
  then begin
  labgewver.Caption := 'GEWONNEN!';
     {labgewver.Caption := 'SPIELER 1 GEWONNEN!';  FÜR 2 SPIELER}
  labgewver.Visible :=true;
  timer1.Enabled := false;
  butStart.Visible := true;
  end;

 //Verloren...
 if PunkteR = 2
  then begin
  labgewver.Caption := 'VERLOREN!';
     {labgewver.Caption := 'SPIELER 2 GEWONNEN!';  FÜR 2 SPIELER }
  labgewver.Visible := true;
  timer1.Enabled := false;
  butStart.Visible := true;
  end ;

 //ballgeschwindigkiet
 if beruehrung = 0
  then timer1.Interval := 60;
 if beruehrung = 1
  then timer1.Interval := 55;
 if beruehrung = 2
  then timer1.Interval := 50;
 if beruehrung = 3
  then timer1.Interval := 45;
 if beruehrung = 4
  then timer1.Interval := 40;
 if beruehrung = 5
  then timer1.Interval := 35;
 if beruehrung = 6
  then timer1.Interval := 30;
 if beruehrung = 7
  then timer1.Interval := 25;
 if beruehrung = 8
  then timer1.Interval := 20;
 if beruehrung = 9
  then timer1.Interval := 17;

 //berührungszähler
 labBeruehrungL.caption := inttostr(beruehrungl);
 labberuehrungR.Caption := inttostr(beruehrung);

 //K I
 if x>=0 then paddelR.Top := Ball.Top -50;








end;

procedure TForm1.FormCreate(Sender: TObject);
begin
randomize;
x:= random(5) + 5;
y:= round(sqrt(10*10-x*x)) ;
PunkteL:= 0;
PunkteR:= 0;
end;


//paddel bewegen
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
If key=vk_Down
 then paddelL.Top := paddelL.Top + 20;
if key=vk_up
 then paddelL.Top := paddelL.Top - 20;
   {if key= VkKeyScan('-')
    then paddelr.Top := paddelr.Top + 20;    FÜR 2 SPIELER
    if key=VkKeyScan('+')
     then paddelr.Top := paddelr.Top - 20;}

end ;


procedure TForm1.butStartClick(Sender: TObject);
begin
  butstart.Visible := false;
  ball.Visible := true;
  labPunkteL.Visible := true;
  labpunkter.visible := true;
  paddell.Visible := true;
  paddelr.Visible := true;
  Timer1.Enabled := true;
  startzeit := now;
  PunkteL := 0;
  punkter := 0;
  labgewver.visible := false;
  labZeit.Visible := true;
  timer1.interval := 60;
  beruehrungl := 0;
  beruehrung := 0;
  labberuehrungL.Visible := True;
  labberuehrungR.Visible := True;

 
end;

end.
Angehängte Dateien
Dateityp: rar pong_178.rar (167,8 KB, 13x aufgerufen)
  Mit Zitat antworten Zitat