2012-09-19 85 views
3

我必須爲學校的一個小項目做一個蛇遊戲。到目前爲止,我已經得到了這條蛇無止境地向一個方向移動。然而按另一個方向只會增加蛇的運動方向。所以當你先按下然後向左走向東北。我很困擾如何解決這個問題。我寫了一個stop()方法,但我不知道如何實現它。如果你們可以給我一些提示,那將是非常感謝。c#蛇遊戲如何阻止蛇

  using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Drawing; 

namespace Snake 
{ 
    class Snake 
    { 
     //Attributes go here.... 
     private Form frmSnake; 
     private static Button btnSnake; 
     private static Label lblStatus; 
     private static Timer timerUP; 
     private static Timer timerLEFT; 
     private static Timer timerDOWN; 
     private static Timer timerRIGHT; 

     //Initialize a Snake Object... 
     public Snake() 
     {    
      initComponents(); 
      initTimers(); 
      startAllTimers(); 
      initEventHandler(this); 
      frmSnake.ShowDialog(); 
     } 

     //give the Components of snake object values   
     private void initComponents() 
     { 
      frmSnake = new Form(); 
      lblStatus = new Label(); 
      btnSnake = new Button();    

      frmSnake.Controls.Add(btnSnake); 
      frmSnake.Controls.Add(lblStatus); 
      frmSnake.SetBounds(400, 400, 700, 550); 
      frmSnake.Text = "Snake"; 

      lblStatus.SetBounds(0, 495, 700, 20); 
      lblStatus.BackColor = System.Drawing.Color.Red; 

      btnSnake.SetBounds((frmSnake.Width/2) - (btnSnake.Width/2), ((frmSnake.Height - lblStatus.Height)/2) - (btnSnake.Height/2), 10, 10); 
     } 

     private void initTimers() 
     {    
      timerUP = new Timer(); 
      timerLEFT = new Timer(); 
      timerDOWN = new Timer(); 
      timerRIGHT = new Timer(); 
      timerUP.Enabled = true; 
      timerUP.Interval = 100; 
      timerLEFT.Enabled = true; 
      timerLEFT.Interval = 100; 
      timerDOWN.Enabled = true; 
      timerDOWN.Interval = 100; 
      timerRIGHT.Enabled = true; 
      timerRIGHT.Interval = 100; 
     } 

     private void disposeTimers() 
     { 
      timerUP.Dispose(); 
      timerLEFT.Dispose(); 
      timerDOWN.Dispose(); 
      timerRIGHT.Dispose(); 
     } 

     private void stopTimer (Timer timer) 
     { 
      timer.Stop(); 
     } 

     private void stopAllTimers() 
     { 
      timerUP.Stop(); 
      timerDOWN.Stop(); 
      timerLEFT.Stop(); 
      timerRIGHT.Stop(); 
     } 
     private void startAllTimers() 
     { 
      timerUP.Start(); 
      timerDOWN.Start(); 
      timerRIGHT.Start(); 
      timerLEFT.Start(); 
     } 

     private void startTimer (Timer timer) 
     { 
      timer.Start(); 
     } 

     //initializes the event handler which handles a key press W,A,S,D from user 
     //W,A,S,D correspondents UP,LEFT,DOWN,RIGHT 
     private void initEventHandler(Snake snake) 
     { 
      snake.frmSnake.KeyPreview = true; 
      snake.frmSnake.KeyDown += new KeyEventHandler(snake.frm_KeyDown); 
     } 

     //handle the key press event sent from previous method 
     private void frm_KeyDown(object sender, KeyEventArgs e) 
     { 
      disposeTimers(); 
      initTimers(); 
      switch (e.KeyData) 
      { 

       //if up is pressed 
       case Keys.W: 
        {       
         startTimer (timerUP); 
         timerUP.Tick += new EventHandler (timerUP_Tick); 
        } 
        break; 
       //if left is pressed 
       case Keys.A: 
        {       
         startTimer (timerLEFT); 
         timerLEFT.Tick += new EventHandler (timerLEFT_Tick); 
         //move snake 5 spaces 
        } 
        break; 
       //if down is pressed 
       case Keys.S: 
        {       
         startTimer (timerDOWN); 
         timerDOWN.Tick += new EventHandler (timerDOWN_Tick); 
         //move snake 5 spaces 
        } 
        break; 
       //if right is pressed 
       case Keys.D: 
        {       
         startTimer (timerRIGHT); 
         timerRIGHT.Tick += new EventHandler (timerRIGHT_Tick); 
         //move snake 5 spaces 
        } 
        break; 
      }    
     } 

     void timerRIGHT_Tick (object sender, EventArgs e) 
     { 
      btnSnake.Location = new Point (btnSnake.Location.X + 5, btnSnake.Location.Y); 
     } 

     void timerDOWN_Tick (object sender, EventArgs e) 
     { 
      btnSnake.Location = new Point (btnSnake.Location.X, btnSnake.Location.Y + 5); 
     } 

     void timerLEFT_Tick (object sender, EventArgs e) 
     { 
      btnSnake.Location = new Point (btnSnake.Location.X - 5, btnSnake.Location.Y); 
     } 

     void timerUP_Tick (object sender, EventArgs e) 
     { 
      //move snake 5 spaces 
      btnSnake.Location = new Point (btnSnake.Location.X, btnSnake.Location.Y - 5); 
     }   



     private void setLabelStatus(string text, Color color) 
     { 
      lblStatus.Text = ""; 
      lblStatus.BackColor = color; 
      lblStatus.Text = text; 
     } 

     //main method initializes a snake game 
     static void Main(string[] args) 
     { 
      Snake snake = new Snake(); 
     } 
    } 
} 
+0

您是否試過在按下某個鍵時停止其他計時器?看起來你永遠不會停止計時器,因爲它是雙向運​​行的原因。 –

+0

這基本上是我的問題,我試圖從代碼中的各個位置調用stop()方法,但無濟於事。 – LeonidasFett

+0

你是否嘗試過爲每個定時器聲明私有全局變量?即'Public Timer UpTimer = new Timer();'然後在DOWN鍵上,爲所有與按鍵和DownTimer.Start()不相關的定時器執行'UpTimer.Stop()'等。 –

回答

4

您需要跟蹤它的時間你已經創建,所以你可以稍後刪除它們(大概是內部有一個「一站式」的方法)。或者你可以有一個定時器用於總是觸發的移動(在遊戲啓動時啓動它),然後存儲'當前方向'並將其用於定時器內部。

第二種方法可能會更好,因爲您可以稍後在其中滾動某些「每幀」遊戲邏輯(檢查蛇是否已自己吃掉等),而不是在所有各種計時器回調中重複它。

1

將每個密鑰的計時器聲明爲私有全局變量,然後在您希望更改路線時調用Start()Stop()方法。

// Declare 
Timer UpTimer = new Timer(); 
Timer DownTimer = new Timer(); 
Timer LeftTimer = new Timer(); 
Timer RightTimer = new Timer(); 


// If Left Key pressed 
UpTimer.Stop(); 
DownTimer.Stop(); 
LeftTimer.Start(); 
RightTimer.Stop();