Example #1
0
 private void NewGame()
 {
     timer1.Interval = time;
     CreatePanel(10, 20);
     CreatePanel(4, 4);
     labelNext.Text = Convert.ToString(upScore);
     figure         = (Tetris.Figure)figures.GetValue(r.Next(figures.Length));
     Tetris.SetNewFigure(ref current, figure);
     Tetris.Place(current, ref moving, array, ref x, ref y, ref coordinates);
     Update(true);
     nextFigure = (Tetris.Figure)figures.GetValue(r.Next(figures.Length));
     Tetris.SetNewFigure(ref next, nextFigure);
     Draw(next, nextFigure);
 }
Example #2
0
        static public void SetPCShips(ref int[,] array)
        {
            Random r    = new Random();
            int    rand = r.Next(0, 2);

            SetShip(ref array, 0, 0, true, 1, 4);
            SetShip(ref array, 0, 5, true, 1, 2);
            SetShip(ref array, 0, 8, true, 1, 2);
            if (rand == 0)
            {
                SetShip(ref array, 2, 0, true, 1, 3);
                SetShip(ref array, 2, 4, true, 1, 3);
                SetShip(ref array, 2, 8, true, 1, 2);
            }
            else
            {
                SetShip(ref array, 9, 0, true, 1, 3);
                SetShip(ref array, 9, 4, true, 1, 3);
                SetShip(ref array, 9, 8, true, 1, 2);
            }
            for (int i = 0; i < 4; i++)
            {
                int x = 0;
                int y = 0;
                while (array[x + 1, y + 1] != (int)State.Empty)
                {
                    if (x == 0)
                    {
                        x = r.Next(4, 10);
                    }
                    else
                    {
                        x = r.Next(2, 8);
                    }
                    y = r.Next(0, 10);
                }
                SetShip(ref array, x, y, true, 1, 1);
            }
            int n = r.Next(0, 4);

            for (int i = 0; i < n; i++)
            {
                Tetris.Rotate(ref array);
            }
        }
Example #3
0
        private void ChangePosition(Tetris.Direction direction)
        {
            for (int i = 0; i < 4; i++)
            {
                moving[coordinates[0, i], coordinates[1, i]] = 0;
            }
            Update(false);
            if (direction == Tetris.Direction.Rotate)
            {
                Tetris.Rotate(ref current);
            }
            else
            {
                for (int i = 0; i < 4; i++)
                {
                    switch (direction)
                    {
                    case Tetris.Direction.Down: coordinates[1, i] += 1; break;

                    case Tetris.Direction.Left: coordinates[0, i] -= 1; break;

                    case Tetris.Direction.Right: coordinates[0, i] += 1; break;
                    }
                }

                for (int i = 0; i < 4; i++)
                {
                    moving[coordinates[0, i], coordinates[1, i]] = Convert.ToInt32(figure);
                }
                switch (direction)
                {
                case Tetris.Direction.Down: y++; break;

                case Tetris.Direction.Left: x--;; break;

                case Tetris.Direction.Right: x++; break;
                }

                Update(true);
            }
        }
