UDP Pong - Client

This is the "fun," part of the application ecosystem that we will send to our users.  Where they can control their very own Paddle that goes up and down.  This is also the graphical portion of the app.  Before, we were just using the MonoGame/XNA libs for collision detection but here we're using them for their original intention (video games)!

 

Don't forget, if you want sounds working, you'll need to uncomment line number 7.  Remember that audio isn't quite working properly with MonoGame 3.5 (DesktopGL version).

 

 

 

You've probably noticed a lot of similarities with the Arena and PongServer code already, but there are some differences.

At the top of the class we have our time measurement members.  They are exactly similar in meaning to those of PlayerInfo, except that the Server/Client relationship is flip-flopped.

In the constructor we setup our game's asset directory, the display settings, initialise the game objects and instantiate the UdpClient.  For those of you unexperienced with MonoGame/XNA, LoadContent() will load our assets into memory.  UnloadContent() will be used to clean up some other things.

Update() is another one of those MonoGame/XNA methods.  At the top it will check for an Esc key press.  If so, it will quit the Client and notify the Server.  After that a timeout check is made.  Next we check for any new NetworkMessages and do some stuff based on that.  First we check for a Bye.  After that we will try to send a JoinRequest if we don't have a connection.  If we're waiting for a game start, we will ping-pong Heartbeats with the Server.  Once we're in the game we will continue to send Heartbeats.  We will periodically send our Paddle's Y position.  If we got a GameState we'll update the rest of the game objects.  If a PlaySoundEffect was sent, we will then try to play that named effect.

Start() will mark the Client as running and start the underlying _networkThread (with the _networkRun() method).

Draw() is once again a MonoGame/XNA method.  Depending upon the current state it will either display a message to the user or draw the game objects.

_drawCentered() is a utility method to draw a Texture2D object to the center of the screen.  _updateWindowTitleWithScore() is another "graphical method" that updates the window's title with the current score (hence the name).  If you notice, it will also denote which side the Client is on.

_networkRun() is a looping function that runs in the _networkThread.  It reads new messages from the Server and tries to send ones we've queued up.  After the loop is done, if we've set _sendBye to true (via the user pressing Esc) it will send a ByePacket.

_sendPacket() will queue up a Packet to be sent and mark the _lastPacketSentTime variable.

I'm not going to spend my time on the last few private methods.  They are all related to Packet transfer.  You can probably guess what they do based upon their names.

And at the end of the class we have our Main() method which will start the Client and try to connect.

© 16BPP.net – Made using & love.
Back to Top of Page
This site uses cookies.