Пример #1
0
        //Nit: Can you make the ball fall a little farther before resetting the ball, something doesn't feel right when it falls
        //Nit: Can you make the ball go in whatever the player last moved
        //Note Form1 has a soundplayer, you can access it with Form1.SoundPlayer
        private void gameTimer_Tick(object sender, EventArgs e)
        {
            //   angleLable.Text = angleposition.ToString();
            // Move the paddle
            if (leftArrowDown && paddle.x > 0)
            {
                paddle.Move("left");
            }
            if (rightArrowDown && paddle.x < (this.Width - paddle.width))
            {
                paddle.Move("right");
            }

            if (start)
            {
                foreach (Ball b in ballList)
                {
                    anglechange();

                    // Move ball
                    b.Move();


                    // Check for collision with top and side walls
                    b.WallCollision(this);

                    // Check for ball hitting bottom of screen and if there is only one ball
                    if (b.BottomCollision(this, paddle) && ballList.Count == 1)
                    {
                        // decrease player 1 lives
                        player1Lives--;

                        // move the ball and paddle back
                        start = false;

                        // reset ball angle
                        angleposition = 3;
                        anglechange();

                        // reset paddle x and y
                        //paddle.x = paddleX;
                        //paddle.y = paddleY;

                        ballList[0].x       = ((paddle.x - ballSize) + (paddle.width / 2));
                        ballList[0].y       = paddle.y - 40;
                        ballList[0].Yangle *= -1;

                        // reset x and y speeds
                        ballList[0].xSpeed = 6;
                        ballList[0].ySpeed = 6;

                        if (player1Lives < 1)
                        {
                            start = false;
                            if (player2Lives < 0)
                            {
                                gameTimer.Enabled = false;
                                scores();
                            }
                            gameTimer.Enabled = false;
                            scores();
                        }
                    }
                    else if (b.BottomCollision(this, paddle))
                    {
                        //Remove ball that hit bottom from list
                        ballList.Remove(b);
                        break;
                    }

                    // Check for collision of ball with paddle, (incl. paddle movement)
                    b.PaddleCollision(paddle, leftArrowDown, rightArrowDown);

                    if (currentlevel.Count == 0)
                    {
                        gameTimer.Enabled = false;
                        OnEnd();
                    }
                }
                // remove any balls that need to be removed
                foreach (Ball b in removeBalls)
                {
                    ballList.Remove(b);
                }
            }

            //Testing Purposes: Score Tracker...1st Tracker...Adding the number to scores
            foreach (Block b in currentlevel)
            {
                if (ballList[0].ScoreTracker(b))
                {
                    score++;
                }
            }

            foreach (Ball ball in ballList)
            {
                for (int i = 0; i < currentlevel.Count(); i++)
                {
                    Block     b  = currentlevel[i];
                    Rectangle ls = leftside[i];
                    Rectangle rs = rightside[i];

                    Rectangle curball = new Rectangle(Convert.ToInt32(ball.x), Convert.ToInt32(ball.y), ball.size, ball.size);
                    if (curball.IntersectsWith(upLeft[i]) || curball.IntersectsWith(upRight[i]) || curball.IntersectsWith(bottomLeft[i]) || curball.IntersectsWith(bottomRight[i]))
                    {
                        ball.Yangle *= -1;
                    }
                    if (ball.BlockCollision(b))
                    {
                        if (ball.side_collision(ls) || ball.side_collision(rs))
                        {
                            ball.xSpeed *= -1;
                        }
                        leftside.RemoveAt(i);
                        rightside.RemoveAt(i);
                    }
                }
            }

            // Check if ball has collided with any currentlevel
            foreach (Ball ba in ballList)
            {
                ba.Move();
                foreach (Block b in currentlevel)
                {
                    if (ba.BlockCollision(b))
                    {
                        b.hp--;
                        if (b.hp < 1)
                        {
                            currentlevel.Remove(b);
                        }

                        score += 100;

                        //Powerup Chance
                        Random randPower = new Random();
                        randomPowerupChance = randPower.Next(1, 21);

                        if (randomPowerupChance == 1)
                        {
                            Powerups p = new Powerups(b.x, b.y, 5, "3");
                            powerup.Add(p);
                            activated = false;
                        }
                        if (randomPowerupChance == 2)
                        {
                            Powerups p = new Powerups(b.x, b.y, 5, "L");
                            powerup.Add(p);
                            activated = false;
                        }
                        if (randomPowerupChance == 3)
                        {
                            Powerups p = new Powerups(b.x, b.y, 5, "l");
                            powerup.Add(p);
                            activated = false;
                        }
                        if (randomPowerupChance == 4)
                        {
                            Powerups p = new Powerups(b.x, b.y, 5, "BS");
                            powerup.Add(p);
                            activated = false;
                        }
                        if (randomPowerupChance == 5)
                        {
                            Powerups p = new Powerups(b.x, b.y, 5, "bs");
                            powerup.Add(p);
                            activated = false;
                        }

                        if (currentlevel.Count < 1)
                        {
                            if (currentlevelnum == levelList.Count())
                            {
                                OnEnd();
                            }
                        }
                        break;
                    }
                }

                if (!start)
                {
                    //center the ball over the paddle
                    ballList[0].x = paddle.x + (paddle.width / 2) - (ballList[0].size / 2);
                    ballList[0].y = paddle.y - 21;

                    //clear the powerups from the screen
                    powerup.Clear();

                    p1 = new Point(Convert.ToInt16(ballList[0].x + (ballList[0].size / 2)), Convert.ToInt16(ballList[0].y));

                    switch (angleposition)
                    {
                    // right
                    case 1:
                        p2 = new Point(Convert.ToInt16(ballList[0].x) + 89, Convert.ToInt16(ballList[0].y) - 44);
                        break;

                    case 2:
                        p2 = new Point(Convert.ToInt16(ballList[0].x) + 70, Convert.ToInt16(ballList[0].y) - 70);
                        break;

                    case 3:
                        p2 = new Point(Convert.ToInt16(ballList[0].x) + 44, Convert.ToInt16(ballList[0].y) - 89);
                        break;

                    case 4:
                        p2 = new Point(Convert.ToInt16(ballList[0].x) - 44, Convert.ToInt16(ballList[0].y) - 89);
                        break;

                    case 5:
                        p2 = new Point(Convert.ToInt16(ballList[0].x) - 70, Convert.ToInt16(ballList[0].y) - 70);
                        break;

                    case 6:
                        p2 = new Point(Convert.ToInt16(ballList[0].x) - 89, Convert.ToInt16(ballList[0].y) - 44);
                        break;

                    // left
                    default:
                        break;
                    }
                }
                else
                {
                    if (currentlevel.Count == 0)
                    {
                        if (currentlevelnum == 9)
                        {
                            //OnEnd();
                            //testing
                            WinScreen ws   = new WinScreen();
                            Form      form = this.FindForm();

                            form.Controls.Add(ws);
                            form.Controls.Remove(this);

                            ws.Location = new Point((form.Width - ws.Width) / 2, (form.Height - ws.Height) / 2);
                        }
                        else
                        {
                            currentlevelnum++;
                            player1Lives++;
                            levelLoad();
                            start         = false;
                            ballList[0].x = paddle.x + (paddle.width / 2) - (ballList[0].size / 2);
                            ballList[0].y = paddle.y - 21;
                        }
                    }
                }

                //Move powerups down
                foreach (Powerups p in powerup)
                {
                    p.powerupMove();
                }

                //Check for collision of powerups
                foreach (Powerups p in powerup)
                {
                    if (p.PowerupCollision() == true && activated == false)
                    {
                        if (p.type == "3")
                        {
                            Random randGen = new Random();
                            int    x, y;

                            x = randGen.Next(1, 301);
                            y = randGen.Next(1, 301);

                            //activate powerup
                            Ball b2 = new Ball(x, y, xSpeed, ySpeed, ballSize, 1, -1);
                            ballList.Add(b2);

                            Ball b3 = new Ball(y, x, xSpeed, ySpeed, ballSize, 1, -1);
                            ballList.Add(b3);

                            break;
                        }
                        else if (p.type == "L")
                        {
                            paddle.width += 25;
                        }
                        else if (p.type == "l")
                        {
                            paddle.width -= 25;
                        }
                        else if (p.type == "BS")
                        {
                            ySpeed -= 2;

                            foreach (Ball b in ballList)
                            {
                                b.ySpeed = ySpeed;
                            }
                        }
                        else if (p.type == "bs")
                        {
                            ySpeed += 2;

                            foreach (Ball b in ballList)
                            {
                                b.ySpeed = ySpeed;
                            }
                        }

                        activated = true;
                        powerup.Remove(p);
                        break;
                    }
                }
                //redraw the screen
                Refresh();
            }
        }
