当前位置:   article > 正文

Android studio 老虎机小游戏_android studio小游戏

android studio小游戏

用 Android studio软件写的一个老虎机小游戏

先上MainActivity.java 的代码。这里我用得定时器,本想用java线程,奈何安卓还不太会,应用会闪退。

  1. package com.example.myapplication;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.os.Handler;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.widget.ImageView;
  8. import android.widget.TextView;
  9. import java.util.Random;
  10. public class MainActivity extends AppCompatActivity {
  11. private ImageView TP1, TP2, TP3;
  12. private Button BTN_START, BTN_FINISH;
  13. private TextView RESULT;
  14. private int[] img = {R.drawable.z1, R.drawable.z2, R.drawable.z3};
  15. Random a = new Random();//随机数
  16. int b, c, d;
  17. Handler handler= new Handler();
  18. Runnable runnable=new Runnable() {
  19. @Override
  20. public void run() {
  21. // TODO Auto-generated method stub
  22. //要做的事情
  23. b = a.nextInt(3);
  24. c = a.nextInt(3);
  25. d = a.nextInt(3);
  26. TP1.setImageResource(img[b]);//放置随机图片
  27. TP2.setImageResource(img[c]);
  28. TP3.setImageResource(img[d]);
  29. handler.postDelayed(this, 20);
  30. }
  31. };
  32. @Override
  33. protected void onCreate(Bundle savedInstanceState) {
  34. super.onCreate(savedInstanceState);
  35. setContentView(R.layout.activity_main);
  36. show();
  37. BTN_START.setOnClickListener(new View.OnClickListener() { //开始按钮监听事件
  38. @Override
  39. public void onClick(View view) {
  40. handler.postDelayed(runnable, 20);//定时器启动
  41. }
  42. });
  43. BTN_FINISH.setOnClickListener(new View.OnClickListener() { //结束按钮监听事件
  44. @Override
  45. public void onClick(View view) {
  46. handler.removeCallbacks(runnable);//定时器结束
  47. if (img[b] == img[c] && img[b] == img[d] && img[c] == img[d] ) {
  48. if(img[b]==img[0]){
  49. RESULT.setText("恭喜您人品大爆发,获得一等奖:三株豌豆射手");}
  50. else if(img[b]==img[1]){
  51. RESULT.setText("恭喜您人品大爆发,获得特等奖:三株玉米投手");
  52. }else if(img[b]==img[2]){
  53. RESULT.setText("恭喜您祖坟冒青烟,获得钻石大礼包");
  54. }
  55. } else
  56. if (img[b] == img[c]) {
  57. if(img[b]==img[0]){
  58. RESULT.setText("恭喜您,获得二等奖:两头豌豆射手");
  59. }
  60. if(img[b]==img[1]){
  61. RESULT.setText("恭喜您,获得二等奖:2株玉米投手");
  62. }
  63. if(img[b]==img[2]){
  64. RESULT.setText("恭喜您,获得二等奖:两颗钻啊!");
  65. }
  66. }
  67. else
  68. if (img[b] == img[d]) {
  69. if (img[b] == img[0]) {
  70. RESULT.setText("恭喜您,获得二等奖:两头豌豆射手");
  71. }
  72. if (img[b] == img[1]) {
  73. RESULT.setText("恭喜您,获得二等奖:2株玉米投手");
  74. }
  75. if (img[b] == img[2]) {
  76. RESULT.setText("恭喜您,获得二等奖:两颗钻啊!");
  77. }
  78. }
  79. else
  80. if (img[c] == img[d]) {
  81. if (img[c] == img[0]) {
  82. RESULT.setText("恭喜您,获得二等奖:两头豌豆射手");
  83. }
  84. if (img[c] == img[1]) {
  85. RESULT.setText("恭喜您,获得二等奖:2株玉米投手");
  86. }
  87. if (img[c] == img[2]) {
  88. RESULT.setText("恭喜您,获得二等奖:两颗钻啊!");
  89. }
  90. }
  91. else {
  92. RESULT.setText("手气也太差了吧!投币再来一次吧。");
  93. }
  94. }
  95. });
  96. }
  97. private void show() {
  98. TP1 = findViewById(R.id.tp1);
  99. TP2 = findViewById(R.id.tp2);
  100. TP3 = findViewById(R.id.tp3);
  101. BTN_START = findViewById(R.id.btn_start);
  102. BTN_FINISH = findViewById(R.id.btn_finish);
  103. RESULT = findViewById(R.id.result);
  104. }
  105. }

在activity_main.xml 放置布局。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:app="http://schemas.android.com/apk/res-auto"
  5. xmlns:tools="http://schemas.android.com/tools"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. android:orientation="vertical"
  9. android:background="@drawable/bj"
  10. tools:context=".MainActivity">
  11. <TextView
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:text="老虎机"
  15. android:textSize="50dp"
  16. android:layout_gravity="center"
  17. android:textColor="#00ff00"
  18. android:layout_marginTop="8dp"
  19. ></TextView>
  20. <TableRow
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:layout_marginLeft="5dp"
  24. android:paddingTop="40dp"
  25. >
  26. <ImageView
  27. android:id="@+id/tp1"
  28. android:layout_width="120dp"
  29. android:layout_height="120dp"
  30. android:src="@drawable/z1"
  31. android:layout_marginLeft="10dp"
  32. ></ImageView>
  33. <ImageView
  34. android:id="@+id/tp2"
  35. android:layout_width="120dp"
  36. android:layout_height="120dp"
  37. android:src="@drawable/z3"
  38. android:layout_marginLeft="15dp"
  39. ></ImageView>
  40. <ImageView
  41. android:id="@+id/tp3"
  42. android:layout_marginLeft="15dp"
  43. android:layout_width="120dp"
  44. android:layout_height="120dp"
  45. android:src="@drawable/z2"
  46. ></ImageView>
  47. </TableRow>
  48. <TableRow
  49. android:layout_width="wrap_content"
  50. android:layout_height="wrap_content"
  51. android:layout_gravity="center"
  52. android:paddingTop="40dp"
  53. >
  54. <Button
  55. android:id="@+id/btn_start"
  56. android:layout_width="wrap_content"
  57. android:layout_height="wrap_content"
  58. android:layout_marginLeft="30dp"
  59. android:text="开始"
  60. android:textSize="40dp"
  61. ></Button>
  62. <Button
  63. android:id="@+id/btn_finish"
  64. android:layout_width="wrap_content"
  65. android:layout_height="wrap_content"
  66. android:layout_marginLeft="40dp"
  67. android:text="结束"
  68. android:textSize="40dp"
  69. ></Button>
  70. </TableRow>
  71. <TextView
  72. android:id="@+id/result"
  73. android:layout_width="match_parent"
  74. android:layout_height="wrap_content"
  75. android:layout_marginTop="35dp"
  76. android:text="抽奖结果:"
  77. android:textColor="@color/white"
  78. android:textSize="30dp"
  79. ></TextView>
  80. </LinearLayout>

图片资源我就不给了,效果如下图

 

最后效果:视频太大

附上几张图,点击开始图片不断切换,点击结束按纽判断结果。

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/264688?site
推荐阅读
相关标签
  

闽ICP备14008679号