Skip to content
TheChatArchiveThe Chat Archive
o1-preview-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

create a fun rogue-like game in lua love with procedurally map and a TON of features,…

  1. User

    create a fun rogue-like game in lua love with procedurally map and a TON of features, feel free to add anything else :)

  2. Assistant

    Sure! I'd be happy to help you create a fun roguelike game using Lua and the LÖVE 2D framework. Below is a basic implementation that includes procedural map generation and various features to get you started. You can expand upon this foundation by adding more features as you see fit.

    Overview

    We'll create a simple roguelike game with the following features:

    • Procedurally generated dungeon maps
    • Player movement and interaction
    • Enemies with basic AI
    • Collectible items
    • Simple combat system
    • Fog of war (optional, for added challenge)

    Setting Up

    First, make sure you have LÖVE 2D installed on your machine. You can run the game by navigating to your project directory and running love . in your terminal.

    We'll have the following file structure:

    • main.lua - The main script file.
    • Optionally, you can create separate modules for organization (e.g., player.lua, enemy.lua, map.lua).

    Main Script (main.lua)

    Here's the code for main.lua:

    -- main.lua
    
    -- Screen dimensions
    local SCREEN_WIDTH = 800
    local SCREEN_HEIGHT = 600
    local TILE_SIZE = 32
    
    -- Game state
    local gameMap = {}
    local player = {}
    local enemies = {}
    local items = {}
    
    -- Load assets
    local tileset = {}
    local quads = {}
    
    -- Seed randomizer
    math.randomseed(os.time())
    
    function love.load()
        love.window.setMode(SCREEN_WIDTH, SCREEN_HEIGHT)
        love.window.setTitle("Lua Roguelike")
    
        -- Load tileset image
        tileset.image = love.graphics.newImage("tileset.png")
        tileset.width = tileset.image:getWidth()
        tileset.height = tileset.image:getHeight()
    
        -- Define quads (assuming tileset.png is a grid of tiles)
        quads.floor = love.graphics.newQuad(0, 0, TILE_SIZE, TILE_SIZE, tileset.width, tileset.height)
        quads.wall = love.graphics.newQuad(TILE_SIZE, 0, TILE_SIZE, TILE_SIZE, tileset.width, tileset.height)
        quads.player = love.graphics.newQuad(0, TILE_SIZE, TILE_SIZE, TILE_SIZE, tileset.width, tileset.height)
        quads.enemy = love.graphics.newQuad(TILE_SIZE, TILE_SIZE, TILE_SIZE, TILE_SIZE, tileset.width, tileset.height)
        quads.item = love.graphics.newQuad(0, TILE_SIZE * 2, TILE_SIZE, TILE_SIZE, tileset.width, tileset.height)
    
        generateDungeon()
    
        -- Initialize player
        player.x = gameMap.spawnX
        player.y = gameMap.spawnY
        player.hp = 10
    end
    
    function love.update(dt)
        -- Handle player input
        handleInput()
    
        -- Update enemies
        updateEnemies()
    end
    
    function love.draw()
        drawMap()
        drawItems()
        drawPlayer()
        drawEnemies()
        drawHUD()
    end
    

    Procedural Dungeon Generation

    We'll use a simple algorithm to generate a dungeon:

    -- Dungeon Generation
    function generateDungeon()
        local width = math.floor(SCREEN_WIDTH / TILE_SIZE)
        local height = math.floor(SCREEN_HEIGHT / TILE_SIZE)
    
        gameMap.width = width
        gameMap.height = height
        gameMap.tiles = {}
    
        -- Initialize map with walls
        for x = 1, width do
            gameMap.tiles[x] = {}
            for y = 1, height do
                gameMap.tiles[x][y] = "wall"
            end
        end
    
        -- Create rooms and corridors
        local roomCount = 10
        local rooms = {}
    
        for i = 1, roomCount do
            local room = {}
            room.x = math.random(2, width - 10)
            room.y = math.random(2, height - 10)
            room.w = math.random(4, 8)
            room.h = math.random(4, 8)
    
            -- Carve out the room
            for x = room.x, math.min(room.x + room.w, width - 1) do
                for y = room.y, math.min(room.y + room.h, height - 1) do
                    gameMap.tiles[x][y] = "floor"
                end
            end
    
            -- Connect to previous room
            if i > 1 then
                local prevRoom = rooms[i - 1]
                local currCenterX = math.floor(room.x + room.w / 2)
                local currCenterY = math.floor(room.y + room.h / 2)
                local prevCenterX = math.floor(prevRoom.x + prevRoom.w / 2)
                local prevCenterY = math.floor(prevRoom.y + prevRoom.h / 2)
    
                if math.random(0, 1) == 1 then
                    carveHorizontalCorridor(prevCenterX, currCenterX, prevCenterY)
                    carveVerticalCorridor(prevCenterY, currCenterY, currCenterX)
                else
                    carveVerticalCorridor(prevCenterY, currCenterY, prevCenterX)
                    carveHorizontalCorridor(prevCenterX, currCenterX, currCenterY)
                end
            end
    
            table.insert(rooms, room)
        end
    
        -- Place the player in the center of the first room
        gameMap.spawnX = math.floor(rooms[1].x + rooms[1].w / 2)
        gameMap.spawnY = math.floor(rooms[1].y + rooms[1].h / 2)
    
        -- Place enemies and items
        placeEntities(rooms)
    end
    
    function carveHorizontalCorridor(x1, x2, y)
        for x = math.min(x1, x2), math.max(x1, x2) do
            if gameMap.tiles[x] and gameMap.tiles[x][y] then
                gameMap.tiles[x][y] = "floor"
            end
        end
    end
    
    function carveVerticalCorridor(y1, y2, x)
        for y = math.min(y1, y2), math.max(y1, y2) do
            if gameMap.tiles[x] and gameMap.tiles[x][y] then
                gameMap.tiles[x][y] = "floor"
            end
        end
    end
    
    function placeEntities(rooms)
        for i, room in ipairs(rooms) do
            if i > 1 then -- Skip the first room
                -- Add an enemy
                local enemy = {}
                enemy.x = math.random(room.x + 1, room.x + room.w - 1)
                enemy.y = math.random(room.y + 1, room.y + room.h - 1)
                enemy.hp = 5
                table.insert(enemies, enemy)
    
                -- Add an item
                local item = {}
                item.x = math.random(room.x + 1, room.x + room.w - 1)
                item.y = math.random(room.y + 1, room.y + room.h - 1)
                item.type = "potion"
                table.insert(items, item)
            end
        end
    end
    

    Player Input and Movement

    function handleInput()
        if love.keyboard.isDown("up") or love.keyboard.isDown("w") then
            tryMovePlayer(0, -1)
        elseif love.keyboard.isDown("down") or love.keyboard.isDown("s") then
            tryMovePlayer(0, 1)
        elseif love.keyboard.isDown("left") or love.keyboard.isDown("a") then
            tryMovePlayer(-1, 0)
        elseif love.keyboard.isDown("right") or love.keyboard.isDown("d") then
            tryMovePlayer(1, 0)
        end
    end
    
    function tryMovePlayer(dx, dy)
        local targetX = player.x + dx
        local targetY = player.y + dy
    
        if isWalkable(targetX, targetY) then
            -- Check for enemies
            local enemy = getEnemyAt(targetX, targetY)
            if enemy then
                attackEnemy(enemy)
            else
                player.x = targetX
                player.y = targetY
    
                -- Check for items
                local item = getItemAt(player.x, player.y)
                if item then
                    pickUpItem(item)
                end
            end
        end
    end
    
    function isWalkable(x, y)
        if gameMap.tiles[x] and gameMap.tiles[x][y] then
            return gameMap.tiles[x][y] == "floor"
        else
            return false
        end
    end
    

    Enemies and Simple AI

    function updateEnemies()
        for i, enemy in ipairs(enemies) do
            -- Simple AI: move towards the player if close enough
            local dx = player.x - enemy.x
            local dy = player.y - enemy.y
            local distance = math.abs(dx) + math.abs(dy)
    
            if distance < 6 then
                local stepX = 0
                local stepY = 0
    
                if dx ~= 0 then stepX = dx / math.abs(dx) end
                if dy ~= 0 then stepY = dy / math.abs(dy) end
    
                tryMoveEnemy(enemy, stepX, stepY)
            end
        end
    end
    
    function tryMoveEnemy(enemy, dx, dy)
        local targetX = enemy.x + dx
        local targetY = enemy.y + dy
    
        if targetX == player.x and targetY == player.y then
            -- Attack the player
            player.hp = player.hp - 1
            if player.hp <= 0 then
                -- Game over
                love.event.quit()
            end
        elseif isWalkable(targetX, targetY) and not getEnemyAt(targetX, targetY) then
            enemy.x = targetX
            enemy.y = targetY
        end
    end
    
    function attackEnemy(enemy)
        -- Attack logic
        enemy.hp = enemy.hp - 2
        if enemy.hp <= 0 then
            -- Remove enemy
            for i, e in ipairs(enemies) do
                if e == enemy then
                    table.remove(enemies, i)
                    break
                end
            end
        end
    end
    

    Items and Inventory

    function pickUpItem(item)
        -- Simple item effect
        if item.type == "potion" then
            player.hp = player.hp + 5
        end
    
        -- Remove the item from the map
        for i, it in ipairs(items) do
            if it == item then
                table.remove(items, i)
                break
            end
        end
    end
    

    Utility Functions

    function getEnemyAt(x, y)
        for _, enemy in ipairs(enemies) do
            if enemy.x == x and enemy.y == y then
                return enemy
            end
        end
        return nil
    end
    
    function getItemAt(x, y)
        for _, item in ipairs(items) do
            if item.x == x and item.y == y then
                return item
            end
        end
        return nil
    end
    

    Drawing Functions

    function drawMap()
        for x = 1, gameMap.width do
            for y = 1, gameMap.height do
                local tile = gameMap.tiles[x][y]
                if tile == "floor" then
                    love.graphics.draw(tileset.image, quads.floor, (x - 1) * TILE_SIZE, (y - 1) * TILE_SIZE)
                elseif tile == "wall" then
                    love.graphics.draw(tileset.image, quads.wall, (x - 1) * TILE_SIZE, (y - 1) * TILE_SIZE)
                end
            end
        end
    end
    
    function drawPlayer()
        love.graphics.draw(tileset.image, quads.player, (player.x - 1) * TILE_SIZE, (player.y - 1) * TILE_SIZE)
    end
    
    function drawEnemies()
        for _, enemy in ipairs(enemies) do
            love.graphics.draw(tileset.image, quads.enemy, (enemy.x - 1) * TILE_SIZE, (enemy.y - 1) * TILE_SIZE)
        end
    end
    
    function drawItems()
        for _, item in ipairs(items) do
            love.graphics.draw(tileset.image, quads.item, (item.x - 1) * TILE_SIZE, (item.y - 1) * TILE_SIZE)
        end
    end
    
    function drawHUD()
        love.graphics.setColor(255, 255, 255)
        love.graphics.print("HP: " .. player.hp, 10, 10)
    end
    

    Notes:

    • Tileset Image: You will need to create or obtain a tileset.png image that contains your game tiles. This image should be arranged in a grid, where each tile is TILE_SIZE pixels square.

      • For example, the first row might contain floor and wall tiles.
      • The second row might contain the player and enemy sprites.
      • The third row can contain item sprites.
    • Game Loop: This basic game loop handles player input, updates enemies, and draws everything on the screen.

    • Player and Enemy Stats: You can expand the player and enemy tables to include more stats like attack power, defense, experience, etc.

    • Combat System: Currently, combat is very simple. You can enhance it by adding more depth, such as different weapons, abilities, and enemy behaviors.

    • Fog of War: For an added challenge, implement a fog of war system that hides unexplored areas.

    • Saving and Loading: Implement save and load functionality to preserve the game state between sessions.

    Additional Features to Consider

    • Multiple Levels: Allow the player to descend deeper into the dungeon with increasing difficulty.

    • Inventory System: Enable the player to collect and manage items, weapons, and equipment.

    • Skill System: Introduce skills or abilities that the player can learn.

    • Random Events: Add traps, secret rooms, or random events to make exploration more exciting.

    Conclusion

    This basic framework should provide a solid starting point for your roguelike game. From here, you can continue to build upon it by adding new features, improving graphics, and enhancing gameplay mechanics. Don't hesitate to experiment and add your unique ideas to make the game even more fun!


    If you have any questions or need further assistance with specific features, feel free to ask!