Android實現打地鼠小游戲
本文實例為大家分享了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} };//創建一個表示地鼠位置的數組 @Override protected 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() { @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.實現地鼠隨機出現*****************/ //創建 Handler 消息處理機制 handler = new Handler() { @Override public 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() { @Override public 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() { @Override public 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='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://m.cgvv.com.cn/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
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章:
