Пример #1
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);
            }
        }
Пример #2
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);
            }
        }
Пример #3
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;
            }
        }