Has anyone done any multi-player game over network? If so, how should one approach such a task? I'm kinda getting stuck all the time just trying to make a basic pong network game.
Are you talking about network games in Basic4GL?
If you are, here is an example off the Forum.
Didn't link do a chat? —hartnell
Hmm… i also need a chat for the game :)
Btw, i'm thinking that somewhere along the path-to-a-game-programmer there should be some tutorial on doing network games, so i guess we need some research on that area also. There are probably many ways of doing it, the simplest being each player has a copy of the world and just sands his own avator to the other player, like in the example matthew posted. My proposal below is also another way, a little bit more difficult as one needs to wrap the entire game state into a network packet and send it :) And then there are all the other ways etc. etc. etc. i guess.
Btwx2, hartnell, i haven't fallen of the face of the earth. I'll post the pacman sprites when i get my old computer connected again, and also do the pseudo code for the snake game.
I'm using SDL with c++ right now. I'm kinda leaning toward something like that example you showed, only instead of just sending the player positions i am making the server send the game state, and the client only sends what keys are pressed. This way, the game will be running on the server while copies of the "simulation" are sent to the client. This is to make sure both players always see the same thing (except a little delay).
I think this might work for pong. Also, if a packet gets lost it is not so important.
But for anything larger, maybe even >2 player games, i am wondering how to make sure that all players are synchronized, and that there isn't too much delay between them. Especially over the internet, packets can have anything from 10ms to >100ms in transit, which for my pong game would mean that the client would see his paddle move with a 200ms delay, which is quite unplayable.
So how to solve this?
Lol, I just found this.
It's a networking library with a sample chat client and server application for SDL.
Yeah, I did once do a chat in the B4GL Network. It sent messages correctly, but I never got around to making it display them properly and in the end you can't see it all that well. :P I also made an ASCII Networked Pong game a little while back, I'll chuck the source for both programs at the end of this post.
I'd imagine that the methods of connecting/sending/receiving messages would be different in B4GL than what you're using, but in the end I guess the hardest part of Networking is implementing it properly, the actual set-up itself isn't that hard once you get the hang of it.
In the Pong game, I had both programs doing the working out for themselves and then sending their positions to each other. But I know there has to be a better way, with this if you were to do something like press Alt or something by accident (effectively pausing the program) the two would get out of time and so it would be ruined. But I haven't really put myself to thinking about a better way to do it, at the time I just wanted to get the hang off the Network Engine and that was enough for then.
This also reminds me that I was thinking of putting a Multiplayer mode in TrackDown. Although of course that's on hold for now.
Anyway, here's the source of the chat and the pong program:
(actually, I can't seem to find the chat code at the moment :P sorry)
dim connection, answer$, server, playery, enemy, playerx, enemx, g, file, playerscore, enemscore, searching, serv, ballx, bally, ballydir, ballxdir dim btimer ballx = 10 bally = 10 ballxdir = 1 ballydir = 1 TextMode(TEXT_BUFFERED) Question: printr "Would you like to be server or client?" DrawText() input answer$ if lcase$(answer$) = "server" then goto Server elseif lcase$(answer$) = "client" then goto Client else printr "Answer not recognised." DrawText() Sleep(2000) cls goto Question endif Server: serv = true server = NewServer(8000) while ConnectionPending(server) = false cls printr "Waiting for Client..." DrawText() wend cls printr "Client found." DrawText() connection = AcceptConnection(server) Sleep(2000) playerx = 39 playery = 10 enemx = 0 enemy = 10 goto Game Client: cls printr "What IP would you like to connect to?" DrawText() input answer$ connection = NewConnection(answer$,8000) while ConnectionHandshaking(connection) cls printr "Searching for server..." DrawText() searching = searching + 1 if searching = 5000 then cls print "No server found." DrawText() searching = 0 Sleep(2000) goto Client endif wend cls printr "Server found." DrawText() Sleep(2000) playerx = 0 playery = 10 enemx = 39 enemy = 10 goto Game Game: while ConnectionConnected(connection) if ScanKeyDown(VK_DOWN) and playery < 21 then playery = playery + 1 elseif ScanKeyDown(VK_UP) and playery > 3 then playery = playery - 1 endif file = SendMessage(connection,1,false,false) WriteInt(file, playery) CloseFile(file) while MessagePending(connection) file = ReceiveMessage(connection) enemy = ReadInt(file) CloseFile(file) wend btimer = btimer + 1 if btimer = 4 then if bally = 1 or bally = 23 then ballydir = -ballydir endif if ballx = playerx - 1 or ballx = playerx + 1 then if bally > playery - 3 and bally < playery + 3 then ballxdir = -ballxdir endif endif if ballx = enemx - 1 or ballx = enemx + 1 then if bally > enemy - 3 and bally < enemy + 3 then ballxdir = -ballxdir endif endif if ballx = playerx then enemscore = enemscore + 1 ballx = 23 bally = 10 ballxdir = -ballxdir endif if ballx = enemx then playerscore = playerscore + 1 ballx = 23 bally = 10 ballxdir = -ballxdir endif bally = bally + ballydir ballx = ballx + ballxdir btimer = 0 endif cls for g = -2 to 2 locate playerx,playery + g: print "O" next for g = -2 to 2 locate enemx,enemy + g: print "O" next locate ballx,bally: print "O" locate 6,0: print "You: " + playerscore locate 18,0: print "Enemy: " + enemscore DrawText() WaitTimer(20) wend DeleteConnection(connection) cls print "Connection lost." DrawText() Sleep(2000) end