Hallo,
hab ein kleines Tutorial für GameNet mit dieser Vorlage erstellt:
http://www.blitzmax.com/Commun...opic=51171
Hauptsächlich habe ich alles so angepasst das es mit der aktuellen Blitzmax Version 1.41 funktioniert.
Hinzukommen kommen sind noch ein paar Variablen und einiges an zusätzlichen deutschen Kommentaren.
Hauptsächlich vermittelt dieses Tutorial die Grundlegenden Kenntnisse um kleinere Netzwerkspiele zu realisieren. Der Quelltext ist hauptsächlich prodzedural gehalten (D.h der Code wird der Reihe nach von oben nach unten abgearbeitet).
Einzige Ausnahme ist beim Client der Typ/Klasse ganz am Ende.
Hoffe das hilft einigen das Prinzip zu verstehen.
Quelltext Server:
BlitzMax: [AUSKLAPPEN] [EINKLAPPEN] Strict
Local serverPort:Int = 8888 Local quitServer = False Local openServer:tgnethost
Local listener:tgnethost = CreateGNetHost() Print "Server wird gestartet ";Print""
If Not GNetListen(listener , serverPort) Print "Server konnte nicht gestartet werden:" Print "Port " + serverPort + " bereits belegt." quitServer = True CloseGNetHost(listener) Return Else openServer:tgnethost = listener EndIf
While Not quitServer GNetSync(openServer)
Local objectGList:TList = GNetObjects( openServer) For Local objectG:TGnetObject = EachIn objectGList Local state = GNetObjectState( objectG ) Select state Case GNET_CREATED Print "New object created" Case GNET_SYNCED Case GNET_MODIFIED Print GetGNetString(objectG,1)+" is moving" Case GNET_CLOSED Print "Connection closed to a peer" Case GNET_MESSAGE EndSelect Next
Delay(1) Wend
Quelltext Client:
BlitzMax: [AUSKLAPPEN] [EINKLAPPEN] Strict
Local serverPort:Int = 8888 Local quitClient = False Local serverAdresse:String = "localhost" Local connectTimeout:Int = 10000 Local session:tgnethost
Local connector:tgnethost = CreateGNetHost()
If Not GNetConnect(connector , serverAdresse , serverPort,connectTimeout) Print "Verbindung zum Server nicht möglich." Else session = connector EndIf
Local localplayer:NetObject = NetObject.Create(connector , "PlayerX" , 320 , 240)
Graphics 640,480,0
While Not quitClient GNetSync(connector)
Local olist:TList = GNetObjects( connector )
Local plist:TList = CreateList() For Local o:TGnetObject = EachIn olist Local state = GNetObjectState( o ) Select state Case GNET_CREATED If GetGNetInt( o , 0 ) = NetObject.kennung plist.addlast( o ) Print "Joined: "+GetGNetString(o,1) EndIf Case GNET_SYNCED If GetGNetInt( o, 0 ) = NetObject.kennung plist.addlast( o ) EndIf Case GNET_MODIFIED If GetGNetInt( o, 0 ) = NetObject.kennung plist.addlast( o ) EndIf Case GNET_CLOSED EndSelect Next
If KeyDown( KEY_LEFT ) Then LocalPlayer.move(-1,0) If KeyDown( KEY_RIGHT ) Then LocalPlayer.move(1,0) If KeyDown( KEY_UP ) Then LocalPlayer.move(0,-1) If KeyDown( KEY_DOWN ) Then LocalPlayer.move(0,1)
Local y = 10 For Local dp:TGnetObject = EachIn olist Local tmp:String = GetGNetString( dp,1 ) DrawText tmp, 10,y y:+17 Local xpos = GetGNetInt(dp,2) Local ypos = GetGNetInt(dp,3) DrawRect xpos - 3 , ypos - 12 , 7 , 12 DrawText tmp, xpos,ypos-30 Next DrawText "Players: "+plist.count(), 5,y
Flip ; Cls Wend
Type NetObject
Const kennung:Int = 0 Field nobj:TGNetObject Field xpos:Int Field ypos:Int Field name:String Function Create:NetObject( connection:TGNetHost, oname:String, x:Int, y:Int ) Local netObjekt:NetObject = New NetObject netObjekt.nobj = CreateGNetObject( connection ) SetGNetInt( netObjekt.nobj, 0, NetObject.kennung) netObjekt.place(x , y) netObjekt.rename(oname) Return netObjekt EndFunction
Method place( x:Int , y:Int ) xpos = x ypos = y SetGNetInt( nobj,2,x ) SetGNetInt( nobj,3,y ) EndMethod
Method Move( xoff:Int, yoff:Int ) xpos:+xoff ypos:+yoff SetGNetInt( nobj,2,xpos ) SetGNetInt( nobj,3,ypos ) EndMethod Method rename( oname:String ) name = oname SetGNetString( nobj,1,name ) EndMethod
EndType
Viel Spaß damit.
Gruß
maanee9
|