/// <summary> /// ��ʼ��һ������Ϸ����ש��/��/��ǿ������Ҫ�����ʱ����� /// </summary> private void InitializeGame() { this.play(); powerUpList.Clear(); this.BackgroundImage = backgrounds[level-1];//ÿ�ػ�������,���ؿ�������ͼƬ����ʱ��ӵ�һ���ٴο�ʼ paddle1 = new Paddle( 300, 380, 66, 24); InitializeBall(level); //��ʼ��һ���� int brickHeight = 20;//ש��� int brickPadding = 5;//ש���� int borderPadding = 20;//ש�鵽�߽�ľ��� InitializeBricks(brickArray, brickColumns, brickRows, brickHeight, brickPadding, borderPadding, level); for (int j = 0; j < brickRows; j++) { for (int i = 0; i < brickColumns; i++) { if (brickArray[i, j].Strength != 0) brickCount++; } } }
/// <summary> /// �����Ļҳ��Ƚ�����ײģ�� /// ����������˰�Ľǣ������䷴�� /// ����������˰���ϲ���Y�����ٶȷ�ת /// ���������һ�������ƶ���������������Ǹ������һ���ٶ� /// ͬ��Ҫȷ������ٶȲ��ܳ�����뾶 /// </summary> /// <param name="paddle">��Ҫ�жϵİ�</param> public void Collide(Paddle paddle) { ReverseY(); //������Σ��ȷ�תY�����ٶ���˵ //����������κ�һ�ǣ���תX�ٶ� if ((paddle.Top > this.Top +dY && paddle.Top < this.Bottom + dY) && (paddle.Left > this.Left + dX && paddle.Left < this.Right + dX && dX > 0) || (paddle.Right > this.Left + dX && paddle.Right < this.Right + dX && dX < 0)) ReverseX(); int ratio = 4; // ���ٶ�/���ٶ� dX += (paddle.X - paddle.OldX)/ratio; dY += (paddle.Y - paddle.OldY)/ratio; //���ٶȶ����ٶȲ�����Ӱ�� if (dX > radius) dX = radius; if (dX < -radius) dX = -radius; if (dY > radius) dY = radius; if (dY < -radius) dY = -radius; //��ֹ����ٶȹ��죨�����뾶������������뾶����ײ���������ײ���������㷨���� this.Active = true; //��֮ǰ���û�м����ô���ںͰ�����֮��������ɵ��� }
/// <summary> /// �����Ͱ��Ƿ�����ײ����������͵���Collision()���� /// </summary> /// <param name="ball">Ҫ������</param> /// <param name="paddle">Ҫ���İ�</param> private void DetectPaddleCollision(Ball ball, Paddle paddle) { if ((ball.Right + ball.XSpeed > paddle.Left) && (ball.Left + ball.XSpeed < paddle.Right) && (ball.Bottom + ball.YSpeed > paddle.Top) && (ball.Top + ball.YSpeed < paddle.Bottom) && ball.YSpeed >= 0) //ͬ�ϱ��жϣ�������ײ��� { ball.Collide(paddle); paddleSound.Play(); } }
/// <summary> /// ��List ball���裬ʹ��Ļ��ֻ����һ�����Ҹ���������ٶȣ�����X�����Y����,����Y������ٶȸ��ڼ����й� /// </summary> private void InitializeBall(int l) { if (newPaddle != null) newPaddle.Dispose(); ballList.Clear(); Random randy = new Random(); int radius = 7; int dX = randy.Next(-radius, radius); int dY; switch (l) { case 1: dY = 1; break; case 2: dY = 2; break; case 3: dY = 3; break; case 4: dY = 4; break; case 5: dY = 5; break; case 6: dY = 6; break; default: dY = 7; break; } ballList.Add(new Ball(Brushes.White, radius, this.Width / 2, this.Height / 2 + 10, dX, dY)); paddle1 = new Paddle(300, 380, 66, 24); this.newPaddle = new Label(); newPaddle.Visible = true; newPaddle.AutoSize = false; newPaddle.Size = new Size(66, 24); newPaddle.Image = pad[1]; newPaddle.BackColor = Color.Transparent; newPaddle.Location = new Point(paddle1.X, paddle1.Y); this.Controls.Add(newPaddle); if (level % 3 == 0)//����BOSS { MessageBox.Show("ǰ�������˰������ĸ������Ȼ��û���κι�������������ȻҪ��ʱ�����������������ٻ�����İ�����ս������", "���Դ���ľ���", MessageBoxButtons.OK, MessageBoxIcon.Information); bossCount = level *4; this.boss = new Label(); this.boss.BackColor = Color.Transparent; boss.Visible = true; boss.Location = new Point((this.Size.Width - boss.Size.Width) / 2, this.Size.Height / 8); this.Controls.Add(boss); switch (level)//���ؿ�ѡ�� { case 3: { alternation = 120; boss.Image = boss_image[0]; boss.Size = boss_image[0].Size; } break; case 6: {alternation = 100; boss.Image = boss_image[1]; boss.Size = boss_image[1].Size;} break; case 9: {alternation = 90; boss.Image = boss_image[2]; boss.Size = boss_image[2].Size;} break; case 12: { alternation = 60; boss.Image = boss_image[3]; boss.Size = boss_image[3].Size; } break; } } }
/// <summary> /// Advanced Collision Model between the ball and a paddle /// If the ball hits a corner of the paddle, it is reflected back /// If the ball hits the top of the paddle, it's y-speed is inverted /// If the paddle is moving in any direction, it will nudge the ball in that direction /// Also ensures the ball moves no further than it's radius per timer tick /// </summary> /// <param name="paddle">The paddle to detect collisions with</param> public void Collide(Paddle paddle) { ReverseY(); //no matter what, the y-speed is always reversed //if it will impact on any corner, reverse the X if ((paddle.Top > this.Top +dY && paddle.Top < this.Bottom + dY) //does the ball intersect the top of the paddle AND && (paddle.Left > this.Left + dX && paddle.Left < this.Right + dX && dX > 0) //is the ball approaching from the left and intersecting the left? || (paddle.Right > this.Left + dX && paddle.Right < this.Right + dX && dX < 0)) //is the ball approaching from the right and intersecting the right? ReverseX(); int ratio = 4; //ratio of paddle speed : ball speed change dX += (paddle.X - paddle.OldX)/ratio; dY += (paddle.Y - paddle.OldY)/ratio; //increment the speed by the speed of the paddle if (dX > radius) dX = radius; if (dX < -radius) dX = -radius; if (dY > radius) dY = radius; if (dY < -radius) dY = -radius; //catch if dX or dY is more than the radius //the collision detection doesn't work if the ball travels too fast //the ball could go right through a brick or wall or paddle without being detected //also the game is more fun if the ball can't get too fast this.Active = true; //if the ball wasn't active before, i.e. it is a powerup from a brick, it is active now }
/// <summary> /// Sets up a new game. Called whenever the bricks/balls/powerups need to be reset /// </summary> private void InitializeGame() { brickCount = brickRows * brickColumns; //resets the brick count powerUpList.Clear(); this.BackgroundImage = backgrounds[level % backgrounds.Length]; //changes background with each level //rotates back to the beginning of the image array after the level number > number images in array paddle1 = new Paddle(Brushes.Black, 100, 100, 50, 10); InitializeBall(); //creates one ball int brickHeight = 20; int brickPadding = 5; int borderPadding = 50; InitializeBricks(brickArray, brickColumns, brickRows, brickHeight, brickPadding, borderPadding); }
/// <summary> /// Checks if a ball is about to hit the paddle and calls the Collide() method of the ball if it is /// </summary> /// <param name="ball">Ball object to check for collision</param> /// <param name="paddle">Paddle object to check for collision</param> private void DetectPaddleCollision(Ball ball, Paddle paddle) { if ((ball.Right + ball.XSpeed > paddle.Left) && (ball.Left + ball.XSpeed < paddle.Right) && (ball.Bottom + ball.YSpeed > paddle.Top) && (ball.Top + ball.YSpeed < paddle.Bottom) && ball.YSpeed >= 0) //is the ball about to hit the paddle { ball.Collide(paddle); paddleSound.Play(); } }
/// <summary> /// ��ʼ��һ������Ϸ����ש��/��/��ǿ������Ҫ�����ʱ����� /// </summary> private void InitializeGame() { powerUpList.Clear(); this.BackgroundImage = backgrounds[level % backgrounds.Length]; //ÿ�ػ������� //���ؿ�������ͼƬ����ʱ��ӵ�һ���ٴο�ʼ paddle1 = new Paddle(Brushes.Black, 300, 400, 50, 10); InitializeBall(level); //��ʼ��һ���� int brickHeight = 20;//ש��� int brickPadding = 5;//ש���� int borderPadding = 50;//ש�鵽�߽�ľ��� InitializeBricks(brickArray, brickColumns, brickRows, brickHeight, brickPadding, borderPadding,level); for (int j = 0; j < brickRows; j++) { for (int i = 0; i < brickColumns; i++) { if (brickArray[i, j].Strength != 0) brickCount++; } } if (level ==1) { bossCount = 1; Image ba = Resource1.ball; this.boss = new Label(); boss.Visible = true; boss.AutoSize = false; boss.Size = new Size(40, 40); boss.Image = ba; boss.Location = new Point((this.Size.Width-boss.Size .Width)/2,this.Size .Height/8 ); this.Controls.Add(boss); } }
/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); Texture2D tempTexture = Content.Load<Texture2D>("paddle"); paddle = new Paddle(tempTexture, screenRectangle); tempTexture = Content.Load<Texture2D>("ball"); ball = new Ball(tempTexture, screenRectangle); brickImage = Content.Load<Texture2D>("brick"); StartGame(); }