GNet 2 Player Pong
BlitzMax Forums/BlitzMax Beginners Area/GNet 2 Player Pong
| ||
Maybe some of you guys could learn from this code: server: ' gnetserver AppTitle="GNet Server Example" Graphics 640,480,0,80 Host=CreateGNetHost() If Host Print "Einen Server erstellt" Else Print "Konnte Server nicht erstellen" EndIf player1:TGNetObject=CreateGNetObject(Host) Local player_x:Float=0 Local player_y:Float=460 Local ball_x:Float=320 Local ball_y:Float=240 SeedRnd(MilliSecs()) Local ball_dirx:Float=RndFloat() Local ball_diry:Float=RndFloat() Local punktestand:Int=0 Local punktestand_him:Int=0 Const GNET_PLAYER_X=0 Const GNET_PLAYER_Y=1 Const GNET_BALL_X=2 Const GNET_BALL_Y=3 Const GNET_BALL_DIRX=4 Const GNET_BALL_DIRY=5 Const GNET_PUNKTESTAND_ME=6 Const GNET_PUNKTESTAND_HIM=7 SetGNetFloat player1,GNET_BALL_DIRX,ball_dirx SetGNetFloat player1,GNET_BALL_DIRY,ball_diry erfolg=GNetListen(Host,12345) If erfolg Print "Server horcht nun auf den Port 12345" Else Print "Konnte Socket nicht binden" EndIf While Not KeyDown(KEY_ESCAPE) Cls GNetSync(Host) If (GNetAccept(Host)) Print "Ein Client will sich verbinden" End If player_x=MouseX() 'player_y=MouseY() SetGNetFloat player1,GNET_PLAYER_X,player_x SetGNetFloat player1,GNET_PLAYER_Y,player_y For Local obj:TGNetObject=EachIn GNetObjects( host,GNET_ALL ) If obj=player1 'DrawText "Me",player_x,player_y DrawRect(player_x,player_y,50,20) Else 'DrawText "Him",GetGNetFloat(obj,GNET_PLAYER_X),GetGNetFloat(obj,GNET_PLAYER_Y) Local posenemy_x=GetGNetFloat(obj,GNET_PLAYER_X) Local posenemy_y=GetGNetFloat(obj,GNET_PLAYER_Y) DrawRect(posenemy_x,posenemy_y,50,20) If ball_x>=posenemy_x And ball_x<=posenemy_x+50 And ball_y<=20 And ball_y>=15 ball_diry=RndFloat()+1 EndIf EndIf Next ball_x:+ball_dirx ball_y:+ball_diry If ball_x>640 Then ball_dirx=-RndFloat()-1 If ball_x<0 Then ball_dirx=RndFloat()+1 If ball_y>480 ball_diry=-RndFloat()-1 punktestand_him:+1 SetGNetInt player1,GNET_PUNKTESTAND_HIM,punktestand_him ball_y=320 EndIf If ball_y<0 ball_diry=RndFloat()+1 punktestand:+1 SetGNetInt player1,GNET_PUNKTESTAND_ME,punktestand ball_y=320 EndIf If ball_x>=player_x And ball_x<=player_x+50 And ball_y<=460-15 And ball_y>=455-15 ball_diry=-RndFloat()-1 EndIf SetGNetFloat player1,GNET_BALL_X,ball_x SetGNetFloat player1,GNET_BALL_Y,ball_y DrawText "Me: "+punktestand+" Him: "+punktestand_him,10,10 DrawOval ball_x,ball_y,30,30 Flip FlushMem() Wend CloseGNetHost(Host) Print "Server runtergefahren" client: ' gnetclient AppTitle="GNet Client Example" Graphics 640,480,0,80 Host=CreateGNetHost() player2:TGNetObject=CreateGNetObject(Host) Local player_x:Int=0 Local player_y:Int=20 Const GNET_PLAYER_X=0 Const GNET_PLAYER_Y=1 Const GNET_BALL_X=2 Const GNET_BALL_Y=3 Const GNET_BALL_DIRX=4 Const GNET_BALL_DIRY=5 Const GNET_PUNKTESTAND_ME=7 Const GNET_PUNKTESTAND_HIM=6 'Client=GNetConnect(Host,"127.0.0.1",12345) Client=GNetConnect(Host,"regaa.dyndns.org",12345) While Not KeyDown(KEY_ESCAPE) Cls GNetSync(Host) player_x=MouseX() 'player_y=MouseY() SetGNetFloat player2,GNET_PLAYER_X,player_x SetGNetFloat player2,GNET_PLAYER_Y,player_y For Local obj:TGNetObject=EachIn GNetObjects( host,GNET_ALL ) If obj=player2 'DrawText "Me",player_x,player_y DrawRect(player_x,player_y,50,20) Else 'DrawText "Him",GetGNetFloat(obj,GNET_PLAYER_X),GetGNetFloat(obj,GNET_PLAYER_Y) DrawRect(GetGNetFloat(obj,GNET_PLAYER_X),GetGNetFloat(obj,GNET_PLAYER_Y),50,20) DrawOval(GetGNetFloat(obj,GNET_BALL_X),GetGNetFloat(obj,GNET_BALL_Y),30,30) DrawText "Me: "+GetGNetInt(obj,GNET_PUNKTESTAND_ME)+" Him: "+GetGNetInt(obj,GNET_PUNKTESTAND_HIM),10,10 EndIf Next Flip FlushMem() Wend I am too lazy to remove the german comments :P . |
| ||
Thanks, Might try this out once I get more advanced. Below is the English version of the server (no German in the client). server ' gnetserver AppTitle="GNet Server Example" Graphics 640,480,0,80 Host=CreateGNetHost() If Host Print "Server initiated" Else Print "Server initialization problem" EndIf player1:TGNetObject=CreateGNetObject(Host) Local player_x:Float=0 Local player_y:Float=460 Local ball_x:Float=320 Local ball_y:Float=240 SeedRnd(MilliSecs()) Local ball_dirx:Float=RndFloat() Local ball_diry:Float=RndFloat() Local punktestand:Int=0 Local punktestand_him:Int=0 Const GNET_PLAYER_X=0 Const GNET_PLAYER_Y=1 Const GNET_BALL_X=2 Const GNET_BALL_Y=3 Const GNET_BALL_DIRX=4 Const GNET_BALL_DIRY=5 Const GNET_PUNKTESTAND_ME=6 Const GNET_PUNKTESTAND_HIM=7 SetGNetFloat player1,GNET_BALL_DIRX,ball_dirx SetGNetFloat player1,GNET_BALL_DIRY,ball_diry erfolg=GNetListen(Host,12345) If erfolg Print "Server listening on Port 12345" Else Print "Couldn't bind socket" EndIf While Not KeyDown(KEY_ESCAPE) Cls GNetSync(Host) If (GNetAccept(Host)) Print "A client wants to connect" End If player_x=MouseX() 'player_y=MouseY() SetGNetFloat player1,GNET_PLAYER_X,player_x SetGNetFloat player1,GNET_PLAYER_Y,player_y For Local obj:TGNetObject=EachIn GNetObjects( host,GNET_ALL ) If obj=player1 'DrawText "Me",player_x,player_y DrawRect(player_x,player_y,50,20) Else 'DrawText "Him",GetGNetFloat(obj,GNET_PLAYER_X),GetGNetFloat(obj,GNET_PLAYER_Y) Local posenemy_x=GetGNetFloat(obj,GNET_PLAYER_X) Local posenemy_y=GetGNetFloat(obj,GNET_PLAYER_Y) DrawRect(posenemy_x,posenemy_y,50,20) If ball_x>=posenemy_x And ball_x<=posenemy_x+50 And ball_y<=20 And ball_y>=15 ball_diry=RndFloat()+1 EndIf EndIf Next ball_x:+ball_dirx ball_y:+ball_diry If ball_x>640 Then ball_dirx=-RndFloat()-1 If ball_x<0 Then ball_dirx=RndFloat()+1 If ball_y>480 ball_diry=-RndFloat()-1 punktestand_him:+1 SetGNetInt player1,GNET_PUNKTESTAND_HIM,punktestand_him ball_y=320 EndIf If ball_y<0 ball_diry=RndFloat()+1 punktestand:+1 SetGNetInt player1,GNET_PUNKTESTAND_ME,punktestand ball_y=320 EndIf If ball_x>=player_x And ball_x<=player_x+50 And ball_y<=460-15 And ball_y>=455-15 ball_diry=-RndFloat()-1 EndIf SetGNetFloat player1,GNET_BALL_X,ball_x SetGNetFloat player1,GNET_BALL_Y,ball_y DrawText "Me: "+punktestand+" Him: "+punktestand_him,10,10 DrawOval ball_x,ball_y,30,30 Flip FlushMem() Wend CloseGNetHost(Host) Print "Closing server" |
| ||
Runtergefahren is English? =] |
| ||
Sorry missed that :) I've translated now. |