系列文章
【Android】移动端设备介绍(工业手持机)
本文链接:https://blog.csdn.net/youcheng_ge/article/details/130604517
【Android】开发”打地鼠“小游戏
本文链接:https://blog.csdn.net/youcheng_ge/article/details/129398048
【H5】avalon前端数据双向绑定
本文链接:https://blog.csdn.net/youcheng_ge/article/details/131067187
【H5】安卓APP自动升级方案(最全)
本文链接:https://blog.csdn.net/youcheng_ge/article/details/126759498
【H5】移动端,常见界面布局模板
本文链接:https://blog.csdn.net/youcheng_ge/article/details/131229989
【Java】『蓝桥杯』10道编程题及答案(一)
本文链接:https://blog.csdn.net/youcheng_ge/article/details/130223115
前言
关注我论坛的可能知道,上期我提及要制作另一个Android程序的计划,经过我的努力和各位网友的帮助下,今日我终于完成了任务。现写下程序的制作过程,以及程序遇到的瓶颈和秘方,一来,与各位编程好友分享;其次就是,丰富我的日志,并提升我的编程水准,牢记问题的解决办法,但这一切都是次要的……
一、项目准备
资源准备工作:我们就要进入游戏的准备工作了,既然我们要让游戏生动形象就必然需要一些图片资源,我们就从百度上获取吧。
我们选择划圈的这张图,用photoshop或者美图秀秀什么的扣两张图,记住背景一定是透明的(关于透明背景图片的制作我会专门撰写)效果如图所示:
二、解决方案
首先,我简述一下程序要实现的功能:本程序非常简单,既然是打地鼠游戏,顾名思义地鼠会随机出现在屏幕的几个位置上,通过手指单击地鼠实现打地鼠的目的,打中一个地鼠会显示击中效果,并加分;打错会弹出错误提示,并扣分。游戏难度会随等级的提高而提高。
三、软件开发(源码展示)
最后就是最关键的代码编写了。
3.1 图片资源拷贝
将图片资源拷贝到res\drawable文件夹下,drawable文件夹自己新建的,我们这个程序对图片要求不高,采用统一的图片。
3.2 鼠洞布局文件activity_main.xml
然后就是我们的布局了,我要初始化显示9个鼠洞,activity_main.xml(采用绝对布局,间距差值100dp)代码如下:
<ImageView
android:id="@+id/img1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="30dp"
android:layout_y="100dp"
android:src="@drawable/img_ds0" />
<ImageView
android:id="@+id/img2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="130dp"
android:layout_y="100dp"
android:src="@drawable/img_ds0" />
<ImageView
android:id="@+id/img3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="230dp"
android:layout_y="100dp"
android:src="@drawable/img_ds0" />
<ImageView
android:id="@+id/img4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="30dp"
android:layout_y="200dp"
android:src="@drawable/img_ds0" />
<ImageView
android:id="@+id/img5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="130dp"
android:layout_y="200dp"
android:src="@drawable/img_ds0" />
<ImageView
android:id="@+id/img6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="230dp"
android:layout_y="200dp"
android:src="@drawable/img_ds0" />
<ImageView
android:id="@+id/img7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="30dp"
android:layout_y="300dp"
android:src="@drawable/img_ds0" />
<ImageView
android:id="@+id/img8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="130dp"
android:layout_y="300dp"
android:src="@drawable/img_ds0" />
<ImageView
android:id="@+id/img9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="230dp"
android:layout_y="300dp"
android:src="@drawable/img_ds0" />
<TextView
android:id="@+id/txtFen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="11dp"
android:layout_y="14dp"
android:text="得分:"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/txtsmz"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="10dp"
android:layout_y="50dp"
android:text="生命值:" />
<TextView
android:id="@+id/txtwcd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="10dp"
android:layout_y="73dp"
android:text="完成度:" />
<TextView
android:id="@+id/txtShiWu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="128dp"
android:layout_y="13dp"
android:text="失误:"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/txtdj"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="234dp"
android:layout_y="14dp"
android:text="等级:"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/btnKaiGuan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="120dp"
android:layout_y="360dp"
android:text="开始" />
<TextView
android:id="@+id/txtzhizuo"
android:textSize="6pt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="220dp"
android:layout_y="415dp"
android:text="by:春天的故事"/>
3.3 游戏关卡界面布局文件guanka_main.xml
游戏关卡界面布局文件guanka_main.xml代码如下:
<Button
android:id="@+id/btn_up"
android:layout_width="185dp"
android:layout_height="wrap_content"
android:layout_x="50dp"
android:layout_y="30dp"
android:text="上一关" />
<Button
android:id="@+id/btn_next"
android:layout_width="185dp"
android:layout_height="wrap_content"
android:layout_x="50dp"
android:layout_y="90dp"
android:text="下一关" />
<Button
android:id="@+id/btn_null"
android:layout_width="185dp"
android:layout_height="wrap_content"
android:layout_x="50dp"
android:layout_y="150dp"
android:text="返回" />
<TextView
android:id="gk_zhizuozhe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="220"
android:layout_y="430"
android:text="by:春天的故事"/>
3.4 菜单按钮的布局
接下来到菜单按钮的布局书写,其中关卡文件实现就是通过菜单按钮实现:
<item android:id="@+id/menu_tg"
android:title="关卡:1"
android:orderInCategory="100" />
<item android:id="@+id/menu_nd"
android:title="难度"
android:orderInCategory="100" />
3.5 values值的定义
然后是values值的定义如下:
<string name="app_name">打地鼠</string>
<string name="action_settings">Settings</string>
<string name="menu_settings">设置</string>
<string name="title_activity_main">打地鼠(春天的故事制作)</string>
3.6 游戏图标变更AndroidMainfest.xml
游戏图标的更改在AndroidMainfest.xml文件夹:
android:icon="@drawable/icon_dds"
3.7 操控核心代码
以上都是编程的准备工作,是程序实现的前提,最后我们终于来到了最最关键的代码书写了,这里我只建立了一个主类,当然你们也可以拆分开来。
MainActivity.java代码:
public classMainActivity extends Activity {
private long count = 0;
private String stwcd="0%",stsmz="100%",strs="";
private boolean YesOrNo = true, sfdj = false;
private boolean run = false;
private Button btnKaiGuan;
private TextView txtFen,txtwcd,txtsmz,txtdj;
private TextView txtShiWu;
private int num=0, td = 8,tt=0, tdtime = 500, fen = 0, sw = 0,wcd=0,smz=100,dj=1,i=0;
private ImageView img1, img2, img3, img4, img5, img6, img7, img8, img9;
private Handler handler = new Handler();
public void imggb() {
// if(num==0){
// img1.setImageDrawable(getResources().getDrawable(R.drawable.img_ds0));
// img2.setImageDrawable(getResources().getDrawable(R.drawable.img_ds0));
// img3.setImageDrawable(getResources().getDrawable(R.drawable.img_ds0));
// img4.setImageDrawable(getResources().getDrawable(R.drawable.img_ds0));
// img5.setImageDrawable(getResources().getDrawable(R.drawable.img_ds0));
// img6.setImageDrawable(getResources().getDrawable(R.drawable.img_ds0));
// img7.setImageDrawable(getResources().getDrawable(R.drawable.img_ds0));
// img8.setImageDrawable(getResources().getDrawable(R.drawable.img_ds0));
// img9.setImageDrawable(getResources().getDrawable(R.drawable.img_ds0));
// }
if (num == 1) {
img1.setImageDrawable(getResources().getDrawable(R.drawable.img_ds1));
sfdj = true;
}else{
img1.setImageDrawable(getResources().getDrawable(
R.drawable.img_ds0));
}
if (num == 2) {
img2.setImageDrawable(getResources().getDrawable(R.drawable.img_ds1));
sfdj = true;
}else{
img2.setImageDrawable(getResources().getDrawable(
R.drawable.img_ds0));
}
if (num == 3) {
img3.setImageDrawable(getResources().getDrawable(R.drawable.img_ds1));
sfdj = true;
}else{
img3.setImageDrawable(getResources().getDrawable(
R.drawable.img_ds0));
}
if (num == 4) {
img4.setImageDrawable(getResources().getDrawable(R.drawable.img_ds1));
sfdj = true;
}else{
img4.setImageDrawable(getResources().getDrawable(
R.drawable.img_ds0));
}
if (num == 5) {
img5.setImageDrawable(getResources().getDrawable(R.drawable.img_ds1));
sfdj = true;
}else{
img5.setImageDrawable(getResources().getDrawable(
R.drawable.img_ds0));
}
if (num == 6) {
img6.setImageDrawable(getResources().getDrawable(R.drawable.img_ds1));
sfdj = true;
}else{
img6.setImageDrawable(getResources().getDrawable(
R.drawable.img_ds0));
}
if (num == 7) {
img7.setImageDrawable(getResources().getDrawable(R.drawable.img_ds1));
sfdj = true;
}else{
img7.setImageDrawable(getResources().getDrawable(
R.drawable.img_ds0));
}
if (num == 8) {
img8.setImageDrawable(getResources().getDrawable(R.drawable.img_ds1));
sfdj = true;
}else{
img8.setImageDrawable(getResources().getDrawable(
R.drawable.img_ds0));
}
if (num == 9) {
img9.setImageDrawable(getResources().getDrawable(R.drawable.img_ds1));
sfdj = true;
}else{
img9.setImageDrawable(getResources().getDrawable(
R.drawable.img_ds0));
// Toast.makeText(getApplicationContext(),"默认Toast样式",
// Toast.LENGTH_SHORT).show();
}
}
private Runnable task = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
if (run) {
handler.postDelayed(this, tdtime);
count++;
if (smz <= 0) {
run = false;
handler.post(task);
btnKaiGuan.setText("开始");
AlertDialog.BuilderalertDialog = newAlertDialog.Builder(
MainActivity.this);
alertDialog.setTitle("游戏结束");
alertDialog.setMessage("再玩一次吧!");
alertDialog.setPositiveButton("好的", null);
alertDialog.show();
}
if (tt == td) {
num = (int) (Math.random()* 9) + 1;//产生随机数
tt = 0;
if (sfdj) {
fen = fen - 5;
sw++;
smz=smz-10;
}
imggb();
smz=smz-5;
}
tt++;
if(smz>=100){
smz=100;
}
strs="";
for(i=0;i<smz/3;i++){
strs=strs+"/";
}
stsmz=strs+smz+"%";
strs="";
for(i=0;i<wcd/dj/3;i++){
strs=strs+"/";
}
stwcd=strs+wcd/dj+"%";
if(wcd>=100*dj){
tdtime=tdtime*2/3;
dj++;
wcd=0;
smz=100;
}
}
txtFen.setText("得分: " + fen);
txtShiWu.setText("失误:" + sw);
txtdj.setText("等级:"+dj);
txtsmz.setText("生命值:"+stsmz);
txtwcd.setText("完成度:"+stwcd);
}
};
//程序创建方法
@Override
public void onCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img1 = (ImageView)findViewById(R.id.img1);
img2 = (ImageView)findViewById(R.id.img2);
img3 = (ImageView)findViewById(R.id.img3);
img4 = (ImageView)findViewById(R.id.img4);
img5 = (ImageView)findViewById(R.id.img5);
img6 = (ImageView)findViewById(R.id.img6);
img7 = (ImageView)findViewById(R.id.img7);
img8 = (ImageView)findViewById(R.id.img8);
img9 = (ImageView)findViewById(R.id.img9);
btnKaiGuan = (Button)findViewById(R.id.btnKaiGuan);
txtFen = (TextView) this.findViewById(R.id.txtFen);
txtShiWu = (TextView) this.findViewById(R.id.txtShiWu);
txtsmz = (TextView) this.findViewById(R.id.txtsmz);
txtwcd = (TextView) this.findViewById(R.id.txtwcd);
txtdj=(TextView) this.findViewById(R.id.txtdj);
btnKaiGuan.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (smz <= 0) {
num=0; td = 8;tt=0; tdtime = 500; fen = 0; sw = 0;count = 0;wcd=0;smz=100;dj=1;
YesOrNo = true; sfdj = false;
imggb();
YesOrNo = false;
run = true;
handler.postDelayed(task, 1000);
btnKaiGuan.setText("暂停");
}else{
if (YesOrNo) {
YesOrNo = false;
run = true;
handler.postDelayed(task, 1000);
btnKaiGuan.setText("暂停");
}else{
YesOrNo = true;
run = false;
handler.post(task);
btnKaiGuan.setText("开始");
}
}
}
});
img1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (num == 1) {
if (sfdj) {
fen = fen + 10;
sfdj = false;
smz=smz+10;
wcd=wcd+10;
img1.setImageDrawable(getResources().getDrawable(
R.drawable.img_ds2));
}
}else{
// 消息框弹出,失误信息
Toast.makeText(getApplicationContext(),"逗比",
Toast.LENGTH_SHORT).show();
smz=smz-5;
fen = fen - 5;
sw++;
}
}
});
img2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (num == 2) {
if (sfdj) {
fen = fen + 10;
sfdj = false;
smz=smz+10;
wcd=wcd+10;
img2.setImageDrawable(getResources().getDrawable(
R.drawable.img_ds2));
}
}else{
// 消息框弹出,失误信息
Toast.makeText(getApplicationContext(),"逗比",
Toast.LENGTH_SHORT).show();
fen = fen - 5;
sw++;
smz=smz-5;
}
}
});
img3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (num == 3) {
if (sfdj) {
fen = fen + 10;
sfdj = false;
smz=smz+10;
wcd=wcd+10;
img3.setImageDrawable(getResources().getDrawable(
R.drawable.img_ds2));
}
}else{
// 消息框弹出,失误信息
Toast.makeText(getApplicationContext(),"逗比",
Toast.LENGTH_SHORT).show();
fen = fen - 5;
sw++;
smz=smz-5;
}
}
});
img4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (num == 4) {
if (sfdj) {
fen = fen + 10;
sfdj = false;
smz=smz+10;
wcd=wcd+10;
img4.setImageDrawable(getResources().getDrawable(
R.drawable.img_ds2));
}
}else{
// 消息框弹出,失误信息
Toast.makeText(getApplicationContext(),"逗比",
Toast.LENGTH_SHORT).show();
fen = fen - 5;
sw++;
smz=smz-5;
}
}
});
img5.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (num == 5) {
if (sfdj) {
fen = fen + 10;
sfdj = false;
smz=smz+10;
wcd=wcd+10;
img5.setImageDrawable(getResources().getDrawable(
R.drawable.img_ds2));
}
}else{
// 消息框弹出,失误信息
Toast.makeText(getApplicationContext(),"逗比",
Toast.LENGTH_SHORT).show();
fen = fen - 5;
sw++;
smz=smz-5;
}
}
});
img6.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (num == 6) {
if (sfdj) {
fen = fen + 10;
sfdj = false;
smz=smz+10;
wcd=wcd+10;
img6.setImageDrawable(getResources().getDrawable(
R.drawable.img_ds2));
}
}else{
// 消息框弹出,失误信息
Toast.makeText(getApplicationContext(),"逗比",
Toast.LENGTH_SHORT).show();
fen = fen - 5;
sw++;
smz=smz-5;
}
}
});
img7.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (num == 7) {
if (sfdj) {
fen = fen + 10;
sfdj = false;
smz=smz+10;
wcd=wcd+10;
img7.setImageDrawable(getResources().getDrawable(
R.drawable.img_ds2));
}
}else{
// 消息框弹出,失误信息
Toast.makeText(getApplicationContext(),"逗比",
Toast.LENGTH_SHORT).show();
fen = fen - 5;
sw++;
smz=smz-5;
}
}
});
img8.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (num == 8) {
if (sfdj) {
fen = fen + 10;
sfdj = false;
smz=smz+10;
wcd=wcd+10;
img8.setImageDrawable(getResources().getDrawable(
R.drawable.img_ds2));
}
}else{
// 消息框弹出,失误信息
Toast.makeText(getApplicationContext(),"逗比",
Toast.LENGTH_SHORT).show();
fen = fen - 5;
sw++;
smz=smz-5;
}
}
});
img9.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (num == 9) {
if (sfdj) {
fen = fen + 10;
sfdj = false;
smz=smz+10;
wcd=wcd+10;
img9.setImageDrawable(getResources().getDrawable(
R.drawable.img_ds2));
}
}else{
// 消息框弹出,失误信息
Toast.makeText(getApplicationContext(),"逗比",
Toast.LENGTH_SHORT).show();
fen = fen - 5;
sw++;
smz=smz-5;
}
}
});
}
@Override
public booleanonCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(final MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_tg:
item.setTitle("游戏关卡:"+dj);
final AlertDialog alertDialog= newAlertDialog.Builder(
MainActivity.this).create();
alertDialog.setIcon(R.drawable.icon_dds);
LayoutInflaterfactory = LayoutInflater.from(this);
View view = factory.inflate(R.layout.guanka_main, null);//布局文件夹下的关卡,guanka_main文件
final Button btn_up =(Button) view.findViewById(R.id.btn_up);
final Button btn_next =(Button) view.findViewById(R.id.btn_next);
final Button btn_null =(Button) view.findViewById(R.id.btn_null);
btn_up.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(dj==1){
AlertDialog.BuilderalertDialog = newAlertDialog.Builder(MainActivity.this);
alertDialog.setTitle("警告!");
alertDialog.setMessage("已经是第一关了!");
alertDialog.setPositiveButton("取消", null);
alertDialog.show();
}else{
dj--;
tt=0;wcd=0;smz=100;fen=0;sw=0;
tdtime=500;
for(i=0;i<dj;i++){
tdtime=tdtime*2/3;
item.setTitle("关卡:"+dj);
alertDialog.dismiss();
}
}
}
});
btn_next.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dj++;
tt=0;wcd=0;smz=100;fen=0;sw=0;
tdtime=500;
for(i=0;i<dj;i++){
tdtime=tdtime*2/3;
item.setTitle("关卡:"+dj);
alertDialog.dismiss();
}
}
});
btn_null.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
alertDialog.dismiss();
}
});
alertDialog.setView(view);
alertDialog.show();
break;
}
return true;
}
}
四、总结及问题分析
看到这儿相信你的程序已经敲好了,下面的我将谈谈我编程时一直困扰我的一个问题,并给出我的心得。
4.1 关于在Android开发中替换资源图片不起作用的问题
现象:
在android开发中,经常会需要替换res\drawable中的图片,打开res\layout下的文件预览布局页面发现图片已经被替换,但在模拟器或者真实机器上运行时发现该图片并没有被替换,还是使用原来的图片。从注释掉的那段代码中可以看出我在这方面浪费了不少时间(文件重命名快捷键:F2)
原因:
在开发过程中,由于使用模拟器测试了程序,在首次运行后会将res文件夹下的图片资源文件(如drawable-hdpi、drawable-ldpi和drawable-mdpi)拷贝到bin文件夹下。在替换资源图片后,eclipse并不清楚是否有图片改变,所以会使用原来bin下的res文件夹中的资源文件进行打包,而图片用的还是第一次eclipse所拷贝进去的文件,所以当运行程序后会发现替换资源图片在程序中没起作用。
解决方法:
既然已经明白是由于eclipse没有把最新的资源文件拷贝过去,所以我们只需要把项目bin目录下的res目录删除(可连同bin下生成的.apk、.dex和.ap_等三个文件一并删除),然后再编译apk等文件会再次生成。
游戏效果图:
文章来源:https://www.toymoban.com/news/detail-451242.html
五、资源链接
游戏apk下载链接:http://pan.baidu.com/s/1mgoq8UO
希望能对大家有帮助,当然我的程序必然也有些瑕疵与不足,望各位能坚持己见,如果可以的话还望指出我的错误。文章来源地址https://www.toymoban.com/news/detail-451242.html
到了这里,关于【Android】开发”打地鼠“小游戏的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!