Thema: Delphi ping-pong->übermittlung

Einzelnen Beitrag anzeigen

mangaka

Registriert seit: 9. Apr 2005
44 Beiträge
 
Delphi 7 Personal
 
#6

Re: ping-pong->übermittlung

  Alt 6. Mai 2005, 19:25
das ist der neue code und es ist immernoch nicht synchron:
ich habs versucht die posi des schlägers schon vorher zu senden, aber es geht nicht...
das beste wäre es ich benutze nanosekunden, aber wenn ich da 1/10000000 hinschreibe ändert sich nicht wirklich was. meine vermutung -> es geht nicht mit nanosek. ^^

vllt habt ihr noch ne idee

Code:
unit pp_mpq;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, IdSocketHandle, StdCtrls, IdIPWatch, IdTCPServer,
  IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
  ExtCtrls;

type
  TForm1 = class(TForm)
    IdTCPClient1: TIdTCPClient;
    IdTCPServer1: TIdTCPServer;
    IdIPWatch1: TIdIPWatch;
    Edit1: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure Edit1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure IdTCPServer1Execute(AThread: TIdPeerThread);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    Zeit_1: DWord;
    Zeit_2: DWord;
    procedure Timer(Sender: TObject; var Done: Boolean);
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  Player1, Player2: TImage;
  p1_oben, p1_unten: Boolean;
  p2_top: Integer;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  Binding: TIdSocketHandle;
begin
p1_oben := false;
p1_unten := false;
p2_top := 160;
Application.OnIdle := Timer;

DoubleBuffered := True;

//server einstellen:
  IdTCPServer1.Bindings.Clear;
  Binding := IdTCPServer1.Bindings.Add;
  Binding.IP := idIPWatch1.LocalIP;
  Binding.Port := 9099;
  IDTCPServer1.Active := true;
//

//grafik: player1
Player1 := TImage.Create(Form1);
Player1.Name := 'player1';
Player1.Parent := Form1;
Player1.Top := 160;
Player1.Left := 10;
Player1.Picture.LoadFromFile('player1.bmp');
Player1.Visible := true;

//grafik: player2
Player2 := TImage.Create(Form1);
Player2.Name := 'player2';
Player2.Parent := Form1;
Player2.Top := 160;
Player2.Left := Form1.ClientWidth - 20;
Player2.Picture.LoadFromFile('player2.bmp');
Player2.Visible := true;
end;

//zum gegn. server verbinden
procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = VK_Return then
    Begin
      IdTCPClient1.Host := Edit1.Text;
      IdTCPClient1.Port := 9099;
      IdTCPClient1.Connect;

      Edit1.Enabled := false;
    End;
end;
//hört mit den obigen aktionen auf
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
{p1_oben/p1_unten bewegen den eigenen schläger
 mit hilfe von senden_oben/senden_unten senden
 wir unsere posi}
  if Key = VK_UP then
    Begin
      p1_oben := true;
    End;

  if Key = VK_Down then
    Begin
      p1_unten := true;
    End;
end;

procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
begin
  while Form1.IdTCPClient1.Connected do
    Begin
      p2_top := StrToInt(AThread.Connection.ReadLn);
    End;
end;

procedure TForm1.Timer(Sender: TObject; var Done: Boolean);
begin
Done:=false;

  If GetTickCount - Zeit_1 > 10 then //in 10ms ein schritt
    Begin
        if (p1_oben) and (Player1.Top > 11) then
          Begin
            Player1.Top := Player1.Top - 7;
            p1_oben := false;
          End;

        if (p1_unten) and (Player1.Top < 379) then
          Begin
            Player1.Top := Player1.Top + 7;
            p1_unten := false;
          End;

      Label1.Caption := IntToStr(Player1.Top);
      Label2.Caption := IntToStr(Player2.Top);
      Zeit_1:= GetTickCount; //neue Start Zeit ermitteln
    End;

  if GetTickCount - Zeit_2 > 1 then
    Begin
      if IdTCPClient1.Connected then
        IdTCPClient1.Writeln(IntToStr(Player1.Top));

      Player2.Top := p2_top;
      Zeit_2 := GetTickCount;
    End;

end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
IdTCPClient1.Disconnect;
end;

end.
  Mit Zitat antworten Zitat