1

代码现在更新,只剩下两个问题,直到我很高兴我已经完成了它。

当玩家撞到墙时,计数器会以每次击中 2 条生命值下降。

当游戏开始时我的声音(beep.wav)应该只在玩家撞墙时发出,每次我开始游戏时都会发出声音并且应该在整个游戏中播放的声音(onestop.wav)没有播放一点也不。

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();
    }


}
4

3 回答 3

1

您没有很清楚地发布代码,但我会尽力提供帮助。

不确定您的声音问题。但是:要“调用你的计时器”,我想你想在文本框中输入一个数字?你已经成功了一半。该Timer对象只是每秒调用一次滴答事件,因此您需要执行显示部分。

将文本框放在表单上,​​假设它被称为textBox1.

放在begin()方法之前:

Stopwatch stopWatch = new Stopwatch();

放在begin()方法里面

stopWatch.Start();

方法内部timer1_Tick

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

对于您的“生活”位,您需要在begin()方法之前放置,

int lives = 0;

里面begin(),放

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

在里面wall_MouseEnter(),放

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

(假设你livesTextBox在表格上画了一个)

于 2012-12-17T17:11:15.140 回答
1

只需在您的表单上创建一个间隔为 1000 的计时器并使其启用,然后将 game_elpased 定义为整数作为类中的私有字段,并在 timer_tick 中写入:

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

对于 live times ,当播放器失败时,您需要像 live 一样控制公共变量:

if(fails && lives>0){
  lives--;
}
else if (lives<0)
{
  MessageBox.Show("You are a looser ...");
}
于 2012-12-17T17:17:42.387 回答
0

生命可以是玩家类的属性。

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.
            }
        }
    }
}
于 2012-12-17T17:19:11.453 回答