西安**大学
安卓游戏课程设计报告书
Android益智游戏
一、题目:
设计一个Android游戏(Been there done that)
社交益智游戏一直很流行,它可以使大家熟悉众多的Android SDK的功能。
这是一个有关旅行的社交益智游戏。在这个游戏中,将向用户询问一些有关旅行和相关经历的问题。例如:
你见过埃及的金字塔吗?
你挤过牛奶吗?
你与海豚一起潜过水吗?
二、基本功能:
1、显示应用程序的名称、版本和开发人员的启动屏幕。
2、解释游戏规则:显示文本文件解释如何玩游戏。
3、游戏介绍:在帮助中以文本形式提供游戏的介绍。
4、游戏向用户提出一系列问题,并记录用户做出肯定回答的次数。对于每一个问题,都将显示文本和图形。例如,游戏屏幕可能显示一张山脉图片,询问用户是否爬过山,并记录用户的答案:yes或者NO。
5、可以退出游戏。
三、功能模块图:
四、系统流程图:
五、游戏运行图:
1、游戏主界面:
2、游戏规则界面与帮助界面:
3、游戏界面:(包含三个问题界面)
4、结果页面(根据选择可显示三种不同结果)
六、源代码:
1、Java代码(实现页面跳转,按钮功能,传值的功能的Avtivity类)
(1)、主界面:(实现游戏开始、规则、帮助页面的跳转,并实现退出功能)
package com.luman.luman;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button button01;
private Button button02;
private Button button03;
private Button button04;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button01 = (Button)findViewById(R.id.begin);
button02 = (Button)findViewById(R.id.rule);
button03 = (Button)findViewById(R.id.help);
button04 = (Button)findViewById(R.id.tuichuId);
button01.setOnClickListener(new MyButtonListener01());
button02.setOnClickListener(new MyButtonListener02());
button03.setOnClickListener(new MyButtonListener03());
button04.setOnClickListener(new MyButtonListener04());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
class MyButtonListener01 implements OnClickListener{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(MainActivity.this, Activity01.class);
MainActivity.this.startActivity(intent);
}
}
class MyButtonListener02 implements OnClickListener{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(MainActivity.this, Other01.class);
MainActivity.this.startActivity(intent);
}
}
class MyButtonListener03 implements OnClickListener{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(MainActivity.this, Other02.class);
MainActivity.this.startActivity(intent);
}
}
class MyButtonListener04 implements OnClickListener{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
finish();
}
}
}
(2)游戏界面:(包含三个问题界面与一个结果界面,并传递数据,结果界面可实现返回主界面功能)
1>问题一:
package com.luman.luman;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class Activity01 extends Activity{
private RadioGroup radioGroup;
private RadioButton YesButton;
private RadioButton NoButton;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity01);
button = (Button)findViewById(R.id.quedingId);
button.setOnClickListener(new MyListener());
radioGroup = (RadioGroup)findViewById(R.id.radioGroupId);
YesButton = (RadioButton)findViewById(R.id.YesId);
NoButton = (RadioButton)findViewById(R.id.NoId);
RadioGroupListener listener = new RadioGroupListener();
radioGroup.setOnCheckedChangeListener(listener);
}
class RadioGroupListener implements OnCheckedChangeListener{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if(checkedId == YesButton.getId()){
((First)getApplication()).setScore1();
}
else if(checkedId == NoButton.getId()){
((First)getApplication()).setScore2();
}
}
}
class MyListener implements OnClickListener{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(Activity01.this, Activity02.class);
Activity01.this.startActivity(intent);
}
}
}
2>问题二:
package com.luman.luman;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class Activity02 extends Activity{
private RadioGroup radioGroup;
private RadioButton YesButton;
private RadioButton NoButton;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity02);
button = (Button)findViewById(R.id.quedingId);
button.setOnClickListener(new MyListener());
radioGroup = (RadioGroup)findViewById(R.id.radioGroupId);
YesButton = (RadioButton)findViewById(R.id.YesId);
NoButton = (RadioButton)findViewById(R.id.NoId);
RadioGroupListener listener = new RadioGroupListener();
radioGroup.setOnCheckedChangeListener(listener);
}
class RadioGroupListener implements OnCheckedChangeListener{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if(checkedId == YesButton.getId()){
((First)getApplication()).setScore1();
}
else if(checkedId == NoButton.getId()){
((First)getApplication()).setScore2();
}
}
}
class MyListener implements OnClickListener{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(Activity02.this, Activity03.class);
Activity02.this.startActivity(intent);
}
}
}
3>问题三:
package com.luman.luman;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class Activity03 extends Activity{
private RadioGroup radioGroup;
private RadioButton YesButton;
private RadioButton NoButton;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity03);
button = (Button)findViewById(R.id.quedingId);
button.setOnClickListener(new MyListener());
radioGroup = (RadioGroup)findViewById(R.id.radioGroupId);
YesButton = (RadioButton)findViewById(R.id.YesId);
NoButton = (RadioButton)findViewById(R.id.NoId);
RadioGroupListener listener = new RadioGroupListener();
radioGroup.setOnCheckedChangeListener(listener);
}
class RadioGroupListener implements OnCheckedChangeListener{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if(checkedId == YesButton.getId()){
((First)getApplication()).setScore1();
}
else if(checkedId == NoButton.getId()){
((First)getApplication()).setScore2();
}
}
}
class MyListener implements OnClickListener{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(Activity03.this, Other03.class);
Activity03.this.startActivity(intent);
}
}
}
4>结果界面:
package com.luman.luman;
import com.luman.luman.MainActivity.MyButtonListener01;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Other03 extends Activity{
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other03);
button = (Button)findViewById(R.id.fanhuiId);
button.setOnClickListener(new MyButtonListener());
int score=((First)getApplicationContext()).getScore();
TextView result = (TextView)findViewById(R.id.resultId);
if(score == 3) result.setText("年轻人应该多尝试有意思的事情,不要让自己总是宅在家里啦!");
if(score == 21) result.setText("劳逸结合,不错哦,继续保持这种生活习惯,越活越年轻哦!");
if(score == 12) result.setText("劳逸结合,不错哦,继续保持这种生活习惯,越活越年轻哦!");
if(score == 30) result.setText("朋友,你太爱玩啦,偶尔也要停下来休息休息哦!");
}
class MyButtonListener implements OnClickListener{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(Other03.this, MainActivity.class);
Other03.this.startActivity(intent);
}
}
}
(3)、全局变量的定义:
package com.luman.luman;
import android.app.Application;
publicclass First extends Application{
privateint score=0;
publicint getScore(){
return score;
}
publicvoid setScore1(){
//aredPreference pre = new SharedPreferene();
score=score+10;
}
publicvoid setScore2(){
score=score+1;
}
}
2、界面.xml代码(共7个界面)
(1)、主界面
<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:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="@drawable/girl"
>
<Button
android:id="@+id/begin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp"
android:layout_marginTop="40dp"
android:text="开始游戏"
/>
<Button
android:id="@+id/rule"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp"
android:layout_marginTop="30dp"
android:text="游戏规则"
/>
<Button
android:id="@+id/help"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp"
android:layout_marginTop="30dp"
android:text="帮助"
/>
<Button
android:id="@+id/tuichuId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp"
android:layout_marginTop="30dp"
android:text="退出游戏"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="30dp"
android:text="@string/edition"
/>
</LinearLayout>
(2)、游戏界面
<1>、问题一:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/jinzita"
>
<TextView
android:id="@+id/question01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="10dp"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp"
android:layout_marginTop="50dp"
android:textSize="20sp"
android:text="你见过埃及的金字塔吗?"
/>
<RadioGroup
android:id="@+id/radioGroupId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/YesId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="30dp"
android:layout_marginTop="30dp"
android:text="Yes"
/>
<RadioButton
android:id="@+id/NoId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="50dp"
android:layout_marginTop="30dp"
android:text="No"
/>"
</RadioGroup>
<Button
android:id="@+id/quedingId"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp"
android:layout_marginTop="40dp"
android:text="确定"
/>
</LinearLayout>
<2>、问题二:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:scaleType="centerCrop"
android:background="@drawable/jiniunai1"
>
<TextView
android:id="@+id/question02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp"
android:layout_marginTop="50dp"
android:textSize="20sp"
android:text="你挤过牛奶吗?"
/>
<RadioGroup
android:id="@+id/radioGroupId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/YesId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="30dp"
android:layout_marginTop="30dp"
android:text="Yes"
/>
<RadioButton
android:id="@+id/NoId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="30dp"
android:layout_marginTop="30dp"
android:text="No"
/>"
</RadioGroup>
<Button
android:id="@+id/quedingId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp"
android:layout_marginTop="40dp"
android:text="确定"
/>
</LinearLayout>
<3>、问题三:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/qianshui"
>
<TextView
android:id="@+id/question03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp"
android:layout_marginTop="50dp"
android:textSize="20sp"
android:text="你有没有和海豚一起潜水呢?"
/>
<RadioGroup
android:id="@+id/radioGroupId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/YesId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="30dp"
android:layout_marginTop="30dp"
android:text="Yes"
/>
<RadioButton
android:id="@+id/NoId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="50dp"
android:layout_marginTop="30dp"
android:text="No"
/>"
</RadioGroup>
<Button
android:id="@+id/quedingId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp"
android:layout_marginTop="40dp"
android:text="确定"
/>
</LinearLayout>
<4>、结果:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/xiao"
>
<TextView
android:id="@+id/helpId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp"
android:layout_marginTop="50dp"
android:textSize="20sp"
android:text="你的测试结果如下,此结果仅供参考:"
/>
<TextView
android:id="@+id/resultId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp"
android:layout_marginTop="50dp"
android:textSize="20sp"
/>
<Button
android:id="@+id/fanhuiId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp"
android:layout_marginTop="60dp"
android:text="返回主界面"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="20dp"
android:layout_marginTop="60dp"
android:text="@string/edition"
/>
</LinearLayout>
(3)、游戏规则界面:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/xiao"
>
<TextView
android:id="@+id/ruleId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp"
android:layout_marginTop="50dp"
android:textSize="20sp"
android:text="本游戏总共 不超过5道问题,请你根据自身情况进行选择,是选Yes,不是选No,选择好都按确定按钮,答题结束后,我们将为您提供测试的结果供您参考,谢谢使用! "
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="20dp"
android:layout_marginTop="60dp"
android:text="@string/edition"
/>
</LinearLayout>
(4)、帮助界面
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/xiao"
>
<TextView
android:id="@+id/helpId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp"
android:layout_marginTop="100dp"
android:textSize="20sp"
android:text="本游戏为益智类问答游戏,通过简单的问答,我们会为你提供建议,用以帮助你更健康的生活"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="20dp"
android:layout_marginTop="100dp"
android:text="@string/edition"
/>
</LinearLayout>
3、AndroidManifest.xml文件配置:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.luman.luman"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:name="First" >
<activity
android:name="com.luman.luman.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="Activity01" android:label="@string/label"/>
<activity android:name="Activity02" android:label="@string/label"/>
<activity android:name="Activity03" android:label="@string/label"/>
<activity android:name="Other01" android:label="@string/a"/>
<activity android:name="Other02" android:label="@string/b"/>
<activity android:name="Other03" android:label="@string/c"/>
</application>
</manifest>
七、总结:
第一次接触安卓平台上的开发,从一无所知到渐渐的有了眉目,其中的过程是繁复却有趣的。
因为安卓平台上的开发是以Java语言为基础,所以我意识到熟练的掌握Java语言才是实现在该平台上应用程序开发最重要的一点。本次课程设计是实现一个问答类的益智型游戏,在代码实现上并不难,主要通过实现OnClickListener或OnClickChangeListener对按钮进行监听、Intent方法进行页面跳转等来实现Activity的各种功能;在界面布局上,要使用xml语言进行布局,这里主要涉及了界面布局的一些知识,我是通过Mars的安卓开发视频边学习边完成的,因为需要做的界面比较简单,所以这里主要使用的是线性布局。
最后程序运行,各功能模块基本能够实现,界面也比较友好。
在编写该游戏的代码时,我遇见了两个问题,一是数据传输和存储,二是游戏退出模块的设置。首先说下数据的传输存储问题,最初是用Intent来传输数据的,因为对Intent方法的使用不够透彻,发现数据没有实现正常的传输,后来通过设置一个继承了Application的类解决了数据在不同Activity中传输的问题。数据的存储因为时间紧迫没有实现,思路是可通过关联数据库或者使用SharedPreferene()对程序进行的改进,这个以待课下完成;在设置游戏退出模块时,首先启动时可正常实现游戏的退出功能,但当程序全部完成进行测试时发现,游戏结束返回主界面后,该功能出现异常,通过分析,该异常可能是因为从结果页面返回主界面时为其产生了新的对象,这个问题也待课下解决。
总的来说,通过这次的课程设计,接触了安卓平台上的应用程序开发,也收获到了宝贵的经验和成果,程序虽然有一些Bug还需要继续修改,不过基本完成了该游戏的基本功能,成果比较满意。
八、参考资料:
《Java语言程序设计》 作者:朱晓龙 北京邮电大学出版社
《软件工程导论(第5版)》 作者:张海藩 清华大学出版社
安卓开发视频 作者:Mars陈