Android實現打地鼠小遊戲
阿新 • • 發佈:2020-04-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,{832,{1067,{1328,{285,360},{645,{1014,{1348,{319,600},{764,{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: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
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。