android小游戏代码

1.MainActivity.java类中的代码:

package com.example.administrator.youxi;

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

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

    TextView tvTarget;
    TextView tvScore;
    TextView tvIndex;
    TextView goTarget;
    TextView tvNumber;
    Button btnOk;
    Button btnHelp;
    Button btnReplay;
    SeekBar sbBulsseye;
    SeekBar goBulsseye;
    int number;
    int rand;
    int index = 0;
    Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findView();//初始化变量
        context = this;
        rand = new Random().nextInt(100);
        goTarget.setText("请把数字拖动到:"+ rand);
        tvIndex.setText("局数:"+index);
        //设置难度
        setsbBulsseye();
        //开始按钮
        setbtnOk();
        //重置按钮
        setbtnReplay();
        //帮助按钮
        setbtnHelp();
   ;

    }
    public void setsbBulsseye(){
        //设置sbBulsseye大小 0 — 100;
        sbBulsseye.setProgress(0);
        sbBulsseye.setMax(10);
        //通过sbBulsseye监听达到拖动改变进度值
        sbBulsseye.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            //拖动条进度改变的时候调用
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                number = sbBulsseye.getProgress();
                tvTarget.setText("60分以上看美女,美女展示时间:"+(number+1)+"秒");
            }
            //拖动条开始拖动的时候调用
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                number = sbBulsseye.getProgress();
                tvTarget.setText("60分以上看美女,美女展示时间:"+(number+1)+"秒");
            }
            //拖动条停止拖动的时候调用
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                number = sbBulsseye.getProgress();
                tvTarget.setText("60分以上看美女,美女展示时间:"+(number+1)+"秒");
            }
        });
    }
    public void setbtnOk(){
        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                index++;
                int pro = goBulsseye.getProgress();
                int total =(int) (100-Math.abs(pro - rand) - Math.abs(pro - rand)*number);
                if(total>60){
                    Intent intent = new Intent(MainActivity.this,ImageActivity.class);
                    /* 通过Bundle对象存储需要传递的数据 */
                    Bundle bundle = new Bundle();
                    /*字符、字符串、布尔、字节数组、浮点数等等,都可以传*/
     //               bundle.putString("time", "feng88724");
                    bundle.putInt("times",number);
                    bundle.putBoolean("image", true);
                    /*把bundle对象assign给Intent*/
                    intent.putExtras(bundle);
                    startActivity(intent);
                }else{
                    AlertDialog.Builder builder = new AlertDialog.Builder(context);
                    builder.setTitle("很遗憾").
                            setMessage("您得分太低,无法观看美女,可尝试缩短美女展示时间降低难度!").
                            setPositiveButton("确认", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            });
                    AlertDialog dialog = builder.create();
                    dialog.show();
                }
