Skip to content

Commit 7c1fe59

Browse files
authored
Create game.lua
1 parent 2d7378a commit 7c1fe59

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

luaquest/game.lua

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
require("data")
2+
3+
function describe_location()
4+
local room = rooms[player.location]
5+
print("\n" .. room.desc)
6+
if room.enemy then
7+
print("Enemy spotted: " .. room.enemy.name)
8+
end
9+
if room.item then
10+
print("Item here: " .. room.item.name)
11+
end
12+
print("Exits: " .. table.concat(table.keys(room.exits), ", "))
13+
end
14+
15+
function move(direction)
16+
local room = rooms[player.location]
17+
if room.exits[direction] then
18+
player.location = room.exits[direction]
19+
describe_location()
20+
else
21+
print("Can't go that way.")
22+
end
23+
end
24+
25+
function pickup()
26+
local room = rooms[player.location]
27+
if room.item then
28+
table.insert(player.inventory, room.item)
29+
print("Picked up: " .. room.item.name)
30+
room.item = nil
31+
else
32+
print("Nothing to pick up here.")
33+
end
34+
end
35+
36+
function fight()
37+
local room = rooms[player.location]
38+
if room.enemy then
39+
local enemy = room.enemy
40+
print("Fighting " .. enemy.name .. "!")
41+
while enemy.hp > 0 and player.hp > 0 do
42+
enemy.hp = enemy.hp - 5 -- fixed player attack
43+
print("Hit! " .. enemy.name .. " HP: " .. enemy.hp)
44+
if enemy.hp <= 0 then
45+
print(enemy.name .. " defeated!")
46+
room.enemy = nil
47+
break
48+
end
49+
player.hp = player.hp - enemy.attack
50+
print("Ouch! Player HP: " .. player.hp)
51+
if player.hp <= 0 then
52+
print("You died! Game Over.")
53+
os.exit()
54+
end
55+
end
56+
else
57+
print("No enemy here.")
58+
end
59+
end

0 commit comments

Comments
 (0)