首页 > 其他分享 >『牛角书』鸿蒙小游戏之石头剪刀布

『牛角书』鸿蒙小游戏之石头剪刀布

时间:2022-12-18 15:33:33浏览次数:38  
标签:牛角 鸿蒙 text height 小游戏 ResourceTable ohos id match

一、游戏逻辑

通过页面下方按钮与游戏中的人物pk,赢了加一分,输了减一分,平局不加分也不减分。

二、游戏效果

如图所示,选择石头后游戏中人物出布,因此我的得分为-1。


『牛角书』鸿蒙小游戏之石头剪刀布_HarmonyOS

三、主要代码

3.1 代码目录

『牛角书』鸿蒙小游戏之石头剪刀布_HarmonyOS_02

3.1 页面布局

<?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:paper"
></Image>

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

<Button
ohos:id="$+id:paper"
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:scissors"
ohos:right_of="$id:paper"
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:rock"
ohos:right_of="$id:scissors"
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>
<?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>

3.2 通过MainAbility配置此应用唯一一个slice并编写slice

package com.example.caiquan.slice;

import com.example.caiquan.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 paper;
static Button rock;
static Button scissors;
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搞到相关应用
paper= (Button) findComponentById(ResourceTable.Id_paper);
rock= (Button) findComponentById(ResourceTable.Id_rock);
scissors= (Button) findComponentById(ResourceTable.Id_scissors);
defen= (Text) findComponentById(ResourceTable.Id_defen);
enemy= (Image) findComponentById(ResourceTable.Id_enemy);
//给石头剪刀布三个按钮绑定单击事件
paper.setClickedListener(this::chuli);
rock.setClickedListener(this::chuli);
scissors.setClickedListener(this::chuli);

}
//根据component的不同做出不同处理
private void chuli(Component component) {
int way;
String name=component.getName();
if(name.equals("Id_rock"))
way=0;
else if(name.equals("Id_scissors"))
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_rock);
else if(enemyway==1)
enemy.setPixelMap(ResourceTable.Media_scissors);
else
enemy.setPixelMap(ResourceTable.Media_paper);

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);
}
}

四、总结

在此次鸿蒙应用开发学习中我明白自身还有许多不足,该项目也存在一些问题,欢迎大家帮忙指正。

标签:牛角,鸿蒙,text,height,小游戏,ResourceTable,ohos,id,match
From: https://blog.51cto.com/u_15916478/5950880

相关文章

  • 入门鸿蒙应用+页面跳转
    1、开发第一个鸿蒙应用1●创建鸿蒙应用打开DevEcoStudio,在欢迎页单击CreateHarmonyOSProject,创建一个新工程,如图1所示。根据工程创建向导,选择需要的Ability工程模板,然后......
  • 【牛角书】基于HarmoyOS的登陆界面实现
    本项目作为鸿蒙开发课的大作业,供大家学习,本项目主要实现了,登录、注册修改密码界面的实现,并且实现了三个页面之间的互相切换。一、项目搭建1.1、项目配置这里注意API要用......
  • 『牛角书』基于鸿蒙开发小小音乐播放器
    鸿蒙开发小应用-音乐播放器话不多说,展示。第一次进去会申请访问权限,点击“始终允许”点击“始终允许”后退出一下,再次点击进入该应用会看到一首音乐DreamitPossible,因为模......
  • Python写个“点球大战”小游戏
    大家好,欢迎来到Crossin的编程教室!看过我Python入门教程的朋友应该会看到其中有提到一个点球小游戏的作业。在世界杯决赛即将到来之际,我们再来回顾一下这个小游戏。......
  • 基于鸿蒙的手写板
    一、前言本着要带着目标去学习的态度,做了一个简易的手写板,使用的语言是JAVA,效果图如下:二、准备工作下载DevEcoStudio,创建一个新项目(鸿蒙2.0)。接着我思考了一下手写......
  • 『牛角书』鸿蒙基础计算器
    简介这是我自己的鸿蒙期末考查大作业,通过一学期课程的学习,研究出来的一些成果,代码还有很多需要优化的地方,本文内容仅为利用组件简单的计算器页面。成果展示开发思路计算器......
  • 小游戏:基于鸿蒙的24点纸牌游戏
    开发设计实现文档 一、编写目的 灵感来源于二十四点纸牌游戏,将生活中的纸牌游戏在电脑网页上实现。学会以后在空闲时可以玩一玩,锻炼一下速算能力。 二、项目目......
  • pyqt5制作俄罗斯方块小游戏-----源码解析
    一、前言最近学习pyqt5中文教程时,最后一个例子制作了一个俄罗斯方块小游戏,由于解释的不是很清楚,所以源码有点看不懂,查找网上资料后,大概弄懂了源码的原理。二、绘制主窗口......
  • 『牛角书』基于HarmonyOS的计算器页面
    @​​toc​简介这里分享的是我自己的鸿蒙期末考查大作业,通过一学期课程的学习,研究出来的一些成果,代码还有很多需要优化的地方,本文内容仅为利用组件形成的计算器页面。成果展......
  • 【FAQ】在华为鸿蒙车机上集成华为帐号的常见问题总结
    随着新一代信息技术与汽车产业的深度融合,智能网联汽车正逐渐成为汽车产业发展的战略制高点,无论是传统车企还是新势力都瞄准了“智能座舱”这种新一代人机交互方式。面对竞......