Exemple #1
0
 public override void Rotate(TetrisGrid tetris)
 {
     if (BottomedOut(tetris))
     {
         return;
     }
     Draw(tetris);
 }
Exemple #2
0
 public PlayWindow()
 {
     InitializeComponent();
     tg = new TetrisGrid(10, 18);
     UpdateCanvas();
     timer.Interval    = TimeSpan.FromSeconds(currentinterval);
     timer.Tick       += Update;
     this.currentLevel = 1;
     GetHighScore();
 }
Exemple #3
0
 public void ShiftShapeDown(TetrisGrid tetris)
 {
     UnDraw(tetris);
     foreach (Block block in AllBlocks)
     {
         block.X++;
     }
     Draw(tetris);
     LowestPoint++;
 }
Exemple #4
0
        public bool BottomedOut(TetrisGrid tetris)
        {
            int[][] grid = tetris.GetGrid();
            int     lowestpointofgrid = grid.Length - 1;

            if (LowestPoint == lowestpointofgrid || this.IsBottomedOut)
            {
                this.IsBottomedOut = true;
                return(true);
            }
            else if (LowestPoint < lowestpointofgrid)
            {
                List <Block> BottomBlocks = new List <Block>();
                List <int>   BlockColumns = new List <int>();
                foreach (Block block in AllBlocks)
                {
                    BlockColumns.Add(block.Y);
                }
                foreach (Block block in AllBlocks)
                {
                    if (block.X == LowestPoint)
                    {
                        BottomBlocks.Add(block);
                    }
                    if (block.X != LowestPoint)
                    {
                        List <Block> listofthiscolumnsblock = new List <Block>();
                        foreach (Block b in AllBlocks)
                        {
                            if (b.Y == block.Y)
                            {
                                listofthiscolumnsblock.Add(b);
                            }
                        }
                        if (block.X == listofthiscolumnsblock.Max(Block => Block.X))
                        {
                            BottomBlocks.Add(block);
                        }
                    }
                }
                foreach (Block block in BottomBlocks)
                {
                    if ((block.X + 1 <= 17) && grid[block.X + 1][block.Y] != 0)
                    {
                        this.IsBottomedOut = true;
                        return(true);
                    }
                }
            }
            return(false);
        }
Exemple #5
0
 public void UnDraw(TetrisGrid tetris)
 {
     int[][] grid = tetris.GetGrid();
     foreach (Block block in AllBlocks)
     {
         if (block.X > 17 || block.X < 0)
         {
             tetris.SetGrid(grid);
             return;
         }
         if (block.Y > 9 || block.Y < 0)
         {
             tetris.SetGrid(grid);
             return;
         }
         grid[block.X][block.Y] = 0;
     }
     tetris.SetGrid(grid);
 }
Exemple #6
0
        public void ShiftLeft(TetrisGrid tetris)
        {
            int[][] grid    = tetris.GetGrid();
            int     min     = AllBlocks.Min(Block => Block.Y);
            int     minleft = 0;

            if (min <= minleft)
            {
                return;
            }
            foreach (Block block in AllBlocks)
            {
                if (min > 0 && (grid[block.X][min - 1] != 0))
                {
                    return;
                }
            }
            UnDraw(tetris);
            foreach (Block block in AllBlocks)
            {
                block.Y--;
            }
            Draw(tetris);
        }
Exemple #7
0
        public void ShiftRight(TetrisGrid tetris)
        {
            int[][] grid     = tetris.GetGrid();
            int     right    = AllBlocks.Max(Block => Block.Y);
            int     maxright = 9;

            if (right >= maxright)
            {
                return;
            }
            foreach (Block block in AllBlocks)
            {
                if (right < 9 && (grid[block.X][right + 1] != 0))
                {
                    return;
                }
            }
            UnDraw(tetris);
            foreach (Block block in AllBlocks)
            {
                block.Y++;
            }
            Draw(tetris);
        }
