I once tryed a to make a network game in PureBasic, but I did'nt finish it.
It's only two dot's moving, but it might help you.
Code: Select all
Buffer.l = AllocateMemory(1000)
IP.s
Port.w
Event.l
ConnID.l
ClientID.l
Quit.b
IsServer.b
PreviousX.w
PreviousY.w
Player1X.w = 320
Player1Y.w = 120
Player2X.w = 320
Player2Y.w = 360
Procedure DrawTextXY(x.w, y.w, string.s)
Locate(x,y)
DrawText(string)
EndProcedure
OpenConsole()
ConsoleTitle("Game")
Print("Initializing the network... ")
If InitNetwork() = #False
PrintN("Error: Can't initialize the network.")
PrintN("Press enter to quit.")
Input()
End
EndIf
Print("Done")
PrintN("")
Print("Press 1 for server or 2 for client: ")
If Input() = "1" ;Server
PrintN("")
IsServer = #True
Print("Enter the network port: ")
Port = Val(Input())
PrintN("")
If CreateNetworkServer(Port)
PrintN("Server created.")
Else
PrintN("Error: Can't use that port.")
PrintN("Press enter to quit.")
Input()
End
EndIf
Else ;Client
PrintN("")
IsServer = #False
Print("Enter the network port: ")
Port = Val(Input())
PrintN("")
Print("Enter the network adress: ")
IP = Input()
PrintN("")
PrintN("Connecting to: "+IP+":"+Str(Port))
ConnID = OpenNetworkConnection(IP,Port)
If ConnID
PrintN("Client connected.")
Else
PrintN("Error: Can't find the server.")
PrintN("Press enter to quit.")
Input()
End
EndIf
EndIf
CloseConsole()
If InitKeyboard()=0 Or InitSprite()=0
MessageRequester("Error","Can't initialize the game, try updating DirectX.")
Quit = #True
ElseIf OpenScreen(640,480,16,"Game")=0
MessageRequester("Error","Can't open a 640x480 16bit screen.")
Quit = #True
EndIf
SetFrameRate(60)
While Quit = #False
;{/User input
ExamineKeyboard()
If KeyboardPushed(#PB_Key_Escape): Quit = #True: EndIf
If IsServer
PreviousX = Player1X
PreviousY = Player1Y
If KeyboardPushed(#PB_Key_Up)
Player1Y - 1
EndIf
If KeyboardPushed(#PB_Key_Down)
Player1Y + 1
EndIf
If KeyboardPushed(#PB_Key_Left)
Player1X - 1
EndIf
If KeyboardPushed(#PB_Key_Right)
Player1X + 1
EndIf
If Player1X <> PreviousX Or Player1Y <> PreviousY
SendNetworkString(ClientID,Str(Player1X)+","+Str(Player1Y)+"|")
EndIf
Else
PreviousX = Player2X
PreviousY = Player2Y
If KeyboardPushed(#PB_Key_Up)
Player2Y - 1
EndIf
If KeyboardPushed(#PB_Key_Down)
Player2Y + 1
EndIf
If KeyboardPushed(#PB_Key_Left)
Player2X - 1
EndIf
If KeyboardPushed(#PB_Key_Right)
Player2X + 1
EndIf
If Player2X <> PreviousX Or Player2Y <> PreviousY
SendNetworkString(ConnID,Str(Player2X)+","+Str(Player2Y)+"|")
EndIf
EndIf
;}
;{/Game logic
If IsServer
Event = NetworkServerEvent()
If Event
ClientID = NetworkClientID()
Select Event
Case 1: ;New client connected
Case 2: ;Raw data
ReceiveNetworkData(ClientID,Buffer,1000)
Player2X = Val( StringField(PeekS(Buffer),1,",") )
Player2Y = Val( StringField(PeekS(Buffer),2,",") )
Case 3: ;A file has been recieved
Case 4: ;Client deconnection
EndSelect
EndIf
Else
Event = NetworkClientEvent(ConnID)
If Event
Select Event
Case 2: ;Raw data
ReceiveNetworkData(ConnID,Buffer,1000)
Player1X = Val( StringField(PeekS(Buffer),1,",") )
Player1Y = Val( StringField(PeekS(Buffer),2,",") )
Case 3: ;A file has been recieved
Case 5: ;A string has been recieved
EndSelect
EndIf
EndIf
;}
;{/Draw screen
FlipBuffers(): Delay(2)
ClearScreen(0,0,0)
StartDrawing(ScreenOutput())
DrawingMode(1)
FrontColor(255,255,255)
DrawTextXY(0,16,"P1x: "+Str(Player1X)+" P1y: "+Str(Player1Y))
DrawTextXY(0,32,"P2x: "+Str(Player2X)+" P2y: "+Str(Player2Y))
FrontColor(128,0,0)
Circle(Player1X,Player1Y,10)
FrontColor(0,0,128)
Circle(Player2X,Player2Y,10)
StopDrawing()
;}
Wend
If IsServer
CloseNetworkServer()
Else
CloseNetworkConnection(ConnID)
EndIf
I like logic, hence I dislike humans but love computers.