Пример #2
0
        public void IanMethod()
        {
            string type = "";
            Color  c    = Color.White;

            int ballX    = this.Width / 2;
            int ballY    = this.Height - paddle.height - 80;
            int ballSize = 20;

            Random powerGen = new Random();

            foreach (Block b in blocks)
            {
                if (ball.BlockCollision(b))
                {
                    int powerupChance = powerGen.Next(1, 4);
                    int powerupType   = powerGen.Next(1, 6);
                    if (b.hp == 1)
                    {
                        if (powerupChance == 1)
                        {
                            if (powerupType == 1) // add 300 points
                            {
                                type = "points";

                                c = Color.Blue;
                            }
                            else if (powerupType == 2) // add another ball
                            {
                                type = "ball";
                                c    = Color.Yellow;
                            }
                            else if (powerupType == 3) // add extra life
                            {
                                type = "life";
                                c    = Color.Red;
                            }
                            else if (powerupType == 4) // bigger paddle
                            {
                                type = "big";
                                c    = Color.Green;
                            }
                            else if (powerupType == 5) // makes paddle faster
                            {
                                type = "fast";
                                c    = Color.Pink;
                            }
                            SolidBrush powerBrush = new SolidBrush(c);
                            Powerups   newPowerup = new Powerups(b.x + b.width / 2 - 5, b.y + b.height / 2 - 5, 10, type, powerBrush);
                            Powerup.Add(newPowerup);
                        }
                    }
                }
            }
            foreach (Powerups p in Powerup)
            {
                p.Move(3);
            }
            foreach (Powerups d in Powerup)
            {
                Rectangle powerRec  = new Rectangle(d.x, d.y, d.size, d.size);
                Rectangle paddleRec = new Rectangle(paddle.x, paddle.y, paddle.width, paddle.height);

                if (powerRec.IntersectsWith(paddleRec))
                {
                    if (d.type == "points")
                    {
                        score += 300;
                    }
                    else if (d.type == "ball")
                    {
                        Rectangle ballRec = new Rectangle(ball.x, ball.y, ball.size, ball.size);
                        if (powerupBall.Count == 0)
                        {
                            Ball powerball = new Ball(this.Width / 2 - 10, this.Height / 2 + 80, -6, -6, ballSize);
                            powerupBall.Add(powerball);
                        }
                    }
                    else if (d.type == "life")
                    {
                        lives++;
                    }
                    else if (d.type == "big")
                    {
                        paddle.width += 100;
                    }
                    else if (d.type == "fast")
                    {
                        paddle.speed += 1;
                    }
                    d.y = this.Height + 1;
                }
            }
            int index = Powerup.FindIndex(b => b.y > this.Height);

            if (index >= 0 && Powerup.Count() > 0)
            {
                Powerup.RemoveAt(index);
            }
        }