Пример #1
0
        public void Repaint(double width, double height)
        {
            drawings.Clear();
            CalculateCellSize(width, height);

            grid   = new Grid(this, gridWidth, gridHeight);
            Height = (gridHeight * 3) * cellSize;
            Width  = (gridWidth * 3) * cellSize;
            border = new GameBorder(this.height, this.width);

            drawings.Add(grid);
            drawings.Add(border);

            foreach (PuzzlePiece puzzlePiece in puzzlePieces)
            {
                if (puzzlePiece.isInitialised)
                {
                    puzzlePiece.PaintShape(this);
                    drawings.Add(puzzlePiece);
                }
            }
        }
Пример #2
0
        public Game(Options options, Berry berry, Border border, GameBorder gameBorder, Movement movement, )
        {
            var gameover = false;
            var xPosBody = new List <int>();
            var yPosBody = new List <int>();

            double gameSpeed = 150;

            int BerrysEaten = 0;

            var currentMovement = Movement.Right;

            while (true)
            {
                gameborder.Clear(options);
                border.Render();
                berry.Render();
                snakeHead.Render();

                if (snakeHead.Hits(border))
                {
                    gameover = true;
                    SetCursorPosition(28, 20);
                }

                Console.ForegroundColor = options.BodyColor;
                if (snakeHead.Hits(berry))
                {
                    BerrysEaten++;
                    berry.PutBerryAtRandomPosition();
                    gameSpeed *= 0.925;
                }

                for (int i = 0; i < xPosBody.Count(); i++)
                {
                    Console.SetCursorPosition(xPosBody[i], yPosBody[i]);
                    Console.Write(options.Block);
                    if (xPosBody[i] == snakeHead.XPos && yPosBody[i] == snakeHead.YPos)
                    {
                        gameover = true;
                    }
                }



                var sw = Stopwatch.StartNew();
                while (sw.ElapsedMilliseconds <= gameSpeed)
                {
                    currentMovement = ReadMovement(currentMovement);
                }

                // Assign the current head position to the body
                xPosBody.Add(snakeHead.XPos);
                yPosBody.Add(snakeHead.YPos);

                // Move head to the next position
                switch (currentMovement)
                {
                case Movement.Up:
                    snakeHead.YPos--;
                    break;

                case Movement.Down:
                    snakeHead.YPos++;
                    break;

                case Movement.Left:
                    snakeHead.XPos--;
                    break;

                case Movement.Right:
                    snakeHead.XPos++;
                    break;
                }

                if (xPosBody.Count() > BerrysEaten)
                {
                    xPosBody.RemoveAt(0);
                    yPosBody.RemoveAt(0);
                }

                if (gameover)
                {
                    // Game over screen, shows score based on the lenght of your snake
                    SetCursorPosition(options.BoardWidth / 5, options.BoardHeight / 2);
                    WriteLine("Game over, Score: " + BerrysEaten);
                    SetCursorPosition(options.BoardWidth / 5, options.BoardHeight / 2 + 1);
                    WriteLine("Press enter to continue.");
                    BerrysEaten = 0;
                    ReadLine();
                    Clear();
                    ShowMenu(out userAction);
                }
            }
        }