Exemplo n.º 1
0
        //fragen wieso point und logik von rendershape top sowie rotation erklären
        public override void RotateTetromino(RotationState currentRotationState)
        {
            Point oldLocation = new Point(Shape[0].Left, Shape[0].Top);

            if (currentRotationState == RotationState.Default)
            {
                if (Shape[0].Left + TetrisConfig.BlockSize * 3 < TetrisConfig.getFieldWidth())
                {
                    for (int i = 1; i < Shape.Length; i++)
                    {
                        Shape[i].Top  = oldLocation.Y;
                        Shape[i].Left = oldLocation.X + i * TetrisConfig.BlockSize;
                    }
                    this.RotationState = RotationState.Left;
                }
            }
            else
            {
                for (int i = 1; i < Shape.Length; i++)
                {
                    Shape[i].Left = oldLocation.X;
                    Shape[i].Top  = oldLocation.Y + i * TetrisConfig.BlockSize;
                }
                this.RotationState = RotationState.Default;
            }
        }
Exemplo n.º 2
0
        public override void RotateTetromino(RotationState currentRotationState)
        {
            Point oldLocation = new Point(Shape[0].Left, Shape[0].Top);

            if (currentRotationState == RotationState.Default)
            {
                if (Shape[0].Left >= TetrisConfig.BlockSize)
                {
                    for (int i = 0; i < Shape.Length; i++)
                    {
                        Shape[i].Top = oldLocation.Y + (i - 1) * TetrisConfig.BlockSize;
                        if (i >= 2)
                        {
                            Shape[i].Top = oldLocation.Y + (i - 2) * TetrisConfig.BlockSize;
                        }
                        Shape[i].Left = oldLocation.X;
                        if (i >= 2)
                        {
                            Shape[i].Left = oldLocation.X - TetrisConfig.BlockSize;
                        }
                    }
                    this.RotationState = RotationState.Left;
                }
            }
            else
            {
                if (Shape[0].Left + TetrisConfig.BlockSize * 2 <= TetrisConfig.getFieldWidth())
                {
                    for (int i = 0; i < Shape.Length; i++)
                    {
                        Shape[i].Left = oldLocation.X - i * TetrisConfig.BlockSize;
                        if (i >= 2)
                        {
                            Shape[i].Left = oldLocation.X + (i - 2) * TetrisConfig.BlockSize;
                        }
                        Shape[i].Top = oldLocation.Y + TetrisConfig.BlockSize;
                        if (i >= 2)
                        {
                            Shape[i].Top = oldLocation.Y + 2 * TetrisConfig.BlockSize;
                        }
                    }
                    this.RotationState = RotationState.Default;
                }
            }
        }
Exemplo n.º 3
0
        public void MoveTetromino(MovingDirections movingDirection)
        {
            switch (movingDirection)
            {
            case MovingDirections.Left:
                bool allowMovementLeft = true;
                foreach (Block block in Shape)
                {
                    if (block.Left <= 0)
                    {
                        allowMovementLeft = false;
                    }
                }

                if (!allowMovementLeft)
                {
                    return;
                }
                else
                {
                    foreach (Block block in Shape)
                    {
                        block.MoveBlock(movingDirection);
                    }
                }

                break;

            case MovingDirections.Right:
                bool allowMovementRight = true;
                foreach (Block block in Shape)
                {
                    if (block.Right >= TetrisConfig.getFieldWidth())
                    {
                        allowMovementRight = false;
                    }
                }

                if (!allowMovementRight)
                {
                    return;
                }
                else
                {
                    foreach (Block block in Shape)
                    {
                        block.MoveBlock(movingDirection);
                    }
                }
                break;

            case MovingDirections.Down:
                bool allowMovementDown = true;
                foreach (Block block in Shape)
                {
                    if (block.Bottom >= TetrisConfig.getFieldHeight())
                    {
                        allowMovementDown = false;
                    }
                }

                if (!allowMovementDown)
                {
                    TetrominoDocked?.Invoke(this);
                    return;
                }
                else
                {
                    foreach (Block block in Shape)
                    {
                        block.MoveBlock(movingDirection);
                    }
                }
                break;

            default:
                break;
            }
        }
