Quantcast
Channel: Active questions tagged xna-4.0 - Game Development Stack Exchange
Viewing all articles
Browse latest Browse all 33

Simple WP7 pong game with real time multiplayer using sockets

$
0
0

I'm making a pong clone for Windows Phone 7 and I'd love to add a multiplayer option. I've been trying to use this Ricky Tan's tutorial:

Link

The problem is it is designed around the platformer example which has several classes such as Player, Level and Game whereas my game takes place in a single Game class with much less logic for player movements.

I've tried putting my handleInput() inside the Channel_PacketsReceived method but there's still no connection between devices. I've been at this for days and I can't get it. Is there something I'm missing? I'd love some help with this or if anyone knows a more straight forward tutorial with things happening in the same class.

Also if it matters my game includes the gsm example.

 //Sockets    private UdpAnySourceMulticastChannel Channel;    private Vector2 oldPosition;    int identifier; public void HandleInput(int id, Vector2 position)    {           TouchCollection touchCollection = TouchPanel.GetState();           foreach (TouchLocation touchLocation in touchCollection)           {               // upper left corner touche detection               if (touchLocation.Position.Y < 400)               {                   if (touchLocation.Position.X < blueBar.X && touchLocation.Position.Y < 400)                   {                       blueBar.X -= 30;                   }                   else if (touchLocation.Position.X > blueBar.X + 128 && touchLocation.Position.Y < 400)                   {                       blueBar.X += 30;                   }                   else if (touchLocation.Position.X > blueBar.X && touchLocation.Position.X < blueBar.X + 128 && touchLocation.Position.Y < 400)                       blueBar.X = (int)touchLocation.Position.X - 64;               }               BlueSaber.X = blueBar.X;               // upper right corner touche detection               if (touchLocation.Position.Y > 400)               {                   if (touchLocation.Position.X < redBar.X && touchLocation.Position.Y > 400)                   {                       redBar.X -= 30;                   }                   else if (touchLocation.Position.X > redBar.X + 128 && touchLocation.Position.Y > 400)                   {                       redBar.X += 30;                   }                   else if (touchLocation.Position.X > redBar.X && touchLocation.Position.X < redBar.X + 128 && touchLocation.Position.Y > 400)                       redBar.X = (int)touchLocation.Position.X - 64;               }               RedSaber.X = redBar.X;               if (touchLocation.Position.X > start.X &&                   touchLocation.Position.X < start.X + start.Width &&                   touchLocation.Position.Y > start.Y &&                   touchLocation.Position.Y < start.Y + start.Height)               {                   play = true;                   touches++;                   if (redServe)                   {                       // default velocity                       Random rand = new Random();                       // randomize the ball orientation                       switch (rand.Next(2))                       {                           case 0: ballVelocity.Y = -speed1; ballVelocity.X = speed1; break;                           case 1: ballVelocity.Y = -speed1; ballVelocity.X = -speed1; break;                       }                   }                   else if (!redServe)                   {                       // default velocity                       Random rand = new Random();                       // randomize the ball orientation                       switch (rand.Next(2))                       {                           case 0: ballVelocity.Y = speed1; ballVelocity.X = speed1; break;                           case 1: ballVelocity.Y = speed1; ballVelocity.X = -speed1; break;                       }                   }               }           }    }

// Outside of update method

private void InitializeSockets()    {        this.Channel = new UdpAnySourceMulticastChannel(IPAddress.Parse("224.109.108.107"), 3007);        this.Channel.PacketReceived += new EventHandler<UdpPacketReceivedEventArgs>(Channel_PacketReceived);        this.Channel.Open();    }    void Channel_PacketReceived(object sender, UdpPacketReceivedEventArgs e)    {        string data = e.Message;        Console.WriteLine(data);        string[] pos = data.Split(',');        //Discard packets that do not match        if (pos.Length != 5)        {            return;        }        try        {            if (pos[0] != identifier.ToString()) //if not originated from this phone            {                    Vector2 position = new Vector2(float.Parse(pos[1]), float.Parse(pos[2]));                    Vector2 velocity = new Vector2(float.Parse(pos[3]), float.Parse(pos[4]));                   // UpdateOtherPlayer(int.Parse(pos[0]), position);                    HandleInput(int.Parse(pos[0]), position);              }        }        catch (Exception ex)        {            System.Diagnostics.Debug.WriteLine("Caught unexpected exception: " + ex.Message);        }    }    private void SendPosition(string position)    {        if (redBar.X != oldPosition.X)        {            oldPosition.X = redBar.X;            this.Channel.Send(position);        }    }

and i am calling the SendPosition method inside of update


Viewing all articles
Browse latest Browse all 33

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>