Ejemplo n.º 1
0
 public SnakeGame(Options options)
 {
     _options   = options;
     _border    = new Border(options);
     _snakeHead = new SnakeHead(options);
     _berry     = new Berry(options);
     _gameBoard = new GameBoard(options);
 }
Ejemplo n.º 2
0
        public Snake()
        {
            body = new Stack <SnakeBodyPart>();
            head = new SnakeHead();

            currentDirection = Directions.RIGHT;
            lastDirection    = Directions.RIGHT;

            StartPoint = new Point(25, 450);
            _gameOn    = true;
        }
Ejemplo n.º 3
0
        public override void Move(ArrowDirection arrowDirection, Drawer drawer)
        {
            if (Parts.Count > 1 && (Parts.Select(x => x.Y).Distinct().Count() == 1 || Parts.Select(p => p.Y).Distinct().Count() == 1))
            {
                ReverseSnakeDirection();
            }


            // update snake parts locations
            var oldSnakeHeadPart = (SnakePart)SnakeHead.Clone();

            // update head part location
            switch (arrowDirection)
            {
            case ArrowDirection.Up:
                SnakeHead.Y -= oldSnakeHeadPart.Height;
                break;

            case ArrowDirection.Down:
                SnakeHead.Y += oldSnakeHeadPart.Height;
                break;

            case ArrowDirection.Right:
                SnakeHead.X += oldSnakeHeadPart.Width;
                break;

            case ArrowDirection.Left:
                SnakeHead.X -= oldSnakeHeadPart.Width;
                break;
            }

            // update tail parts locations
            moveTailParts(oldSnakeHeadPart);

            // graphically draw snake parts
            Draw(drawer);

            if (IsColided)
            {
                FireOnSnakeColided();
                return;
            }
        }
Ejemplo n.º 4
0
        public void Spawn(GameObjectType type)
        {
            switch (type)
            {
            case GameObjectType.SnakeHead:
                head = new SnakeHead();
                break;

            case GameObjectType.SnakeSegment:
                gameObjects.Add(new SnakeSegment(new Point(100, 100)));
                break;

            case GameObjectType.Apple:
                Random rng = new Random();
                int    x   = rng.Next(1, xBounds - 1);
                int    y   = rng.Next(1, yBounds - 1);
                apple.Pos.X = x;
                apple.Pos.Y = y;
                break;
            }
        }
Ejemplo n.º 5
0
        public void UpdateModelBasedOnKeyPressed(object sender, EventArgs e)
        {
            SnakeHead head = model.Get <PlayerSnake>().Get <SnakeHead>();

            switch (Char.ToLower(PressedKey))
            {
            case 'p':
                model.GameState = State.PAUSE;
                break;

            case 'n':
                model.Initialize();
                frameTimer.Interval = GameProperties.SPEED;
                break;

            case 'a':
                model.GameState = State.IN_GAME;
                head.Direction  = Direction.LEFT;
                break;

            case 'd':
                model.GameState = State.IN_GAME;
                head.Direction  = Direction.RIGHT;
                break;

            case 'w':
                model.GameState = State.IN_GAME;
                head.Direction  = Direction.UP;
                break;

            case 's':
                model.GameState = State.IN_GAME;
                head.Direction  = Direction.DOWN;
                break;
            }
        }