Android實(shí)現(xiàn)老虎機(jī)小游戲代碼示例
用?Android studio軟件寫的一個(gè)老虎機(jī)小游戲
先上MainActivity.java 的代碼。這里我用得定時(shí)器,本想用java線程,奈何安卓還不太會(huì),應(yīng)用會(huì)閃退。
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();//隨機(jī)數(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]);//放置隨機(jī)圖片
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);//定時(shí)器啟動(dòng)
}
});
BTN_FINISH.setOnClickListener(new View.OnClickListener() { //結(jié)束按鈕監(jiān)聽事件
@Override
public void onClick(View view) {
handler.removeCallbacks(runnable);//定時(shí)器結(jié)束
if (img[b] == img[c] && img[b] == img[d] && img[c] == img[d] ) {
if(img[b]==img[0]){
RESULT.setText("恭喜您人品大爆發(fā),獲得一等獎(jiǎng):三株豌豆射手");}
else if(img[b]==img[1]){
RESULT.setText("恭喜您人品大爆發(fā),獲得特等獎(jiǎng):三株玉米投手");
}else if(img[b]==img[2]){
RESULT.setText("恭喜您祖墳冒青煙,獲得鉆石大禮包");
}
} else
if (img[b] == img[c]) {
if(img[b]==img[0]){
RESULT.setText("恭喜您,獲得二等獎(jiǎng):兩頭豌豆射手");
}
if(img[b]==img[1]){
RESULT.setText("恭喜您,獲得二等獎(jiǎng):2株玉米投手");
}
if(img[b]==img[2]){
RESULT.setText("恭喜您,獲得二等獎(jiǎng):兩顆鉆啊!");
}
}
else
if (img[b] == img[d]) {
if (img[b] == img[0]) {
RESULT.setText("恭喜您,獲得二等獎(jiǎng):兩頭豌豆射手");
}
if (img[b] == img[1]) {
RESULT.setText("恭喜您,獲得二等獎(jiǎng):2株玉米投手");
}
if (img[b] == img[2]) {
RESULT.setText("恭喜您,獲得二等獎(jiǎng):兩顆鉆啊!");
}
}
else
if (img[c] == img[d]) {
if (img[c] == img[0]) {
RESULT.setText("恭喜您,獲得二等獎(jiǎng):兩頭豌豆射手");
}
if (img[c] == img[1]) {
RESULT.setText("恭喜您,獲得二等獎(jiǎng):2株玉米投手");
}
if (img[c] == img[2]) {
RESULT.setText("恭喜您,獲得二等獎(jiǎng):兩顆鉆??!");
}
}
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="老虎機(jī)"
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="結(jié)束"
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="抽獎(jiǎng)結(jié)果:"
android:textColor="@color/white"
android:textSize="30dp"
></TextView>
</LinearLayout>
圖片資源我就不給了,效果如下圖

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

?到此這篇關(guān)于Android實(shí)現(xiàn)老虎機(jī)小游戲代碼示例的文章就介紹到這了,更多相關(guān)Android老虎機(jī)小游戲內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android編程實(shí)現(xiàn)webview將網(wǎng)頁打包成apk的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)webview將網(wǎng)頁打包成apk的方法,以打包HTML5為例分析了webview打包apk的相關(guān)方法、屬性與事件操作技巧,需要的朋友可以參考下2017-08-08
Kotlin中內(nèi)置函數(shù)的用法和區(qū)別總結(jié)
眾所周知相比Java, Kotlin提供了不少高級(jí)語法特性。對(duì)于一個(gè)Kotlin的初學(xué)者來說經(jīng)常會(huì)寫出一些不夠優(yōu)雅的代碼。下面這篇文章主要給大家介紹了關(guān)于Kotlin中內(nèi)置函數(shù)的用法和區(qū)別的相關(guān)資料,需要的朋友可以參考下2018-06-06
Android RxJava創(chuàng)建操作符Timer的方法
這篇文章主要為大家詳細(xì)介紹了Android RxJava創(chuàng)建操作符Timer的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android Gridview布局出現(xiàn)滾動(dòng)條或組件沖突解決方法
這篇文章主要介紹了Android Gridview布局出現(xiàn)滾動(dòng)條或組件沖突解決方法,GridView是一個(gè)在二維可滾動(dòng)的網(wǎng)格中展示內(nèi)容的控件。網(wǎng)格中的內(nèi)容通過使用adapter自動(dòng)插入到布局中2022-07-07
android socket聊天室功能實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了android socket聊天室功能實(shí)現(xiàn)方法,不單純是聊天室,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
Android編程之殺毒的實(shí)現(xiàn)原理及具體實(shí)例
這篇文章主要介紹了Android編程之殺毒的實(shí)現(xiàn)原理及具體實(shí)例,結(jié)合實(shí)例形式分析了Android殺毒功能的原理與簡(jiǎn)單實(shí)現(xiàn)技巧,需要的朋友可以參考下2015-12-12
Android Activity的生命周期與啟動(dòng)模式全面解讀
雖然說我們天天都在使用Activity,但是你真的對(duì)Activity的生命機(jī)制完全了解了嗎?Activity的生命周期方法只有七個(gè),但是其實(shí)那只是默認(rèn)的情況。也就是說在其他情況下,Activity的生命周期可能不會(huì)是按照我們以前所知道的流程,這就要說到Activity的啟動(dòng)模式2021-10-10

