Home
2018年3月21日 星期三

[程式] Android Studio : Simple Quiz Game

Android Studio : Simple Quiz Game

這個益智問答遊戲主要是四選一選擇題,透過亂數產生問題,若一直達對問題,便能累積分數,若答錯後,還有提示視窗,詢問要離開或是繼續,遊戲的完成度比較好,可以與 [程式] Android Studio : Simple True False Quiz Game 的遊戲整合,讓題目不重複出現,只是這樣最高分就比較沒有趣味了,換個方向想,或許這樣問題會重複出現的遊戲適合用在語文學習類,例如背單字等...需要加深印象的的資訊。


範例網址:

https://www.youtube.com/watch?v=JA9s_Fntg_4

相關資訊圖片:

Simple Quiz Game View畫面
Simple Quiz Game View畫面


初始畫面
初始畫面


與初始畫面的題目一樣
與初始畫面的題目一樣


只要答對遊戲會一直進行
只要答對遊戲會一直進行


結束畫面
結束畫面


Source Code:

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="tw.idv.wenyen.simplequizgame.MainActivity">

    <Button
        android:id="@+id/answer4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

    <Button
        android:id="@+id/answer3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/answer4"
        android:layout_centerHorizontal="true"
        android:text="Button" />

    <Button
        android:id="@+id/answer2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/answer3"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:text="Button" />

    <Button
        android:id="@+id/answer1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/answer2"
        android:layout_centerHorizontal="true"
        android:text="Button" />

    <TextView
        android:id="@+id/score"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="TextView"
        android:gravity="center"
        android:textSize="24dp" />

    <TextView
        android:id="@+id/question"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/answer1"
        android:layout_below="@+id/score"
        android:layout_centerHorizontal="true"
        android:text="TextView"
        android:gravity="center"
        android:textSize="24dp" />
</RelativeLayout>


MainActivity.java
package tw.idv.wenyen.simplequizgame;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.Random;

public class MainActivity extends AppCompatActivity {
//    public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Button answer1, answer2, answer3, answer4;

    TextView score, question;

    private Questions mQuestions = new Questions();

    // 問題答案
    private String mAnswer;
    // 初始分數
    private int mScore = 0;
    // 問題題目數
    private int mQuestionsLength = mQuestions.mQuestions.length;

    Random r;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        r = new Random();

        answer1 = findViewById(R.id.answer1);
        answer2 = findViewById(R.id.answer2);
        answer3 = findViewById(R.id.answer3);
        answer4 = findViewById(R.id.answer4);

        score = findViewById(R.id.score);
        question = findViewById(R.id.question);

        score.setText("Score: " + mScore);

        updateQuestion(r.nextInt(mQuestionsLength));

        answer1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (answer1.getText() == mAnswer) {
                    mScore++;
                    score.setText("Score: " + mScore);
                    updateQuestion(r.nextInt(mQuestionsLength));
                } else {
                    gameOver();
                }
            }
        });

        answer2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (answer2.getText() == mAnswer) {
                    mScore++;
                    score.setText("Score: " + mScore);
                    updateQuestion(r.nextInt(mQuestionsLength));
                } else {
                    gameOver();
                }
            }
        });

        answer3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (answer3.getText() == mAnswer) {
                    mScore++;
                    score.setText("Score: " + mScore);
                    updateQuestion(r.nextInt(mQuestionsLength));
                } else {
                    gameOver();
                }
            }
        });

        answer4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (answer4.getText() == mAnswer) {
                    mScore++;
                    score.setText("Score: " + mScore);
                    updateQuestion(r.nextInt(mQuestionsLength));
                } else {
                    gameOver();
                }
            }
        });


    }

    /**
     * 取得題庫中的問題、選項與答案
     * @param num
     */
    private void updateQuestion(int num) {
        question.setText(mQuestions.getQuestion(num));
        answer1.setText(mQuestions.getChoice1(num));
        answer2.setText(mQuestions.getChoice2(num));
        answer3.setText(mQuestions.getChoice3(num));
        answer4.setText(mQuestions.getChoice4(num));

        mAnswer = mQuestions.getCorrectAnswer(num);
    }


