We're going to something a little bit more fun for this example. We're going to write a clone of Pong that we can play over the Internet. There will be a central server that many players can connect to and play one on one matches. Pong really is the "Hello, World!" of video game programming, so I think it would be a good first game to try to network.
Since we're doing video game programming I chose to use MonoGame as our media library. It's cross platform, uses C#, and it's pretty easy to use. I wanted to keep as much of this tutorials in this series in the command line, but I had to break that guideline here. I used the
MonoGame.Framework.DesktopGL package. If you're on an OS other than Linux, you might want to try Windows or MacOS versions.
Note: Audio playback is umm... kind of broken with MonoGame 3.5 (the current stable release at the time of writing this). As far as I know, this only applies to the DesktopGL package. This is really an un-fun problem because I've got projects that use an older version of MonoGame that still plays sounds fine; If you're on a Linux system you might want to try getting a slightly older version if you really want to play sounds.
I've included a
#define that allows you to toggle audio playback on the Client app at compile time. It's disabled by default.
This tutorial is going to be broken up into two applications, the
PongServer and the
PongClient (make two separate Projects in your Solution). The Server is going to be authoritative in handling collisions (Ball with Paddles, Goals, and top/bottom bounds), notifying the client to play sound effects, and position updates. All the client will need to do connect to the Server and shout it's Paddle's Y position.
In a game instance (referred to as an
Arena in the Server code), there will be only two players maximum, though the server should be able to handle multiple games at the same time. A game instance will not start unless two Clients have connected. If a Client connects and then disconnects, the game instance is considered done, regardless if the match had started or not.
Put the below files inside of a folder called
Content in the Client's Project. Make sure that all of these files in the Project are copied to the build directory in compilation (right click on file >
Quick Properties >
Copy to Output Directory). I've also got them all in .zip and .tar.gz format if you'd like.