コード例 #1
0
        private void Init()
        {
            _score     = 0;
            _time      = 0;
            _speed     = 2000;
            _gameState = GameState.Playing;
            var piece           = _random.Next(1, 8);
            var generatedPieces = PieceList.GetPiece(piece);

            _activePieces  = new List <Vector2>();
            _nextPieces    = new List <Vector2>();
            _passivePieces = new List <Vector2>();
            foreach (var generatedPiece in generatedPieces)
            {
                _activePieces.Add(new Vector2((generatedPiece.X + (_size.X - generatedPieces.Max(g => g.X)) / 2), generatedPiece.Y - 1));
            }
            piece           = _random.Next(1, 7);
            generatedPieces = PieceList.GetPiece(piece);
            foreach (var generatedPiece in generatedPieces)
            {
                _nextPieces.Add(new Vector2(generatedPiece.X, generatedPiece.Y));
            }
        }
コード例 #2
0
        public void Update(double delta)
        {
            if (_gameState == GameState.GameOverFilling)
            {
                _time += delta;
                while (_time > _speed)
                {
                    _time -= _speed;
                    if (_passivePieces.Count >= _size.X * _size.Y)
                    {
                        _gameState = GameState.GameOverEmptying;
                    }
                    bool breakout = false;
                    for (var y = _size.Y - 1; y >= 0; y--)
                    {
                        for (var x = 0; x < _size.X; x++)
                        {
                            if (!_passivePieces.Any(p => p.X == x && p.Y == y))
                            {
                                _passivePieces.Add(new Vector2(x, y));
                                breakout = true;
                                break;
                            }
                        }
                        if (breakout)
                        {
                            break;
                        }
                    }
                }
            }
            if (_gameState == GameState.GameOverEmptying)
            {
                _time += delta;
                while (_time > _speed)
                {
                    _time -= _speed;
                    if (_passivePieces.Count == 0)
                    {
                        Init();
                    }
                    else
                    {
                        var piece = _passivePieces.OrderByDescending(p => p.X).OrderBy(p => p.Y).First();

                        _passivePieces.Remove(piece);
                    }
                }
            }
            if (_gameState != GameState.Playing)
            {
                return;
            }
            _time += delta;
            while (_time > _speed)
            {
                _time -= _speed;
                bool becomePassive = false;
                foreach (var activePiece in _activePieces)
                {
                    if (activePiece.Y == _size.Y - 1)
                    {
                        becomePassive = true;
                    }
                    if (_passivePieces.Any(p => p.X == activePiece.X && p.Y == activePiece.Y + 1))
                    {
                        becomePassive = true;
                    }
                }
                if (!becomePassive)
                {
                    foreach (var activePiece in _activePieces)
                    {
                        activePiece.Y += 1;
                    }
                }
                else
                {
                    foreach (var activePiece in _activePieces)
                    {
                        _passivePieces.Add(new Vector2(activePiece.X, activePiece.Y));
                    }
                    int scoredLines = 0;
                    for (int y = 0; y < _size.Y; y++)
                    {
                        if (_passivePieces.Count(p => p.Y == y) == _size.X)
                        {
                            scoredLines++;
                            //Remove line
                            foreach (var passivePiece in _passivePieces.ToList())
                            {
                                if (passivePiece.Y == y)
                                {
                                    _passivePieces.Remove(passivePiece);
                                }
                            }
                            //Drop all pieces above
                            foreach (var passivePiece in _passivePieces.ToList())
                            {
                                if (passivePiece.Y < y)
                                {
                                    passivePiece.Y += 1;
                                }
                            }
                        }
                    }
                    if (scoredLines == 1)
                    {
                        _score += 100;
                    }
                    if (scoredLines == 2)
                    {
                        _score += 250;
                    }
                    if (scoredLines == 3)
                    {
                        _score += 500;
                    }
                    if (scoredLines == 4)
                    {
                        _score += 1000;
                    }
                    var piece           = _random.Next(1, 8);
                    var generatedPieces = PieceList.GetPiece(piece);
                    _activePieces = new List <Vector2>();
                    foreach (var nextPiece in _nextPieces)
                    {
                        _activePieces.Add(new Vector2((nextPiece.X + (_size.X - generatedPieces.Max(g => g.X)) / 2), nextPiece.Y - 1));
                    }
                    foreach (var activePiece in _activePieces)
                    {
                        if (_passivePieces.Any(p => p.X == activePiece.X && p.Y == activePiece.Y))
                        {
                            _gameState    = GameState.GameOverFilling;
                            _speed        = 100;
                            _activePieces = new List <Vector2>();
                        }
                    }
                    _nextPieces = new List <Vector2>();
                    foreach (var generatedPiece in generatedPieces)
                    {
                        _nextPieces.Add(new Vector2(generatedPiece.X, generatedPiece.Y));
                    }
                }
            }
        }