时间:2021-05-20
本文实例为大家分享了android利用handler实现打地鼠游戏的具体代码,供大家参考,具体内容如下
xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/main_imageview" android:layout_width="80dp" android:layout_height="80dp" android:src="@mipmap/icon" android:visibility="gone" /> <Button android:id="@+id/main_button" android:layout_width="90dp" android:layout_height="50dp" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="30dp" android:text="开始游戏" /> <TextView android:id="@+id/main_textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" /></RelativeLayout>java
package com.tcy.digettetest;import androidx.annotation.NonNull;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.MotionEvent;import android.view.View;import android.widget.Button;import android.widget.ImageView;import android.widget.TextView;import android.widget.Toast;import java.lang.ref.WeakReference;import java.util.IllegalFormatCodePointException;import java.util.Random;public class MainActivity extends AppCompatActivity implements View.OnClickListener, View.OnTouchListener { public static final int CODE = 1001; public static final int MAX_NUMBER = 10; public static final int RANDOM_NUMBER = 500; Button statrGameButton; TextView resultTextView; ImageView saratogaImage; int[][] mPostion = new int[][]{ {342, 180}, {432, 880}, {521, 256}, {429, 780}, {456, 976}, {145, 665}, {123, 678}, {564, 567}, }; int mTotalCount; int mSuccessCount; SaratogaHandler mHandler = new SaratogaHandler(this); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); setTitle("打加加"); } private void initView() { statrGameButton = findViewById(R.id.main_button); resultTextView = findViewById(R.id.main_textview); saratogaImage = findViewById(R.id.main_imageview); statrGameButton.setOnClickListener(this); saratogaImage.setOnTouchListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.main_button: startGame(); break; } } private void startGame() { statrGameButton.setEnabled(false); resultTextView.setText("开始啦"); statrGameButton.setText("游戏中……"); //发送消息 next(0); } public void next(int delayTime) { int position = new Random().nextInt(mPostion.length); Message message = Message.obtain(); message.what = CODE; message.arg1 = position; mHandler.sendMessageDelayed(message, delayTime); mTotalCount++; } @Override public boolean onTouch(View v, MotionEvent event) { v.setVisibility(View.GONE); mSuccessCount++; resultTextView.setText("打到了" + mSuccessCount + "只,共" + MAX_NUMBER + "只."); return false; } public static class SaratogaHandler extends Handler { final WeakReference<MainActivity> weakReference; public SaratogaHandler(MainActivity activity) { this.weakReference = new WeakReference<>(activity); } @Override public void handleMessage(@NonNull Message msg) { super.handleMessage(msg); MainActivity activity = weakReference.get(); switch (msg.what) { case CODE: if (activity.mTotalCount > MAX_NUMBER) { activity.clear(); Toast.makeText(activity, "小加加打完了", Toast.LENGTH_SHORT).show(); return; } int position = msg.arg1; activity.saratogaImage.setX(activity.mPostion[position][0]); activity.saratogaImage.setY(activity.mPostion[position][1]); activity.saratogaImage.setVisibility(View.VISIBLE); int randomTime = new Random().nextInt(RANDOM_NUMBER) + RANDOM_NUMBER; activity.next(randomTime); break; } } } private void clear() { mTotalCount = 0; mSuccessCount = 0; saratogaImage.setVisibility(View.GONE); statrGameButton.setText("点击开始"); statrGameButton.setEnabled(true); }}以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
今天上课老师用Java实现了打地鼠游戏的界面和具体逻辑,那么我也尝试使用Android语言实现其功能。首先是打地鼠游戏的玩法1.每隔1秒或者0.5秒地鼠会出现在
本文实例为大家分享了vue实现打地鼠小游戏的具体代码,供大家参考,具体内容如下效果图如下:代码如下:打地鼠游戏{{n}}号地鼠设置参数速度:总数:开始统计分数面
本文实例讲述了Java实现的打地鼠小游戏。分享给大家供大家参考,具体如下:这里涉及到java线程和GUI的相关知识,一个简单的java小游戏打地鼠,有兴趣的朋友
本文实例为大家分享了vue实现简易打地鼠游戏的具体代码,供大家参考,具体内容如下打地鼠简易版*{margin:0;padding:0;}#main{border
本文实例为大家分享了C++实现打地鼠游戏的具体代码,供大家参考,具体内容如下代码:#includeclassCMyWnd:publicCFrameWnd{pri