user1910371
user1910371

Reputation: 21

Maze Game few small problems

Code updated now, only two problems left until I'm happy to feel I've completed it.

When the player hits a wall the counter goes down at 2 lifes per hit.

When the game starts the sound I have(beep.wav) which should only sound when the player hits a wall, is sounding each time I begin the game and the sound which should play throughout the game(onestop.wav) isn't playing at all.

public partial class Form1 : Form       
{
            // This SoundPlayer plays a song when the game begins.
    System.Media.SoundPlayer onestop = new System.Media.SoundPlayer(@"C:\Users\kC\Favorites\Desktop\Kev stuff\Projects\MazeGame\onestop.wav");

    // This SoundPlayer plays a sound whenever the player hits a wall.
    System.Media.SoundPlayer beepSound = new System.Media.SoundPlayer(@"C:\Users\kC\Favorites\Desktop\Kev stuff\Projects\MazeGame\beep.wav");

    // This SoundPlayer plays a sound when the player finishes the game.
    System.Media.SoundPlayer clapSound = new System.Media.SoundPlayer(@"C:\Users\kC\Favorites\Desktop\Kev stuff\Projects\MazeGame\winningApplause.wav");


    public Form1()
    {
        InitializeComponent();
        timer1.Interval = (1000) * (1);
        timer1.Enabled = true;
        timer1.Start();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

        begin();

    }

    private void begin()
    {
        onestop.Play();
        livesTextBox.Text = lives.ToString();
        Point startingPoint = panel1.Location;
        startingPoint.Offset(10, 10);
        Cursor.Position = PointToScreen(startingPoint);
    }

    int lives = 5;

    private void MoveToStart()
    {

        if ( lives > 0)
        {
            lives--;
        }
        if (lives == 0)
        {
            MessageBox.Show("You Lose!!");
            Close();
        }

        else if (lives < 0)
        {
            MessageBox.Show("You Lose!!");
            Close();
        }
    }

    private void wall_MouseEnter(object sender, EventArgs e)
    {
        // When the mouse pointer hits a wall or enters the panel,
        // call the MoveToStart() method.
        beepSound.Play();
        MoveToStart();

        lives--;
        livesTextBox.Text = lives.ToString();
    }

    private void finishLabel_MouseEnter(object sender, EventArgs e)
    {
        // Play a sound, show a congratulatory MessageBox, then close the form.
        clapSound.Play();
        MessageBox.Show("Congratulations! You've beaten the maze!");
        Close();
    }

    int gameElapsed = 0;

    private void timer1_Tick(object sender, EventArgs e)
    {
        gameElapsed++;
        textBox1.Text = "" + gameElapsed.ToString();
    }


}

Upvotes: 1

Views: 527

Answers (3)

MrFox
MrFox

Reputation: 5106

The lives can be a property of the player class.

public class Player
{
    int lives = 5;

    public bool Kill()
    {
        this.lives--;
        return this.lives <= 0;
    }

    public void run()
    {
        Player player = new Player();

        // do stuff

        // Check whether the player needs to die
        if ("player fails".Contains("fail"))
        {
            if (player.Kill())
            {
                // restart level.
            }
            else
            {
                // Game over.
            }
        }
    }
}

Upvotes: 0

S.A.Parkhid
S.A.Parkhid

Reputation: 2868

Just Create a Timer on your form with interval 1000 and make it enable and then define game_elpased as integer as private field in the class and in timer_tick write :

void Timer1_Tick(object Sender,EventArgs e)
{
  game_elapsed++;
  textBox1.Text = "Elapsed Seconds : " + game_elapsed.ToString();
}

For the live times , You need to control the public variable like lives, when the player fails :

if(fails && lives>0){
  lives--;
}
else if (lives<0)
{
  MessageBox.Show("You are a looser ...");
}

Upvotes: 1

Sanjay Manohar
Sanjay Manohar

Reputation: 7026

You haven't posted the code very clearly, but I'll try and help.

Not sure about your sound issues. But: to 'call in your timer', I presume you want to put a number in the text box? You are half-way there. The Timer object simply calls the tick event every second, so you need to do the displaying part.

place the text box on your form, assume it is called textBox1.

put before the begin() method:

Stopwatch stopWatch = new Stopwatch();

Put inside the begin() method

stopWatch.Start();

inside the timer1_Tick method:

TimeSpan ts = stopWatch.Elapsed;
textBox1.Text = String.Format("{0:00}:{1:00}", ts.Minutes, ts.Seconds);

for your 'lives' bit, you'll need to put, before begin() method,

int lives = 0;

inside the begin(), put

int lives = 5;
livesTextBox.Text = lives.toString();

and inside wall_MouseEnter(), put

lives--;
livesTextBox.Text = lives.toString();

(assuming you have drawn a livesTextBox on your form)

Upvotes: 1

Related Questions