Menu
CoddyTech

游戏循环

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

challenge icon

挑战

简单

实现主游戏循环,在游戏运行时持续显示当前房间的描述。

你已在上一关中获得 startingRoom、gardenRoom 和 player 变量。

创建一个名为 gameRunning 的变量,并将其设为 true。

编写一个 while 循环,只要 gameRunning 为 true,循环就会继续。在循环中:

  • 打印玩家当前所在房间的描述
  • 将 gameRunning 设为 false,以退出循环(目前先这样做,下一课中我们会添加正确的输入处理)

预期输出格式:

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

亲手试试

-- Create the starting room
local startingRoom = {
    name = "Ancient Library",
    description = "You find yourself in a dusty library filled with ancient books and scrolls."
}

-- Create the garden room
local gardenRoom = {
    name = "Secret Garden",
    description = "A beautiful garden with exotic flowers and a mysterious fountain."
}

-- Link the rooms
startingRoom.nextRoom = gardenRoom
gardenRoom.previousRoom = startingRoom

-- Create the player table
local player = {
    currentRoom = startingRoom
}

-- Print the player's current room name and description
print(player.currentRoom.name)
print(player.currentRoom.description)

逻辑与流程中的所有课

自己练习: Lua 在线编译器