Ejemplo n.º 1
0
 //Constructor takes the type of block, a handle to the gameField, and the x,y coordinates of this block
 public Block(Square.SquareTypes st, Canvas canvas, int row, int col)
 {
     Row     = row;
     Col     = col;
     this.st = st;
     CreateBlock(canvas);
 }
        private void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            lock (block)
            {
                if (CanFall())
                {
                    block.Fall();
                }
                else
                {
                    if (!field.AddBlock(block))
                    {
                        YouLose();
                    }

                    scoreBox.Text            = Score.ToString();
                    levelBox.Text            = Level.ToString();
                    dispatcherTimer.Interval = new TimeSpan(2750000 - (Level * fallrate * 25000));

                    block     = new Block(nextBlock, gameField, 0, 6);
                    nextBlock = (Square.SquareTypes)rand.Next(0, 7);
                    for (int i = 0; i < 4; i++)
                    {
                        nblock.Squares[i].DeleteSquare();
                    }
                    nblock = new Block(nextBlock, nextBox, 0, 0);
                }
            }

            gameField.Focus();

            CommandManager.InvalidateRequerySuggested();
        }
        //private bool first = true;

        public GamePage()
        {
            InitializeComponent();

            nextBlock = (Square.SquareTypes)rand.Next(0, 7);
            block     = new Block(nextBlock, gameField, 0, 6);
            nextBlock = (Square.SquareTypes)rand.Next(0, 7);
            nblock    = new Block(nextBlock, nextBox, 0, 0);

            field = new Field();

            Level         = 1;
            scoreBox.Text = Score.ToString();
            levelBox.Text = Level.ToString();

            this.dispatcherTimer     = new DispatcherTimer();
            dispatcherTimer.Tick    += new EventHandler(dispatcherTimer_Tick);
            dispatcherTimer.Interval = new TimeSpan(2750000 - (Level * fallrate * 25000));
            dispatcherTimer.Start();

            gameField.Focus();
        }
        private void DropBlock()
        {
            lock (block)
            {
                while (CanFall())
                {
                    block.Fall();
                }
            }

            field.AddBlock(block);

            scoreBox.Text            = Score.ToString();
            levelBox.Text            = Level.ToString();
            dispatcherTimer.Interval = new TimeSpan(2750000 - (Level * fallrate * 25000));

            block     = new Block(nextBlock, gameField, 0, 6);
            nextBlock = (Square.SquareTypes)rand.Next(0, 7);
            for (int i = 0; i < 4; i++)
            {
                nblock.Squares[i].DeleteSquare();
            }
            nblock = new Block(nextBlock, nextBox, 0, 0);
        }
        private void GameField_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
            case Key.Left:
                if (field.CanShift(false, block))
                {
                    block.ShiftBlock(false);
                }
                break;

            case Key.Right:
                if (field.CanShift(true, block))
                {
                    block.ShiftBlock(true);
                }
                break;

            case Key.Down:
                if (CanFall())
                {
                    block.Fall();
                }
                else
                {
                    field.AddBlock(block);

                    scoreBox.Text            = Score.ToString();
                    levelBox.Text            = Level.ToString();
                    dispatcherTimer.Interval = new TimeSpan(2750000 - (Level * fallrate * 25000));


                    block     = new Block(nextBlock, gameField, 0, 6);
                    nextBlock = (Square.SquareTypes)rand.Next(0, 6);
                    for (int i = 0; i < 4; i++)
                    {
                        nblock.Squares[i].DeleteSquare();
                    }
                    nblock = new Block(nextBlock, nextBox, 0, 0);
                }
                break;

            case Key.Up:
                lock (block)
                {
                    block.RotateBlock(true);
                }
                break;

            case Key.Escape:
                GameQuit();
                break;

            case Key.Space:
                DropBlock();
                break;

            default:
                break;
            }
        }