目标效果:

  

 

游戏页面有几个小游戏,因为时间原因只做了第一个猜成语,是用的选择题方式,十道题以内答对六题算闯关成功。

1.新建GameActivity.java页面和activity_game.xml页面,activity_game.xml作为显示游戏目录页面:

activity_game.xml页面:

<LinearLayout 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"android:background="@drawable/bg_ling"android:orientation="vertical"tools:context=".GameActivity" ><TextViewandroid:id="@+id/tvGameGuess"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:layout_marginTop="50dp"android:clickable="true"android:onClick="gamePlay"android:text="@string/tvGameGuess"android:textColor="#000000"android:textSize="23sp" /><TextViewandroid:id="@+id/tvGameWrite"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:layout_marginTop="30dp"android:clickable="true"android:onClick="gamePlay"android:text="@string/tvGameWrite"android:textColor="#000000"android:textSize="23sp" /><TextViewandroid:id="@+id/tvGameConn"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:layout_marginTop="30dp"android:clickable="true"android:onClick="gamePlay"android:text="@string/tvGameConn"android:textColor="#000000"android:textSize="23sp" /></LinearLayout>
2.新建GameGuessActivity.java页面和activity_game_guess.xml页面,activity_game_guess.xml页面显示猜成语游戏规则。
activity_game_guess.xml页面:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/bg_animal"android:orientation="vertical" ><TextViewandroid:id="@+id/tvGuessHelp"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:layout_marginTop="60dp"android:text="@string/tvGuessHelp"android:textColor="#000000"android:textSize="25sp" /><!-- android:background="#00ffffff"去掉边缘 --><ImageButtonandroid:id="@+id/ibStartGame"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="150dp"android:layout_marginTop="300dp"android:background="#00ffffff"android:onClick="startGame"android:src="@drawable/btnstart" /></LinearLayout>
3.新建PlayGuessActivity.java页面和activity_play_guess.xml页面,activity_play_guess.xml页面作为猜成语游戏界面。
activity_play_guess.xml页面:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/bg_animal"android:orientation="vertical" ><!-- 始终使TextView显示在中间 --><TextViewandroid:id="@+id/tvExplain"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:layout_marginTop="50dp"android:text="TextView"android:textColor="#000000"android:textSize="22sp" /><TextViewandroid:id="@+id/tvRightOrWrong"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:layout_marginTop="70dp"android:text=" "android:textColor="#08c50d"android:textSize="22sp" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginBottom="60dp"android:orientation="vertical" ><RadioGroupandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical" ><RadioButtonandroid:id="@+id/rbPhraseOne"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="120dp"android:textColor="#000000"android:textSize="20sp" /><RadioButtonandroid:id="@+id/rbPhraseTwo"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="120dp"android:textColor="#000000"android:textSize="20sp" /><RadioButtonandroid:id="@+id/rbPhraseThree"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="120dp"android:textColor="#000000"android:textSize="20sp" /><RadioButtonandroid:id="@+id/rbPhraseFour"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="120dp"android:textColor="#000000"android:textSize="20sp" /></RadioGroup></LinearLayout><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:layout_marginBottom="30dp"android:orientation="horizontal" ><ImageButtonandroid:id="@+id/ibSubmit"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="#00ffffff"android:onClick="AnswerSubmit"android:src="@drawable/btnsubmit" /><ImageButtonandroid:id="@+id/ibNext"android:layout_marginLeft="70dp"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="#00ffffff"android:onClick="AnswerSubmit"android:src="@drawable/btnnext" /></LinearLayout></LinearLayout>
4.GameActivity.java页面跳转每一个游戏页面。
GameActivity.java页面:
package cn.edu.bztc.happyidion.activity;import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;public class GameActivity extends Activity {private Intent intent;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_game);}public void gamePlay(View view) {switch (view.getId()) {case R.id.tvGameGuess:intent=new Intent(this,GameGuessActivity.class);startActivity(intent);break;case R.id.tvGameConn:break;default:break;}}}
5.GameGuessActivity.java页面点击编写开始游戏点击事件。
GameGuessActivity.java页面:
package cn.edu.bztc.happyidion.activity;import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;public class GameGuessActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_game_guess);}public void startGame(View view){switch (view.getId()) {case R.id.ibStartGame:Intent intent=new Intent(this,PlayGuessActivity.class);startActivity(intent);finish();break;default:break;}}
}
6.GameDao.java页面定义游戏中对成语表的查询方法。
GameDao.java页面:
package cn.edu.bztc.happyidiom.dao;import java.util.ArrayList;
import java.util.List;import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import cn.edu.bztc.happyidiom.db.DBOpenHelper;
import cn.edu.bztc.happyidiom.db.MyDatabaseHelper;
import cn.edu.bztc.happyidiom.entity.Animal;public class GameDao {private static GameDao gameDao;private SQLiteDatabase db;public GameDao(Context context){DBOpenHelper dbHelper=new DBOpenHelper(context);db=dbHelper.openDatabase();}public synchronized static GameDao getInstance(Context context){if(gameDao==null){gameDao=new GameDao(context);}return gameDao;}/*根据id获取成语信息*/public Animal getPhrase(String id){Animal animal = null;Cursor cursor=db.query("animal",null,"_id=?",new String[]{id},null,null,null);if(cursor.moveToFirst()){animal=new Animal();animal.setId(cursor.getInt(cursor.getColumnIndex("_id")));animal.setName(cursor.getString(cursor.getColumnIndex("name")));animal.setPronounce(cursor.getString(cursor.getColumnIndex("pronounce")));animal.setAntonym(cursor.getString(cursor.getColumnIndex("antonym")));animal.setHomoionym(cursor.getString(cursor.getColumnIndex("homoionym")));animal.setDerivation(cursor.getString(cursor.getColumnIndex("derivation")));animal.setExamples(cursor.getString(cursor.getColumnIndex("examples")));animal.setExplain(cursor.getString(cursor.getColumnIndex("explain")));}cursor.close();return animal;}
}
7.PlayGuessActivity.java页面为游戏界面。
PlayGuessActivity.java页面:
package cn.edu.bztc.happyidion.activity;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;import cn.edu.bztc.happyidiom.dao.AnimalDao;
import cn.edu.bztc.happyidiom.dao.GameDao;
import cn.edu.bztc.happyidiom.entity.Animal;import android.R.color;
import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.TextView;public class PlayGuessActivity extends Activity {int score=0;// 总成绩int number=1;// 题目数目private TextView tvRightOrWrong;private ImageButton ibNext,ibSubmit;private Random random;// 随机数private AnimalDao animalDao;private GameDao gameDao;private List<Animal> animalList;private Animal animal, animalTwo, animalThree, animalFour;// 随机生成的成语对象private SharedPreferences pref;private Editor editor;private int[] array = { 5, 5, 5, 5 };// 整形数组,用于生成随机顺序显示选项private String answer;// 选择的答案private TextView tvExplain;private RadioButton rbPhraseOne, rbPhraseTwo, rbPhraseThree, rbPhraseFour;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_play_guess);tvExplain = (TextView) findViewById(R.id.tvExplain);rbPhraseOne = (RadioButton) findViewById(R.id.rbPhraseOne);rbPhraseTwo = (RadioButton) findViewById(R.id.rbPhraseTwo);rbPhraseThree = (RadioButton) findViewById(R.id.rbPhraseThree);rbPhraseFour = (RadioButton) findViewById(R.id.rbPhraseFour);tvRightOrWrong = (TextView) findViewById(R.id.tvRightOrWrong);ibSubmit=(ImageButton) findViewById(R.id.ibSubmit);ibNext=(ImageButton) findViewById(R.id.ibNext);pref = getSharedPreferences("FourPhrase", MODE_PRIVATE);// 将正确成语和随机生成的三个成语保存到文档editor = pref.edit();// 获取SharedPreferences.Editor对象/* 获取随机值对应的成语解释 */getNumber();/* 显示解释到TextView */showPhrase();/* 获取三个随机成语和一个正确成语的选项 */getFourPhrase();/* 显示到RadioButton */showRadiButton();}/* 获取随机值对应的成语 */private void getNumber() {animalList = new ArrayList<Animal>();animalDao = AnimalDao.getInstance(this);animalList = animalDao.getAllAnimals();random = new Random();String id = Integer.toString(random.nextInt(animalList.size()));gameDao = GameDao.getInstance(this);animal = gameDao.getPhrase(id);animalList = new ArrayList<Animal>();animalDao = AnimalDao.getInstance(this);animalList = animalDao.getAllAnimals();}/* 显示解释到页面 */private void showPhrase() {tvExplain.setText(animal.getExplain());}/* 获取四个成语选项 */private void getFourPhrase() {random = new Random();String two = null, three = null, four = null;two = Integer.toString(random.nextInt(animalList.size()));three = Integer.toString(random.nextInt(animalList.size()));four = Integer.toString(random.nextInt(animalList.size()));gameDao = GameDao.getInstance(this);animalTwo = gameDao.getPhrase(two); // 获取随机生成值对应的成语animalThree = gameDao.getPhrase(three);animalFour = gameDao.getPhrase(four);editor.putString("0", animal.getName());// 将四个成语存入文档editor.putString("1", animalTwo.getName());editor.putString("2", animalThree.getName());editor.putString("3", animalFour.getName());editor.commit();int no;for (int i = 0; i < array.length; i++) { // 随机生成四个不重复的值作为显示顺序no = random.nextInt(4);int j;for (j = 0; j <= i; j++) {if (no == array[j]) {i--;break;}}if (j == i + 1) {array[i] = no;}}Log.i("MainActivity", "0:" + array[0] + "1:" + array[1] + "2:"+ array[2] + "3:" + array[3]);}/* 显示到RadioButton */private void showRadiButton() {rbPhraseOne.setText(pref.getString(Integer.toString(array[0]), ""));rbPhraseTwo.setText(pref.getString(Integer.toString(array[1]), ""));rbPhraseThree.setText(pref.getString(Integer.toString(array[2]), ""));rbPhraseFour.setText(pref.getString(Integer.toString(array[3]), ""));}/* 提交答案 */public void AnswerSubmit(View view) {switch (view.getId()) {case R.id.ibSubmit:if (rbPhraseOne.isChecked())answer = rbPhraseOne.getText().toString();else if (rbPhraseTwo.isChecked())answer = rbPhraseTwo.getText().toString();else if (rbPhraseThree.isChecked())answer = rbPhraseThree.getText().toString();elseanswer = rbPhraseFour.getText().toString();if (answer == animal.getName()) {tvRightOrWrong.setText("真棒,回答正确");tvRightOrWrong.setTextColor(Color.rgb(7, 200, 12));ibSubmit.setClickable(false);      //回答正确或错误,提交按钮不能被点击,防止一个一个尝试获取答案加分score+=10;} else {tvRightOrWrong.setText("啊偶,回答错了");tvRightOrWrong.setTextColor(Color.rgb(255, 00, 00));ibSubmit.setClickable(false);}if(score==60&&number<=10){tvRightOrWrong.setText("恭喜,闯关成功");tvRightOrWrong.setTextColor(Color.rgb(7, 200, 12));ibNext.setClickable(false);      //闯关成功或失败,下一道题的按钮不能被点击}else if(score<=60&&number>=10){tvRightOrWrong.setText("抱歉,闯关失败");tvRightOrWrong.setTextColor(Color.rgb(255, 00, 00));ibNext.setClickable(false);}break;case R.id.ibNext:   //生成下一道题clearPhrase();//清空TextView和RadioButton属性getNumber();    showPhrase();getFourPhrase();showRadiButton();number+=1;  //题目数目加一break;}}public void clearPhrase(){     //跳到下一道题,删除之前控件的属性tvRightOrWrong.setText(" ");      //清空提示语ibSubmit.setClickable(true);    //提交按钮可以点击rbPhraseOne.setChecked(false);    //单选按钮都不被选中rbPhraseTwo.setChecked(false);rbPhraseThree.setChecked(false);rbPhraseFour.setChecked(false);}
}
8.源码分享: 点击打开链接
9.时间原因页面并没有美化,界面布局也有些问题,暂时只能提供简单学习,请谅解。