Exemplo n.º 4
0
        public override void RotateTetromino(RotationState currentRotationState)
        {
            Point oldLocation = new Point(Shape[0].Left, Shape[0].Top);

            if (currentRotationState == RotationState.Default)
            {
                if (Shape[0].Left + TetrisConfig.BlockSize < TetrisConfig.getFieldWidth())
                {
                    for (int i = 0; i < Shape.Length; i++)
                    {
                        Shape[i].Left = oldLocation.X + (i - 1) * TetrisConfig.BlockSize;
                        Shape[i].Top  = oldLocation.Y + TetrisConfig.BlockSize;
                        if (i == 3)
                        {
                            Shape[i].Left = oldLocation.X - TetrisConfig.BlockSize;
                            Shape[i].Top  = oldLocation.Y;
                        }
                    }
                    this.RotationState = RotationState.Right;
                }
            }
            else if (currentRotationState == RotationState.Right)
            {
                for (int i = 0; i < Shape.Length; i++)
                {
                    Shape[i].Left = oldLocation.X + TetrisConfig.BlockSize;
                    Shape[i].Top  = oldLocation.Y + (i - 1) * TetrisConfig.BlockSize;
                    if (i == 3)
                    {
                        Shape[i].Left = oldLocation.X + 2 * TetrisConfig.BlockSize;
                        Shape[i].Top  = oldLocation.Y - TetrisConfig.BlockSize;
                    }
                }
                this.RotationState = RotationState.Down;
            }
            else if (currentRotationState == RotationState.Down)
            {
                if (Shape[0].Left - TetrisConfig.BlockSize >= 0)
                {
                    for (int i = 0; i < Shape.Length; i++)
                    {
                        Shape[i].Left = oldLocation.X + (i - 1) * TetrisConfig.BlockSize;
                        Shape[i].Top  = oldLocation.Y + TetrisConfig.BlockSize;
                        if (i == 3)
                        {
                            Shape[i].Left = oldLocation.X + TetrisConfig.BlockSize;
                            Shape[i].Top  = oldLocation.Y + 2 * TetrisConfig.BlockSize;
                        }
                    }
                    this.RotationState = RotationState.Left;
                }
            }
            else
            {
                for (int i = 0; i < Shape.Length; i++)
                {
                    Shape[i].Top  = oldLocation.Y + (i - 1) * TetrisConfig.BlockSize;
                    Shape[i].Left = oldLocation.X + TetrisConfig.BlockSize;
                    if (i == 3)
                    {
                        Shape[i].Left = oldLocation.X;
                        Shape[i].Top  = oldLocation.Y + TetrisConfig.BlockSize;
                    }
                }
                this.RotationState = RotationState.Default;
            }
        }
Exemplo n.º 5
0
 public void StartGame()
 {
     TetrisConfig.InitializeGame(this);
     //init Tick methods
     TetrisConfig.tmr_move_blocks.Tick += Tmr_move_blocks_Tick;
 }
Exemplo n.º 6
0
        public TetrisGame(ILog logger, int row = 10, int column = 20, WeightedPool <BlockUnit> objectPool = null, Queue <BlockUnit> initialQueue = null)
        {
            this.logger = logger;
            logger.Debug($"TetrisInstance Creating : row{row},column{column}");

            TetrisConfig config = new TetrisConfig();

            config.Load();
            if (!config.Load())
            {
                config.Save();
            }
            if (objectPool == null && config.UseCustomObjectList)
            {
                var serializable = (SerializableObjectPool.Load(typeof(SerializableObjectPool), ConfigBase.Directory, config.ObjectListFile) as SerializableObjectPool);
                if (serializable == null)
                {
                    serializable = new SerializableObjectPool()
                    {
                        ObjectPool = DefaultObjectPool
                    };
                    serializable.Save(ConfigBase.Directory, config.ObjectListFile);
                }
                objectPool = serializable.ObjectPool;
            }

            timer          = new Timer();
            timer.Interval = TimerSpan;
            timer.Elapsed += new ElapsedEventHandler((object sender, ElapsedEventArgs e) => controller?.OnTimerTick());

            Setting = new GameSetting()
            {
                Row = row, Column = column
            };

            field = new Field(row, column);

            _state = new GameState()
            {
                Round = 0, Score = 0, RemovedLines = new Dictionary <int, int>()
            };

            _gameWatch = new Stopwatch();
            _playData  = new GamePlayData();

            ObjectPool   = objectPool ?? TetrisGame.DefaultObjectPool;
            _objectQueue = initialQueue ?? new Queue <BlockUnit>();

            field.OnBlockChanged += (object sender, Point point) =>
            {
                //logger.Debug($"Block was changed:{point}");
            };
            field.OnRoundStart += (object sender) =>
            {
                if (RecordPlayDataEnabled)
                {
                    _gameWatch.Restart();
                }
                lock (_objectQueue)
                {
                    field.SetObject(Dequeue());
                }
            };
            field.OnBlockPlaced += (object sender, BlockObject obj) =>
            {
                //logger.Debug("Block was placed");
                Draw();
            };
            field.OnLinesRemoved += (object sender, int[] lines, int eroded) =>
            {
                if (lines.Length != 0)
                {
                    if (!_state.RemovedLines.ContainsKey(lines.Length))
                    {
                        _state.RemovedLines.Add(lines.Length, 0);
                    }
                    _state.RemovedLines[lines.Length]++;
                }
            };
            field.OnRoundEnd += (object sender, RoundResult result) =>
            {
                logger.Debug($"Round {_state.Round} End");
                _state.Round++;
                _state.Score += result.Score;
                if (MaxRound > 0 && _state.Round >= MaxRound)
                {
                    timer.Stop();
                    OnGameEnd?.Invoke(this, new GameResult()
                    {
                        Score = State.Score, Round = State.Round
                    });
                    return;
                }
                if (RecordPlayDataEnabled)
                {
                    _playData.Save();
                }
                field.StartRound();
            };
            field.OnGameOver += (object sender) =>
            {
                logger.Debug("Game Over");
                timer.Stop();
                OnGameEnd?.Invoke(this, new GameResult()
                {
                    Score = State.Score, Round = State.Round
                });
            };
            OnGameEnd += (object sender, GameResult result) =>
            {
                logger.Debug(State.Score);
                _gameWatch.Stop();
            };
        }