如何在 SUI 上为角色扮演游戏 (RPG) 创建角色和道具?
2023-03-28 15:00
Sui World
2023-03-28 15:00
订阅此专栏
收藏此文章
Sui Foundation 与 Encode Club 合作提供了系列以开发者为中心的教育视频,本系列视频共计六则,范围从 Sui 的基础知识到构建智能合约和使用 Sui Move 中的对象的教程。往期视频参照 SUI World 推文。
本教程将给我们展示如何为角色扮演游戏 (RPG) 创建角色和道具,以及如何让它们互动,千万不要错过!详情点击「阅读原文」查看视频。
学习亮点
由于 Sui Move 的以对象为中心的编程模型及其可扩展性,Sui 有望成为第一个真正将 web2 体验交付到 web3 的区块链,这种体验包括游戏,游戏的编程本质上都很复杂,并且需要强大的基础设施来确保玩家的无缝体验。得益于 Sui Move 的以对象为中心的编程模型及其可扩展性,Sui 能够应对挑战。
让我们看一下 Sui Move 中链上 RPG 的编码示例。以下示例改编自 Sam Blackshear 的 hero.move 代码。
https://github.com/MystenLabs/sui/blob/main/sui_programmability/examples/games/sources/hero.move?ref=blog.suifoundation.org
/// Our hero!struct Hero has key, store {  id: UID,  /// Hit points. If they go to zero, the hero can't do anything  hp: u64,  /// Experience of the hero. Begins at zero  experience: u64,  /// The hero's minimal inventory  sword: Option<Sword>,  /// An ID of the game user is playing  game_id: ID,}
上面的代码定义了我们的可玩角色。从它的领域可以看出,这个英雄可以与角色扮演游戏中的其他角色相媲美,它具有生命值 (HP)、经验和库存。
/// The hero's trusty swordstruct Sword has key, store {  id: UID,  /// Constant set at creation. Acts as a multiplier on sword's strength.  /// Swords with high magic are rarer (because they cost more).  magic: u64,  /// Sword grows in strength as we use it  strength: u64,  /// An ID of the game  game_id: ID,}
上面的代码展示了我们英雄的剑,请注意,这把剑具有钥匙和存储能力。回顾一下本系列之前的课程,key 意味着它是一种可拥有的资产,可以存在于顶级存储中。此类别中的 Move 对象也可以从外部 API 访问,从而创造了 Sui 在多个游戏中使用项目的独特可能性。而存储意味着这个对象是可以自由包装和转移的。
/// A creature that the hero can slay to level upstruct Boar has key {  id: UID,  /// Hit points before the boar is slain  hp: u64,  /// Strength of this particular boar  strength: u64,  /// An ID of the game  game_id: ID,}
上面,我们在游戏中定义了野猪、不可玩角色 (NPC) 或敌人,与该类型的其他游戏类似,我们可以为我们的英雄创建 NPC 来战斗和获得经验,或者购买物品和接受任务。
The Action
/// Slay the `boar` with the `hero`'s sword, get experience./// Aborts if the hero has 0 HP or is not strong enough to slay the boarpublic entry fun slay(  game: &GameInfo, hero: &mut Hero, boar: Boar, ctx: &TxContext) {  check_id(game, hero.game_id);  check_id(game, boar.game_id);  let Boar { id: boar_id, strength: boar_strength, hp, game_id: _ } = boar;  let hero_strength = hero_strength(hero);  let boar_hp = hp;  let hero_hp = hero.hp;  // attack the boar with the sword until its HP goes to zero  while (boar_hp > hero_strength) {    // first, the hero attacks    boar_hp = boar_hp - hero_strength;    // then, the boar gets a turn to attack. if the boar would kill    // the hero, abort--we can't let the boar win!    assert!(hero_hp >= boar_strength , EBOAR_WON);    hero_hp = hero_hp - boar_strength;  };  // hero takes their licks  hero.hp = hero_hp;  // hero gains experience proportional to the boar, sword grows in  // strength by one (if hero is using a sword)  hero.experience = hero.experience + hp;  if (option::is_some(&hero.sword)) {    level_up_sword(option::borrow_mut(&mut hero.sword), 1)  };  // let the world know about the hero's triumph by emitting an event!  event::emit(BoarSlainEvent {    slayer_address: tx_context::sender(ctx),    hero: object::uid_to_inner(&hero.id),    boar: object::uid_to_inner(&boar_id),    game_id: id(game)  });  object::delete(boar_id);}
上面代码中显示的动作描述了 slay 函数。在高层次上,这个函数首先检查以确保 Hero 和 Boar 都属于同一个游戏实例。然后英雄和野猪之间的决斗发生,检查以确保英雄的 HP 不能达到零。决斗结束后,英雄获得与野猪成比例的经验值,英雄的剑的力量增加 1( 如果英雄使用剑 )。最后,该函数发出一个事件 BoarSlayEvent。Sui Move 中的事件让索引器跟踪链上的动作,这是实现普遍认可的对象状态的重要手段。
上面的代码示例简要摘录了 Sam 的 hero.move 代码。此代码为 Sui 上的游戏开发者提供了宝贵的示例,并且由于它是开源的,请随时 Fork 并构建您自己的游戏!
原文:SUI Foundation
编译:SUI World


公众号后台回复“
1”,加入 SUI World 社区
回复“
加速”,获得 Move 加速器支持

推荐阅读

发现 Move 的价值·Move Accelerator Meetup (杭州站)
a16z 对话 Move 语言之父:从编程语言说起,为何 Move 是未来智能合约的重要方向
主网将近,社区如何获得 SUI Token ?
首期 Sui Demo Day 将于 4 月 16 日在香港举办

相关Wiki

【免责声明】市场有风险,投资需谨慎。本文不构成投资建议,用户应考虑本文中的任何意见、观点或结论是否符合其特定状况。据此投资,责任自负。

在 App 打开
hyperliquid
wal
jellyjelly
particle
空投
香港
以太坊
rwa
movement
bera
monad
sui