Android游戏的逻辑流程

时间:2013-04-25 14:09:05

标签: java android

嗨,我是Android游戏开发的新手,我想问你们关于我这个安卓游戏的正确逻辑流程。所以这是我的游戏。这是一个非常简单的游戏,用户必须选择正确的颜色。游戏有3个阶段。在每个阶段,它有10个问题。在每个问题中,它有一个30秒计时器。有问题和选择,当然需要随机化。如果用户选择了正确的颜色,它将继续进行下一个问题。但是如果用户选择了错误的颜色,他/她只有3个试验可供选择,如果他/她达到第3次试验,游戏 OVER 并显示再次尝试按钮。

这是我尝试过的一段代码:

    // I created a custom countdown timer c/o Say
    counter = new MyCount(30000,1000);
     counter.start();
     // Call for correct object
    getCorrectObject();


    @Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch(v.getId()){
    case R.id.pause:
        if(mLastResourceId == R.drawable.pause){
            pause.setImageResource(R.drawable.resume);
            mLastResourceId = R.drawable.resume;
            counter.cancel();
        } else if (mLastResourceId == R.drawable.resume) {
            pause.setImageResource(R.drawable.pause);
            mLastResourceId = R.drawable.pause;
            counter = new MyCount(s1,1000);
            counter.start();
        }
        break;
  }

 public class MyCount extends CountDownTimer
 {
    public MyCount(long millisInFuture, long countDownInterval)
    {
        super(millisInFuture, countDownInterval);
    }
    @Override     
    public void onFinish()
    {
        Intent i = new Intent(getApplicationContext(), Stage1_2.class);
        startActivity(i);
    }

    TextView tx = (TextView) findViewById(R.id.timer);
    @Override     
    public void onTick(long millisUntilFinished)
    {
        s1 = millisUntilFinished;
        tx.setText("" + millisUntilFinished / 1000);
    }
}

我还在考虑的是,如果用户在没有计时器结束时选择了正确的答案,我应该在onTick方法中加入什么来强制计时器结束?我面临的一个棘手的部分是选择或对象的随机化。

这也是我到目前为止所尝试的内容:

 private void getCorrectObject() {
    // TODO Auto-generated method stub

    List<Integer> objects = new ArrayList<Integer>();
    objects.add(1);
    objects.add(2);
    objects.add(3);
    objects.add(4);
    objects.add(5);
    objects.add(6);
    objects.add(7);

    Collections.shuffle(objects);
    int correctObject =  objects.get(0);
    Log.d("test", String.valueOf(correctObject));

    switch(correctObject)
    {
    case 1:
        bObjectCorrect.setImageResource(R.drawable.tree1);
        bObjectCorrect.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                // TODO Auto-generated method stub
                Intent i = new Intent(getApplicationContext(), Stage1_2.class);
                startActivity(i);
                new Thread(){
                    public void run(){
                        MediaPlayer mp = MediaPlayer.create(Stage1_1.this, R.raw.brown);
                        mp.start();
                    }
                }.start();
                finish();
            }
        });
        break;
    case 2:
        bObject1.setImageResource(R.drawable.tree1);
        bObject1.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                // TODO Auto-generated method stub
                Intent i = new Intent(getApplicationContext(), Stage1_2.class);
                startActivity(i);
                new Thread(){
                    public void run(){
                        MediaPlayer mp = MediaPlayer.create(Stage1_1.this, R.raw.brown);
                        mp.start();
                    }
                }.start();
                finish();
            }
        });
        break;
    case 3:
        bObject2.setImageResource(R.drawable.tree1);
        bObject2.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                // TODO Auto-generated method stub
                Intent i = new Intent(getApplicationContext(), Stage1_2.class);
                startActivity(i);
                new Thread(){
                    public void run(){
                        MediaPlayer mp = MediaPlayer.create(Stage1_1.this, R.raw.brown);
                        mp.start();
                    }
                }.start();
                finish();
            }
        });
        break;
    case 4:
        bObject3.setImageResource(R.drawable.tree1);
        bObject3.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                // TODO Auto-generated method stub
                Intent i = new Intent(getApplicationContext(), Stage1_2.class);
                startActivity(i);
                new Thread(){
                    public void run(){
                        MediaPlayer mp = MediaPlayer.create(Stage1_1.this, R.raw.brown);
                        mp.start();
                    }
                }.start();
                finish();
            }
        });
        break;
    case 5:
        bObject4.setImageResource(R.drawable.tree1);
        bObject4.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                // TODO Auto-generated method stub
                Intent i = new Intent(getApplicationContext(), Stage1_2.class);
                startActivity(i);
                new Thread(){
                    public void run(){
                        MediaPlayer mp = MediaPlayer.create(Stage1_1.this, R.raw.brown);
                        mp.start();
                    }
                }.start();
                finish();
            }
        });
        break;
    case 6:
        bObject5.setImageResource(R.drawable.tree1);
        bObject5.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                // TODO Auto-generated method stub
                Intent i = new Intent(getApplicationContext(), Stage1_2.class);
                startActivity(i);
                new Thread(){
                    public void run(){
                        MediaPlayer mp = MediaPlayer.create(Stage1_1.this, R.raw.brown);
                        mp.start();
                    }
                }.start();
                finish();
            }
        });
        break;
    case 7:
        bObject6.setImageResource(R.drawable.tree1);
        bObject6.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                // TODO Auto-generated method stub
                Intent i = new Intent(getApplicationContext(), Stage1_2.class);
                startActivity(i);
                new Thread(){
                    public void run(){
                        MediaPlayer mp = MediaPlayer.create(Stage1_1.this, R.raw.brown);
                        mp.start();
                    }
                }.start();
                finish();
            }
        });
        break;
    }
}

