Text-Based Adventure Game
For your final project, you'll create a text-based adventure game that incorporates all the Lua concepts you've learned throughout the course.
Project Requirements
Your text-based adventure game should include the following features:
- Multiple locations for the player to explore
- Items that can be collected and used
- NPCs with dialogue options
- A combat system with health, damage, and enemy AI
- A save/load system for game progress
- A scoring system to track player achievements
Game Structure
Here's a suggested structure for your game with multiple modules:
main.lua
-- main.lua
-- Import modules
local game = require("game")
local player = require("player")
local world = require("world")
local ui = require("ui")
-- Initialize game
game.init()
-- Main game loop
while game.isRunning() do
-- Clear screen and display current location
ui.clear()
ui.displayLocation(player.getCurrentLocation())
-- Display player stats
ui.displayStats(player.getStats())
-- Get player input
local command = ui.getCommand()
-- Process command
game.processCommand(command)
-- Update game state
game.update()
end
-- Game over
ui.displayGameOver(player.getStats())
Here's an example of the player module:
player.lua
-- player.lua
local player = {}
-- Player state
local stats = {
name = "",
health = 100,
maxHealth = 100,
attack = 10,
defense = 5,
experience = 0,
level = 1,
inventory = {},
currentLocation = "start",
score = 0
}
-- Initialize player
function player.init(name)
stats.name = name
return true
end
-- Get player stats
function player.getStats()
return stats
end
-- Get current location
function player.getCurrentLocation()
return stats.currentLocation
end
-- Set current location
function player.setLocation(location)
stats.currentLocation = location
return true
end
-- Add item to inventory
function player.addItem(item)
table.insert(stats.inventory, item)
return true
end
-- Check if player has an item
function player.hasItem(itemName)
for _, item in ipairs(stats.inventory) do
if item.name == itemName then
return true
end
end
return false
end
-- Use an item
function player.useItem(itemName)
for i, item in ipairs(stats.inventory) do
if item.name == itemName then
-- Apply item effects
if item.healthEffect then
stats.health = math.min(stats.maxHealth, stats.health + item.healthEffect)
end
-- Remove consumable items
if item.consumable then
table.remove(stats.inventory, i)
end
return true
end
end
return false
end
-- Handle combat
function player.attack(enemy)
local damage = math.max(1, stats.attack - enemy.defense)
enemy.health = enemy.health - damage
return damage
end
-- Take damage
function player.takeDamage(amount)
local damage = math.max(1, amount - stats.defense)
stats.health = stats.health - damage
return damage
end
-- Add experience
function player.addExperience(amount)
stats.experience = stats.experience + amount
-- Check for level up
local newLevel = math.floor(1 + stats.experience / 100)
if newLevel > stats.level then
stats.level = newLevel
stats.maxHealth = stats.maxHealth + 10
stats.health = stats.maxHealth
stats.attack = stats.attack + 2
stats.defense = stats.defense + 1
return true
end
return false
end
-- Add to score
function player.addScore(points)
stats.score = stats.score + points
return stats.score
end
-- Check if player is alive
function player.isAlive()
return stats.health > 0
end
-- Save player data
function player.save()
-- In a real game, this would save to a file
return stats
end
-- Load player data
function player.load(savedStats)
stats = savedStats
return true
end
return player
Development Steps
Follow these steps to develop your game:
- Create the base modules: game, player, world, ui, combat, and items
- Design the game world with different locations and connections
- Implement the command parser to handle player input
- Add NPCs and dialogue systems
- Create the combat system
- Implement the inventory and item usage
- Add a save/load system
- Test and refine the gameplay