Beispiel #1
0
        /*键盘操作*/
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (currentBlock == null)
            {
                switch (e.KeyCode)
                {
                case Keys.Right: beginGame();; break;

                case Keys.Left: beginGame();; break;

                case Keys.Up: beginGame();; break;

                case Keys.Down: beginGame();; break;

                case Keys.Enter:
                    beginGame();
                    break;
                }
                picBackGround.Focus();
            }
            if (!timer1.Enabled && currentBlock != null && currentBlock.Top() != 0)
            {
                switch (e.KeyCode)
                {
                case Keys.Enter:     //回车继续游戏
                    beginGame();
                    break;
                }

                picBackGround.Focus();
            }
            if (currentBlock != null && timer1.Enabled && currentBlock.Top() != 0)
            {
                switch (e.KeyCode)
                {
                case Keys.Right: if (!currentBlock.right())
                    {
                        GameField.PlaySound("CanNotDo");
                    }
                    break;                                                                         //向右移动

                case Keys.Left: if (!currentBlock.left())
                    {
                        GameField.PlaySound("CanNotDo");
                    }
                    break;                                   //向左移动

                case Keys.Up: currentBlock.Rotate(); break;  //旋转

                case Keys.Down:  currentBlock.down(); break; //向下加速

                case Keys.Space: while (currentBlock.down())
                    {
                        ;
                    }
                    break;

                case Keys.Back:                     //空格:暂停
                    timer1.Enabled = !timer1.Enabled;
                    if (!timer1.Enabled)
                    {
                        showMsg("暂 停");
                    }
                    else
                    {
                        msg.SendToBack();
                    }
                    break;

                case Keys.Enter: //回车继续游戏
                    beginGame();
                    break;
                }
                picBackGround.Focus();
            }
        }
Beispiel #2
0
 /*窗口重绘*/
 private void Form1_Activated(object sender, EventArgs e)
 {
     picBackGround.Invalidate();
     Application.DoEvents();
     GameField.Redraw();
 }
