Android實現(xiàn)老虎機小游戲代碼示例
用?Android studio軟件寫的一個老虎機小游戲
先上MainActivity.java 的代碼。這里我用得定時器,本想用java線程,奈何安卓還不太會,應用會閃退。
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private ImageView TP1, TP2, TP3;
private Button BTN_START, BTN_FINISH;
private TextView RESULT;
private int[] img = {R.drawable.z1, R.drawable.z2, R.drawable.z3};
Random a = new Random();//隨機數(shù)
int b, c, d;
Handler handler= new Handler();
Runnable runnable=new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
//要做的事情
b = a.nextInt(3);
c = a.nextInt(3);
d = a.nextInt(3);
TP1.setImageResource(img[b]);//放置隨機圖片
TP2.setImageResource(img[c]);
TP3.setImageResource(img[d]);
handler.postDelayed(this, 20);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
show();
BTN_START.setOnClickListener(new View.OnClickListener() { //開始按鈕監(jiān)聽事件
@Override
public void onClick(View view) {
handler.postDelayed(runnable, 20);//定時器啟動
}
});
BTN_FINISH.setOnClickListener(new View.OnClickListener() { //結束按鈕監(jiān)聽事件
@Override
public void onClick(View view) {
handler.removeCallbacks(runnable);//定時器結束
if (img[b] == img[c] && img[b] == img[d] && img[c] == img[d] ) {
if(img[b]==img[0]){
RESULT.setText("恭喜您人品大爆發(fā),獲得一等獎:三株豌豆射手");}
else if(img[b]==img[1]){
RESULT.setText("恭喜您人品大爆發(fā),獲得特等獎:三株玉米投手");
}else if(img[b]==img[2]){
RESULT.setText("恭喜您祖墳冒青煙,獲得鉆石大禮包");
}
} else
if (img[b] == img[c]) {
if(img[b]==img[0]){
RESULT.setText("恭喜您,獲得二等獎:兩頭豌豆射手");
}
if(img[b]==img[1]){
RESULT.setText("恭喜您,獲得二等獎:2株玉米投手");
}
if(img[b]==img[2]){
RESULT.setText("恭喜您,獲得二等獎:兩顆鉆??!");
}
}
else
if (img[b] == img[d]) {
if (img[b] == img[0]) {
RESULT.setText("恭喜您,獲得二等獎:兩頭豌豆射手");
}
if (img[b] == img[1]) {
RESULT.setText("恭喜您,獲得二等獎:2株玉米投手");
}
if (img[b] == img[2]) {
RESULT.setText("恭喜您,獲得二等獎:兩顆鉆??!");
}
}
else
if (img[c] == img[d]) {
if (img[c] == img[0]) {
RESULT.setText("恭喜您,獲得二等獎:兩頭豌豆射手");
}
if (img[c] == img[1]) {
RESULT.setText("恭喜您,獲得二等獎:2株玉米投手");
}
if (img[c] == img[2]) {
RESULT.setText("恭喜您,獲得二等獎:兩顆鉆??!");
}
}
else {
RESULT.setText("手氣也太差了吧!投幣再來一次吧。");
}
}
});
}
private void show() {
TP1 = findViewById(R.id.tp1);
TP2 = findViewById(R.id.tp2);
TP3 = findViewById(R.id.tp3);
BTN_START = findViewById(R.id.btn_start);
BTN_FINISH = findViewById(R.id.btn_finish);
RESULT = findViewById(R.id.result);
}
}
在activity_main.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="vertical"
android:background="@drawable/bj"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="老虎機"
android:textSize="50dp"
android:layout_gravity="center"
android:textColor="#00ff00"
android:layout_marginTop="8dp"
></TextView>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:paddingTop="40dp"
>
<ImageView
android:id="@+id/tp1"
android:layout_width="120dp"
android:layout_height="120dp"
android:src="@drawable/z1"
android:layout_marginLeft="10dp"
></ImageView>
<ImageView
android:id="@+id/tp2"
android:layout_width="120dp"
android:layout_height="120dp"
android:src="@drawable/z3"
android:layout_marginLeft="15dp"
></ImageView>
<ImageView
android:id="@+id/tp3"
android:layout_marginLeft="15dp"
android:layout_width="120dp"
android:layout_height="120dp"
android:src="@drawable/z2"
></ImageView>
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingTop="40dp"
>
<Button
android:id="@+id/btn_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:text="開始"
android:textSize="40dp"
></Button>
<Button
android:id="@+id/btn_finish"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:text="結束"
android:textSize="40dp"
></Button>
</TableRow>
<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="35dp"
android:text="抽獎結果:"
android:textColor="@color/white"
android:textSize="30dp"
></TextView>
</LinearLayout>
圖片資源我就不給了,效果如下圖

最后效果:視頻太大
附上幾張圖,點擊開始圖片不斷切換,點擊結束按紐判斷結果。

?到此這篇關于Android實現(xiàn)老虎機小游戲代碼示例的文章就介紹到這了,更多相關Android老虎機小游戲內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Android 自定義對話框 showSetPwdDialog
這篇文章主要介紹了Android 自定義對話框 showSetPwdDialog的相關資料,需要的朋友可以參考下2016-03-03
android使用PopupWindow實現(xiàn)頁面點擊頂部彈出下拉菜單
這篇文章主要給大家介紹android使用PopupWindow實現(xiàn)頁面點擊頂部彈出下拉菜單,實現(xiàn)此功能主要通過PopupWindow方法,代碼也很簡單,需要的朋友可以參考下2015-08-08
Android 運用@JvmName解決函數(shù)簽名沖突問題詳解
JvmName注解是Kotlin提供的一個可以變更編譯器輸出的注解,這里簡單的介紹一下其使用規(guī)則,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2022-07-07
Android 單例模式實現(xiàn)可復用數(shù)據(jù)存儲的詳細過程
本文介紹了如何使用單例模式實現(xiàn)一個可復用的數(shù)據(jù)存儲類,該類可以存儲不同類型的數(shù)據(jù),并提供統(tǒng)一的接口來訪問這些數(shù)據(jù),通過雙重檢查鎖定機制,該類在多線程環(huán)境下是線程安全的,感興趣的朋友跟隨小編一起看看吧2025-02-02
Android開發(fā)之a(chǎn)ndroid_gps定位服務簡單實現(xiàn)
這篇文章主要介紹了Android開發(fā)之a(chǎn)ndroid_gps定位服務簡單實現(xiàn) ,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-04-04
Notification消息通知 自定義消息通知內(nèi)容布局
這篇文章主要為大家詳細介紹了Notification消息通知,消息合并且顯示條數(shù),自定義消息通知內(nèi)容布局,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-09-09

