鸿蒙开发初体验之测试反应速度小游戏

话不多说直接上代码

我这边是自己创建了一个SpeedReactionSlice页面

ability_speed_reaction.xml文件

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_parent"
    ohos:width="match_parent"
    ohos:orientation="vertical">

    <Text
        ohos:height="50vp"
        ohos:width="319vp"
        ohos:layout_alignment="horizontal_center"
        ohos:text="测试反应速度小游戏"
        ohos:text_size="30fp"
        ohos:text_alignment="center"
        ohos:top_margin="10vp"
        />
    <Text
        ohos:id="$+id:speed_tips"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:text_alignment="center"
        ohos:text_size="25fp"
        ohos:layout_alignment="horizontal_center"
        ohos:top_margin="10vp"
        />
    <Button
        ohos:id="$+id:start_game"
        ohos:height="50vp"
        ohos:width="150vp"
        ohos:text="开始游戏"
        ohos:text_size="25fp"
        ohos:background_element="cyan"
        ohos:layout_alignment="horizontal_center"
        ohos:top_margin="10vp"
        />
    <Button
        ohos:id="$+id:move_bt"
        ohos:height="50vp"
        ohos:width="100vp"
        ohos:text=""
        ohos:text_size="15fp"
        ohos:text_color="white"
        ohos:top_margin="10vp"
        ohos:background_element="#999"
        />



</DirectionalLayout>

SpeedReactionSlice.java文件

package com.example.component_learn.slice;
import com.example.component_learn.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import java.util.Comparator;
import java.util.ArrayList;
import java.util.Random;

public class SpeedReactionSlice extends AbilitySlice implements Component.ClickedListener {
    Button move_bt;
    Button start_game;
    Text speed_tips;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_speed_reaction);
        // 获取页面组件
        speed_tips = (Text) findComponentById(ResourceTable.Id_speed_tips);
        move_bt = (Button)findComponentById(ResourceTable.Id_move_bt);
        start_game = (Button)findComponentById(ResourceTable.Id_start_game);
        // 设置移动按钮不可见
        move_bt.setVisibility(Component.HIDE);
        // 添加监听事件
        start_game.setClickedListener(this);
        move_bt.setClickedListener(this);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
    // 存放记录
    ArrayList <Long> list;
    // 是否为第一次点击
    Boolean isStart = true;
    // 开始时间
    long start_time;
    // Random随机对象
    Random random = new Random();
    @Override
    public void onClick(Component component) {
        // 如果为开始按钮
        if (component == start_game){
            // 如果是开始游戏
            if(isStart){
                // 初始化开始时间
                start_time = System.currentTimeMillis();
                // 设置开始游戏按钮的内容
                start_game.setText("结束游戏");
                // 初始化移动按钮
                // 随机坐标
                int x = random.nextInt(500);
                int y = random.nextInt(1200);
                move_bt.setTranslation(x, y);
                move_bt.setText("来抓我啊");
                move_bt.setVisibility(Component.VISIBLE);
                // 初始化存放记录列表
                list = new ArrayList<>();
                // 设置为结束游戏状态
                isStart = false;
            // 如果是结束游戏
            }else{
                // 设置结束游戏按钮内容
                start_game.setText("开始游戏");
                // 设置移动按钮不可见
                move_bt.setVisibility(Component.HIDE);
                list.sort(Comparator.naturalOrder());
                // 获取最佳记录
                long beat_time = list.get(0);
                // 显示最佳记录
                speed_tips.setText("最快反应时间: "+beat_time+"毫秒");
                // 重置开始游戏按钮
                isStart = true;
            }
        // 如果为移动按钮
        }else if(component == move_bt){
            // 不可再次点击
            move_bt.setClickable(false);
            // 计算反应时间
            long reaction_time = System.currentTimeMillis() - start_time;
            // 添加进记录列表
            list.add(reaction_time);
            // 显示本次的反应时间
            speed_tips.setText("反应时间: "+reaction_time+"毫秒");
            //设置移动按钮 随机坐标
            int x = random.nextInt(500);
            int y = random.nextInt(1200);
            move_bt.setTranslation(x, y);
            // 可以点击
            move_bt.setClickable(true);
            // 重置开始时间
            start_time = System.currentTimeMillis();
        }
    }
}

真机测试看效果
在这里插入图片描述
点击开始游戏
在这里插入图片描述

点击来抓我按钮
在这里插入图片描述
点击结束游戏
在这里插入图片描述
在这里插入图片描述
原创不易,请给博主一个小小的赞吧~

  • 11
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值