android简单的答题游戏

本文转自:http://blog.csdn.net/sp6645597/article/details/9981365

感谢,大神的无私,小弟深深拜服...

笔者最近开始沦陷于android,从开始入门到现在已经快半个月的时间,于是便写一个较综合,用到了数据库,多线程操作,以及时钟的添加和停止消除,activity之间的动画转换等,适用于初学者学以致用的小游戏来巩固自己的知识,有需要的读者可以去我的资源库中下载源码。

   以下是主游戏程序的部分代码,带有笔者的丰富注释:

 

[java]  view plain copy
  1. package com.example.pingping_game1;  
  2.   
  3. import java.util.HashMap;  
  4. import java.util.Map;  
  5. import java.util.Random;  
  6. import java.util.Timer;  
  7. import java.util.TimerTask;  
  8.   
  9. import com.example.pingping_game1.Tools.JudgeAnswer;  
  10. import com.example.pingping_game1.Tools.MakeIntToString;  
  11. import com.example.pingping_game1.getsqldatabase.getquestion;  
  12. import com.example.pingping_game1.getsqldatabase.getsqldatabase;  
  13.   
  14. import android.R.integer;  
  15. import android.app.Activity;  
  16. import android.app.AlertDialog;  
  17. import android.app.Dialog;  
  18. import android.content.DialogInterface;  
  19. import android.database.sqlite.SQLiteDatabase;  
  20. import android.os.Bundle;  
  21. import android.os.Handler;  
  22. import android.os.Message;  
  23. import android.support.v4.widget.SimpleCursorAdapter.ViewBinder;  
  24. import android.text.style.BulletSpan;  
  25. import android.view.View;  
  26. import android.view.View.OnClickListener;  
  27. import android.widget.Button;  
  28. import android.widget.ProgressBar;  
  29. import android.widget.SeekBar;  
  30. import android.widget.TextView;  
  31. import android.widget.Toast;  
  32.   
  33. public class GameActivity1 extends Activity implements OnClickListener {  
  34.   
  35.     public GameActivity1() {  
  36.         // TODO Auto-generated constructor stub  
  37.     }  
  38.   
  39.     private TextView stateView, stateprogressView, questionView; // 各种状态信息  
  40.     private Button aswA, aswB, aswC, aswD; // 4个答案选项按钮  
  41.     private ProgressBar timeprogress; // 时间进度条  
  42.     private int wr = 0// 答错的题数  
  43.     private int tr = 0// 答对的题数  
  44.     private int qnumber = 1// 当前题目的题号  
  45.     private int statenum = 1// 当前关数  
  46.     private final static int sum = 5// 总共需要答对的题数  
  47.     private final static int wrsum = 3// 总共可答错的次数  
  48.     private final static int LASTSTATE = 2// 最终关数  
  49.     private final static int CHANGE_QUESTION = 1// 变换游戏界面题目的标识符  
  50.     private final static int SETPROGRESS = 2// 表示设置时间进度条的标识符  
  51.     private final static int RESTARTGAME = 3// 重新开始游戏的标识符  
  52.     private static boolean OVERTIME = false// 是否已经超时标识符  
  53.     // 用mainMap来存储该题对应的信息  
  54.     private Map<String, String> mainMap = new HashMap<String, String>();  
  55.     private boolean flag = false// 此题是否答对  
  56.     private int progressBarValue = 0// 表示时间进度条的进度  
  57.     private final static int TOTALPROGRESS = 30// 设置时间进度条的最大值  
  58.     private Timer timer; // 设置一个定时器  
  59.     private Random random = new Random(); // 设置一个随机数来随机抽取题目  
  60.     private int[] QuestionNum = new int[8]; // 每一关题目的序列号  
  61.   
  62.     // 用线程和handler来处理消息  
  63.     private Handler handler = new Handler() {  
  64.         @Override  
  65.         public void handleMessage(android.os.Message msg) {  
  66.             switch (msg.what) {  
  67.             case CHANGE_QUESTION:  
  68.                 mainMap = (Map<String, String>) msg.obj;  
  69.                 stateView.setText("第" + statenum + "关");  
  70.                 stateprogressView.setText(tr + "/" + sum + "\n" + wr + "/"  
  71.                         + wrsum);  
  72.                 questionView.setText(qnumber + ":" + mainMap.get("questions"));  
  73.                 aswA.setText("A." + mainMap.get("a"));  
  74.                 aswB.setText("B." + mainMap.get("b"));  
  75.                 aswC.setText("C." + mainMap.get("c"));  
  76.                 aswD.setText("D." + mainMap.get("d"));  
  77.                 break;  
  78.             case SETPROGRESS:  
  79.                 int progress = (Integer) msg.obj;  
  80.                 timeprogress.setProgress(progress);  
  81.                 break;  
  82.             case RESTARTGAME:  
  83.                 timer.cancel();  
  84.                 OVERTIME = true// 设置为超时  
  85.                 new ShowTimeOverDialog().showdialog();  
  86.                 break;  
  87.             }  
  88.         };  
  89.     };  
  90.   
  91.     @Override  
  92.     protected void onCreate(Bundle savedInstanceState) {  
  93.         // TODO Auto-generated method stub  
  94.         super.onCreate(savedInstanceState);  
  95.         setContentView(R.layout.ggg);  
  96.         stateView = (TextView) this.findViewById(R.id.statetext);  
  97.         stateprogressView = (TextView) this.findViewById(R.id.stateprogress);  
  98.         questionView = (TextView) this.findViewById(R.id.questiontext);  
  99.         aswA = (Button) this.findViewById(R.id.aswA);  
  100.         aswA.setAlpha((float0.5);  
  101.         aswA.setOnClickListener(this);  
  102.         aswB = (Button) this.findViewById(R.id.aswB);  
  103.         aswB.setAlpha((float0.5);  
  104.         aswB.setOnClickListener(this);  
  105.         aswC = (Button) this.findViewById(R.id.aswC);  
  106.         aswC.setAlpha((float0.5);  
  107.         aswC.setOnClickListener(this);  
  108.         aswD = (Button) this.findViewById(R.id.aswD);  
  109.         aswD.setAlpha((float0.5);  
  110.         aswD.setOnClickListener(this);  
  111.         timeprogress = (ProgressBar) this.findViewById(R.id.progressBar1);  
  112.         timeprogress.setMax(TOTALPROGRESS);  
  113.         InitialQNum(); // 初始化题号序列数组  
  114.         new Thread(new StartGame()).start();  
  115.         timer = new Timer(true);  
  116.         timer.schedule(new TimerTask() {  
  117.   
  118.             @Override  
  119.             public void run() {  
  120.                 // TODO Auto-generated method stub  
  121.                 if (progressBarValue == TOTALPROGRESS) {  
  122.                     // 超出游戏时间,弹出对话框提示玩家  
  123.                     handler.sendEmptyMessage(RESTARTGAME);  
  124.                 } else {  
  125.                     // 将信息传送给handler来更新进度条  
  126.                     Message message = Message.obtain();  
  127.                     message.obj = progressBarValue;  
  128.                     message.what = SETPROGRESS;  
  129.                     handler.sendMessage(message);  
  130.                     // 时间进度自增  
  131.                     progressBarValue++;  
  132.                 }  
  133.             }  
  134.         }, 01000);  
  135.     }  
  136.   
  137.     // 初始化QuestionNum数组,随机抽取  
  138.     private void InitialQNum() {  
  139.         int count = 0;  
  140.         while (count < 8) {  
  141.             boolean flag1 = true// 标志是否重复  
  142.             int cur = Math.abs(random.nextInt() % 8) + 1;  
  143.             for (int i = 0; i < count; i++) {  
  144.                 if (cur == QuestionNum[i]) {  
  145.                     flag1 = false;  
  146.                     break;  
  147.                 }  
  148.             }  
  149.             if (flag1) {  
  150.                 QuestionNum[count] = cur;  
  151.                 count++;  
  152.             }  
  153.         }  
  154.     }  
  155.   
  156.     public class StartGame implements Runnable {  
  157.         @Override  
  158.         public void run() {  
  159.             // TODO Auto-generated method stub  
  160.             getquestion getq = new getquestion(GameActivity1.this);  
  161.             Map<String, String> map = new HashMap<String, String>();  
  162.             // 用MakeIntToString工具类来转换字符,并选择对应题目  
  163.             String str = MakeIntToString.getString(QuestionNum[qnumber - 1]  
  164.                     + (statenum - 1) * 8);  
  165.             String str1 = String.valueOf(statenum);  
  166.             String[] strs = new String[] { str, str1 };  
  167.             map = getq.getquestionMap(strs);  
  168.             // 用message来向主线程传递信息并处理  
  169.             Message message = Message.obtain();  
  170.             message.obj = map; // 将map信息放入message中  
  171.             message.what = CHANGE_QUESTION; // 设定message的标示符  
  172.             handler.sendMessage(message); // 向主线程中的handler发送信息  
  173.         }  
  174.   
  175.     }  
  176.   
  177.     // 游戏进入下一关  
  178.     private void GoToNextState() {  
  179.         if (OVERTIME) {  
  180.             return;  
  181.         }  
  182.         timer.cancel(); // 关闭时钟  
  183.         statenum++; // 关数自增  
  184.         qnumber = 1// 题号重置为1  
  185.         wr = 0// 答错重置  
  186.         tr = 0// 答对重置  
  187.         InitialQNum(); // 重新抽取随机数组为题目序列  
  188.         progressBarValue = 0// 将时间进度重置为0  
  189.         Toast.makeText(GameActivity1.this"恭喜你进入第" + statenum + "关!"0)  
  190.                 .show();  
  191.         new Thread(new StartGame()).start();  
  192.         timer = null;  
  193.         timer = new Timer();  
  194.         timer.schedule(new TimerTask() {  
  195.   
  196.             @Override  
  197.             public void run() {  
  198.                 // TODO Auto-generated method stub  
  199.                 if (progressBarValue == TOTALPROGRESS) {  
  200.                     // 超出游戏时间,弹出对话框提示玩家  
  201.                     handler.sendEmptyMessage(RESTARTGAME);  
  202.                 } else {  
  203.                     // 将信息传送给handler来更新进度条  
  204.                     Message message = Message.obtain();  
  205.                     message.obj = progressBarValue;  
  206.                     message.what = SETPROGRESS;  
  207.                     handler.sendMessage(message);  
  208.                     // 时间进度自增  
  209.                     progressBarValue++;  
  210.                 }  
  211.             }  
  212.         }, 01000);  
  213.     }  
  214.   
  215.     // 重新开始游戏  
  216.     private class RestartGame {  
  217.         public RestartGame() {  
  218.   
  219.         }  
  220.   
  221.         public void restart() {  
  222.             statenum = 1;  
  223.             qnumber = 1// 重置题号为1  
  224.             wr = 0;  
  225.             tr = 0;  
  226.             progressBarValue = 0;  
  227.             InitialQNum();  
  228.             timer = null;  
  229.             timer = new Timer(true);  
  230.             timer.schedule(new TimerTask() {  
  231.   
  232.                 @Override  
  233.                 public void run() {  
  234.                     // TODO Auto-generated method stub  
  235.                     if (progressBarValue == TOTALPROGRESS) {  
  236.                         // 超出游戏时间,弹出对话框提示玩家  
  237.                         handler.sendEmptyMessage(RESTARTGAME);  
  238.                     } else {  
  239.                         // 将信息传送给handler来更新进度条  
  240.                         Message message = Message.obtain();  
  241.                         message.obj = progressBarValue;  
  242.                         message.what = SETPROGRESS;  
  243.                         handler.sendMessage(message);  
  244.                         // 时间进度自增  
  245.                         progressBarValue++;  
  246.                     }  
  247.                 }  
  248.             }, 01000);  
  249.             new Thread(new StartGame()).start();  
  250.         }  
  251.     }  
  252.   
  253.     // 游戏超时弹出对话框  
  254.     public class ShowTimeOverDialog {  
  255.         public ShowTimeOverDialog() {  
  256.   
  257.         }  
  258.   
  259.         public void showdialog() {  
  260.             AlertDialog.Builder builder = new AlertDialog.Builder(  
  261.                     GameActivity1.this);  
  262.             builder.setTitle("提示");  
  263.             builder.setMessage("对不起,你的智商太低,没有在规定时间内完成答题!");  
  264.             builder.setPositiveButton("重新开始",  
  265.                     new DialogInterface.OnClickListener() {  
  266.   
  267.                         @Override  
  268.                         public void onClick(DialogInterface dialog, int which) {  
  269.                             // TODO Auto-generated method stub  
  270.                             OVERTIME = false// 设置为不超时  
  271.                             new RestartGame().restart();  
  272.                         }  
  273.                     });  
  274.             builder.setNegativeButton("主界面",  
  275.                     new DialogInterface.OnClickListener() {  
  276.   
  277.                         @Override  
  278.                         public void onClick(DialogInterface dialog, int which) {  
  279.                             // TODO Auto-generated method stub  
  280.                             GameActivity1.this.finish();  
  281.                         }  
  282.                     });  
  283.             builder.setCancelable(false);  
  284.             Dialog dialog = builder.create();  
  285.             dialog.show();  
  286.   
  287.         }  
  288.     }  
  289.   
  290.     // 游戏失败时弹出的对话框  
  291.     public class ShowGameOverDialog {  
  292.   
  293.         public ShowGameOverDialog() {  
  294.   
  295.         }  
  296.   
  297.         public void showdialog() {  
  298.             timer.cancel();  
  299.             AlertDialog.Builder builder = new AlertDialog.Builder(  
  300.                     GameActivity1.this);  
  301.             builder.setTitle("提示");  
  302.             builder.setMessage("对不起,愚蠢的人类,你闯关失败了!");  
  303.             builder.setPositiveButton("重新闯关",  
  304.                     new DialogInterface.OnClickListener() {  
  305.   
  306.                         @Override  
  307.                         public void onClick(DialogInterface dialog, int which) {  
  308.                             // TODO Auto-generated method stub  
  309.                             new RestartGame().restart();  
  310.                         }  
  311.                     });  
  312.             builder.setNegativeButton("主界面",  
  313.                     new DialogInterface.OnClickListener() {  
  314.   
  315.                         @Override  
  316.                         public void onClick(DialogInterface dialog, int which) {  
  317.                             // TODO Auto-generated method stub  
  318.                             GameActivity1.this.finish();  
  319.                         }  
  320.                     });  
  321.             builder.setCancelable(false);  
  322.             Dialog dialog = builder.create();  
  323.             dialog.show();  
  324.         }  
  325.     }  
  326.   
  327.     private void GoOverGame() {  
  328.         if (OVERTIME) {  
  329.             return;  
  330.         }  
  331.         timer.cancel();  
  332.         AlertDialog.Builder builder = new AlertDialog.Builder(  
  333.                 GameActivity1.this);  
  334.         builder.setTitle("提示");  
  335.         builder.setMessage("恭喜您通关!!~您的智商真是高!");  
  336.         builder.setPositiveButton("谦让谦让",  
  337.                 new DialogInterface.OnClickListener() {  
  338.   
  339.                     @Override  
  340.                     public void onClick(DialogInterface dialog, int which) {  
  341.                         // TODO Auto-generated method stub  
  342.                         GameActivity1.this.finish();  
  343.                     }  
  344.                 });  
  345.         builder.setCancelable(false);  
  346.         Dialog dialog = builder.create();  
  347.         dialog.show();  
  348.     }  
  349.   
  350.     @Override  
  351.     public void onBackPressed() { // 按返回键时触发事件  
  352.         // TODO Auto-generated method stub  
  353.         super.onBackPressed();  
  354.         timer.cancel(); // 将时钟取消并置空  
  355.         timer = null;  
  356.         GameActivity1.this.finish();  
  357.     }  
  358.   
  359.     @Override  
  360.     public void onClick(View v) {  
  361.         // TODO Auto-generated method stub  
  362.         switch (v.getId()) {  
  363.         case R.id.aswA:  
  364.             // 返回当前是否答对  
  365.             flag = new JudgeAnswer(GameActivity1.this).judgeit("a", mainMap);  
  366.             if (flag) { // 如果答对,对应参数进行改变  
  367.                 tr++;  
  368.                 qnumber++;  
  369.                 if (tr == sum) {  
  370.                     if (statenum == LASTSTATE) {  
  371.                         GoOverGame();  
  372.                     } else {  
  373.                         GoToNextState();  
  374.                     }  
  375.                 } else {  
  376.                     new Thread(new StartGame()).start();  
  377.                 }  
  378.             } else {  
  379.                 wr++;  
  380.                 qnumber++;  
  381.                 if (wr == wrsum) { // 当错误题量达到上限,弹出游戏结束对话框  
  382.                     new ShowGameOverDialog().showdialog();  
  383.                 } else { // 否则更换题目  
  384.                     new Thread(new StartGame()).start();  
  385.                 }  
  386.             }  
  387.             break;  
  388.         case R.id.aswB:  
  389.             flag = new JudgeAnswer(GameActivity1.this).judgeit("b", mainMap);  
  390.             if (flag) {  
  391.                 tr++;  
  392.                 qnumber++;  
  393.                 if (tr == sum) {  
  394.                     if (statenum == LASTSTATE) {  
  395.                         GoOverGame();  
  396.                     } else {  
  397.                         GoToNextState();  
  398.                     }  
  399.                 } else {  
  400.                     new Thread(new StartGame()).start();  
  401.                 }  
  402.             } else {  
  403.                 wr++;  
  404.                 qnumber++;  
  405.                 if (wr == wrsum) {  
  406.                     new ShowGameOverDialog().showdialog();  
  407.                 } else {  
  408.                     new Thread(new StartGame()).start();  
  409.                 }  
  410.             }  
  411.             break;  
  412.         case R.id.aswC:  
  413.             flag = new JudgeAnswer(GameActivity1.this).judgeit("c", mainMap);  
  414.             if (flag) {  
  415.                 tr++;  
  416.                 qnumber++;  
  417.                 if (tr == sum) {  
  418.                     if (statenum == LASTSTATE) {  
  419.                         GoOverGame();  
  420.                     } else {  
  421.                         GoToNextState();  
  422.                     }  
  423.                 } else {  
  424.                     new Thread(new StartGame()).start();  
  425.                 }  
  426.             } else {  
  427.                 wr++;  
  428.                 qnumber++;  
  429.                 if (wr == wrsum) {  
  430.                     new ShowGameOverDialog().showdialog();  
  431.                 } else {  
  432.                     new Thread(new StartGame()).start();  
  433.                 }  
  434.             }  
  435.             break;  
  436.         case R.id.aswD:  
  437.             flag = new JudgeAnswer(GameActivity1.this).judgeit("d", mainMap);  
  438.             if (flag) {  
  439.                 tr++;  
  440.                 qnumber++;  
  441.                 if (tr == sum) {  
  442.                     if (statenum == LASTSTATE) {  
  443.                         GoOverGame();  
  444.                     } else {  
  445.                         GoToNextState();  
  446.                     }  
  447.                 } else {  
  448.                     new Thread(new StartGame()).start();  
  449.                 }  
  450.             } else {  
  451.                 wr++;  
  452.                 qnumber++;  
  453.                 if (wr == wrsum) {  
  454.                     new ShowGameOverDialog().showdialog();  
  455.                 } else {  
  456.                     new Thread(new StartGame()).start();  
  457.                 }  
  458.             }  
  459.             break;  
  460.         }  
  461.     }  
  462. }  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值