Android-小游戏

Android:打地鼠游戏

前端界面(布局文件) :TableLayout(表格布局)+TableRow(行)+TextView(文本框)+ImageView(图片框)
java代码:Handler(消息处理)+Runnable(建子线程)+setOnClickListener(开始结束按钮监听事件)+CountDownTimer(倒计时类)
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//设置横屏

布局文件
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:layout_marginLeft="30dp">

    <TableRow>

    <TextView
        android:id="@+id/tvCount"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="打到个数:" />

    <ImageView
        android:background="#0000"
        android:id="@+id/img12"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/d1"/>

    <ImageView
        android:background="#0000"
        android:id="@+id/img13"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/d1"/>

    <ImageView
        android:background="#0000"
        android:id="@+id/img14"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/d1"/>

        <ImageView
            android:background="#0000"
            android:id="@+id/img15"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/d1"/>

    <TextView
        android:id="@+id/tvRemainTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="倒计时:" />
</TableRow>



    <TableRow>

        <ImageView
            android:id="@+id/img21"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/d1"/>

        <ImageView
            android:id="@+id/img22"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/d1"/>

        <ImageView
            android:id="@+id/img23"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/d1"/>

        <ImageView
            android:id="@+id/img24"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/d1"/>

        <ImageView
            android:id="@+id/img25"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/d1"/>
        <ImageView
            android:id="@+id/img26"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/d1"/>
    </TableRow>


    <TableRow>

        <ImageView
            android:id="@+id/img31"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/d1"/>

        <ImageView
            android:id="@+id/img32"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/d1"/>

        <ImageView
            android:id="@+id/img33"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/d1"/>

        <ImageView
            android:id="@+id/img34"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/d1"/>

        <ImageView
            android:id="@+id/img35"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/d1"/>
        <ImageView
            android:id="@+id/img36"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/d1"/>
    </TableRow>

    <TableRow>

        <ImageView
            android:id="@+id/img41"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/d1"/>

        <ImageView
            android:id="@+id/img42"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/d1"/>

        <ImageView
            android:id="@+id/img43"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/d1"/>

        <ImageView
            android:id="@+id/img44"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/d1"/>

        <ImageView
            android:id="@+id/img45"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/d1"/>

        <ImageView
            android:id="@+id/img46"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/d1"/>
    </TableRow>


    <TableRow>

        <Button
            android:id="@+id/btnPlay"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="开始"/>

        <ImageView
            android:id="@+id/img52"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/d1"/>

        <ImageView
            android:id="@+id/img53"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/d1"/>
        <ImageView
            android:id="@+id/img54"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/d1"/>

        <ImageView
            android:id="@+id/img55"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/d1"/>

        <Button
            android:id="@+id/btnFinish"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="结束"/>
    </TableRow>


</TableLayout>
Java代码
package com.example.qq.myfirstgame;

import android.content.pm.ActivityInfo;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    //声明组件
    private TextView tvCount, tvRemainTime;
    private Button btnPlay, btnFinish;
    private ImageView[] images = new ImageView[26];
    public int[] ids = {R.id.img12, R.id.img13, R.id.img14, R.id.img15,
            R.id.img21, R.id.img22, R.id.img23, R.id.img24, R.id.img25, R.id.img26,
            R.id.img31, R.id.img32, R.id.img33, R.id.img34, R.id.img35, R.id.img36,
            R.id.img41, R.id.img42, R.id.img43, R.id.img44, R.id.img45, R.id.img46,
            R.id.img52, R.id.img53, R.id.img54, R.id.img55};

    //flag判断游戏是否结束,count记录打中地鼠的个数,oldID,newID记录位置
    boolean flag = true;
    int count = 0;
    int oldID = 0, newID = 0;
    //子线程与主线程通过Handler来进行通信。子线程可以通过Handler来通知主线程进行UI更新
    Handler handler = new Handler() {//创建一个Handler,消息处理,在handler中捕获所需消息,实现响应
        @Override
        //设置地洞,地鼠随机出洞
        public void handleMessage(Message message) {
            images[oldID].setImageResource(R.mipmap.d1);//设置背景图片为地洞
            newID = (int) (Math.random() * 26);//地鼠随机出洞位置
            images[newID].setImageResource(R.mipmap.d3);//设置背景图片为地鼠
            oldID=newID;

        }
    };

    //创建子线程用于数秒,计时器
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            while (flag) {
                try {
                    Message message = Message.obtain();//当前的Handler中获取指定的Message以供再次使用,得到msg不会产生内存开销
                    handler.sendMessage(message);//从子线程中发出消息
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game);
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//设置横屏
        initView();//初始化页面
        //按钮,组件绑定监听事件
        btnPlay.setOnClickListener(this);
        btnFinish.setOnClickListener(this);
        for (int j = 0; j < images.length; j++) {
            images[j].setOnClickListener(this);
        }

    }


    @Override
    public void onClick(View v) {//按钮重玩/开始的监听方法重写
        switch (v.getId()) {
            case R.id.btnPlay://若单击开始按钮就开始倒计时(新建线程)
                new MyCount(20 * 1000, 1000).start();
                Thread thread1 = new Thread(runnable);
                thread1.start();
                flag=true;
                break;


            case R.id.btnFinish://若单击重玩按钮就就开始倒计时(新建线程)
                images[oldID].setBackgroundResource(R.mipmap.d1);
                flag=false;
                break;

            default://游戏未结束时,点击到有地鼠的图片位置击中个数就+1
                if (images[oldID].getId() == v.getId() && flag == true) {
                    count++;
                    tvCount.setText("击中数:" + count);
                }
        }
    }

    //倒计时功能
    private class MyCount extends CountDownTimer {//继承倒计时类并重写倒计时方法
        public MyCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long l) {//每个时间间隔会回调一次(上面设置的是1秒)
            long hour = l / 1000 / 3600;
            long minute = l / 1000 % 3600 / 60;
            long second = l / 1000 % 3600 % 60;
            tvRemainTime.setText("倒计时" + hour + ":" + minute + ":" + second);
        }

        @Override
        public void onFinish() {//在整个计时器结束之后回调该方法,并将flag置为false,游戏结束。
            tvRemainTime.setText("游戏时间到了!");
            flag = false;
            images[oldID].setBackgroundResource(R.mipmap.d1);//图片背景置为地洞
            count=0;

        }
    }

    public void initView(){
        //初始化组件
        tvCount= (TextView) this.findViewById(R.id.tvCount);
        tvRemainTime=(TextView) this.findViewById(R.id.tvRemainTime);
        btnPlay=(Button)this.findViewById(R.id.btnPlay);
        btnFinish=(Button)this.findViewById(R.id.btnFinish);
        for(int i=0;i<images.length;i++){
            images[i]=(ImageView)this.findViewById(ids[i]);
        }
    }
}

运行效果图

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值