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: 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.hdgsjgj.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
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. nestjs實(shí)現(xiàn)圖形校驗(yàn)和單點(diǎn)登錄的示例代碼2. html小技巧之td,div標(biāo)簽里內(nèi)容不換行3. 以PHP代碼為實(shí)例詳解RabbitMQ消息隊(duì)列中間件的6種模式4. laravel ajax curd 搜索登錄判斷功能的實(shí)現(xiàn)5. Python 如何將integer轉(zhuǎn)化為羅馬數(shù)(3999以內(nèi))6. python實(shí)現(xiàn)自動(dòng)化辦公郵件合并功能7. 將properties文件的配置設(shè)置為整個(gè)Web應(yīng)用的全局變量實(shí)現(xiàn)方法8. python開發(fā)飛機(jī)大戰(zhàn)游戲9. Echarts通過dataset數(shù)據(jù)集實(shí)現(xiàn)創(chuàng)建單軸散點(diǎn)圖10. css進(jìn)階學(xué)習(xí) 選擇符
