示例#1
0
        private void InitialiseGameScreenForm()
        {
            tmrGame.Tick    += new EventHandler(onTick);
            this.KeyDown    += new KeyEventHandler(onKeyDown);
            this.MouseMove  += new MouseEventHandler(onMouseMove);
            this.MouseClick += new MouseEventHandler(onMouseClick);
            this.Paint      += new PaintEventHandler(onPaint);

            DoubleBuffered = true;
            KeyPreview     = true;

            this.MinimizeBox = false;
            this.MaximizeBox = false;

            this.BackColor       = Color.SkyBlue;
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.Size            = new Size(640, 482);
            this.Text            = "Breakout Game";

            int intBrickRows    = 5;
            int intBrickColumns = 5;
            int intBrickWidth   = 80;
            int intBrickHeight  = 20;
            int paddleWidth     = 80;
            int paddleHeight    = 15;
            int paddleX         = 300;
            int paddleY         = 434;
            int ballWidth       = 20;
            int ballHeight      = 20;
            int ballX           = paddleX + paddleHeight / 2 - (ballHeight / 2);
            int ballY           = paddleY - ballWidth;

            float defaultSpeed = 20;
            float defaultXVel  = (float)Math.Cos(defaultSpeed) * defaultSpeed;
            float defaultYVel  = (float)Math.Cos(defaultSpeed) * defaultSpeed;

            paddle = new MoveableGameObject(paddleX, paddleY,
                                            paddleWidth, paddleHeight,
                                            true,
                                            defaultSpeed,
                                            0, 0,
                                            false,
                                            Brushes.Pink);

            ball = new MoveableGameObject(ballX, ballY,
                                          ballWidth, ballHeight,
                                          true,
                                          defaultSpeed,
                                          defaultXVel, defaultYVel,
                                          true,
                                          Brushes.Yellow);

            arrBricks = new GameObject[intBrickRows, intBrickColumns];

            tmrGame.Interval = 20;
            tmrGame.Start();

            int intXOffset = (intBrickWidth / 2) + 1;
            int intYOffset = 100;

            for (int intRow = 0; intRow <= arrBricks.GetUpperBound(0); intRow++)
            {
                for (int intColumn = 0; intColumn <= arrBricks.GetUpperBound(1); intColumn++)
                {
                    arrBricks[intRow, intColumn] = new GameObject(intXOffset, intYOffset,
                                                                  intBrickWidth, intBrickHeight,
                                                                  true,
                                                                  Brushes.Red);
                    intXOffset += intBrickWidth + 10;
                }
                intYOffset += intBrickHeight + 10;
                intXOffset  = (intBrickWidth / 2) + 1;
            }
        }
示例#2
0
        private void InitialiseGameScreenForm()
        {
            timer.Tick      += new EventHandler(onTick);
            this.KeyDown    += new KeyEventHandler(onKeyDown);
            this.MouseMove  += new MouseEventHandler(onMouseMove);
            this.MouseClick += new MouseEventHandler(onMouseClick);

            this.MinimizeBox = false;
            this.MaximizeBox = false;

            this.BackColor       = Color.SkyBlue;
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.Size            = new Size(640, 482);
            this.Text            = "Breakout Game";

            int brickRows    = 5;
            int brickCols    = 5;
            int brickWidth   = 80;
            int brickHeight  = 20;
            int paddleWidth  = 80;
            int paddleHeight = 15;
            int paddleX      = 300;
            int paddleY      = 434;
            int ballWidth    = 20;
            int ballHeight   = 20;
            int ballX        = paddleX + paddleHeight / 2 - (ballHeight / 2);
            int ballY        = paddleY - ballWidth;

            float defaultSpeed = 20;
            float defaultXVel  = (float)Math.Cos(45) * defaultSpeed;
            float defaultYVel  = (float)Math.Sin(45) * defaultSpeed;

            paddle = new MoveableGameObject(this,                        // the current form
                                            paddleX, paddleY,
                                            paddleWidth, paddleHeight,
                                            true,                        // visibility
                                            defaultSpeed,
                                            0, 0,                        // initial velocities
                                            false,                       // glued or not
                                            System.Drawing.Color.Pink);

            ball = new MoveableGameObject(this,
                                          ballX, ballY,
                                          ballWidth, ballHeight,
                                          true,
                                          defaultSpeed,
                                          defaultXVel, defaultYVel,
                                          true,
                                          System.Drawing.Color.Yellow);

            bricks = new GameObject[brickRows, brickCols];

            timer.Interval = 20;
            timer.Start();

            int xOffset = (brickWidth / 2) + 1;
            int yOffset = 100;

            for (int row = 0; row <= bricks.GetUpperBound(0); row++)
            {
                for (int column = 0; column <= bricks.GetUpperBound(1); column++)
                {
                    bricks[row, column] = new GameObject(this,
                                                         xOffset, yOffset,
                                                         brickWidth, brickHeight,
                                                         true,
                                                         System.Drawing.Color.Red);
                    xOffset += brickWidth + 10;
                }
                yOffset += brickHeight + 10;
                xOffset  = (brickWidth / 2) + 1;
            }
        }