学习了一段鸿蒙课,觉得有必要晒一晒自己的进步,开发了一个石头剪刀布小程序,希望对学习鸿蒙开发的童靴有帮助。

  1. 在DevEco Studio中创建一个新项目,
  1. 并选择“应用程序”模板。
  2. 在“MainAbility”类中,添加以下代码:
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.components.element.ElementScatter;
import ohos.agp.components.image.Image;
import ohos.agp.components.stacklayout.StackLayout;
import ohos.app.dispatcher.TaskDispatcher;
import ohos.app.dispatcher.TaskDispatcherContext;
import ohos.global.resource.RawFileEntry;
import ohos.global.resource.Resource;
import ohos.global.resource.ResourceManager;
import ohos.global.resource.ResourceManagerImpl;
import ohos.media.image.PixelMap;
import ohos.media.image.PixelMapFactory;
import java.io.IOException;
import java.util.Random;

public class MainAbilitySlice extends AbilitySlice {
    private Text resultText;
    private Image playerImage;
    private Image computerImage;
    private int playerChoice;
    private int computerChoice;

    private static final int ROCK = 0;
    private static final int PAPER = 1;
    private static final int SCISSORS = 2;

    private static final int[] images = {
            ResourceTable.Media_rock,
            ResourceTable.Media_paper,
            ResourceTable.Media_scissors
    };

    private TaskDispatcher uiTaskDispatcher = new TaskDispatcherContext("uiTaskDispatcher").getUITaskDispatcher();

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        resultText = (Text) findComponentById(ResourceTable.Id_result_text);
        playerImage = (Image) findComponentById(ResourceTable.Id_player_image);
        computerImage = (Image) findComponentById(ResourceTable.Id_computer_image);

        StackLayout rockLayout = (StackLayout) findComponentById(ResourceTable.Id_rock_layout);
        StackLayout paperLayout = (StackLayout) findComponentById(ResourceTable.Id_paper_layout);
        StackLayout scissorsLayout = (StackLayout) findComponentById(ResourceTable.Id_scissors_layout);

        rockLayout.setClickedListener(this::onRockClicked);
        paperLayout.setClickedListener(this::onPaperClicked);
        scissorsLayout.setClickedListener(this::onScissorsClicked);
    }

    private void onRockClicked(Component component) {
        playerChoice = ROCK;
        playerImage.setPixelMap(getPixelMap(images[playerChoice]));
        computerChoice = getRandomChoice();
        computerImage.setPixelMap(getPixelMap(images[computerChoice]));
        showResult();
    }

    private void onPaperClicked(Component component) {
        playerChoice = PAPER;
        playerImage.setPixelMap(getPixelMap(images[playerChoice]));
        computerChoice = getRandomChoice();
        computerImage.setPixelMap(getPixelMap(images[computerChoice]));
        showResult();
    }

    private void onScissorsClicked(Component component) {
        playerChoice = SCISSORS;
        playerImage.setPixelMap(getPixelMap(images[playerChoice]));
        computerChoice = getRandomChoice();
        computerImage.setPixelMap(getPixelMap(images[computerChoice]));
        showResult();
    }

    private int getRandomChoice() {
        Random random = new Random();
        return random.nextInt(3);
    }

    private void showResult() {
        if (playerChoice == computerChoice) {
            resultText.setText("It's a tie!");
        } else if ((playerChoice == ROCK && computerChoice == SCISSORS) ||
                   (playerChoice == PAPER && computerChoice == ROCK) ||
                   (playerChoice == SCISSORS && computerChoice == PAPER)) {
            resultText.setText("You win!");
        } else {
            resultText.setText("Computer wins!");
        }
    }

    private PixelMap getPixelMap(int imageId) {
        RawFileEntry rawFileEntry = this.getResourceManager().getRawFileEntry(imageId);
        try {
            Resource resource = rawFileEntry.openRawFile();
            PixelMap pixelMap = PixelMapFactory.createPixelMap(resource);
            resource.close();
            return pixelMap;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    private ResourceManager getResourceManager() {
        ResourceManagerImpl resourceManager = (ResourceManagerImpl) this.getResourceManager();
        return resourceManager;
    }
}
  1. 在“main.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:id="$+id:result_text"
        ohos:text="Make your choice"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:marginTop="20vp"
        ohos:textSize="20fp"
        ohos:textAlignment="center"/>

    <StackLayout
        ohos:id="$+id_rock_layout"
        ohos:height="80vp"
        ohos:width="match_parent"
        ohos:orientation="horizontal">

        <Image
            ohos:id="$+id/player_image"
            ohos:height="match_parent"
            ohos:width="0vp"
            ohos:layout_alignment="vertical_center"
            ohos:layout_weight="1"/>

        <Image
            ohos:id="$+id/computer_image"
            ohos:height="match_parent"
            ohos:width="0vp"
            ohos:layout_alignment="vertical_center"
            ohos:layout_weight="1"/>
    </StackLayout>

    <StackLayout
        ohos:id="$+id_paper_layout"
        ohos:height="80vp"
        ohos:width="match_parent"
        ohos:orientation="horizontal">

        <Image
            ohos:id="$+id/player_image"
            ohos:height="match_parent"
            ohos:width="0vp"
            ohos:layout_alignment="vertical_center"
            ohos:layout_weight="1"/>

        <Image
            ohos:id="$+id/computer_image"
            ohos:height="match_parent"
            ohos:width="0vp"
            ohos:layout_alignment="vertical_center"
            ohos:layout_weight="1"/>
    </StackLayout>

    <StackLayout
        ohos:id="$+id_scissors_layout"
        ohos:height="80vp"
        ohos:width="match_parent"
        ohos:orientation="horizontal">

        <Image
            ohos:id="$+id/player_image"
            ohos:height="match_parent"
            ohos:width="0vp"
            ohos:layout_alignment="vertical_center"
            ohos:layout_weight="1"/>

        <Image
            ohos:id="$+id/computer_image"
            ohos:height="match_parent"
            ohos:width="0vp"
            ohos:layout_alignment="vertical_center"
            ohos:layout_weight="1"/>
    </StackLayout>

</DirectionalLayout>
  1. 运行应用程序,可以点击不同的布局来选择石头、剪刀或布,并显示结果。