예제 #1
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="form">The window to draw in</param>
        /// <param name="gameType">The type of game</param>
        /// <param name="playerArrow">True if player controlling with arrow keys is playing</param>
        /// <param name="playerWASD">True if player controlling with W, A, S and D is playing</param>
        /// <param name="playerNumeric">True if player controlling with numeric keys is playing</param>
        /// <param name="playerIJKL">True if player controlling with I, J, K, L is playing</param>
        /// <param name="hostname">The hostname to connect to if game type is client</param>
        /// <param name="pixelSize">The width of a "game pixel" measured in "screen pixels"</param>
        /// <param name="xWidth">The width (X) of the playfield. Not used if game type is client</param>
        /// <param name="yHeight">The height (Y) of the playfield. Not used if game type is client</param>
        /// <param name="speed">The speed of the game, lower is faster. Not used if game type is client</param>
        /// <param name="tcpPort">The TCP port used to connect to/listen on. Not used if game type is standalone</param>
        /// <param name="respawn">Should snakes respawn when they dies. Not used if game type is client</param>
        /// <param name="foodMin">The minimum size of a piece of food. Not used if game type is client</param>
        /// <param name="foodMax">The maximum size of a piece of food. Not used if game type is client</param>
        /// <param name="foodCount">Number of foo pieces. Not used if game type is client</param>
        public SnakeGame(frmPlayGround form, GameType gameType,
                         bool playerArrow, bool playerWASD, bool playerNumeric, bool playerIJKL,
                         String hostname,
                         //advanced options:
                         int pixelSize, int xWidth, int yHeight,
                         int speed, int tcpPort, bool respawn,
                         int foodMin, int foodMax, int foodCount
                         )
        {
            this.gameType = gameType;
            this.form     = form;

            field     = new Area();
            this.draw = new SnakeGameDraw(canvas, pixelSize, field);

            if (gameType == GameType.Standalone || gameType == GameType.Server)
            {
                Field = new Area(0, 0, xWidth - 1, yHeight - 1);;

                //Create the timer used to set the speed
                this.speed          = speed;
                this.timer          = new System.Windows.Forms.Timer();
                this.timer.Interval = speed;
                this.timer.Tick    += new System.EventHandler(this.MoveStuff);

                this.respawn = respawn;

                //Create the food objects
                snakeFood = new SnakeFood[foodCount];
                for (int i = 0; i < foodCount; i++)
                {
                    snakeFood[i] = new SnakeFood(draw, allSnakes, field, foodMin, foodMax);
                }

                #region Snake creation
                if (playerArrow == true)
                {
                    Snake snake = CreateSnake();
                    handlerKeys[(int)HandlerKeys.Arrow] = snake;
                }
                if (playerWASD == true)
                {
                    Snake snake = CreateSnake();
                    handlerKeys[(int)HandlerKeys.WASD] = snake;
                }
                if (playerNumeric == true)
                {
                    Snake snake = CreateSnake();
                    handlerKeys[(int)HandlerKeys.Numeric] = snake;
                }
                if (playerIJKL == true)
                {
                    Snake snake = CreateSnake();
                    handlerKeys[(int)HandlerKeys.IJKL] = snake;
                }

                if (gameType == GameType.Server)
                {
                    tcpServer = new SnakeGameTCPServer(this, tcpPort);
                }
                #endregion

                draw.DrawAll();
                timer.Enabled = true;
            }
            else if (gameType == GameType.Client)
            {
                tcpClient = new SnakeGameTCPClient(this, hostname, tcpPort);

                #region Snake controller creation
                if (playerArrow == true)
                {
                    SnakeTCPController snake = new SnakeTCPController(snakeID++, tcpClient);
                    handlerKeys[(int)HandlerKeys.Arrow] = snake;
                }
                if (playerWASD == true)
                {
                    SnakeTCPController snake = new SnakeTCPController(snakeID++, tcpClient);
                    handlerKeys[(int)HandlerKeys.WASD] = snake;
                }
                if (playerNumeric == true)
                {
                    SnakeTCPController snake = new SnakeTCPController(snakeID++, tcpClient);
                    handlerKeys[(int)HandlerKeys.Numeric] = snake;
                }
                if (playerIJKL == true)
                {
                    SnakeTCPController snake = new SnakeTCPController(snakeID++, tcpClient);
                    handlerKeys[(int)HandlerKeys.IJKL] = snake;
                }
                #endregion

                tcpClient.Connect(snakeID);
            }
        }
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="snakeID">The local snake ID</param>
 /// <param name="tcpClient">TCP client to send to</param>
 public SnakeTCPController(int snakeID, SnakeGameTCPClient tcpClient)
 {
     this.snakeID   = snakeID;
     this.tcpClient = tcpClient;
 }