Example #4
0
        private void FormTetris_KeyDown(object sender, KeyEventArgs e)
        {
            if (game == true)
            {
                switch (e.KeyCode)
                {
                case Keys.Left:
                case Keys.A:
                    if (coordinates[0, 0] != 0 && coordinates[0, 1] != 0 && coordinates[0, 2] != 0 && coordinates[0, 3] != 0 &&
                        array[coordinates[0, 0] - 1, coordinates[1, 0]] == 0 && array[coordinates[0, 1] - 1, coordinates[1, 1]] == 0 &&
                        array[coordinates[0, 2] - 1, coordinates[1, 2]] == 0 && array[coordinates[0, 3] - 1, coordinates[1, 3]] == 0)
                    {
                        MoveSound.Play();
                        ChangePosition(Tetris.Direction.Left);
                    }
                    break;


                case Keys.Right:
                case Keys.D:
                    if (coordinates[0, 0] != 9 && coordinates[0, 1] != 9 && coordinates[0, 2] != 9 && coordinates[0, 3] != 9
                        &&
                        array[coordinates[0, 0] + 1, coordinates[1, 0]] == 0 && array[coordinates[0, 1] + 1, coordinates[1, 1]] == 0 &&
                        array[coordinates[0, 2] + 1, coordinates[1, 2]] == 0 && array[coordinates[0, 3] + 1, coordinates[1, 3]] == 0)
                    {
                        MoveSound.Play();
                        ChangePosition(Tetris.Direction.Right);
                    }
                    break;

                case Keys.Down:
                case Keys.S:
                    while (coordinates[1, 0] != 19 && coordinates[1, 1] != 19 && coordinates[1, 2] != 19 && coordinates[1, 3] != 19 &&
                           array[coordinates[0, 0], coordinates[1, 0] + 1] == 0 && array[coordinates[0, 1], coordinates[1, 1] + 1] == 0 &&
                           array[coordinates[0, 2], coordinates[1, 2] + 1] == 0 && array[coordinates[0, 3], coordinates[1, 3] + 1] == 0)
                    {
                        ChangePosition(Tetris.Direction.Down);
                    }
                    if (coordinates[1, 0] == 19 || coordinates[1, 1] == 19 || coordinates[1, 2] == 19 || coordinates[1, 3] == 19 ||
                        array[coordinates[0, 0], coordinates[1, 0] + 1] != 0 || array[coordinates[0, 1], coordinates[1, 1] + 1] != 0 ||
                        array[coordinates[0, 2], coordinates[1, 2] + 1] != 0 || array[coordinates[0, 3], coordinates[1, 3] + 1] != 0)
                    {
                        Down();
                    }
                    break;

                case Keys.Up:
                case Keys.W:
                    ChangePosition(Tetris.Direction.Rotate);
                    if (Tetris.Place(current, ref moving, array, ref x, ref y, ref coordinates) == true)
                    {
                        Rotate.Play();
                        Update(true);
                    }
                    else
                    {
                        for (int i = 0; i < 3; i++)
                        {
                            Tetris.Rotate(ref current);
                        }
                        Tetris.Place(current, ref moving, array, ref x, ref y, ref coordinates);
                        Update(true);
                    }
                    break;
                }
            }
            switch (e.KeyCode)
            {
            case Keys.Escape:
            {
                timer1.Enabled = false;
                FormPause Pause = new FormPause(this);
                Pause.ShowDialog();
            }
            break;
            }
        }
Example #5
0
        public void Down()
        {
            if (coordinates[1, 0] != 19 && coordinates[1, 1] != 19 && coordinates[1, 2] != 19 && coordinates[1, 3] != 19 &&
                array[coordinates[0, 0], coordinates[1, 0] + 1] == 0 && array[coordinates[0, 1], coordinates[1, 1] + 1] == 0 &&
                array[coordinates[0, 2], coordinates[1, 2] + 1] == 0 && array[coordinates[0, 3], coordinates[1, 3] + 1] == 0)
            {
                ChangePosition(Tetris.Direction.Down);
            }
            else
            {
                DownSound.Play();
                AddScore(0);
                for (int i = 0; i < 4; i++)
                {
                    moving[coordinates[0, i], coordinates[1, i]] = 0;
                }
                for (int i = 0; i < 4; i++)
                {
                    array[coordinates[0, i], coordinates[1, i]] = Convert.ToInt32(figure);
                }
                int linesCount = Tetris.FullLine(ref array);

                if (linesCount > 0)
                {
                    Win.Play();
                    AddScore(linesCount);
                    Draw(array, figure);
                }
                if (score >= upScore)
                {
                    Tetris.NextLevel(ref level, out time, out upScore);
                    labelNext.Text  = Convert.ToString(upScore);
                    timer1.Interval = time;
                }
                x = 3;
                y = 0;
                Tetris.NullArray(ref current);
                Tetris.NullArray(ref coordinates);
                figure = nextFigure;
                Tetris.SetNewFigure(ref current, figure);
                Clear(Next);
                nextFigure = (Tetris.Figure)figures.GetValue(r.Next(figures.Length));
                Tetris.SetNewFigure(ref next, nextFigure);
                Draw(next, nextFigure);
                if (Tetris.Place(current, ref moving, array, ref x, ref y, ref coordinates) == true)
                {
                    Update(true);
                }
                else
                {
                    Lose.Play();
                    timer1.Enabled = false;
                    game           = false;
                    DialogResult result = MessageBox.Show("Continue", "You Lose", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        Restart();
                    }
                }
            }
        }