Exemplo n.º 1
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="snakeGame">SnakeGame object</param>
 /// <param name="hostname">The hostname to connect to</param>
 /// <param name="port">The TCP port to connect to</param>
 public SnakeGameTCPClient(SnakeGame snakeGame, String hostname, int port)
 {
     this.snakeGame = snakeGame;
     this.hostname  = hostname;
     this.port      = port;
     this.draw      = snakeGame.SnakeGameDraw;
 }
Exemplo n.º 2
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="draw">Drawing object</param>
 /// <param name="allSnakes">A list with all the snakes in the game</param>
 /// <param name="field">The playfield</param>
 /// <param name="minSize">The minimum size of a piece of food</param>
 /// <param name="maxSize">The maximum size of a piece of food</param>
 public SnakeFood(SnakeGameDraw draw, List <Snake> allSnakes, Area field, int minSize, int maxSize)
 {
     this.draw      = draw;
     this.allSnakes = allSnakes;
     this.field     = field;
     this.minSize   = minSize;
     this.maxSize   = maxSize;
     Move();
 }
Exemplo n.º 3
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="iD">The ID of the snake</param>
 /// <param name="draw">Drawing object</param>
 /// <param name="field">The playfield</param>
 /// <param name="allSnakes">A list with all the snakes in the game</param>
 /// <param name="snakeFood">A list with all the food</param>
 /// <param name="respawn">should the snake respawn when it dies</param>
 public Snake(int iD, SnakeGameDraw draw, Area field, List <Snake> allSnakes, SnakeFood[] snakeFood, bool respawn)
 {
     this.iD        = iD;
     this.draw      = draw;
     this.field     = field;
     this.allSnakes = allSnakes;
     this.snakeFood = snakeFood;
     this.respawn   = respawn;
     CreateSnake();
 }
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="snakeGame">SnakeGame object</param>
        /// <param name="socket">Socket to send to/recieve from</param>
        public SnakeGameTCPServerConnection(SnakeGame snakeGame, Socket socket)
        {
            this.snakeGame = snakeGame;
            this.socket    = socket;
            this.draw      = snakeGame.SnakeGameDraw;

            drawHook = new SnakeGameDraw.DrawHook(DrawHook);

            reciever = new Thread(new ThreadStart(RecieveLoop));
            reciever.Start();
        }
Exemplo n.º 5
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);
            }
        }