コード例 #1
0
        public void hitBrickCheck()
        {
            bool        hit  = false;
            List <bool> hits = new List <bool>();

            foreach (var ball in balls)
            {
                hits.Add(false);
            }
            List <Rectangle> delete = new List <Rectangle>();

            foreach (var brick in bricks)
            {
                if (Collide.hit(Ball, acceleration, brick) && !hit)
                {
                    if ((string)brick.Tag == "special")
                    {
                        addUp(brick.Margin.Bottom, brick.Margin.Left, getRandomColor());
                        poweracc.Add(new List <int>()
                        {
                            8, 270
                        });
                    }
                    delete.Add(brick);
                    hit = true;
                }
                for (int index = 0; index < balls.Count; index++)
                {
                    if (Collide.hit(balls[index], acc[index], brick) && !hits[index])
                    {
                        delete.Add(brick);
                        hits[index] = true;
                    }
                }
            }
            foreach (var brick in delete)
            {
                Grid.Children.Remove(brick);
                bricks.Remove(brick);
                count++;
                for (int index = 0; index < powerCount.Count; index++)
                {
                    powerCount[index]--;
                }
            }
        }
コード例 #2
0
        private void dispatcherTimer_Tick(object sender, object e)
        {
            MovePaddles();
            acceleration[0] = acceleration[0] < 0 ? 0 : acceleration[0];
            acceleration[0] = acceleration[0] > 10 ? 10 : acceleration[0];
            if (paused)
            {
                acceleration[0] = 0;
            }
            EnsurePaddleIsInBounds(BottomPaddle);
            hitBrickCheck();

            if (power != null)
            {
                int rem = -1;
                for (int index = 0; index < power.Count; index++)
                {
                    if (Collide.hit(power[index], poweracc[index], BottomPaddle))
                    {
                        Grid.Children.Remove(power[index]);
                        poweracc.RemoveAt(index);
                        power.RemoveAt(index);
                        powerCount.Add(10);
                        BottomPaddle.Width = BottomPaddle.Width * 1.5;
                    }
                    else if (power[index].Margin.Bottom < paddleTop - 5)
                    {
                        Grid.Children.Remove(power[index]);
                        poweracc.RemoveAt(index);
                        power.RemoveAt(index);
                    }
                    else
                    {
                        double mov = poweracc[index][0] * Math.Sin((poweracc[index][1] * Math.PI) / 180);
                        power[index].Margin = new Thickness(power[index].Margin.Left, 0, 0, power[index].Margin.Bottom + mov);
                    }
                }
                for (int index = 0; index < powerCount.Count; index++)
                {
                    if (powerCount[index] <= 0)
                    {
                        rem = index;
                    }
                }
                if (rem != -1)
                {
                    powerCount.RemoveAt(rem);
                    BottomPaddle.Width = (BottomPaddle.Width * 2) / 3;
                }
            }

            if (count >= options_ballCount && add)
            {
                count = 0;
                addBall(paddleTop + 20, leftBound + 20, getRandomColor());
                acc.Add(new List <int>()
                {
                    5, 40 + random.Next(-10, 10)
                });
            }

            BallFall();
            double bottom = Ball.Margin.Bottom;

            if (bottom < paddleTop - 5)
            {
                timer.Stop();
                ElementSoundPlayer.Play(ElementSoundKind.GoBack);
                Frame.Navigate(typeof(Start), new Passing
                {
                    color = getRandomColor(),
                    text  = "Game Over",
                    size  = 48
                });

                g_ply.Stop(); //end music
            }

            if (bricks.Count == 0)
            {
                timer.Stop();
                ElementSoundPlayer.Play(ElementSoundKind.GoBack);
                Frame.Navigate(typeof(Start), new Passing {
                    color = getRandomColor(),
                    text  = "Game Won",
                    size  = 48
                });
            }

            moveBalls();
            for (int index = 0; index < balls.Count; index++)
            {
                Collide.hit(balls[index], acc[index], BottomPaddle);
            }

            Collide.wall(Ball, acceleration, leftBound, 'l');
            Collide.wall(Ball, acceleration, rightBound, 'r');
            Collide.wall(Ball, acceleration, topBound, 't');

            Collide.hit(Ball, acceleration, BottomPaddle);
            // x left-right: cos y up-down: sin
            // !! remeber, 0 deg is --> NOT NORTH !!
            double xmov = acceleration[0] * Math.Cos((acceleration[1] * Math.PI) / 180);
            double ymov = acceleration[0] * Math.Sin((acceleration[1] * Math.PI) / 180);

            Ball.Margin = new Thickness(Ball.Margin.Left + xmov, 0, 0, Ball.Margin.Bottom + ymov);
        }