//                tvScore.setText("当前分数:"+total);
                tvNumber.setText(String.valueOf(total));
                tvIndex.setText("局数:"+index);
                rand = new Random().nextInt(100);
                goTarget.setText("请把数字拖动到:"+ rand);
            }
        });
    }
    public void setbtnReplay(){
        btnReplay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goBulsseye.setProgress(0);
                tvNumber.setText("0");
                tvIndex.setText("局数:"+0);
                rand = new Random().nextInt(100);
                goTarget.setText("请把数字拖动到:"+ rand);
                index = 0;
            }
        });
    }
    public void setbtnHelp(){
        btnHelp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setTitle("帮助").
                        setMessage("获得60分以方可跳转观看美女,图片显示时间越长,难度越高!").
                        setPositiveButton("确认", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                });
                AlertDialog dialog = builder.create();
                dialog.show();
            }
        });
    }

    private void findView() {
        tvTarget = this.findViewById(R.id.tv_target);
        tvScore = findViewById(R.id.tv_score);
        tvIndex = findViewById(R.id.tv_index);
        goTarget = findViewById(R.id.go_target);
        btnOk = findViewById(R.id.btn_ok);
        btnHelp = findViewById(R.id.btn_help);
        btnReplay = findViewById(R.id.btn_replay);
        sbBulsseye = findViewById(R.id.sb_bulsseye);
        goBulsseye = findViewById(R.id.go_bulsseye);
        tvNumber = findViewById(R.id.tv_number);
    }
}
2.activity_main.xml中代码:
<?xml version="1.0" encoding="utf-8"?>
<!-- 定义布局方向:垂直排列 android:orientation="vertical"-->
<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity"
    android:background="#D5D5D5">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center">
        <TextView
            android:id="@+id/tv_target"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="60分以上看美女,美女展示时间:1秒" />
        <SeekBar
            android:id="@+id/sb_bulsseye"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:thumb="@mipmap/a15" />
        <TextView
            android:id="@+id/go_target"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="请把数字拖动到:"
            android:textColor="#FF22A7"
            android:textSize="20sp"/>
    </LinearLayout>
    <LinearLayout
        android:orientation="vertical"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
        <SeekBar
            android:id="@+id/go_bulsseye"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:thumb="@mipmap/a4"
            style="@style/CustomSeekbarStyle"/>
        <Button
            android:id="@+id/btn_ok"
            android:text="确定"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#888888"/>

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center">
            <Button
                android:id="@+id/btn_replay"
                android:text="重置"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#888888"/>
            <TextView
                android:id="@+id/tv_score"
                android:text="分数:  "
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <TextView
                android:id="@+id/tv_number"
                android:text="0"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="30sp"
                android:textColor="#FF22A7"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center">
            <Button
                android:id="@+id/btn_help"
                android:text="帮助"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#888888"/>

            <TextView
                android:id="@+id/tv_index"
                android:text="局数"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

3.ImageActivity.java类中的代码:

package com.example.administrator.youxi;

import androidx.appcompat.app.AppCompatActivity;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.widget.ImageView;
import android.widget.TextView;


import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

public class ImageActivity extends AppCompatActivity {
    private ImageView imageId;
    private int rand;
    private Context mContext;
    private int times;
    private TextView plan_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image);
        mContext = this;
        imageId = findViewById(R.id.imge_id);
        plan_id = findViewById(R.id.plan_id);
        rand = new Random().nextInt(40);
        String name = "b"+rand;
        imageId.setImageResource(getResources().getIdentifier(
                name, "drawable",
                mContext.getPackageName()));

        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                finish();
                /*Intent intent = new Intent(ImageActivity.this,MainActivity.class);
                startActivity(intent);*/
            }
        };
        /*获取Intent中的Bundle对象*/
        Bundle bundle = this.getIntent().getExtras();;

        /*获取Bundle中的数据,注意类型和key*/
        times  = bundle.getInt("times");
        boolean ismale = bundle.getBoolean("Ismale");
        times++;
        times = times*1000;
        CountDownTimer timers = new CountDownTimer(times, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                plan_id.setEnabled(false);
                plan_id.setText((millisUntilFinished+1000) / 1000 + "秒");

            }

            @Override
            public void onFinish() {
                plan_id.setEnabled(true);
            }
        }.start();
   //     plan_id.setText(times/1000+"秒");
        Timer timer = new Timer();
        timer.schedule(task, times);


    }

}

4.activity_image.xml中的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="horizontal"
    tools:context=".ImageActivity">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:layout_weight="1">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="0dp"
            android:layout_marginLeft="50dp">
            <TextView
                android:id="@+id/plan_id"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="15秒"
                android:textColor="#1111EE"
                android:gravity="center"/>
        </LinearLayout>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center">
        <ImageView
            android:id="@+id/imge_id"
            android:src="@mipmap/b1"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
</LinearLayout>

5.AndroidMainfest.xml中的代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.administrator.youxi">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/a4"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/a4"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <meta-data
            android:name="com.google.android.actions"
            android:resource="@drawable/seekbar_progress_drawable" />

        <activity
            android:name=".MainActivity"
            android:screenOrientation="landscape">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".ImageActivity">
        </activity>
    </application>

</manifest>

该代码与apk仅供参考,请勿发布与上架。这里是上传小游戏百度云盘代码:https://pan.baidu.com/s/1_3DqtjR323FkPBmWkzPi_Q

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值