Android实现 * 小游戏代码示例
作者:z2955 发布时间:2022-08-04 04:15:11
标签: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 * 小游戏内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
来源:https://blog.csdn.net/z2955/article/details/121913172


猜你喜欢
- 线程(英语:thread)是操作系统能够进行运算调度的最小单位。它被包含在进程之中,是进程中的实际运作单位。一条线程指的是进程中一个单一顺序
- springboot默认读取的配置文件名字是:“application.properties”和&a
- 概述 这是一个自定义色盘,根据点,直线和圆的几何学加上hsv颜色模型完成技术点几何:圆的标准方程式:(x-a)²
- 一、前言知识补充:Arrays.copyOf函数:public static int[] copyOf(int[] original, in
- 在本文中,我们将看到如何将double转换为int。在Java编程中,您将有一个double原语值(例如82.14),但是要执行进一步的操作
- 一个简单的拼图小游戏,供大家参考,具体内容如下1.首先设计视图面板。2.添加所需要的图片按钮。3.最主要的是设计监听事件,添加图片的监听按钮
- 前言在一个名为种花家的小镇上,生活着一群热爱编程的人。他们致力于构建出高效、可维护的软件系统,而 Spring Boot 框架成为了他们的不
- Alt+Enter快捷键是Idea中比较特殊的一个快捷键。它有很多功能,比如:导入包,自动修正代码 解决出现的问题 也可以生成返回值。这里有
- @Bean修饰的方法参数的注入方法参数默认注入方式为Autowired,即先根据类型匹配,若有多个在根据名称进行匹配。1:复杂类型可以通过@
- 本文实例讲述了Android编程之绘制文本(FontMetrics)实现方法。分享给大家供大家参考,具体如下:Canvas 作为绘制文本时,
- 本文实例讲述了C#中ExecuteNonQuery()返回值注意点。对于C#数据库程序设计有一定的借鉴价值。分享给大家供大家参考之用。具体分
- 一、概述Socket类是Java执行客户端TCP操作的基础类,这个类本身使用代码通过主机操作系统的本地TCP栈进行通信。Socket类的方法
- this:this理解为:当前对象 或 当前正在创建的对象可以调用的结构:属性、方法;构造器this调用属性、方法:先了解一下形参:形参的意
- 前言众所周知Java提供File类,让我们对文件进行操作,下面就来简单整理了一下File类的用法。 话不多说了,来一起看看详细的介绍吧1.基
- 一般查询手机归属地内容应该很好用json格式保存,在网上找到了淘宝的归属地API,并下了处理json相关的jar包,做了这个手机归属地查询功
- App.config是C#开发WinForm程序的配置文件,开发Web程序的配置文件叫Web.config。本文介绍App.config的简
- 方式一:例如:”0000123” (字符串必须全为数字)处理过程:String tempStr = "0000123";
- 本文实例讲述了Android编程实现为ListView创建上下文菜单(ContextMenu)的方法。分享给大家供大家参考,具体如下:Con
- Android标题栏最右边添加按钮的实例step1:重写activity的onCreateOptionsMenu方法@Override pu
- 一、关于idea中Maven的位置idea自带一个maven,其路径大致如下C:/java/IntelliJ IDEA 2020.1/plu