ArkTS语言模仿秀 HarmonyOS 鸿蒙Next 用代码来来一把猜拳,并体验一场周星驰的经典语录

发布于 1周前 作者 ionicwang 来自 鸿蒙OS

ArkTS语言模仿秀 HarmonyOS 鸿蒙Next 用代码来来一把猜拳,并体验一场周星驰的经典语录 #ArkTS语言模仿秀# 用代码来来一把猜拳,并体验一场周星驰的经典语录

@Entry
@Component
struct Mypage {
  @State private playerMove: string = ‘’;
  @State private computerMove: string = ‘’;
  @State private roundResult: string = ‘’;
  @State private playerQuote: string = ‘’;
  @State private isPlayerTurn: boolean = true;
  private moves: string[] = [‘石头’, ‘剪刀’, ‘布’];

  private winQuotes: string[] = [
    ‘其实我是一个演员。’, // 《喜剧之王》
    ‘曾经有一份真诚的爱情放在我面前,我没有珍惜。’, // 《大话西游》
    ‘做人如果没有梦想,那跟咸鱼有什么分别啊?’ // 《少林足球》
  ];

  private loseQuotes: string[] = [
    ‘生亦何哀,死亦何苦。’, // 《大话西游》
    ‘球,不是这么踢滴。’, // 《功夫足球》
    ‘小强!小强你怎么了小强?小强,你不能死啊!我跟你相依为命,同甘共苦了这么多年,一直把你当亲生骨肉一样教你养你,想不到今天,白发人送黑发人!’ // 《唐伯虎点秋香》
  ];

  private drawQuotes: string[] = [
    ‘I服了You。’, // 《大话西游》
    ‘聊得挺投缘,赔点医药费算了。’, // 《功夫》
    ‘腰里揣着死耗子还冒充打猎的。’ // 《九品芝麻官》
  ];

  @Builder playerButton() {
    Button(‘甲方出拳’)
      .onClick(() => {
        if (this.isPlayerTurn) {
          this.playerMove = this.getRandomMove();
          this.isPlayerTurn = false;
          this.checkResult();
        }
      })
      .margin({ top: 20 })
  }

  @Builder computerButton() {
    Button(‘乙方出拳’)
      .onClick(() => {
        if (!this.isPlayerTurn) {
          this.computerMove = this.getRandomMove();
          this.isPlayerTurn = true;
          this.checkResult();
        }
      })
      .margin({ top: 50 })
  }

  private getRandomMove(): string {
    const randomIndex = Math.floor(Math.random() * this.moves.length);
    return this.moves[randomIndex];
  }

  private checkResult(): void {
    this.roundResult = ‘’;
    this.playerQuote = ‘’;
    if (this.playerMove && this.computerMove) {
      if (this.playerMove === this.computerMove) {
        this.roundResult = this.getRandomQuote(this.drawQuotes);
        this.playerQuote = ‘打平!’ + this.getRandomQuote(this.drawQuotes);
      } else if ((this.playerMove === ‘石头’ && this.computerMove === ‘剪刀’) ||
        (this.playerMove === ‘剪刀’ && this.computerMove === ‘布’) ||
        (this.playerMove === ‘布’ && this.computerMove === ‘石头’)) {
        this.roundResult = this.getRandomQuote(this.winQuotes);
        this.playerQuote = ‘你赢了!’ + this.getRandomQuote(this.loseQuotes);
      } else {
        this.roundResult = this.getRandomQuote(this.loseQuotes);
        this.playerQuote = ‘你输了!’ + this.getRandomQuote(this.winQuotes);
      }
    }
  }

  private getRandomQuote(quotes: string[]): string {
    const randomIndex = Math.floor(Math.random() * quotes.length);
    return quotes[randomIndex];
  }

  build() {
    Column() {
      if (this.roundResult) {
        Text(this.roundResult).fontSize(20).fontColor(Color.Red).margin({ top: 100 })
      }
      this.playerButton()
      Column() {
        Text(this.playerMove).fontSize(24).fontWeight(FontWeight.Bold).margin({ top: 20 })
        Text(this.computerMove).fontSize(24).fontWeight(FontWeight.Bold).margin({ top: 100 })
      }
      .justifyContent(FlexAlign.Center)
      .margin({ top: 20 })
      this.computerButton()
      Column({ space: 10 }) {
        if (this.playerQuote) {
          Text(this.playerQuote).fontSize(18).fontColor(Color.Blue).margin({ top: 20 })
        }
      }
    }
    .justifyContent(FlexAlign.Center)
    .width(‘100%’)
    .height(‘100%’)
    .padding(20)
  }
}


更多关于ArkTS语言模仿秀 HarmonyOS 鸿蒙Next 用代码来来一把猜拳,并体验一场周星驰的经典语录的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html

9 回复

有点意思

更多关于ArkTS语言模仿秀 HarmonyOS 鸿蒙Next 用代码来来一把猜拳,并体验一场周星驰的经典语录的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


不错不错

不错不错,支持一个

哟哟,这个不错

不错吧兄弟们

在HarmonyOS鸿蒙系统中,使用ArkTS语言进行猜拳游戏并融入周星驰经典语录的实现可以如下:

首先,定义一个枚举类型来表示拳法的三种可能结果:

enum RockPaperScissors {
    ROCK,
    PAPER,
    SCISSORS
}

接着,定义一个函数来生成随机的拳法:

function getRandomChoice() -> RockPaperScissors {
    let choices = [RockPaperScissors.ROCK, RockPaperScissors.PAPER, RockPaperScissors.SCISSORS]
    return choices[Math.floor(Math.random() * choices.length)]
}

然后,实现判断胜负的逻辑,并输出周星驰的经典语录:

function playGame() {
    let userChoice = ... // 用户输入的拳法
    let computerChoice = getRandomChoice()

    if (userChoice == computerChoice) {
        console.log("平手!周星驰说:'人生就像一场戏,因为有缘才相聚。'")
    } else if ((userChoice == RockPaperScissors.ROCK && computerChoice == RockPaperScissors.SCISSORS) ||
               (userChoice == RockPaperScissors.PAPER && computerChoice == RockPaperScissors.ROCK) ||
               (userChoice == RockPaperScissors.SCISSORS && computerChoice == RockPaperScissors.PAPER)) {
        console.log("你赢了!周星驰说:'曾经有一份真诚的爱情放在我面前,我没有珍惜,等我失去的时候我才后悔莫及。'")
    } else {
        console.log("你输了!周星驰说:'做人如果没有梦想,那跟咸鱼有什么分别?'")
    }
}

注意,上述代码示例省略了用户输入部分的具体实现,你需要根据实际需求补充完整。

如果问题依旧没法解决请联系官网客服,官网地址是 https://www.itying.com/category-93-b0.html

回到顶部