Android Studio : Simple True False Quiz Game
簡單的益智問答遊戲,只有True False值,算是入門的遊戲,另外有多選一模式,下次再來實作吧!
範例網址:
https://www.youtube.com/watch?v=-kdzyGXziSA相關資訊圖片:
![]() |
Simple True False 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.truefalsequiz.MainActivity"> <Button android:id="@+id/b_false" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:text="FALSE" /> <Button android:id="@+id/b_ture" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@+id/b_false" android:layout_centerHorizontal="true" android:text="TRUE" /> <TextView android:id="@+id/tv_question" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@+id/b_ture" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:gravity="center" android:text="" /> </RelativeLayout>
package tw.idv.wenyen.truefalsequiz; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; import java.util.Collections; public class MainActivity extends AppCompatActivity { TextView tv_question; Button b_true, b_false; Questions mQuestions; int questionsLength; ArrayList<Item>- questionsList; int currentQuestion = 0; boolean winner = false; private static final String TAG = "MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.d(TAG, "onCreate(Bundle savedInstanceState)"); //init stuff tv_question = findViewById(R.id.tv_question); b_true = findViewById(R.id.b_ture); b_false = findViewById(R.id.b_false); mQuestions = new Questions(); questionsLength = mQuestions.mQuestions.length; Log.d(TAG, "questionsLength=" + questionsLength); questionsList = new ArrayList<>(); //save all the questions in the list for(int i = 0; i < questionsLength; i++) { questionsList.add(new Item(mQuestions.getQuestion(i), mQuestions.getAnswer(i))); Log.d(TAG, "questionsList[" + i + "," + mQuestions.getQuestion(i) + "," + mQuestions.getAnswer(i) + "]"); } //shuffle the questions Collections.shuffle(questionsList); //start the questions setQuestion(currentQuestion); b_true.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (checkQuestion(currentQuestion)) { //current - the game continues currentQuestion++; if (currentQuestion < questionsLength) { setQuestion(currentQuestion); } else { //game over - winner winner = true; endGame(); } } else { //wrong - the game ends endGame(); } } }); b_false.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (!checkQuestion(currentQuestion)) { //current - the game continues currentQuestion++; if (currentQuestion < questionsLength) { setQuestion(currentQuestion); } else { //game over - winner winner = true; endGame(); } } else { //wrong - the game ends endGame(); } } }); } //show question on the screen private void setQuestion(int number) { Log.d(TAG, "setQuestion(int number)"); tv_question.setText(questionsList.get(number).getQuestion()); } //check if the answer is right private boolean checkQuestion(int number) { Log.d(TAG, "checkQuestion(int number)"); String answer = questionsList.get(number).getAnswer(); return answer.equals("true"); } //game over private void endGame() { Log.d(TAG, "endGame()"); if (winner) { Toast.makeText(this, "Game Over! You win!", Toast.LENGTH_SHORT).show(); finish(); } else { Toast.makeText(this, "Game Over! You lose!", Toast.LENGTH_SHORT).show(); finish(); } } }
Item.java
package tw.idv.wenyen.truefalsequiz;
/**
* Created by kingbow on 2018/3/18.
*/
public class Item {
private String question, answer;
public Item(String question, String answer) {
this.question = question;
this.answer = answer;
}
public String getQuestion() {
return question;
}
public String getAnswer() {
return answer;
}
}
Questions.java
package tw.idv.wenyen.truefalsequiz;
/**
* Created by kingbow on 2018/3/18.
*/
public class Questions {
public String mQuestions[] = {
"Mercury is the first planet in the Solar System",
"Venus is the third planet in the Solar System",
"Earth is the fourth planet in the Solar System",
"Mars is the first planet in the Solar System",
"Jupiter is the seventh planet in the Solar System",
"Saturn is the fifth planet in the Solar System",
"Uranus is the seventh planet in the Solar System",
"Neptune is the eight planet in the Solar System",
"Pluto is the ninth planet in the Solar System"
};
public String mAnswers[] = {
"true",
"false",
"false",
"false",
"false",
"false",
"true",
"true",
"true"
};
public String getQuestion(int number) {
return mQuestions[number];
};
public String getAnswer(int number) {
return mAnswers[number];
};
}
實作心得:
遊戲結束後會直接關閉APP,這樣的設計不太理想,應該要讓玩家可以重玩或確認要結束遊戲再關閉,下一個實作範例 Simple Quiz Game ,整體的完成度較佳,到時再將True False Game 重新改寫吧。
Collections.shuffle(questionsList) 是相當有用的程式,短短的一行就可以做到洗牌的效果,值得花點時間研究。
整體來說難度不高,因此不另外補充註解。
整體來說難度不高,因此不另外補充註解。
0 意見:
張貼留言