/// <summary> /// "Physics" collision for a Zetromino piece /// </summary> /// <param name="piece"> Piece to check collision from</param> /// <param name="rotation"> Rotation of the piece</param> /// <param name="posX"> position X to check</param> /// <param name="posY"> position Y to check</param> /// <returns> False if the piece can't be in the position, /// True if it can</returns> bool PieceFits(Zetromino piece, int rotation, int posX, int posY) { // Number 4 because pieces cannot be bigger than a 4x4 for (int px = 0; px < 4; px++) { for (int py = 0; py < 4; py++) { // Index into piece int pi = RotatePiece(px, py, rotation); // Get game pos of index int gi = (posY + py) * _gameDim.x + (posX + px); // If its inside the game field if (posX + px >= 0 && posX + px < _gameDim.x) { if (posY + py >= 0 && posY + py < _gameDim.y) { // Is the game field empty in the next position if (piece.Shape[pi].Equals('X') && board[gi] != 0) { return(false); } } } } } return(true); }
/// <summary> /// Picks a new piece to be used as the current piece and resets values /// </summary> private void PickNewPiece() { _currentPiece = _spawner.GetNewPiece(); _currentX = _spawn.x; _currentY = _spawn.y; _pieceRotation = 0; _nextPiecePreview.Content = _spawner.NextShape; }
/// <summary> /// Creates a new Zetronimo /// </summary> /// <returns> Returns the new Zetronimo piece</returns> public Zetromino GetNewPiece() { Zetromino newPiece = new Zetromino (NextShape, _currentPiece); // Pick the next piece _currentPiece = _rnd.Next() % 7; NextShape = _PIECES[_currentPiece]; return(newPiece); }
/// <summary> /// Zetris board constructor /// </summary> /// <param name="playerName">Current player name</param> public ZetrisBoard(string playerName) { _playerName = playerName; // Classic tetris dimensions _gameDim = new Vector2(12, 18); // Spawn location _spawn = new Vector2((_gameDim.x / 2) - 2, 0); // Current piece X and Y position _currentX = _spawn.x; _currentY = _spawn.y; _spawner = new PieceSpawner(); _currentPiece = _spawner.GetNewPiece(); _pieceRotation = 0; _pieceAcceleration = 16; _hasRotated = false; _lines = new List <int>(4); _pieceCounter = 0; _scoreDisplay = new ScoreDisplay(new Vector2(_gameDim.x, _gameDim.y - 7)); _scoreSystem = new ScoreSystem(_scoreDisplay.UpdateScore); // Create the board board = new byte[_gameDim.x * _gameDim.y]; for (int x = 0; x < _gameDim.x; x++) { for (int y = 0; y < _gameDim.y; y++) { board[y * _gameDim.x + x] = (x == 0 || x == _gameDim.x - 1 || y == _gameDim.y - 1) // 9 is the last character of the board string in Game ? (byte)9 : (byte)0; } } Childs = new List <IGameObject>(); _nextPiecePreview = new PreviewBox(new Vector2 (_gameDim.x, _gameDim.y + 3), "Next Piece", 'o'); Childs.Add(_nextPiecePreview); Childs.Add(_scoreDisplay); _nextPiecePreview.Content = _spawner.NextShape; lineComplete += _scoreSystem.LineComplete; piecePlaced += _scoreSystem.PiecePlaced; }