# ❤️ LÖVE (Love2D) Game Development Guide (Lua) LÖVE is a framework used to make 2D games in Lua. It handles the complicated stuff (like rendering graphics and handling input) and lets you focus on the game logic. Every LÖVE game follows a simple structure defined by three main functions. ## 1. The Core Game Loop Structure Every LÖVE game must have a main file (usually `main.lua`) which defines the three essential functions that control the entire game lifecycle. |Function|When it Runs|Purpose| |---|---|---| |`love.load()`|**Once, at the very beginning.**|Initialization: Load all assets (images, sounds), set up initial variables, and define starting values.| |`love.update(dt)`|**Repeatedly, many times per second.**|Logic: Handle game state changes, move objects, check for collisions, and calculate physics. `dt` is the time passed since the last frame.| |`love.draw()`|**Repeatedly, right after `love.update()`.**|Rendering: Draw everything to the screen (images, text, shapes). **Never** put game logic here.| ### The Essential Template ``` -- Runs once when the game starts. function love.load() print("Game Initialized!") -- Setup our player variables player = { x = 100, y = 100, speed = 200 -- Pixels per second } -- Load an image for the player -- Use a simple placeholder image URL for documentation purposes. -- In a real Love2D environment, you'd load a local file: love.graphics.newImage("player.png") player_image = love.graphics.newImage("[https://placehold.co/64x64/0000FF/FFFFFF?text=P](https://placehold.co/64x64/0000FF/FFFFFF?text=P)") end -- Runs repeatedly for game logic (e.g., 60 times per second) function love.update(dt) -- Handle movement logic based on key presses if love.keyboard.isDown("right") then -- Move player_x by speed multiplied by delta-time (dt) -- dt ensures speed is constant regardless of frame rate. player.x = player.x + player.speed * dt elseif love.keyboard.isDown("left") then player.x = player.x - player.speed * dt end end -- Runs repeatedly for drawing graphics to the screen function love.draw() -- Draw the player image at the player's current (x, y) position love.graphics.draw(player_image, player.x, player.y) -- Draw the player's position as text love.graphics.print("Player X: " .. player.x, 10, 10) end ``` ## 2. Key Modules (Libraries) LÖVE organizes its features into modules, all starting with `love.`. ### 2.1. `love.graphics` (Drawing) This module handles everything you see on the screen. |Function|Purpose|Example| |---|---|---| |`love.graphics.setBackgroundColor(r, g, b, a)`|Sets the background color of the screen (0-255 or 0-1 range).|`love.graphics.setBackgroundColor(0.2, 0.4, 0.6)` (Dark Blue)| |`love.graphics.draw(drawable, x, y)`|Draws an image, text, or canvas to the screen.|`love.graphics.draw(player_image, 100, 100)`| |`love.graphics.print(text, x, y)`|Draws text to the screen.|`love.graphics.print("Score: 100", 50, 50)`| |`love.graphics.rectangle("fill", x, y, w, h)`|Draws a solid rectangle.|`love.graphics.rectangle("fill", 200, 200, 50, 50)`| |`love.graphics.setColor(r, g, b, a)`|Sets the color for all subsequent drawing operations.|`love.graphics.setColor(1, 0, 0)` (Sets color to Red)| ### 2.2. `love.keyboard` (Input) This module handles key presses and releases. |Function|Purpose|Example| |---|---|---| |`love.keyboard.isDown(key)`|Checks if a key is currently being held down (best used in `love.update`).|`if love.keyboard.isDown("space") then shoot() end`| |`love.keypressed(key)`|**Callback:** Runs once when a key is pressed down (best for single actions, like jumping).|See example below.| **Example of `love.keypressed`:** ``` -- This function is part of the core structure, but only runs on a key press event. function love.keypressed(key) if key == "escape" then -- This function tells LÖVE to close the game love.event.quit() elseif key == "j" then print("Jump initiated!") -- Trigger a jump animation or logic here end end ``` ### 2.3. `love.mouse` (Input) This handles mouse input (position, button presses). |Function|Purpose|Example| |---|---|---| |`love.mouse.getX()`|Gets the current X coordinate of the mouse.|`local mx = love.mouse.getX()`| |`love.mouse.isDown(button)`|Checks if a mouse button is held down (`1` for left, `2` for right).|`if love.mouse.isDown(1) then click_logic() end`| |`love.mousepressed(x, y, button)`|**Callback:** Runs once when a mouse button is pressed.|`function love.mousepressed(x, y, button) print("Click!") end`| ## 3. Essential Game Logic: Delta Time (`dt`) The `dt` variable in `love.update(dt)` is perhaps the most important concept for smooth games. 1. **What is `dt`?** It stands for "Delta Time"—the time elapsed (in seconds) since the last time `love.update` ran. It is usually a very small number (e.g., $0.016$ for $60$ FPS). 2. **Why use it?** If the game runs slower (low FPS), `dt` is larger. If it runs faster (high FPS), `dt` is smaller. By multiplying movement by `dt`, you ensure that objects move the **same distance over time** regardless of the computer's frame rate. ### Using `dt` for Speed (The Correct Way) If you want a player to move $200$ pixels per second: ``` -- Player speed is defined in pixels/second local SPEED = 200 function love.update(dt) -- This is framerate independent movement player.x = player.x + SPEED * dt -- If dt is 0.016 (60 FPS), you move 200 * 0.016 = 3.2 pixels. -- If dt is 0.032 (30 FPS), you move 200 * 0.032 = 6.4 pixels. -- In both cases, the player travels 200 pixels over 1 second. end ``` ### The Wrong Way (Without `dt`) ``` -- INCORRECT: Speed is tied to framerate player.x = player.x + 5 -- On a fast computer (high FPS), this moves too fast. -- On a slow computer (low FPS), this moves too slow. ``` ## 4. Setting up the Window You can define a separate function, `love.conf`, to configure the game window settings. ``` -- Must be in the main.lua file and outside the main loop functions function love.conf(t) -- t is a table that holds the configuration settings t.title = "My First Love2D Game" -- The text in the window title bar t.window.width = 800 -- Set the window width to 800 pixels t.window.height = 600 -- Set the window height to 600 pixels t.window.vsync = true -- Use VSync to cap the framerate (recommended) t.identity = "my_awesome_game" -- Unique name for save game files end ``` This guide should give you a fantastic foundation for starting your first LÖVE project! The best way to learn is to start experimenting with the `love.load`, `love.update`, and `love.draw` functions.