实现游戏循环

时间:2015-03-07 22:44:36

标签: android

我是Android新手,需要您的帮助。

我的代码的目标如下:我在一个数组中有8个单词。前4个是正确的,其余4个是错误的。每次移动,数组都被洗牌,单词放在图像按钮中。

我的问题是:我打算多次重复这个练习,每次重复之间的独特变化是:

  • 布局(例如按钮的位置)。
  • String [] arrWords。
  • 的值

你能给我建议实施这个“游戏圈”吗?非常感谢你。

可能有java代码:

private MediaPlayer player;
private ImageView chest;
private TextView tvPoints;
private int points;
private int hits;
private ImageView[] arrImgV = new ImageView[8];


private TextView[] arrTextV = new TextView[8];
private String[] arrWords = {"Sun", "Cloud", "Moon", "Sky", "River", "Tree", "Flower", "Stone"};
private Integer MAX = 8;
WordList[] word = new WordList[MAX];


@Override
public void onCreate(Bundle savedInstanceState)
{

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    points = 0;
    hits = 0;
    tvPoints = (TextView) findViewById(R.id.tvPoints);
    chest = (ImageView) findViewById(R.id.ivChest);

    setWordsArray(word);

    ShuffleArray(word);


    String nameIV = "Resp";
    String nameTV = "tResp";
    int val, idResourceIV, idResourceTV;

    for (int i = 0; i < MAX; i++)
    {
        val = i;
        idResourceIV = getResources().getIdentifier(nameIV + val, "id", getPackageName());
        arrImgV[i] = (ImageView) findViewById(idResourceIV);
        idResourceTV = getResources().getIdentifier(nameTV + val, "id", getPackageName());
        arrTextV[i] = (TextView) findViewById(idResourceTV);
        arrTextV[i].setText(word[i].getWord());
    }

    for (int i = 0; i < arrImgV.length; i++)
    {
        if (word[i].getResult() == 1)
            arrImgV[i].setOnClickListener(checkAnswer);
    }


}


View.OnClickListener checkAnswer = new OnClickListener()
{
    public void onClick(View v)
    {
        int index= Integer.parseInt(v.getTag().toString());
        if (word[index].getResult() == 1)
            runTrueAnswer(arrImgV[index]);
        else
            runFalseAnswer(arrImgV[index]);

    }
};

public void setWordsArray(WordList[] array)
{
    for(int i = 0; i<MAX; i++)
    {
        WordList wrd = new WordList();
        wrd.setWord(arrWords[i]);
        if (i < 4)
            wrd.setResult(1);
        else
            wrd.setResult(0);
        array[i] = wrd;
    }

}

private void ShuffleArray(WordList[] array)
{
    int index;
    WordList temp;
    Random random = new Random();
    for (int i = array.length - 1; i > 0; i--)
    {
        index = random.nextInt(i + 1);
        temp = array[index];
        array[index] = array[i];
        array[i] = temp;
    }
}

public void runTrueAnswer(ImageView img)
{
    player=MediaPlayer.create(MainActivity.this,R.raw.correct);
    player.start();
    img.setImageResource(R.drawable.coco_correct);
    img.setEnabled(false);
    points++;
    hits++;
    updateChest(points);
    if (hits == 4)
    {
        showFinalMsg();
    }
}


public void runFalseAnswer(ImageView img)
{
    player=MediaPlayer.create(MainActivity.this,R.raw.wrong);
    player.start();
    img.setImageResource(R.drawable.coco_wrong);
    img.setEnabled(false);
    points--;
    updateChest(points);
}

public void updateChest(int pt)
{
    chest.setImageResource(pt > 0 ? R.drawable.chest_cheio : R.drawable.emptychest);
    tvPoints.setText("Points: " + pt);
}

public void showFinalMsg()
{
    AlertDialog dlg = null;
    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Congratulations!!")
    .setMessage("You Win!")
    .setCancelable(false)
    .setPositiveButton("OK", new DialogInterface.OnClickListener()
    {
        public void onClick(DialogInterface dialog, int id)
        {
            MainActivity.this.recreate();
        }
    }) ;
    ;
    dlg = builder.create();
    dlg.show();
}
}

class WordList
{
    private String Word;
    private int Result;

    public WordList()
    {
        this.Word = null;
        this.Result = -1;
    }

    public WordList(String word, int result)
    {
        this.Word = word;
        this.Result = result;
    }

    public String getWord()
    {
        return Word;
    }

    public void setWord(String w)
    {
        this.Word = w;
    }

    public Integer getResult()
    {
        return Result;
    }

    public void setResult(int r)
    {
        this.Result = r;
    }
}

0 个答案:

没有答案