gpt4 book ai didi

java - Android游戏的逻辑流程

转载 作者:行者123 更新时间:2023-12-01 14:38:41 26 4
gpt4 key购买 nike

您好,我是 Android 游戏开发新手,我想向你们询问我的这款 Android 游戏的正确逻辑流程。这是我的游戏。这是一个非常简单的游戏,用户必须选择正确的颜色。游戏有3个阶段。每个阶段有 10 个问题。每个问题都有一个 30 秒的计时器。有了问题和选择,当然需要随机化。如果用户选择了正确的颜色,就会进入下一个问题。但如果用户选择了错误的颜色,他/她只有3次尝试可供选择,如果他/她达到了第3次尝试,游戏就OVER并显示TRY AGAIN按钮。

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

    // 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

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

最佳答案

您不需要在 onTick 中添加任何内容来处理这种情况。调用 cancel()(在点击处理程序中执行)后,将不会再次调用 onTick()

关于java - Android游戏的逻辑流程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16217000/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com