Exemple #8
0
        public void Update(object sender, EventArgs e)
        {
            if (this.tg.youlost())
            {
                if (this.tg.CurrentScore > this.hiscore)
                {
                    this.SetHighScore(this.tg.CurrentScore);
                }
                this.timer.Stop();
                this.tg = new TetrisGrid(10, 18);
                this.UpdateCanvas();
                this.scorebox.Text        = "Current Score: 0";
                this.playButt.IsEnabled   = true;
                this.pauseButt.IsEnabled  = false;
                this.Save.IsEnabled       = false;
                this.Save.Foreground      = new SolidColorBrush(Colors.DarkGray);
                this.playButt.Foreground  = new SolidColorBrush(Colors.Red);
                this.pauseButt.Foreground = new SolidColorBrush(Colors.DarkGray);
                this.msgbox.Text          = "You lost!";
                return;
            }
            this.tg.UpdateGrid();
            this.UpdateCanvas();

            Shape next = this.tg.GetNextShape();

            if (next is LineShape)
            {
                this.nextUp.Source = new BitmapImage(new Uri("line.jpg", UriKind.RelativeOrAbsolute));
            }
            if (next is Square)
            {
                this.nextUp.Source = new BitmapImage(new Uri("square.jpg", UriKind.RelativeOrAbsolute));
            }
            if (next is LeftHook)
            {
                this.nextUp.Source = new BitmapImage(new Uri("righthook.png", UriKind.RelativeOrAbsolute));
            }
            if (next is RightHook)
            {
                this.nextUp.Source = new BitmapImage(new Uri("righthook.png", UriKind.RelativeOrAbsolute));
            }
            if (next is LeftZed)
            {
                this.nextUp.Source = new BitmapImage(new Uri("sideS.png", UriKind.RelativeOrAbsolute));
            }
            if (next is RightZed)
            {
                this.nextUp.Source = new BitmapImage(new Uri("sideS.png", UriKind.RelativeOrAbsolute));
            }
            if (next is TeeShape)
            {
                this.nextUp.Source = new BitmapImage(new Uri("pyramid.png", UriKind.RelativeOrAbsolute));
            }

            this.scorebox.Text = "Current Score: " + this.tg.CurrentScore;
            if (this.tg.CurrentScore >= 10 && this.tg.CurrentScore % 10 == 0)
            {
                this.tg.CurrentScore += 5;
                this.currentinterval *= 0.75;
                this.currentLevel++;
                timer.Interval = TimeSpan.FromSeconds(currentinterval);
            }
            this.msgbox.Text = "Level " + this.currentLevel;
        }
Exemple #9
0
        public override void Rotate(TetrisGrid tetris)
        {
            int[][] grid = tetris.GetGrid();
            int     maxRight = grid[0].Length - 1, maxTop = grid.Length - 1;

            if (BottomedOut(tetris))
            {
                return;
            }
            UnDraw(tetris);
            switch (DegreesRotated)
            {
            case 0:
                if (PivotBlock.X + 1 > maxTop)
                {
                    PivotBlock.X -= 3;
                }
                else if (PivotBlock.X + 2 > maxTop)
                {
                    PivotBlock.X -= 2;
                }
                else if (PivotBlock.X + 3 > maxTop)
                {
                    PivotBlock.X -= 1;
                }
                AllBlocks.Clear();
                AllBlocks.Add(PivotBlock);
                AllBlocks.Add(new Block(PivotBlock.X + 1, PivotBlock.Y));
                AllBlocks.Add(new Block(PivotBlock.X + 2, PivotBlock.Y));
                AllBlocks.Add(new Block(PivotBlock.X + 3, PivotBlock.Y));
                LowestPoint          = PivotBlock.X + 3;
                this.DegreesRotated += 90;
                break;

            case 90:
                if (PivotBlock.Y - 1 < 0)
                {
                    PivotBlock.Y += 3;
                }
                else if (PivotBlock.Y - 2 < 0)
                {
                    PivotBlock.Y += 2;
                }
                else if (PivotBlock.Y - 3 < 0)
                {
                    PivotBlock.Y += 1;
                }
                AllBlocks.Clear();
                AllBlocks.Add(PivotBlock);
                AllBlocks.Add(new Block(PivotBlock.X, PivotBlock.Y - 1));
                AllBlocks.Add(new Block(PivotBlock.X, PivotBlock.Y - 2));
                AllBlocks.Add(new Block(PivotBlock.X, PivotBlock.Y - 3));
                LowestPoint          = PivotBlock.X;
                this.DegreesRotated += 90;
                break;

            case 180:
                if (PivotBlock.X - 1 < 0)
                {
                    PivotBlock.X += 3;
                }
                else if (PivotBlock.X - 2 < 0)
                {
                    PivotBlock.X += 2;
                }
                else if (PivotBlock.X - 3 < 0)
                {
                    PivotBlock.X += 1;
                }
                AllBlocks.Clear();
                AllBlocks.Add(PivotBlock);
                AllBlocks.Add(new Block(PivotBlock.X - 1, PivotBlock.Y));
                AllBlocks.Add(new Block(PivotBlock.X - 2, PivotBlock.Y));
                AllBlocks.Add(new Block(PivotBlock.X - 3, PivotBlock.Y));
                LowestPoint          = PivotBlock.X;
                this.DegreesRotated += 90;
                break;

            case 270:
                if (PivotBlock.Y + 1 > maxRight)
                {
                    PivotBlock.Y -= 3;
                }
                else if (PivotBlock.Y + 2 > maxRight)
                {
                    PivotBlock.Y -= 2;
                }
                else if (PivotBlock.Y + 3 > maxRight)
                {
                    PivotBlock.Y -= 1;
                }
                AllBlocks.Clear();
                AllBlocks.Add(PivotBlock);
                AllBlocks.Add(new Block(PivotBlock.X, PivotBlock.Y + 1));
                AllBlocks.Add(new Block(PivotBlock.X, PivotBlock.Y + 2));
                AllBlocks.Add(new Block(PivotBlock.X, PivotBlock.Y + 3));
                LowestPoint         = PivotBlock.X;
                this.DegreesRotated = 0;
                break;
            }
            Draw(tetris);
        }
Exemple #10
0
 public abstract void Rotate(TetrisGrid tetris);