Beispiel #1
0
 /// <summary>
 /// Play the game-over sound effect
 /// </summary>
 public void PlayGameOverSound()
 {
     try
     {
         // A thread is needed to get the sound to play
         new Thread(() =>
         {
             this.PlaySound(50, 500);
         }).Start();
     }
     catch (Exception)
     {
         // Sometimes thread creation fails. Make sure it doesn't crash the game.
         MeadowApp.DebugWriteLine($"Exception in PlayGameOverSound");
     }
 }
Beispiel #2
0
        /// <summary>
        /// Check for collisions between the ball and the sides, top and bottom of the display,
        /// also checking for collisions with the paddle.
        /// </summary>
        /// <returns>Value indicating that the paddle was missed, signaling game-over</returns>
        private bool CheckForCollision()
        {
            bool isPaddleMissed = false;
            bool isBorderHit    = false;

            int ballCenterX = this.xPosition + (this.width / 2);

            MeadowApp.DebugWriteLine($"checkForCollision: {ballCenterX},{this.yPosition}");

            if (this.yPosition >= this.maxY)
            {
                if (ballCenterX >= this.paddle.Left && ballCenterX < this.paddle.Right)
                {
                    // Paddle hit!
                    this.soundGenerator.PlayPaddleHitSound();
                    this.scoreKeeper.Increment();

                    this.GetVelocityChangeAdjustments(ballCenterX, out int extraX, out int extraY);

                    this.yIncrement = -this.yIncrement;

                    // Apply the x and y increments
                    this.ChangeXIncrement(extraX);
                    this.ChangeYIncrement(extraY);

                    // Make the paddle smaller
                    this.paddle.Shrink();
                }
                else
                {
                    // Missed the paddle.  Time to explode...
                    this.asyncGraphics.Stop();
                    this.StopMoving();
                    this.soundGenerator.PlayGameOverSound();
                    this.Explode();

                    isPaddleMissed = true;
                }
            }
            else
            {
                if (this.xPosition >= this.maxX || this.xPosition <= 0)
                {
                    MeadowApp.DebugWriteLine("x border hit");
                    isBorderHit     = true;
                    this.xIncrement = -this.xIncrement;
                    this.ChangeYIncrement(0);
                }

                if (this.yPosition <= this.minY)
                {
                    MeadowApp.DebugWriteLine("y border hit");
                    isBorderHit     = true;
                    this.yIncrement = -this.yIncrement;
                    this.ChangeXIncrement(0);
                }

                if (isBorderHit)
                {
                    this.soundGenerator.PlayBorderHitSound();
                }
            }

            MeadowApp.DebugWriteLine($"leaving checkForCollision: {isPaddleMissed}");
            return(isPaddleMissed);
        }
Beispiel #3
0
        /// <summary>
        /// Event handler called when the movement timer elapses
        /// </summary>
        /// <param name="sender">Event sender</param>
        /// <param name="e">Event arguments</param>
        private void MoveTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            try
            {
                MeadowApp.DebugWriteLine("Move timer elapsed");

                if (this.isMoveComplete)
                {
                    this.isMoveComplete = false;

                    if (!this.CheckForCollision())
                    {
                        int oldX = this.xPosition;
                        int oldY = this.yPosition;

                        MeadowApp.DebugWriteLine($"Move timer elapsed: {oldX},{oldY} {xIncrement},{yIncrement}");

                        this.xPosition += this.xIncrement;

                        if (this.xPosition > this.maxX)
                        {
                            this.xPosition = this.maxX;
                        }

                        MeadowApp.DebugWriteLine($"new x = {this.xPosition}");

                        if (this.xPosition < 0)
                        {
                            this.xPosition = 0;
                        }

                        this.yPosition += this.yIncrement;

                        MeadowApp.DebugWriteLine($"new y = {this.yPosition}");

                        if (this.yPosition > this.maxY)
                        {
                            this.yPosition = this.maxY;
                        }

                        if (this.yPosition < this.minY)
                        {
                            this.yPosition = this.minY;
                        }

                        MeadowApp.DebugWriteLine($"new x,y = {this.xPosition},{this.yPosition}");

                        if (!(this.xPosition == oldX && this.yPosition == oldY))
                        {
                            lock (this.asyncGraphics.LockObject)
                            {
                                if (this.moveTimer.Enabled)
                                {
                                    this.Draw(oldX, oldY, this.backgroundColor);
                                    this.Draw(this.xPosition, this.yPosition, this.color);
                                }
                            }
                        }
                    }

                    this.isMoveComplete = true;
                }

                MeadowApp.DebugWriteLine("leaving move timer elapsed");
            }
            catch (Exception ex)
            {
                MeadowApp.DebugWriteLine($"Exception in MoveTimer_Elapsed: {ex}");
                this.isMoveComplete = true;
            }
        }