Beispiel #3
0
        /*旋转block*/
        public void Rotate()
        {
            //保存每个小块的位置
            Point oldPosition1 = square1.location;
            Point oldPosition2 = square2.location;
            Point oldPosition3 = square3.location;
            Point oldPosition4 = square4.location;
            //保存当前的方向
            RotateDirections oldRotation = myRotation;

            //开始试着旋转方块,旋转方向:顺时针方向 旋转中心点为形状拐点
            Erase(GameField.winHandle);
            switch (blockType)
            {
            case BlockTypes.square:
                break;

            case BlockTypes.line:
                //直线的旋转只有两种方向
                switch (myRotation)
                {
                case RotateDirections.North:
                    myRotation       = RotateDirections.East;
                    square1.location = new Point(square2.location.X - squareSize, square2.location.Y);
                    square3.location = new Point(square2.location.X + squareSize, square2.location.Y);
                    square4.location = new Point(square2.location.X + 2 * squareSize, square2.location.Y);
                    break;

                case RotateDirections.East:
                    myRotation       = RotateDirections.North;
                    square1.location = new Point(square2.location.X, square2.location.Y - squareSize);
                    square3.location = new Point(square2.location.X, square2.location.Y + squareSize);
                    square4.location = new Point(square2.location.X, square2.location.Y + 2 * squareSize);
                    break;
                }
                break;

            case BlockTypes.J:
                //J形方块有四种旋转方向
                switch (myRotation)
                {
                case RotateDirections.North:
                    myRotation       = RotateDirections.East;
                    square1.location = new Point(square3.location.X + 2 * squareSize, square3.location.Y);
                    square2.location = new Point(square3.location.X + squareSize, square3.location.Y);
                    square4.location = new Point(square3.location.X, square3.location.Y - squareSize);
                    break;

                case RotateDirections.East:
                    myRotation       = RotateDirections.South;
                    square1.location = new Point(square3.location.X, square3.location.Y + 2 * squareSize);
                    square2.location = new Point(square3.location.X, square3.location.Y + squareSize);
                    square4.location = new Point(square3.location.X + squareSize, square3.location.Y);
                    break;

                case RotateDirections.South:
                    myRotation       = RotateDirections.West;
                    square1.location = new Point(square3.location.X - 2 * squareSize, square3.location.Y);
                    square2.location = new Point(square3.location.X - squareSize, square3.location.Y);
                    square4.location = new Point(square3.location.X, square3.location.Y + squareSize);
                    break;

                case RotateDirections.West:
                    myRotation       = RotateDirections.North;
                    square1.location = new Point(square3.location.X, square3.location.Y - 2 * squareSize);
                    square2.location = new Point(square3.location.X, square3.location.Y - squareSize);
                    square4.location = new Point(square3.location.X - squareSize, square3.location.Y);
                    break;
                }
                break;

            case BlockTypes.L:
                //L形方块有四种旋转方向
                switch (myRotation)
                {
                case RotateDirections.North:
                    myRotation       = RotateDirections.East;
                    square1.location = new Point(square3.location.X + 2 * squareSize, square3.location.Y);
                    square2.location = new Point(square3.location.X + squareSize, square3.location.Y);
                    square4.location = new Point(square3.location.X, square3.location.Y + squareSize);
                    break;

                case RotateDirections.East:
                    myRotation       = RotateDirections.South;
                    square1.location = new Point(square3.location.X, square3.location.Y + 2 * squareSize);
                    square2.location = new Point(square3.location.X, square3.location.Y + squareSize);
                    square4.location = new Point(square3.location.X - squareSize, square3.location.Y);
                    break;

                case RotateDirections.South:
                    myRotation       = RotateDirections.West;
                    square1.location = new Point(square3.location.X - 2 * squareSize, square3.location.Y);
                    square2.location = new Point(square3.location.X - squareSize, square3.location.Y);
                    square4.location = new Point(square3.location.X, square3.location.Y - squareSize);
                    break;

                case RotateDirections.West:
                    myRotation       = RotateDirections.North;
                    square1.location = new Point(square3.location.X, square3.location.Y - 2 * squareSize);
                    square2.location = new Point(square3.location.X, square3.location.Y - squareSize);
                    square4.location = new Point(square3.location.X + squareSize, square3.location.Y);
                    break;
                }
                break;

            case BlockTypes.T:
                //T形方块也有四种旋转方向
                switch (myRotation)
                {
                case RotateDirections.North:
                    myRotation       = RotateDirections.East;
                    square1.location = new Point(square2.location.X, square2.location.Y - squareSize);
                    square3.location = new Point(square2.location.X, square2.location.Y + squareSize);
                    square4.location = new Point(square2.location.X - squareSize, square2.location.Y);
                    break;

                case RotateDirections.East:
                    myRotation       = RotateDirections.South;
                    square1.location = new Point(square2.location.X + squareSize, square2.location.Y);
                    square3.location = new Point(square2.location.X - squareSize, square2.location.Y);
                    square4.location = new Point(square2.location.X, square2.location.Y - squareSize);
                    break;

                case RotateDirections.South:
                    myRotation       = RotateDirections.West;
                    square1.location = new Point(square2.location.X, square2.location.Y + squareSize);
                    square3.location = new Point(square2.location.X, square2.location.Y - squareSize);
                    square4.location = new Point(square2.location.X + squareSize, square2.location.Y);
                    break;

                case RotateDirections.West:
                    myRotation       = RotateDirections.North;
                    square1.location = new Point(square2.location.X - squareSize, square2.location.Y);
                    square3.location = new Point(square2.location.X + squareSize, square2.location.Y);
                    square4.location = new Point(square2.location.X, square2.location.Y + squareSize);
                    break;
                }
                break;

            case BlockTypes.Z:
                //Z形方块有两种旋转方向
                switch (myRotation)
                {
                case RotateDirections.North:
                    myRotation       = RotateDirections.East;
                    square1.location = new Point(square2.location.X, square2.location.Y - squareSize);
                    square3.location = new Point(square2.location.X - squareSize, square2.location.Y);
                    square4.location = new Point(square2.location.X - squareSize, square2.location.Y + squareSize);
                    break;

                case RotateDirections.East:
                    myRotation       = RotateDirections.North;
                    square1.location = new Point(square2.location.X - squareSize, square2.location.Y);
                    square3.location = new Point(square2.location.X, square2.location.Y + squareSize);
                    square4.location = new Point(square2.location.X + squareSize, square2.location.Y + squareSize);
                    break;
                }
                break;

            case BlockTypes.S:
                //S形方块有两种旋转方向
                switch (myRotation)
                {
                case RotateDirections.North:
                    myRotation       = RotateDirections.East;
                    square1.location = new Point(square3.location.X + squareSize, square3.location.Y + squareSize);
                    square2.location = new Point(square3.location.X + squareSize, square3.location.Y);
                    square4.location = new Point(square3.location.X, square3.location.Y - squareSize);
                    break;

                case RotateDirections.East:
                    myRotation       = RotateDirections.North;
                    square1.location = new Point(square3.location.X - squareSize, square3.location.Y + squareSize);
                    square2.location = new Point(square3.location.X, square3.location.Y + squareSize);
                    square4.location = new Point(square3.location.X + squareSize, square3.location.Y);
                    break;
                }
                break;
            }
            //旋转之后检测位置是否冲突
            if (!(GameField.isEmpty(square1.location.X / squareSize, square1.location.Y / squareSize) &&
                  GameField.isEmpty(square2.location.X / squareSize, square2.location.Y / squareSize) &&
                  GameField.isEmpty(square3.location.X / squareSize, square3.location.Y / squareSize) &&
                  GameField.isEmpty(square4.location.X / squareSize, square4.location.Y / squareSize)))
            {//如果有冲突则回到原来的状态
                myRotation       = oldRotation;
                square1.location = oldPosition1;
                square2.location = oldPosition2;
                square3.location = oldPosition3;
                square4.location = oldPosition4;
            }
            Draw(GameField.winHandle);
        }