当前位置:   article > 正文

HarmonyOS:手把手教你写一个石头剪刀布小游戏_harmony os4小游戏开发

harmony os4小游戏开发

源码链接

页面布局

<?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">

    <Image
        ohos:height="200vp"
        ohos:width="200vp"
        ohos:scale_mode="zoom_center"
        ohos:layout_alignment="center"
        ohos:image_src="$media:enemy"
        ></Image>

    <Image
        ohos:top_margin="30vp"
        ohos:height="200vp"
        ohos:id="$+id:enemy"
        ohos:width="200vp"
        ohos:scale_mode="zoom_center"
        ohos:layout_alignment="center"
        ohos:image_src="$media:bu"
        ></Image>

    <DependentLayout
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:margin="20vp"
        ohos:layout_alignment="center"
        >

        <Button
            ohos:id="$+id:bu"
            ohos:height="40vp"
            ohos:width="80vp"
            ohos:text=""
            ohos:margin="10vp"
            ohos:text_size="27fp"
            ohos:background_element="$graphic:capsule_button_element"
            ></Button>
        <Button
            ohos:id="$+id:jiandao"
            ohos:right_of="$id:bu"
            ohos:height="40vp"
            ohos:width="80vp"
            ohos:background_element="$graphic:capsule_button_element"
            ohos:text="剪刀"
            ohos:text_size="27fp"
            ohos:margin="10vp"
            ></Button>
        <Button

            ohos:id="$+id:shitou"
            ohos:right_of="$id:jiandao"
            ohos:margin="10vp"
            ohos:height="40vp"
            ohos:width="80vp"
            ohos:text="石头"
            ohos:text_size="27fp"
            ohos:background_element="$graphic:capsule_button_element"
            ></Button>




    </DependentLayout>

    <DependentLayout
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:left_margin="145vp">

        <Text
            ohos:id="$+id:pp"
            ohos:height="match_content"
            ohos:width="match_content"
            ohos:text_size="27fp"
            ohos:text="得分:"></Text>

        <Text
            ohos:id="$+id:defen"
            ohos:right_of="$id:pp"
            ohos:height="match_content"
            ohos:width="match_parent"
            ohos:text_size="27fp"
            ohos:text="0"></Text>

    </DependentLayout>







</DirectionalLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
       ohos:shape="rectangle">
    <corners
        ohos:radius="100"/>
    <solid
        ohos:color="#007CFD"/>
</shape>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

效果如下(图是随便找的,效果不怎么样,将就了)
在这里插入图片描述
通过MainAbility配置此应用唯一一个slice

import com.example.caiquan2.slice.MainAbilitySlice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;

public class MainAbility extends Ability {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setMainRoute(MainAbilitySlice.class.getName());
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

编写slice


import com.example.caiquan2.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.Image;
import ohos.agp.components.Text;

import java.util.Random;

public class MainAbilitySlice extends AbilitySlice {
    static Button bu;
    static Button shitou;
    static Button jiandao;
    static Text defen;
    static Image enemy;
    static Integer core=0;
    Random random=new Random();

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        //给slice 配置xml布局
        super.setUIContent(ResourceTable.Layout_mainslice);
            initView();

    }

    public void initView()
    {	//通过id搞到相关应用
        bu= (Button) findComponentById(ResourceTable.Id_bu);
        shitou= (Button) findComponentById(ResourceTable.Id_shitou);
        jiandao= (Button) findComponentById(ResourceTable.Id_jiandao);
        defen= (Text) findComponentById(ResourceTable.Id_defen);
        enemy= (Image) findComponentById(ResourceTable.Id_enemy);
       	//给石头剪刀布三个按钮绑定单击事件
        bu.setClickedListener(this::chuli);
        shitou.setClickedListener(this::chuli);
        jiandao.setClickedListener(this::chuli);

    }



	//根据component的不同做出不同处理
    private void chuli(Component component)
    {   int way;
        String name=component.getName();
        if(name.equals("Id_shitou"))
            way=0;
        else if(name.equals("Id_jiandao"))
            way=1;
        else
            way=2;

        System.out.println(name+":"+way);

        int enemyway=random.nextInt(3);
        //0石头 1 剪刀 2 布

        System.out.println(enemyway);
        if(enemyway==0)
            enemy.setPixelMap(ResourceTable.Media_shitou);
        else if(enemyway==1)
            enemy.setPixelMap(ResourceTable.Media_jiandao);
        else
            enemy.setPixelMap(ResourceTable.Media_bu);

        if(way==0)
        {
            if(enemyway==1)
                core++;
            else if(enemyway==2)
                core--;
        }
        else if(way==1)
        {
            if(enemyway==2)
                core++;
            else if(enemyway==0)
                core--;
        }
        else
        {
            if(enemyway==0)
                core++;
            else if(enemyway==1)
                core--;
        }
        defen.setText(core.toString());



    }



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

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/260446
推荐阅读
相关标签