2012-12-17 81 views
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(); 
    } 


} 

回答

1

您還沒有發佈代碼非常清楚,但我會盡力幫助。

不確定你的聲音問題。但是:爲了「打電話給你的計時器」,我想你想在文本框中輸入一個數字?你在這裏一半。 Timer對象每秒都會調用tick事件,因此您需要執行顯示部分。

將文本框放置在窗體上,假定它被稱爲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

+0

感謝大家的幫助,我現在已經進步了在迷宮一點,我已經更新了代碼,還是有一些小問題;/ – user1910371

1

只是創建一個定時器在間隔1000的窗體上啓用然後定義game_elpased爲整數,如類,並在timer_tick寫私人領域:

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

對於直播時間,你需要控制像生活中的公共變量,當玩家失敗:

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

感謝您的幫助,得到了定時器和生活工作,由於某種原因,現在每個人的生命似乎都下降了兩倍;現在我用更新後的代碼編輯了我的帖子。 – user1910371

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. 
      } 
     } 
    } 
}