unit pingpongq;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls, IdBaseComponent, IdComponent,
IdTCPConnection, IdTCPClient, IdTCPServer, IdIPWatch, IdSocketHandle,
DXClass;
type
TForm1 =
class(TForm)
IdTCPClient1: TIdTCPClient;
IdTCPServer1: TIdTCPServer;
Edit1: TEdit;
IdIPWatch1: TIdIPWatch;
Label1: TLabel;
Label2: TLabel;
procedure FormCreate(Sender: TObject);
procedure FormKeyDown(Sender: TObject;
var Key: Word;
Shift: TShiftState);
procedure FormKeyUp(Sender: TObject;
var Key: Word;
Shift: TShiftState);
procedure IdTCPServer1Execute(AThread: TIdPeerThread);
procedure Edit1KeyDown(Sender: TObject;
var Key: Word;
Shift: TShiftState);
procedure IdTCPClient1Connected(Sender: TObject);
private
LastStep: DWord;
procedure Timer(Sender: TObject;
var Done: Boolean);
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
y,p2top: Integer;
p1_oben, p1_unten, senden_oben, senden_unten: Boolean;
Player1, Player2: TImage;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var
Binding: TIdSocketHandle;
//Für die servereinstellungen
begin
//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;
//server einstellen:
IdTCPServer1.Bindings.Clear;
Binding := IdTCPServer1.Bindings.Add;
Binding.IP := idIPWatch1.LocalIP;
Binding.Port := 9099;
IDTCPServer1.Active := true;
Application.OnIdle := Timer;
DoubleBuffered := 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;
//client connected und gibt die posi vom eigenen schläger an
procedure TForm1.IdTCPClient1Connected(Sender: TObject);
begin
IdTCPClient1.WriteLn('
160');
end;
procedure TForm1.FormKeyDown(Sender: TObject;
var Key: Word;
Shift: TShiftState);
begin
if Key = VK_Space
then
Begin
End;
{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;
senden_oben := true;
End;
if Key = VK_Down
then
Begin
p1_unten := true;
senden_unten := true;
End;
end;
//hört mit den obigen aktionen auf
procedure TForm1.FormKeyUp(Sender: TObject;
var Key: Word;
Shift: TShiftState);
begin
if Key = VK_UP
then
Begin
p1_oben := false;
senden_oben := false;
End;
if Key = VK_Down
then
Begin
p1_unten := false;
senden_unten := false;
End;
end;
{server nimmt gegn. nachricht auf und verarbeitet sie zu
der posi vom gegn. schläger}
procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
var
empfangen:
String;
begin
while Form1.IdTCPClient1.Connected
do
Begin
empfangen := AThread.Connection.ReadLn;
p2top := StrToInt(empfangen);
Player2.Top := p2top;
End;
end;
{application.onIdle event fungiert als timer:
entstehung und übermittlung der eigenen bewegung}
procedure TForm1.Timer(Sender: TObject;
var Done: Boolean);
begin
Done:=false;
If GetTickCount-LastStep > 10
then //in 10ms ein schritt
Begin
y := 7;
if (p1_oben)
and (Player1.Top > 11)
then
Player1.Top := Player1.Top - y;
if (p1_unten)
and (Player1.Top < 379)
then
Player1.Top := Player1.Top + y;
Label1.Caption := IntToStr(Player1.Top);
if IdTCPClient1.Connected
then
Begin
if senden_oben
then
IdTCPClient1.WriteLn(IntToStr(Player1.Top));
if senden_unten
then
IdTCPClient1.WriteLn(IntToStr(Player1.Top));
End;
Label2.Caption := IntToStr(Player2.Top);
LastStep:= GetTickCount;
//neue Start Zeit
end;
end;
end.