Android 用Java代码编写一款动态生成幸运数字的小游戏

 

截图:

代码如下:

  第三方依赖库地址:https://github.com/AndroidMsky/RandomTextView

第一步:添加仓库

allprojects {
		repositories {
			...
			maven { url 'https://jitpack.io' }
		}
	}

第二步:

dependencies {
	        compile 'com.github.AndroidMsky:RandomTextView:v1.4'
	}

第三步:编写xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_below="@+id/iv_back"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="@mipmap/rule_bg">
    </LinearLayout>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="@dimen/x40"
        android:text="幸运选号"
        android:textSize="@dimen/x16"
        android:gravity="center"
        android:background="#660108"/>
   <ImageView
       android:id="@+id/iv_back"
       android:layout_width="@dimen/x30"
       android:layout_height="@dimen/x30"
       android:src="@mipmap/back"/>

    <com.example.liangmutian.randomtextviewlibrary.RandomTextView
        android:id="@+id/rt_v"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:padding="0px"
        android:letterSpacing="0.5"
        android:text="00000"
        android:textSize="@dimen/x50"/>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/x40"
        android:onClick="start"
        android:text="幸运选号"
        android:background="@drawable/shape_dialog_text"
        android:layout_below="@+id/rt_v"
        android:layout_centerHorizontal="true"/>


</RelativeLayout>

 

第四步: 编写Activity

public class RandomActivity extends Activity {
    private RandomTextView mRandomTextView;
    private int[] pianyiliang = new int[6];
    private ImageView imageView ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_random);
        mRandomTextView =  findViewById(R.id.rt_v);
        imageView =  findViewById(R.id.iv_back);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        pianyiliang[0] = 10;
        pianyiliang[1] = 9;
        pianyiliang[2] = 8;
        pianyiliang[3] = 7;
        pianyiliang[4] = 6;
        pianyiliang[5] = 5;
        mRandomTextView.setPianyilian(pianyiliang);
        mRandomTextView.start();
    }


    public void start(View v) {
        int[] num = new int [5];
        Random random = new Random();
        String str = "";
        for (int i = 0 ;i <5 ; i++) {
            int randomInt = random.nextInt(10);
            num[i] = randomInt;
            str += num[i] + "";

            mRandomTextView.setText(str);
            pianyiliang[0] = 7;
            pianyiliang[1] = 6;
            pianyiliang[2] = 12;
            pianyiliang[3] = 8;
            pianyiliang[4] = 18;
            pianyiliang[5] = 10;
            mRandomTextView.setMaxLine(40);
            mRandomTextView.setPianyilian(pianyiliang);
            mRandomTextView.start();
        }
    }

}

 

 第五步:  点击运行Demo即可实现动态生成幸运数字的方法

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很好的问题!编写俄罗斯方块小游戏Java语言和Android Studio的一个不错的练习。以下是一些基本步骤,以帮助您开始编写代码: 1. 设置开发环境:您需要安装Java JDK和Android Studio软件,以及设置开发环境。 2. 创建新的Android Studio项目:打开Android Studio软件,点击“新建项目”,选择“空活动”,并输入应用程序名称和包名称。 3. 设计游戏UI:使用Android Studio的设计工具,您可以创建游戏UI,并将其保存到res/layout文件夹中。 4. 编写Java代码:在Android Studio中创建Java类,并编写代码来处理游戏逻辑。您需要实现俄罗斯方块游戏的主要功能,例如旋转、移动和删除方块。 5. 运行游戏:使用Android Studio的模拟器或连接到Android设备来测试游戏。 下面是一些Java代码片段,可以帮助您开始编写俄罗斯方块游戏: ```java public class Block { private int[][] blockShape; private int blockColor; public Block(int[][] blockShape, int blockColor) { this.blockShape = blockShape; this.blockColor = blockColor; } public int[][] getBlockShape() { return blockShape; } public int getBlockColor() { return blockColor; } } public class Game { private Block currentBlock; private int[][] gameBoard; public Game() { // Initialize the game board and set the current block to a random shape and color. } public void moveLeft() { // Move the current block to the left on the game board. } public void moveRight() { // Move the current block to the right on the game board. } public void rotate() { // Rotate the current block on the game board. } public void drop() { // Drop the current block down on the game board until it hits another block or the bottom of the board. } public void deleteRows() { // Delete any completed rows on the game board. } public boolean isGameOver() { // Check if the game is over. } } ``` 以上是一些示例代码,但是编写完整的俄罗斯方块游戏需要更多的代码。希望这些提示可以帮助您开始编写自己的游戏!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值