Created
March 23, 2023 14:23
-
-
Save simsaens/3b231a928392fa109260a22acf7abb4c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Dungeon variables | |
local dungeonWidth = 16 -- The width of the dungeon in tiles | |
local dungeonHeight = 16 -- The height of the dungeon in tiles | |
local tileSize = 40 -- The size of each tile in pixels | |
local dungeon = {} -- The dungeon map | |
local rooms = {} -- The list of rooms in the dungeon | |
local doors = {} -- The list of doors in the dungeon | |
local traps = {} -- The list of traps in the dungeon | |
-- Colors | |
local colors = { | |
background = color(168, 0, 98), -- Retro magenta | |
floor = color(255, 231, 191), -- Beige | |
wall = color(235, 215, 140), -- Light brown | |
door = color(255, 153, 0), -- Orange | |
trap = color(255, 0, 0) -- Red | |
} | |
-- Generate the dungeon map | |
function generateDungeon() | |
-- Initialize the dungeon with all walls | |
for x = 1, dungeonWidth do | |
dungeon[x] = {} | |
for y = 1, dungeonHeight do | |
dungeon[x][y] = "wall" | |
end | |
end | |
-- Add some rooms to the dungeon | |
for i = 1, 10 do | |
local roomWidth = math.random(3, 6) | |
local roomHeight = math.random(3, 6) | |
local x = math.random(1, dungeonWidth - roomWidth) | |
local y = math.random(1, dungeonHeight - roomHeight) | |
local room = { | |
x = x, | |
y = y, | |
width = roomWidth, | |
height = roomHeight | |
} | |
for xi = x, x + roomWidth - 1 do | |
for yi = y, y + roomHeight - 1 do | |
dungeon[xi][yi] = "floor" | |
end | |
end | |
table.insert(rooms, room) | |
end | |
-- Connect the rooms with doors | |
for i, room1 in ipairs(rooms) do | |
for j, room2 in ipairs(rooms) do | |
if i ~= j then | |
local x1 = room1.x + math.random(1, room1.width - 1) | |
local y1 = room1.y + math.random(1, room1.height - 1) | |
local x2 = room2.x + math.random(1, room2.width - 1) | |
local y2 = room2.y + math.random(1, room2.height - 1) | |
if x1 == x2 then | |
for yi = math.min(y1, y2), math.max(y1, y2) do | |
dungeon[x1][yi] = "floor" | |
end | |
table.insert(doors, {x = x1, y = math.floor((y1 + y2) / 2)}) | |
elseif y1 == y2 then | |
for xi = math.min(x1, x2), math.max(x1, x2) do | |
dungeon[xi][y1] = "floor" | |
end | |
table.insert(doors, {x = math.floor((x1 + x2) / 2), y = y1}) | |
end | |
end | |
end | |
end | |
-- Add some traps to the dungeon | |
for i = 1, math.floor(#rooms / 2) do | |
local room = rooms[math.random(#rooms)] | |
local x = math.random(room.x + 1, room.x + room.width - 2) | |
local y = math.random(room.y + 1, room.y + room.height - 2) | |
dungeon[x][y] = "trap" | |
table.insert(traps, {x = x, y = y}) | |
end | |
end | |
function setup() | |
-- Set up the screen | |
displayMode(FULLSCREEN) | |
noSmooth() | |
rectMode(CORNER) | |
-- Generate the dungeon | |
generateDungeon() | |
end | |
function draw() | |
-- Set the background color | |
background(colors.background) | |
translate(WIDTH/2 - (40 * 16)/2, HEIGHT/2 - (40 * 16)/2) | |
-- Draw the dungeon | |
for x = 1, dungeonWidth do | |
for y = 1, dungeonHeight do | |
if dungeon[x][y] == "floor" then | |
fill(colors.floor) | |
elseif dungeon[x][y] == "wall" then | |
fill(colors.wall) | |
elseif dungeon[x][y] == "door" then | |
fill(colors.door) | |
elseif dungeon[x][y] == "trap" then | |
fill(colors.trap) | |
end | |
rect((x - 1) * tileSize, (y - 1) * tileSize, tileSize, tileSize) | |
end | |
end | |
-- Draw the traps | |
pushStyle() | |
fill(255, 0, 0) | |
noStroke() | |
for i, trap in ipairs(traps) do | |
ellipse((trap.x - 0.5) * tileSize, (trap.y - 0.5) * tileSize, tileSize / 2) | |
end | |
popStyle() | |
end | |
function touched(touch) | |
if touch.state == BEGAN then | |
-- Reset the dungeon | |
dungeon = {} | |
rooms = {} | |
doors = {} | |
traps = {} | |
generateDungeon() | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment