GNet 2 Player Pong

BlitzMax Forums/BlitzMax Beginners Area/GNet 2 Player Pong

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

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"