Android小程序-乐学成语游戏(四)相关推荐

  1. Android小程序-乐学成语背景音乐(五)

    目标效果: 第五个帮助页面改为设置页面,添加switch控件,播放背景音乐. 1.创建SetActivity.java页面和activity_set.xml页面,activity_set.xml页面放 ...

  2. Android 小程序APP成语字典课程设计

     内容及要求: 1.设计一个成语字典APP,要求此APP能够在手机上准确的看到成语的释义,并且能正确跳转. 2.设计主要的显示页面布局. 3.实现成语字典中添加成语的功能,删除成语的功能. 4.在 ...

  3. android简单小程序学成语,分享3个成语游戏小程序,让你学习游戏两不误

    原标题:分享3个成语游戏小程序,让你学习游戏两不误 1.杜甫教成语 杜甫教成语,一个轻松简单的学成语小程序,是一个猜字型的小程序,是一个考验我们的成语词库深度的小程序.杜甫教成语其实是一个既可以玩游戏 ...

  4. 视频教程-老司机讲前端之微信小程序开发成语消消乐游戏视频课程-微信开发

    老司机讲前端之微信小程序开发成语消消乐游戏视频课程 中国实战派HTML5培训第一人,微软技术讲师,曾任百合网技术总监,博看文思HTML5总监.陶国荣长期致力于HTML5.JavaScript.CSS3 ...

  5. 老司机讲前端之微信小程序开发成语消消乐游戏视频课程-陶国荣-专题视频课程...

    老司机讲前端之微信小程序开发成语消消乐游戏视频课程-102人已学习 课程介绍         本课通过一个完整.真实的游戏项目,带着学员手动开发代码,本课分项目介绍.界面效果.技术分析.代码实现.打包 ...

  6. 微信小程序(第二十四章)- 数据交互前置

    微信小程序(第二十四章)- 数据交互前置 讲解微信小程序前置的原因 参考文档 理解微信小程序 小程序简介 作用 提问 针对提问--uni-app介绍 小程序和普通网页开发的区别 小程序代码构成 文件个 ...

  7. Andriod小程序——简单制作游戏中控制任务移动的轮盘

    Andriod小程序--简单制作游戏中控制人物移动的轮盘 说明 自定义自己的view继承于View类 重写onDraw()方法 当我们看到这个控件的时候那个样子,如图 完善onDraw()方法 重写O ...

  8. H5网页游戏(js),游戏小程序,网页游戏引擎

    采用了Three.js作为3D引擎库,结合tween.js作为配套的动画库,以及基于webpack的脚手架nowa链接作为技术支持.市面上比较成熟的3D库有Three.js和Babylon.js. B ...

  9. 用eclipse开发android小程序,【转】Eclipse 开发Android小程序遇到的问题总结

    用Eclipse 开发Android小程序遇到些小问题,在一边学习的过程中,将遇到的问题就记录一下,方面挺杂的,有关于程序出错的,关于linux应用的,有eclipse设置等- 1.ERROR: Ap ...

