Skip to content

Instantly share code, notes, and snippets.

@jdp
Created August 3, 2008 22:55
Show Gist options
  • Save jdp/3847 to your computer and use it in GitHub Desktop.
Save jdp/3847 to your computer and use it in GitHub Desktop.
-- We only need the config module
tarn_loadmodule("config")
-- Configure the display window
tarn_display_width = 40
tarn_display_height = 25
tarn_display_title = "Tarn"
tarn_init()
-- Globally accessible variables
player = {}
map, map_width, map_height, fov = {}, 20, 10, nil
-- Set up player stuff
player = {
glyph = "@",
color = color.white,
x = 10,
y = 5,
move = function(self, rx, ry)
local tx, ty = self.x + rx, self.y + ry
self.x, self.y = tx, ty
end,
draw = function(self)
sprite = string.format("$%d%s$0", self.color, self.glyph)
tarn_print(self.x, self.y, sprite)
end
}
-- Generate an empty map (1-based index)
for y = 1, map_height do
for x = 1, map_width do
local tile
if math.random(3) == math.random(3) then
tile = { glyph = '#', color = color.grey }
else
tile = { glyph = '.', color = color.green }
end
map[y*map_width+x] = tile
end
end
-- Generate the FOV overlay (0-based index)
fov = tarn_fov_new(map_width, map_height)
for y = 0,map_height-1 do
for x = 0,map_width-1 do
if map[(y+1)*map_width+(x+1)].glyph == "#" then
tarn_fov_set(fov, x, y, false, false)
else
tarn_fov_set(fov, x, y, true, true)
end
end
end
tarn_fov_do(fov, player.x-1, player.y-1, 3)
-- Main game loop
while true do
tarn_clear()
tarn_fov_do(fov, player.x-1, player.y-1, 3)
for y = 1, map_height do
for x = 1, map_width do
if tarn_fov_has(fov, x-1, y-1) then
tile = string.format("$%d%s$0", map[y*map_width+x].color, map[y*map_width+x].glyph)
else
tile = string.format("$%d%s$0", color.darkgrey, map[y*map_width+x].glyph)
end
tarn_print(x, y, tile)
end
end
player:draw()
tarn_draw()
key = tarn_getkey()
key.c_str = string.format("%c", key.c)
if key.c_str == "Q" then
break
elseif key.vk == vk.up then
player:move(0, -1)
elseif key.vk == vk.down then
player:move(0, 1)
elseif key.vk == vk.left then
player:move(-1, 0)
elseif key.vk == vk.right then
player:move(1, 0)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment