Android实现老虎机小游戏代码示例
用 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();//随机数
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() { //开始按钮监听事件
@override
public void onclick(view view) {
handler.postdelayed(runnable, 20);//定时器启动
}
});
btn_finish.setonclicklistener(new view.onclicklistener() { //结束按钮监听事件
@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("恭喜您人品大爆发,获得一等奖:三株豌豆射手");}
else if(img[b]==img[1]){
result.settext("恭喜您人品大爆发,获得特等奖:三株玉米投手");
}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实现老虎机小游戏代码示例的文章就介绍到这了,更多相关android老虎机小游戏内容请搜索萬仟网以前的文章或继续浏览下面的相关文章希望大家以后多多支持萬仟网!
看完文章,还可以扫描下面的二维码下载快手极速版领4元红包
除了扫码领红包之外,大家还可以在快手极速版做签到,看视频,做任务,参与抽奖,邀请好友赚钱)。
邀请两个好友奖最高196元,如下图所示:







