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);
/************獲取洞穴位置*****************/
//通過 logcat 查看 【注】:getRawY():觸摸點(diǎn)距離屏幕上方的長度(此長度包括程序項(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ù)組長度)
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

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用Android studio查看Kotlin的字節(jié)碼教程
這篇文章主要介紹了使用Android studio查看Kotlin的字節(jié)碼教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Android應(yīng)用中使用ViewPager實(shí)現(xiàn)類似QQ的界面切換效果
這篇文章主要介紹了Android應(yīng)用中使用ViewPager實(shí)現(xiàn)類似QQ的界面切換效果的示例,文中的例子重寫了PagerAdapter,并且講解了如何解決Android下ViewPager和PagerAdapter中調(diào)用notifyDataSetChanged失效的問題,需要的朋友可以參考下2016-03-03
Android自定義View實(shí)現(xiàn)可以拖拽的GridView
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)可以拖拽的GridView,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06
Android實(shí)現(xiàn)視頻播放--騰訊瀏覽服務(wù)(TBS)功能
TBS視頻播放器可以支持市面上幾乎所有的視頻格式,包括mp4, flv, avi, 3gp, webm, ts, ogv, m3u8, asf, wmv, rm, rmvb, mov, mkv等18種視頻格式。這篇文章主要介紹了Android實(shí)現(xiàn)視頻播放--騰訊瀏覽服務(wù)(TBS),需要的朋友可以參考下2018-07-07
Android實(shí)現(xiàn)底部帶刻度的進(jìn)度條樣式
由于公司需要一個(gè)帶刻度的進(jìn)度條樣式,因?yàn)榭潭刃枰獎(jiǎng)討B(tài)去改變,所以換背景圖片的方案肯定是不行的,唯一的辦法就是自己繪制一個(gè)進(jìn)度條,下面小編給大家?guī)砹薃ndroid實(shí)現(xiàn)底部帶刻度的進(jìn)度條樣式及實(shí)例代碼,需要的朋友參考下吧2019-10-10
Android聊天工具基于socket實(shí)現(xiàn)
這篇文章主要介紹了基于socket實(shí)現(xiàn)的一個(gè)簡單的Android聊天工具,實(shí)現(xiàn)方法簡單,具有一定的參考價(jià)值,感興趣的朋友可以參考一下2016-02-02
Android自定義ToolBar并實(shí)現(xiàn)沉浸式的方法
這篇文章主要給大家介紹了關(guān)于Android自定義ToolBar并實(shí)現(xiàn)沉浸式的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05

