Android實(shí)現(xiàn)打地鼠小游戲
本文實(shí)例為大家分享了Android實(shí)現(xiàn)打地鼠小游戲的具體代碼,供大家參考,具體內(nèi)容如下
實(shí)現(xiàn)結(jié)果

代碼實(shí)現(xiàn)
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.定義變量、對(duì)象、洞穴坐標(biāo)******************/
private int i=0;//記錄打到的地鼠個(gè)數(shù)
private ImageView mouse;//定義 mouse 對(duì)象
private TextView info1; //定義 info1 對(duì)象(用于查看洞穴坐標(biāo))
private Handler handler;//聲明一個(gè) Handler 對(duì)象
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}
};//創(chuàng)建一個(gè)表示地鼠位置的數(shù)組 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);//設(shè)置不顯示頂部欄
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//設(shè)置橫屏模式
/************2.綁定控件*****************/
mouse = (ImageView) findViewById(R.id.imageView1);
info1 = findViewById(R.id.info);
/************獲取洞穴位置*****************/
//通過(guò) logcat 查看 【注】:getRawY():觸摸點(diǎn)距離屏幕上方的長(zhǎng)度(此長(zhǎng)度包括程序項(xiàng)目名欄的)
info1.setOnTouchListener(new View.OnTouchListener() {
@Override
public 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.實(shí)現(xiàn)地鼠隨機(jī)出現(xiàn)*****************/
//創(chuàng)建 Handler 消息處理機(jī)制
handler = new Handler() {
@Override
public void handleMessage(@NonNull Message msg) {
//需要處理的消息
int index;
if (msg.what == 0x101) {
index = msg.arg1;//// 獲取位置索引值
mouse.setX(position[index][0]);//設(shè)置 X 軸坐標(biāo)
mouse.setY(position[index][1]);//設(shè)置 Y 軸坐標(biāo)(原點(diǎn)為屏幕左上角(不包括程序名稱欄))
mouse.setVisibility(View.VISIBLE);//設(shè)置地鼠顯示
}
super.handleMessage(msg);
}
};
// 創(chuàng)建線程
Thread t = new Thread(new Runnable() {
@Override
public void run() {
int index = 0;// 定義一個(gè)記錄地鼠位置的索引值
while (!Thread.currentThread().isInterrupted()) {
index = new Random().nextInt(position.length);// 產(chǎn)生一個(gè)隨機(jī)整數(shù)(范圍:0<=index<數(shù)組長(zhǎng)度)
Message m = handler.obtainMessage();//創(chuàng)建消息對(duì)象
m.what = 0x101;//設(shè)置消息標(biāo)志
m.arg1 = index;// 保存地鼠標(biāo)位置的索引值
handler.sendMessage(m);// 發(fā)送消息通知 Handler 處理
try {
Thread.sleep(new Random().nextInt(500) + 500); // 休眠一段時(shí)間
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
t.start();
/************4.實(shí)現(xiàn)點(diǎn)擊地鼠后的事件:讓地鼠不顯示&顯示消息*****************/
// 添加觸摸 mouse 后的事件
mouse.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
v.setVisibility(View.INVISIBLE);//設(shè)置地鼠不顯示
i++;
Toast.makeText(playmouse.this, "打到[ " + i + " ]只地鼠!",
Toast.LENGTH_SHORT).show(); // 顯示消息提示框
return false;
}
});
}}
activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
>
<ImageView
android:id="@+id/imageView1"
android:layout_width="72dp"
android:layout_height="72dp"
android:src="@drawable/mouse1"
/>
<TextView
android: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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android 修改viewpage滑動(dòng)速度的實(shí)現(xiàn)代碼
由于Viewpager的滑動(dòng)速度是固定的,所以很頭疼,下面小編通過(guò)實(shí)例代碼給大家分享android 修改viewpage滑動(dòng)速度的方法,需要的朋友參考下吧2017-09-09
Android實(shí)現(xiàn)關(guān)機(jī)后數(shù)據(jù)不會(huì)丟失問(wèn)題
這篇文章主要介紹了Android實(shí)現(xiàn)關(guān)機(jī)后數(shù)據(jù)不會(huì)丟失問(wèn)題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10
Android消息機(jī)制Handler用法總結(jié)
這篇文章介紹了Android消息機(jī)制Handler用法總結(jié),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-11-11
Android10自動(dòng)連接WiFi問(wèn)題的解決
這篇文章主要介紹了Android10自動(dòng)連接WiFi問(wèn)題的解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
Android編程學(xué)習(xí)之異步加載圖片的方法
這篇文章主要介紹了Android編程學(xué)習(xí)之異步加載圖片的方法,以實(shí)例形式較為詳細(xì)的分析了Android異步加載圖片所涉及的頁(yè)面布局及功能實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
Android實(shí)現(xiàn)簡(jiǎn)單計(jì)時(shí)器功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)單計(jì)時(shí)器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-10-10
Android開(kāi)發(fā)之對(duì)話框案例詳解(五種對(duì)話框)
本文通過(guò)實(shí)例代碼給大家分享了5種android對(duì)話框,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-09-09
android實(shí)現(xiàn)系統(tǒng)信息推送
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)系統(tǒng)信息推送,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04