最新文章

  1. sql sever avg保留小数_《数据库系统概念》笔记 (一)SQL
  2. [图解教程]Axis2与Eclipse整合开发Web Service之二:WSDL逆向生成服务端
  3. UIMenuController的使用,对UILabel拷贝以及定制菜单
  4. 最详细的FPN论文笔记
  5. LeetCode1.两数之和
  6. WinDbg使用摘要
  7. php伪静态后301,php伪静态htaccess实现301重定向方法
  8. 第七次全国人口普查公报[1](第六号) ——人口受教育情况
  9. win10电脑chm文件打不开的解决方法
  10. 【每日一题】一起冲击蓝桥杯吧——Day1【蓝桥真题】
  11. linux 模拟误码率,误码率BER计算原理及仿真输出
  12. 我的同事们(四): Sang Shin
  13. 职工科研项目管理系统的设计与实现附代码
  14. Java 基于mail.jar 和 activation.jar 封装的邮件发送工具类
  15. Panda3D 初学者教程(一)
  16. APP提现之微信服务号红包
  17. 小程序仿朋友圈、表白墙应用实现简单的发表功能
  18. 银行降转账额度 “余额宝”要当心了
  19. 文正机械电子工程专业课_机械电子工程专业解读_机械电子工程专业介绍_机械电子工程专业开设课程-高考圈...
  20. 东北大学软件学院2021-2022软件需求设计与分析五子棋作业

热门文章

  1. IMX6U开发板设置固定ip
  2. 全景AR增强监视系统对接SkeyeIVMS视频云管控系统实现软硬件资源的健康状态管理(二)
  3. Windows下隐藏文件的若干种方法
  4. 前端学习第三阶段-第3章 WebAPI编程
  5. 毕业一年的菜鸟某东面试总结
  6. Duplicate Photos Sweeper for mac (重复照片查找删除工具)
  7. 滋补品微商怎么通过微博引流?微商们如何抓住其中得到商机?
  8. 怎样在Safari中禁用自动播放视频?
  9. outlook 脱机通讯录_使用Outlook 2003中的通讯组列表节省时间
  10. 哈夫曼树制作压缩软件 【此后无良辰】