コード例 #1
0
        private void PlayHandler()
        {
            Mutex mutex = new Mutex();

            Render.RenderStatisticInfo("Start");
            string message = "";

            while (runGame)
            {
                ConsoleKey key = Console.ReadKey(true).Key;

                if (key == ConsoleKey.DownArrow)
                {
                    if (startPosition == StartPosition.Left)
                    {
                        if (posLeftRacket < 22)
                        {
                            mutex.WaitOne();
                            posLeftRacket++;
                            mutex.ReleaseMutex();
                        }
                    }
                    else
                    {
                        if (posRightRacket < 23)
                        {
                            mutex.WaitOne();
                            posRightRacket++;
                            mutex.ReleaseMutex();
                        }
                    }
                }
                else if (key == ConsoleKey.UpArrow)
                {
                    if (startPosition == StartPosition.Left)
                    {
                        if (posLeftRacket > 1)
                        {
                            mutex.WaitOne();
                            posLeftRacket--;
                            mutex.ReleaseMutex();
                        }
                    }
                    else
                    {
                        if (posRightRacket > 1)
                        {
                            mutex.WaitOne();
                            posRightRacket--;
                            mutex.ReleaseMutex();
                        }
                    }
                }
                else if (isChar(key))
                {
                    if (message.Length < 60)
                    {
                        message += key.ToString();
                        mutex.WaitOne();
                        Render.RenderSendMessage(key);
                        mutex.ReleaseMutex();
                    }
                }
                else if (key == ConsoleKey.Spacebar)
                {
                    if (message.Length < 60)
                    {
                        message += " ";
                        mutex.WaitOne();
                        Render.RenderSendMessage(" ");
                        mutex.ReleaseMutex();
                    }
                }
                else if (key == ConsoleKey.Enter)
                {
                    if (message.Length > 0)
                    {
                        chat.AddMsg(selfNick, message);
                        chatSocket.Send(Encoding.Default.GetBytes(message));
                        message = "";
                        mutex.WaitOne();
                        Render.RenderEnter();
                        Render.RenderChat(chat);
                        mutex.ReleaseMutex();
                    }
                }
                else if (key == ConsoleKey.Backspace)
                {
                    if (message.Length > 1)
                    {
                        message = message.Substring(0, message.Length - 1);
                        mutex.WaitOne();
                        Render.RenderBackspace();
                        mutex.ReleaseMutex();
                    }
                }
            }

            if (scoreLeft == 5)
            {
                Render.RenderStatisticInfo("Left win");
            }
            else if (scoreRight == 5)
            {
                Render.RenderStatisticInfo("Right win");
            }
            Render.RenderStatisticInfo("Finish the game");
        }
コード例 #2
0
        private void StartReceive()
        {
            int spec_interval = 0;

            Mutex mutex = new Mutex();

            while (runGame)
            {
                try {
                    Thread.Sleep(spec_interval);
                    serverSocket.Receive(recvBuffer);

                    mutex.WaitOne();
                    //restartTimer = true;
                    sw.Restart();
                    mutex.ReleaseMutex();

                    string raw = Encoding.Default.GetString(recvBuffer);

                    raw = Helper.DeleteSpaces(raw);
                    string[] raws = raw.Split(';');
                    act = raws[0];

                    if (act == "ACTION")
                    {
                        string sub_action = raws[1];
                        if (sub_action == "LOSE")
                        {
                            if (startPosition == StartPosition.Left)
                            {
                                scoreRight++;
                            }
                            else
                            {
                                scoreLeft++;
                            }
                        }
                        else if (sub_action == "WIN")
                        {
                            if (startPosition == StartPosition.Left)
                            {
                                scoreLeft++;
                            }
                            else
                            {
                                scoreRight++;
                            }
                        }
                        else if (sub_action == "END")
                        {
                            runGame = false;
                        }
                    }
                    else if (act == "MOTION")
                    {
                        if (startPosition == StartPosition.Left)
                        {
                            int tmp = int.Parse(raws[2]);
                            if (tmp < 24 && tmp >= 0)
                            {
                                posRightRacket    = tmp;
                                oldPosRightRacket = posRightRacket;
                            }
                        }
                        else
                        {
                            int tmp = int.Parse(raws[1]);
                            if (tmp < 24 && tmp >= 0)
                            {
                                posLeftRacket    = tmp;
                                oldPosLeftRacket = posLeftRacket;
                            }
                        }

                        string[] strPosBall = raws[3].Split(',');
                        oldPosBall[0] = posBall[0];
                        oldPosBall[1] = posBall[1];
                        posBall[0]    = int.Parse(strPosBall[0]);
                        posBall[1]    = int.Parse(strPosBall[1]);

                        if (oldPosBall[0] == 58 & posBall[0] == 57)
                        {
                            sound.Play();
                        }
                        else if (oldPosBall[0] == 1 & posBall[0] == 2)
                        {
                            sound.Play();
                        }

                        string[] strScores = raws[4].Split(',');
                        scoreLeft  = int.Parse(strScores[0]);
                        scoreRight = int.Parse(strScores[1]);
                    }

                    //Mutex mutex = new Mutex();
                    mutex.WaitOne();

                    Render.FastRenderGame(posLeftRacket, posRightRacket, posBall, oldPosLeftRacket, oldPosRightRacket, oldPosBall);
                    Render.RenderStatistic(scoreLeft, scoreRight);
                    Render.RenderTime(time);

                    mutex.ReleaseMutex();

                    time++;
                } catch {
                }
            }
        }