Ejemplo n.º 1
0
        /// <summary>
        /// Move the food to a new location
        /// </summary>
        public void Move()
        {
            //Undraw the old area
            if (area != null)
            {
                draw.Draw(area, Elements.Background);
            }

            //Get an random area
            int size = rnd.Next(minSize - 1, maxSize - 1);

            //Shrink the area if it's bigger than the playfield
            Coordinate topLeft = new Coordinate(field);

            for (int i = 0; i <= size; i++)
            {
                Coordinate bottomRight = new Coordinate(topLeft.X + i, topLeft.Y + i);
                Area       tmpArea     = new Area(topLeft, bottomRight);
                if (tmpArea.InArea(field)) //[FIXME] check for snakebodyS
                {
                    area = tmpArea;
                }
                else
                {
                    break;
                }
            }

            //Draw the new area
            draw.Draw(area, Elements.SnakeFood);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Method containing thread recieving data from server
        /// </summary>
        private void ConnectThread()
        {
            try
            {
                tcpClient = new TcpClient();
                tcpClient.Connect(hostname, port);
                stream = tcpClient.GetStream();
                SendPacket(new SnakeGameTCPPacketInit(numSnakes));

                while (true)
                {
                    Object o = formatter.Deserialize(stream);
                    if (o is SnakeGameTCPPacketInitResponse)
                    {
                        SnakeGameTCPPacketInitResponse packet = (SnakeGameTCPPacketInitResponse)o;
                        snakeGame.Field = packet.Field;
                        draw.DrawAll();
                    }
                    else if (o is SnakeGameTCPPacketDraw)
                    {
                        SnakeGameTCPPacketDraw packet = (SnakeGameTCPPacketDraw)o;
                        draw.Draw(packet.DrawUpdate);
                        draw.DrawUpdate();
                    }
                }
            }
            catch (Exception)
            {
                Stop();
                System.Windows.Forms.MessageBox.Show("Connection lost to server");
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Creates the snake
        /// This is called every time the snake should (re)spawn
        /// </summary>
        private void CreateSnake()
        {
            isDead = false;
            Coordinate startCoordinate = new Coordinate(field);

            //If the snake is in the top of the field, move down, else move up
            if (startCoordinate.IsInTop(field))
            {
                this.direction = Direction.Down;
            }
            else
            {
                this.direction = Direction.Up;
            }

            snakeBody.Add(startCoordinate);
            draw.Draw(snakeBody, Elements.Snake);
        }