Example #1
0
        //Game time method to run periodically on timer
        private void gameTime_Tick(object sender, EventArgs e)
        {
            edgeCollision();     //Check if hits edge
            ballx += ballForceX; //Move ball
            bally += ballForceY;

            if (!returnOk)                              //If returnOK == false
            {
                if (ballx <= width - 35 && ballx >= 25) //Check location
                {
                    returnOk = true;                    //Set true
                }
            }

            if ((collisionA() || collisionB()) && returnOk)                                      //Check if a point has been scored and if its valid
            {
                gameTime.Enabled = false;                                                        //Stop time
                network.sendUDP(message.gameInfo(paddleB, bally, ballx, pointsA, pointsB), ipA); //Send points update
                network2.sendUDP(message.gameInfo(paddleA, bally, ballx, pointsA, pointsB), ipB);
            }

            if (pointsA == 10 || pointsB == 10) //Checks if there is a winner
            {
                gameTime.Enabled     = false;   //Stops timers
                timerSend.Enabled    = false;
                timerRecieve.Enabled = false;
                //Notify clients of winner
                network.sendUDP(generateMessage("420", pointsA.ToString(), pointsB.ToString()), ipA);
                network2.sendUDP(generateMessage("420", pointsA.ToString(), pointsB.ToString()), ipB);
                active = false;  //set game to unactive
                network.close(); //close sockets
                network2.close();
            }
        }