1

C# で基本的な書き込みゲームをプログラミングしています。ゲームは、ピクチャボックスに画像をランダムに表示します。画像は、メイン クラスとは異なるクラスの配列に格納されます。

クラスのコードは次のようになります。

public class bildelisteDyr
{

    public static Bitmap bildeListe (int index)
    {
        Bitmap[] bildeListe = new Bitmap[21];

        bildeListe[0] = Properties.Resources.ål;
        bildeListe[1] = Properties.Resources.ant;
        bildeListe[2] = Properties.Resources.bird; 
        bildeListe[3] = Properties.Resources.bear;
        bildeListe[4] = Properties.Resources.butterfly;
        bildeListe[5] = Properties.Resources.cat;
        bildeListe[6] = Properties.Resources.chicken;
        bildeListe[7] = Properties.Resources.dog;
        bildeListe[8] = Properties.Resources.elephant;
        bildeListe[9] = Properties.Resources.fish;
        bildeListe[10] = Properties.Resources.goat;
        bildeListe[11] = Properties.Resources.horse;
        bildeListe[12] = Properties.Resources.ladybug;
        bildeListe[13] = Properties.Resources.lion;
        bildeListe[14] = Properties.Resources.moose;
        bildeListe[15] = Properties.Resources.polarbear;
        bildeListe[16] = Properties.Resources.reke;
        bildeListe[17] = Properties.Resources.sheep;
        bildeListe[18] = Properties.Resources.snake;
        bildeListe[19] = Properties.Resources.spider;
        bildeListe[20] = Properties.Resources.turtle;
        return bildeListe[index];
    }    
}

配列内の値を呼び出してピクチャボックスに画像をランダムに表示すると、すべてうまく機能します。これは次のように行われます。

pictureBox1.Image = bildelisteDyr.bildeListe(r.Next(0, 20));

しかし、何かをするためにピクチャボックスの値をチェックするコードが必要な場合が 3 回あります。サウンド再生ボタンが1つ、ラベルにテキストを付けるボタンが1つ、テキストボックスからの回答を確認するボタンが1つあります。それらのどれも機能していないようです。ここにいくつかのコードがあります:

ラベルにテキストを与える:

 if (pictureBox1.Image == bildelisteDyr.bildeListe(0))
 {
     svarPåOppgave.Text = "ÅL";
 }
 else if (pictureBox1.Image == bildelisteDyr.bildeListe(1))
 {
     svarPåOppgave.Text = "MAUR";
 }
 // etc.

サウンド再生ボタン :

if (pictureBox1.Image == bildelisteDyr.bildeListe(0))
{
    SoundPlayer player = new SoundPlayer();
    player = new SoundPlayer("lyd/dyr/ål.wav");
    player.PlaySync();
}
else if (pictureBox1.Image == bildelisteDyr.bildeListe(1))
{
    SoundPlayer player = new SoundPlayer();
    player = new SoundPlayer("lyd/dyr/enmaur.wav");
    player.PlaySync();
}

// etc.

正しい答えが与えられているかどうかを確認する:

if (pictureBox1.Image == bildelisteDyr.bildeListe(0))
{
    if (textBox1.Text.Trim().ToLower() == "ål")
    {
        riktigLyd.Play();

        poengInt += 1;

        textBox1.Text = "";

        pictureBox1.Image = bildelisteDyr.bildeListe(tilfeldigBildet);

        tekstTilLabel();

        svarPåOppgave.Visible = false;
    }
    else
    {

        feilLyd.Play();

        poengInt -= 1;

        textBox1.Text = "";
    }

    String poengString = poengInt.ToString();
    label1.Text = poengString;
    textBox1.Select();

}
else if (pictureBox1.Image == bildelisteDyr.bildeListe(1))
{
    if (textBox1.Text.Trim().ToLower() == "maur")
    {
        riktigLyd.Play();

        poengInt += 1;

        textBox1.Text = "";

        pictureBox1.Image = bildelisteDyr.bildeListe(tilfeldigBildet);

        tekstTilLabel();

        svarPåOppgave.Visible = false;
    }
    else
    {

        feilLyd.Play();

        poengInt -= 1;

        textBox1.Text = "";
    }

    String poengString = poengInt.ToString();
    label1.Text = poengString;

} // etc.

次のようなifステートメントに何か問題があったと思います

if (textBox1.Text.Trim().ToLower() == "ål")

しかし、私は何を理解できないようですか?

要約すると、プログラムをデバッグすると、他のクラスからランダムな画像が取得されます。しかし、プログラムのボタンを押しても何も起こりません。音も、ラベルを付けるテキストも、答えのチェックもありません。

4

1 に答える 1

1

ここにはいくつかの珍しいアーキテクチャの選択がありますが、直面している特定の問題は、Bitmap毎回 s を再作成していることと、比較が値ではなく参照によって実行されることです。

bildelisteDyr次のようにクラスを変更します。

public class bildelisteDyr
{
    static Bitmap[] bildeListeInternal;

    static bildelisteDyr() {
        bildeListeInternal = new Bitmap[21];
        bildeListeInternal[0] = Properties.Resources.ål;
        //...
        bildeListeInternal[20] = Properties.Resources.turtle;
    }

    public static Bitmap bildeListe (int index) {
        return bildeListeInternal[index];
    }
}

概念的な問題に関するその他のリソース:

于 2013-06-05T05:32:58.450 に答える