Esempio n. 1
0
        private void ChatHandler()
        {
            int sizeChatMessage = 100;

            byte[] chatBuffer = new byte[sizeChatMessage];
            while (runGame)
            {
                try {
                    Array.Clear(chatBuffer, 0, sizeChatMessage);
                    chatSocket.Receive(chatBuffer);
                    chat.AddMsg(opponentNick, Encoding.Default.GetString(chatBuffer));
                    Render.RenderChat(chat);
                } catch { }
            }
        }
Esempio n. 2
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");
        }