Android--打地鼠

该Android应用包含MainActivity和GameActivity两个主要活动。MainActivity中用户输入名字并选择游戏难度,通过Intent传递数据到GameActivity。GameActivity显示游戏界面,有时间限制和得分系统,用户点击地鼠按钮进行游戏。游戏难度不同,地鼠出现的速度也不同。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目结构

 用到的三张图片

mainActivity如下

package com.example.myapplication;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private EditText editText;
    private Button buttonLevel;
    private Button buttonStart;
    private String userName;
    private int gameLevel=1000;//easy=2000,commmon=1000.hard=500ms



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText=findViewById(R.id.editText);
        buttonLevel=findViewById(R.id.buttonLevel);
        buttonStart=findViewById(R.id.buttonStart);

        buttonLevel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //单选对话框
                AlertDialog alertDialog=new AlertDialog.Builder(MainActivity.this)
                        .setTitle("请选择难度:")
                        .setSingleChoiceItems(new String[]{"简单", "普通", "困难"}, 1, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                if(i==0) gameLevel=2000;
                                if(i==1) gameLevel=1000;
                                if(i==2) gameLevel=500;
                                Toast.makeText(MainActivity.this,gameLevel+"",Toast.LENGTH_SHORT).show();//在面包片上测试一下i可以用吗
                            }
                        })//单选
                        .setPositiveButton("确定",null)
                        .create();
                alertDialog.show();

            }
        });
        buttonStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                userName=editText.getText().toString();//用户输入的用户名
                //显式跳转
                Intent intent=new Intent(MainActivity.this,GameActivity.class);
                intent.putExtra("userName",userName);
                intent.putExtra("gameLevel",gameLevel);
                startActivity(intent);

            }
        });
    }
}

 mainlayout如下

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/mousegame"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="100dp"
        android:orientation="vertical">

        <EditText
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textPersonName"
            android:text="Name" />

        <Button
            android:id="@+id/buttonLevel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="游戏难度" />

        <Button
            android:id="@+id/buttonStart"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="游戏开始" />
    </LinearLayout>
</RelativeLayout>

 主界面展示

 

 gameActivity如下

package com.example.myapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

import java.sql.Time;
import java.util.ArrayList;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

public class GameActivity extends AppCompatActivity {

    private TextView textViewName;
    private TextView textViewLevel;
    private TextView textViewScore;
    private Button buttonAgain;
    private ProgressBar progressBar;

    private TableRow tableRow1;
    private TableRow tableRow2;
    private TableRow tableRow3;
    private TableRow tableRow4;

    private String userName;
    private int gameLevel=1000;
    private ArrayList<Button> buttons;
    private int score=0;
    private Random rd;//地鼠随机出现
    private Button currentButton;//指向地鼠的按钮
    private Timer timer;
    private Handler handler;
    private int gameLength=30;//说明每一局30s



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);
        textViewName=findViewById(R.id.textViewName);
        textViewLevel=findViewById(R.id.textViewLevel);
        textViewScore=findViewById(R.id.textViewScore);
        buttonAgain=findViewById(R.id.buttonAgin);
        progressBar=findViewById(R.id.progressBar);
        tableRow1=findViewById(R.id.tableRow1);
        tableRow2=findViewById(R.id.tableRow2);
        tableRow3=findViewById(R.id.tableRow3);
        tableRow4=findViewById(R.id.tableRow4);

        Intent intent=getIntent();
        userName=intent.getStringExtra("userName");
        gameLevel=intent.getIntExtra("gameLevel",1000);
        textViewName.setText(userName);
        //textViewLevel.setText();
        if(gameLevel==2000)textViewLevel.setText("简单");
        if(gameLevel==1000)textViewLevel.setText("普通");
        if(gameLevel==500)textViewLevel.setText("困难");

        buttons=new ArrayList<>();
        rd=new Random();
        addButtonTableRow(tableRow1);
        addButtonTableRow(tableRow2);
        addButtonTableRow(tableRow3);
        addButtonTableRow(tableRow4);
        currentButton=buttons.get(rd.nextInt(16));
        currentButton.setBackgroundResource(R.drawable.mouse);
        setHandler();
        startTimer();




    }

    //给tablerow增加按钮
    private void addButtonTableRow(TableRow tableRow)
    {
        for(int i=0;i<4;i++){
            Button button=new Button(this);
            TableRow.LayoutParams params=new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT,1);//三个参数 宽 高 比重
            button.setLayoutParams(params);
            button.setBackgroundResource(R.drawable.background);
            button.setOnClickListener(new View.OnClickListener() {//打地鼠
                @Override
                public void onClick(View view) {
                    if(view==currentButton)//打中地鼠
                    {
                        score=score+100;
                        textViewScore.setText(score+"分");

                    }
                }
            });
            tableRow.addView(button);
            buttons.add(button);//按钮增加到按钮组里

        }
    }
    private void startTimer()
    {
        timer=new Timer();
        TimerTask timerTask=new TimerTask() {
            @Override
            public void run() {
                Message msg=new Message();//作用:用handler让计时器线程去操作activity线程。计时器发送message给handler。安卓线程的安全机制就是这样的了。
                msg.what=gameLength--;
                handler.sendMessage(msg);

            }
        };
        timer.schedule(timerTask,20,gameLevel);
    }
    private void setHandler()
    {
        handler=new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);
                if(msg.what>0)
                {
                    currentButton.setBackgroundResource(R.drawable.background);
                    currentButton=buttons.get(rd.nextInt(16));
                    currentButton.setBackgroundResource(R.drawable.mouse);
                    progressBar.setProgress((int)(msg.what/30.0*100));
                }
                else{
                    timer.cancel();
                    Toast.makeText(GameActivity.this,"游戏结束!!",Toast.LENGTH_SHORT).show();

                }
            }
        };
    }
}

 gamelayout如下

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".GameActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="0dp"
        android:layout_marginLeft="13dp"

        android:layout_marginTop="0dp"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/textViewName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="TextView" />

            <TextView
                android:id="@+id/textViewLevel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="TextView" />

            <TextView
                android:id="@+id/textViewScore"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="TextView" />

            <Button
                android:id="@+id/buttonAgin"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="再来一局" />
        </LinearLayout>

        <ProgressBar
            android:id="@+id/progressBar"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <TableLayout
        android:layout_width="409dp"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_centerVertical="true"
        android:layout_marginStart="2dp"
        android:layout_marginLeft="2dp"
        android:layout_marginTop="71dp"
        android:layout_marginEnd="0dp"
        android:layout_marginBottom="-11dp">

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" />

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" />

        <TableRow
            android:id="@+id/tableRow3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" />

        <TableRow
            android:id="@+id/tableRow4"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    </TableLayout>
</RelativeLayout>

 运行效果:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值