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.定義變量、對象、洞穴坐標(biāo)******************/ private int i=0;//記錄打到的地鼠個數(shù) private ImageView mouse;//定義 mouse 對象 private TextView info1; //定義 info1 對象(用于查看洞穴坐標(biāo)) 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} };//創(chuàng)建一個表示地鼠位置的數(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;// 定義一個記錄地鼠位置的索引值while (!Thread.currentThread().isInterrupted()) { index = new Random().nextInt(position.length);// 產(chǎn)生一個隨機(jī)整數(shù)(范圍:0<=index<數(shù)組長度) Message m = handler.obtainMessage();//創(chuàng)建消息對象 m.what = 0x101;//設(shè)置消息標(biāo)志 m.arg1 = index;// 保存地鼠標(biāo)位置的索引值 handler.sendMessage(m);// 發(fā)送消息通知 Handler 處理 try { Thread.sleep(new Random().nextInt(500) + 500); // 休眠一段時間 } 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: android:layout_width='match_parent' android:layout_height='match_parent' android:background='@drawable/background' > <ImageView android: android:layout_width='72dp' android:layout_height='72dp' android:src='http://www.intensediesel.com/bcjs/@drawable/mouse1' /> <TextView android: 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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. idea設(shè)置提示不區(qū)分大小寫的方法2. Java PreparedStatement用法詳解3. IntelliJ IDEA導(dǎo)出項(xiàng)目的方法4. 使用AJAX(包含正則表達(dá)式)驗(yàn)證用戶登錄的步驟5. IDEA 2020.1.2 安裝教程附破解教程詳解6. JS圖片懶加載庫VueLazyLoad詳解7. django queryset相加和篩選教程8. Spring如何集成ibatis項(xiàng)目并實(shí)現(xiàn)dao層基類封裝9. Java利用TCP協(xié)議實(shí)現(xiàn)客戶端與服務(wù)器通信(附通信源碼)10. 利用ajax+php實(shí)現(xiàn)商品價格計(jì)算

網(wǎng)公網(wǎng)安備