2013-01-05 43 views
0

我正在創建一個文字遊戲(行話)。它是如何工作的:從數組中選擇一個隨機單詞,用戶必須猜測正確的單詞。然後這個詞被打印出來。 :錯信 :正確的字母,但錯了位置 綠色:位置和信正確C#幫助繪圖字(文字遊戲)

眼下的問題是,它不僅吸引了猜字的幾個字母。我還需要更改y值,以便所有內容不在同一行上。

這就是我到目前爲止。

public string CorrectPlace; // Correct letter and postion 
    public string WrongPlace; // Correct letter but incorrect postion 
    public string NoneExist; // Wrong 
    public string inputwordstring; // user input 
    public string CorrectWord; // Correct word 
    public char[] CorrectWordchar; 
    public char[] InputWord; 
    int x;// position 
    int y; //position 
    public int points = 0; 
    char replacemt = '#'; 
    public string[] Wordarray = null; // Declare string array 
    public string getRandomWord() // generate random word 
    { 
     Random ran = new Random(); 
     return Wordarray[(ran.Next(0, Wordarray.Length - 1))]; 

    } 

    public void Gamers() // 
    { 
     Wordarray = new string[] { "radar", "andar", "axars", "rapar", "raser", "matar", "rikas", "ratas", "bakar", "bruka" }; // Dictionary 
     CorrectWord = getRandomWord(); // store the random word in a string 
    } 
    public void CheckWords(string name, Graphics g) 
    { 

     if (CorrectWord == inputwordstring) 
     { 

      points += 100; 
      MessageBox.Show("AMAZING"); 




     } 
     for (int i = 0; i < CorrectWord.Length; i++) 
     { 

      for (int b = 0; b < CorrectWord.Length; b++) 
      { 

       CorrectWordchar = CorrectWord.ToCharArray(); 
       InputWord = inputwordstring.ToCharArray(); 

       if (InputWord[i] == CorrectWordchar[b]) // check if the have the same index 
       { 
        if (i == b) 
        { 
         CorrectPlace = CorrectWord; 
         SolidBrush s = new SolidBrush(Color.Green); 
         FontFamily ff = new FontFamily("Arial"); 
         System.Drawing.Font font = new System.Drawing.Font(ff, 20); 
         g.DrawString(InputWord[i].ToString(), font, s, new PointF(x, y)); 
         x += 20; 
         // draw out green letters 
         break; 
        } 
        else 
        { 
         if (InputWord[b] != CorrectWordchar[b]) 
         { 
          SolidBrush s = new SolidBrush(Color.DarkOrange); 
          FontFamily ff = new FontFamily("Arial"); 
          System.Drawing.Font font = new System.Drawing.Font(ff, 20); 

          g.DrawString(InputWord[b].ToString(), font, s, new PointF(x, y)); 
          x += 20; 
          // Yellow letters 

          CorrectWordchar[b] = replacemt; // ersätter rätt bokstav med # 
          break; 
         } 
         else 
         { 
          b = 4; 
          SolidBrush s = new SolidBrush(Color.Red); 
          FontFamily ff = new FontFamily("Arial"); 
          System.Drawing.Font font = new System.Drawing.Font(ff, 20); 
          g.DrawString(InputWord[b].ToString(), font, s, new PointF(x, y)); 
          x += 20; 
          // Red letters 
          break; 
         } 

        } 

       } 
      } 
     } 
    } 
} 

}

回答

2

一些意見和示例代碼。

  1. 你有很多重複的代碼,只是因爲你繪製紅色和綠色。這可以總結。

  2. WinForms繪圖中使用的大多數類都必須處理。否則,你會遇到內存泄漏和GDI +泄漏。畫筆,字體等應予以處置。

  3. 使用Graphics.MeasureString可以獲取每個字符的大小。生成的大小可用於在X和Y中轉發。

  4. 字符串的字符可以通過索引直接訪問。你不需要將它們轉換成char數組。

    void YourDrawMethod(Graphics g) var wrongBrush = new SolidBrush(Color.Red); var correctBrush = new SolidBrush(Color.Green);

    var ff = new FontFamily(「Arial」); (var font = new System.Drawing.Font(ff,20)) { int x = 0; int int y = 0;

    foreach(car letter in InputWord) 
    { 
        SolidBrush brush = InputWord[i] == CorrectWord[b] ? correctBrush : wrongBrush; 
        g.DrawString(letter.ToString(), font, brush, new PointF(x, y));   
        Size sizeOfLetter = g.MeasureString(letter.ToString(), font); 
        x += sizeOfLetter.Width; 
    } 
    

    }

    wrongBrush.Dispose(); correctBrush.Dispose(); }

+0

感謝您的意見。我知道代碼寫得不好,但你認爲你可以通過聊天幫助我(我有很多問題=)。如果可以,我可以上傳遊戲以便嘗試。 – Benny

+0

當然。只需將代碼放在網上,讓我知道何時何地聊天。我現在在維也納時區,可以在星期天下午。 – Matthias