更新:,另一件事是,我很困惑将我的while循环放在这里进行3次试验。

 // I will put 3 trials logic here
    while(trial <= 3){
    trial++;

 switch(correctObject)
    {
    case 1:
        bObjectCorrect.setImageResource(R.drawable.stage1_1_object1);
        bObjectCorrect.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                // TODO Auto-generated method stub
                Intent i = new Intent(getApplicationContext(), Stage1_2.class);
                startActivity(i);
                new Thread(){
                    public void run(){
                        MediaPlayer mp = MediaPlayer.create(Stage1_2.this, R.raw.brown);
                        mp.start();
                    }
                }.start();
                finish();
            }
        });
        break;
    case 2:
        bObject1.setImageResource(R.drawable.stage1_1_object1);
        bObject1.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                // TODO Auto-generated method stub
                Intent i = new Intent(getApplicationContext(), Stage1_2.class);
                startActivity(i);
                new Thread(){
                    public void run(){
                        MediaPlayer mp = MediaPlayer.create(Stage1_2.this, R.raw.brown);
                        mp.start();
                    }
                }.start();
                finish();
            }
        });
        break;
    case 3:
        bObject2.setImageResource(R.drawable.stage1_1_object1);
        bObject2.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                // TODO Auto-generated method stub
                Intent i = new Intent(getApplicationContext(), Stage1_2.class);
                startActivity(i);
                new Thread(){
                    public void run(){
                        MediaPlayer mp = MediaPlayer.create(Stage1_2.this, R.raw.brown);
                        mp.start();
                    }
                }.start();
                finish();
            }
        });
        break;
    case 4:
        bObject3.setImageResource(R.drawable.stage1_1_object1);
        bObject3.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                // TODO Auto-generated method stub
                Intent i = new Intent(getApplicationContext(), Stage1_2.class);
                startActivity(i);
                new Thread(){
                    public void run(){
                        MediaPlayer mp = MediaPlayer.create(Stage1_2.this, R.raw.brown);
                        mp.start();
                    }
                }.start();
                finish();
            }
        });
        break;
    case 5:
        bObject4.setImageResource(R.drawable.stage1_1_object1);
        bObject4.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                // TODO Auto-generated method stub
                Intent i = new Intent(getApplicationContext(), Stage1_2.class);
                startActivity(i);
                new Thread(){
                    public void run(){
                        MediaPlayer mp = MediaPlayer.create(Stage1_2.this, R.raw.brown);
                        mp.start();
                    }
                }.start();
                finish();
            }
        });
        break;
    case 6:
        bObject5.setImageResource(R.drawable.stage1_1_object1);
        bObject5.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                // TODO Auto-generated method stub
                Intent i = new Intent(getApplicationContext(), Stage1_2.class);
                startActivity(i);
                new Thread(){
                    public void run(){
                        MediaPlayer mp = MediaPlayer.create(Stage1_2.this, R.raw.brown);
                        mp.start();
                    }
                }.start();
                finish();
            }
        });
        break;
    case 7:
        bObject6.setImageResource(R.drawable.stage1_1_object1);
        bObject6.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                // TODO Auto-generated method stub
                Intent i = new Intent(getApplicationContext(), Stage1_2.class);
                startActivity(i);
                new Thread(){
                    public void run(){
                        MediaPlayer mp = MediaPlayer.create(Stage1_2.this, R.raw.brown);
                        mp.start();
                    }
                }.start();
                finish();
            }
        });
        break;
    case 8:
        bObject7.setImageResource(R.drawable.stage1_1_object1);
        bObject7.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                // TODO Auto-generated method stub
                Intent i = new Intent(getApplicationContext(), Stage1_2.class);
                startActivity(i);
                new Thread(){
                    public void run(){
                        MediaPlayer mp = MediaPlayer.create(Stage1_2.this, R.raw.brown);
                        mp.start();
                    }
                }.start();
                finish();
            }
        });
        break;
    case 9:
        bObject8.setImageResource(R.drawable.stage1_1_object1);
        bObject8.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                // TODO Auto-generated method stub
                Intent i = new Intent(getApplicationContext(), Stage1_2.class);
                startActivity(i);
                new Thread(){
                    public void run(){
                        MediaPlayer mp = MediaPlayer.create(Stage1_2.this, R.raw.brown);
                        mp.start();
                    }
                }.start();
                finish();
            }
        });
        break;
    } // Last of switch statement

    if(trial == 3){
        new AlertDialog.Builder(this)
        .setTitle("Game Over")
        .setMessage("Sorry you reached your 3rd trial")
        .setPositiveButton("Try Again?", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) { 
                Intent i = new Intent(Stage1_2.this, Stage1_1.class);
                startActivity(i);
            }
         })
        .setNegativeButton("Back to Menu", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) { 
                Intent i = new Intent(Stage1_2.this, ShapingColors.class);
                startActivity(i);
            }
         })
         .show();
    }

    } // Last of while loop

我真的很想听听你的建议。非常感谢您提供的任何帮助。提前谢谢!

1 个答案:

答案 0 :(得分:1)

您无需在onTick中放置任何内容来处理此案例。在您调用cancel()(您在点击处理程序中执行此操作)后,将不会再次调用onTick()