1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > 【Android实例】打地鼠小游戏

【Android实例】打地鼠小游戏

时间:2019-12-14 22:40:25

相关推荐

【Android实例】打地鼠小游戏

实现结果

代码实现

playmouse.java

package com.example.playmouse;import android.content.pm.ActivityInfo;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.util.Log;import android.view.MotionEvent;import android.view.View;import android.view.WindowManager;import android.widget.ImageView;import android.widget.TextView;import android.widget.Toast;import androidx.annotation.NonNull;import androidx.appcompat.app.AppCompatActivity;import java.util.Random;public class playmouse extends AppCompatActivity {/************1.定义变量、对象、洞穴坐标******************/private int i=0;//记录打到的地鼠个数private ImageView mouse;//定义 mouse 对象private TextView info1; //定义 info1 对象(用于查看洞穴坐标)private Handler handler;//声明一个 Handler 对象public int[][] position=new int[][]{{277, 200}, {535, 200}, {832, 200},{1067,200}, {1328, 200}, {285, 360},{645, 360}, {1014,360}, {1348, 360},{319, 600},{764, 600},{1229,600}};//创建一个表示地鼠位置的数组 @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置不显示顶部栏setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//设置横屏模式/************2.绑定控件*****************/mouse = (ImageView) findViewById(R.id.imageView1);info1 = findViewById(R.id.info);/************获取洞穴位置*****************///通过 logcat 查看 【注】:getRawY():触摸点距离屏幕上方的长度(此长度包括程序项目名栏的)info1.setOnTouchListener(new View.OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {switch (event.getAction()) {case MotionEvent.ACTION_DOWN:float x = event.getRawX();float y = event.getRawY();Log.i("x:" + x, "y:" + y);break;default:break;}return false;}});/************3.实现地鼠随机出现*****************///创建 Handler 消息处理机制handler = new Handler() {@Overridepublic void handleMessage(@NonNull Message msg) {//需要处理的消息int index;if (msg.what == 0x101) {index = msg.arg1; 获取位置索引值mouse.setX(position[index][0]);//设置 X 轴坐标mouse.setY(position[index][1]);//设置 Y 轴坐标(原点为屏幕左上角(不包括程序名称栏))mouse.setVisibility(View.VISIBLE);//设置地鼠显示}super.handleMessage(msg);}};// 创建线程Thread t = new Thread(new Runnable() {@Overridepublic void run() {int index = 0;// 定义一个记录地鼠位置的索引值while (!Thread.currentThread().isInterrupted()) {index = new Random().nextInt(position.length);// 产生一个随机整数(范围:0<=index<数组长度)Message m = handler.obtainMessage();//创建消息对象m.what = 0x101;//设置消息标志m.arg1 = index;// 保存地鼠标位置的索引值handler.sendMessage(m);// 发送消息通知 Handler 处理try {Thread.sleep(new Random().nextInt(500) + 500); // 休眠一段时间} catch (InterruptedException e) {e.printStackTrace();}}}});t.start();/************4.实现点击地鼠后的事件:让地鼠不显示&显示消息*****************/// 添加触摸 mouse 后的事件mouse.setOnTouchListener(new View.OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {v.setVisibility(View.INVISIBLE);//设置地鼠不显示i++;Toast.makeText(playmouse.this, "打到[ " + i + " ]只地鼠!",Toast.LENGTH_SHORT).show(); // 显示消息提示框return false;}});}}

activity_main.xml

<FrameLayout xmlns:android="/apk/res/android"android:id="@+id/fl"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/background"><ImageViewandroid:id="@+id/imageView1"android:layout_width="72dp"android:layout_height="72dp"android:src="@drawable/mouse1"/><TextViewandroid:id="@+id/info"android:layout_width="fill_parent"android:layout_height="fill_parent"/></FrameLayout>

styles.xml(把顶部通知栏去掉)

<resources><!-- Base application theme. --><style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"><!-- Customize your theme here. --><item name="colorPrimary">@color/colorPrimary</item><item name="colorPrimaryDark">@color/colorPrimaryDark</item><item name="colorAccent">@color/colorAccent</item></style></resources>

图片资源

background.jpg

mouse.png

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。