UDP Pong - Common Files

Add all of these files to both the Client Project and the Server Project.  If you want to copy & paste all of Packet.cs, I don't blame you (it's very long, boring and repetitive), but be sure to pay attention to the Packet class and it's subclasses that have Payload data.


The proper thing to do would be to create a separate Project in the Solution that contains the common code (e.g. PongFramework), but what I do instead is put all of the common files in one of the Projects (e.g. PongServer), and then add those as linked files in the other Project (e.g. PongClient).  That way everything is nice and synced.



This is only a static data containment class.  Instead of littering our program with hard coded values, it's better to put there here.




This is almost identical to the one in the File Transfer example, but this also adds field where we mark the time when one side has received the Packet




This makes one variable (of a generic type) thread safe (via locking).  It can only be accessed via the Value property.




All right, this is the big one.  It's quite similar to the one of the File Transfer example, except that is has an extra field denoting the time the Packet was created.  There are many subclasses that have specific implementations of the PacketType enumeration.  Like last time, you use the GetBytes() method to get the Packet data as byte array that can be sent over the network.  The only subclasses that have Payloads are AcceptJoinPacket, PaddlePositionPacket, GameStatePacket, and PlaySoundEffectPacket.  Be sure to pay attention to how those ones store data.  Feel free to Ctrl-C, Ctrl-V the rest of the code if you find it too much to type.




This is the game object for the Ball that will be bounced around.  LoadContent() and Draw() should only be called on the Client app.  Where asInitialize() and ServerSideUpdate() will be called on the Server.




There are two enumerations at the top of this file.  The first determines what side of the board the Paddle is one (using None will cause an error).  The other is to mark what kind of collision has happened with the Paddle.  LoadContent(), Draw(), and ClientSideUpdate() are called only on the Client.  That last function is what checks to see if the user has tried to move the Paddle (it also does a bit of bounds checking).  Initialize() and Collides() only need to be called on the Server portion.

The function Collides() is what checks if the Ball has collided with the Paddle.  If it has, the function will return true and the out parametertypeOfCollision will be filled with a value from the PaddleCollision enumeration.


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