Ejemplo n.º 1
0
        public Block Clone(RollTypes roll)
        {
            Block block = new Block();
            block.Id = this.Id;
            block.X = this.X;
            block.Y = this.Y;

            int x, y;

            if (roll == RollTypes.Left)
            {
                for (y = 0; y < 4; y++)
                {
                    for (x = 0; x < 4; x++)
                    {
                        block.Data[y, 3 - x] = this.Data[x, y];
                    }
                }
            }
            else
            {
                for (y = 0; y < 4; y++)
                {
                    for (x = 0; x < 4; x++)
                    {
                        block.Data[3 - y, x] = this.Data[x, y];
                    }
                }
            }
            return block;
        }
Ejemplo n.º 2
0
 public virtual bool HitTest(Block block, RollTypes roll)
 {
     var move = block.Clone(roll);
     return HitTest(move.Data, move.X, move.Y);
 }
Ejemplo n.º 3
0
 public virtual bool HitTest(Block block, int dx, int dy)
 {
     return HitTest(block.Data, block.X + dx, block.Y + dy);
 }
Ejemplo n.º 4
0
        public virtual void SetBlock(Block block)
        {
            int w = this.W;
            int h = this.H;
            for (int x = 0; x < 4; x++)
            {
                for (int y = 0; y < 4; y++)
                {
                    int dat = block.Data[x,y];
                    if (dat == 0)
                        continue;

                    int mx = block.X + x;
                    if (mx < 0 || w <= mx)
                    {
                        continue;
                    }

                    int my = block.Y + y;
                    if (my < 0 || h <= my)
                    {
                        continue;
                    }

                    this.Data[mx,my] = block.Id;
                }
            }
        }
Ejemplo n.º 5
0
        public bool GameLoop()
        {
            // --------------------------------------------------------
            if (field.IsGameOver())
            {
                return false;
            }

            // --------------------------------------------------------
            if (block == null)
            {
                block = new Block(rand.Next(Block.Blocks.Count - 1) + 1, field);
            }

            // --------------------------------------------------------
            if (MoveLeft)
            {
                if (!field.HitTest(block, -1, 0))
                {
                    block.X--;
                }
            }
            if (MoveRight)
            {
                if (!field.HitTest(block, +1, 0))
                {
                    block.X++;
                }
            }
            if (RollLeft)
            {
                if (!field.HitTest(block, RollTypes.Left))
                {
                    block.Roll(RollTypes.Left);
                }
            }
            if (RollRight)
            {
                if (!field.HitTest(block, RollTypes.Right))
                {
                    block.Roll(RollTypes.Right);
                }
            }

            // --------------------------------------------------------
            if (!field.HitTest(block, 0, +1))
            {
                block.Y++;
            }
            else
            {
                field.SetBlock(block);
                block = null;
            }

            // --------------------------------------------------------
            field.ClearLine();

            // --------------------------------------------------------
            MoveLeft  = false;
            MoveRight = false;
            MoveDown  = false;
            RollLeft  = false;
            RollRight = false;

            return true;
        }
Ejemplo n.º 6
0
 public TetrisViewModel()
 {
     rand = new Random();
     field = new Field();
     block = null;
 }