Menu
CoddyTech

玩家状态

本课属于 Coddy Lua 学习旅程的“逻辑与流程”单元。第 26 课,共 54 课。

challenge icon

挑战

简单

基于你连接好的房间,创建一个玩家表,用来跟踪玩家在游戏世界中的当前位置。

上一道挑战已经提供了 startingRoom 和 gardenRoom 变量。

创建一个名为 player 的表,其中包含一个键:

  • currentRoom:将它设为引用 startingRoom

打印玩家当前房间的名称,然后在另起一行打印玩家当前房间的描述。

预期输出格式:

Ancient Library
You find yourself in a dusty library filled with ancient books and scrolls.

亲手试试

-- Create the starting room
local startingRoom = {
    name = "Ancient Library",
    description = "A dusty library filled with ancient tomes and scrolls.",
    exits = {}
}

-- Create the garden room
local gardenRoom = {
    name = "Secret Garden",
    description = "A hidden garden with overgrown plants and a small fountain.",
    exits = {}
}

-- Link the rooms together
startingRoom.exits["east"] = gardenRoom
gardenRoom.exits["west"] = startingRoom

-- Print the required output
print(startingRoom.name)
print(startingRoom.exits["east"].name)
print(startingRoom.exits["east"].exits["west"].name)

逻辑与流程中的所有课

自己练习: Lua 在线编译器