首页 > 编程语言 > android利用handler实现打地鼠游戏
2020
11-25

android利用handler实现打地鼠游戏

本文实例为大家分享了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);
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自学编程网。

编程技巧