android小游戏代码
16lz
2021-12-04
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"?>
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"?>
5.AndroidMainfest.xml中的代码:
<?xml version="1.0" encoding="utf-8"?>
该代码与apk仅供参考,请勿发布与上架。这里是上传小游戏百度云盘代码:
https://pan.baidu.com/s/1_3DqtjR323FkPBmWkzPi_Q
更多相关文章
- 模拟头条
- Android(安卓)SeekBar(拖动条)
- Android中TabHost的使用
- Android(安卓)WebView文件上传(关键代码)
- android,java代码设置背景色
- Android(安卓)widget使用
- Android全屏设置的两种方式
- android ViewPager 竖向滑动
- Android数据库编程教程