I am following this toturial.
Click Here
In the try other feature section:
*Set it up so that the game only plays a sound when the mouse pointer hits a wall, but doesn't play a sound when the program starts. *

I figured moving hitWallSoundPlayer.Play();
from private void MoveToStart() to private void wall_MouseEnter(object sender, EventArgs e)
would be the solution, however, the sound is still played as soon as the program starts.

Is it because the pointer is is not at the correct postion when the program starts?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Maze
{
    public partial class Form1 : Form
    {
        // This SoundPlayer plays a sound whenever the player hits a wall.
        System.Media.SoundPlayer hitWallSoundPlayer = new System.Media.SoundPlayer(@"C:\Windows\Media\ir_begin.wav");
        // This SoundPlayer plays a sound when the player finishes the game.
        System.Media.SoundPlayer finishSoundPlayer = new System.Media.SoundPlayer(@"C:\Windows\Media\tada.wav");


        public Form1()
        {

            InitializeComponent();
            MoveToStart();           
        }

        /// <summary> 
        /// Move the pointer to a point 10 pixels down and to the right 
        /// of the starting point in the upper-left corner of the maze. 
        /// </summary>
        private void MoveToStart()
        {
            //hitWallSoundPlayer.Play();           
            Point startingPoint = panel1.Location;
            startingPoint.Offset(10, 10);
            Cursor.Position = PointToScreen(startingPoint);
        }

        private void finishLabel_MouseEnter(object sender, EventArgs e)
        {
            finishSoundPlayer.Play();
            // Show a congratulatory MessageBox, then close the form.
            MessageBox.Show("Congratulations!!!");
            Close();      
            //MoveToStart();
        }

        private void wall_MouseEnter(object sender, EventArgs e)
        {
            hitWallSoundPlayer.Play();
            MoveToStart();

        }
    }
}

Recommended Answers

All 5 Replies

It should work the way it is. Unless your panel is only the size of the maze and the cursor is starting on a wall. Try sizing the panel to be a lot bigger than you need and moving the maze farther away from the top-left corner.

Thanks for your response!
I tried as you suggested, results were the same.

Unrelated to the OP, I tried to add a counter for how many times the walls were hit to display at the finishLabel...method if the maze is comnplete, but couldn't quite puzzle it out.

Where should such a counter reside?
SHould it be a method?

You could put it in the MouseEnter handler. Just have a label underneath the Finish label and start it at 0. Then everytime the mouse hits a wall increment the count in the label.

Thanks!
That was one approach I tried to use. The problem I run into is I do not know how to declare a variable that can beed seen inside the MoueEnter handler.

Never mind.
I got it, I needed to declare it before public Form1()
Thanks again for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.