//    @Override
//    public void onClick(View view) {
//        switch (view.getId()) {
//            case R.id.answer1:
//                if (answer1.getText() == mAnswer) {
//                    mScore++;
//                    score.setText("Score: " + mScore);
//                    updateQuestion(r.nextInt(mQuestionsLength));
//                } else {
//                    gameOver();
//                }
//                break;
//
//            case R.id.answer2:
//                if (answer2.getText() == mAnswer) {
//                    mScore++;
//                    score.setText("Score: " + mScore);
//                    updateQuestion(r.nextInt(mQuestionsLength));
//                } else {
//                    gameOver();
//                }
//                break;
//
//            case R.id.answer3:
//                if (answer3.getText() == mAnswer) {
//                    mScore++;
//                    score.setText("Score: " + mScore);
//                    updateQuestion(r.nextInt(mQuestionsLength));
//                } else {
//                    gameOver();
//                }
//                break;
//
//            case R.id.answer4:
//                if (answer4.getText() == mAnswer) {
//                    mScore++;
//                    score.setText("Score: " + mScore);
//                    updateQuestion(r.nextInt(mQuestionsLength));
//                } else {
//                    gameOver();
//                }
//                break;
//
//        }
//    }

    /**
     * 遊戲結束後,顯示詢問視窗,
     * 選擇要重新開始或是離開遊戲
     */
    private void gameOver() {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
        alertDialogBuilder
                .setMessage("Game Over! Your score is " + mScore + " points.")
                .setCancelable(false)
                .setPositiveButton("NEW GAME",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                startActivity(new Intent(getApplicationContext(), MainActivity.class));
                            }
                        })
                .setNegativeButton("EXIT",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                finish();
                            }
                        });

        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();
    }
}

Questions.java
package tw.idv.wenyen.simplequizgame;

/**
 * Created by kingbow on 2018/3/20.
 */

public class Questions {

    public String mQuestions[] = {
            "Which is the first planet in the Solar System?",
            "Which is the second planet in the Solar System?",
            "Which is the third planet in the Solar System?",
            "Which is the fourth planet in the Solar System",
            "Which is the fifth planet in the Solar System",
            "Which is the sixth planet in the Solar System",
            "Which is the seventh planet in the Solar System",
            "Which is the eight planet in the Solar System",
            "Which is the ninth planet in the Solar System"
    };

    private String mChoices[][] = {
            {"Mercury", "Venus", "Mars", "Saturn"},
            {"Jupiter", "Venus", "Earth", "Neptune"},
            {"Earth", "Jupiter", "Pluto", "Mars"},
            {"Jupiter", "Saturn", "Mars", "Earth"},
            {"Jupiter", "Pluto", "Mercury", "Earth"},
            {"Uranus", "Venus", "Mars", "Saturn"},
            {"Saturn", "Pluto", "Uranus", "Earth"},
            {"Mercury", "Neptune", "Pluto", "Mars"},
            {"Mercury", "Venus", "Mars", "Pluto"}
    };

    private String mCorrectAnswers[] = {"Mercury", "Venus", "Earth", "Mars",
            "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"};

    public String getQuestion(int a) {
        String question = mQuestions[a];
        return  question;
    };

    public String getChoice1(int a) {
        String choice = mChoices[a][0];
        return  choice;
    };

    public String getChoice2(int a) {
        String choice = mChoices[a][1];
        return  choice;
    };

    public String getChoice3(int a) {
        String choice = mChoices[a][2];
        return  choice;
    };

    public String getChoice4(int a) {
        String choice = mChoices[a][3];
        return  choice;
    };

    public String getCorrectAnswer(int a) {
        String answer = mCorrectAnswers[a];
        return answer;
    }

}

實作心得:

加上音樂與排行榜,就是個完整的小遊戲,程式部份不複雜,卻有完成一個遊戲的主題部份感覺,下次加上音樂或排行榜吧!

參考資料:

0 意見:

張貼留言