|
| 1 | +--- |
| 2 | +title: Get Started with LÖVE2D and Lua |
| 3 | +author: Ellie Popoca |
| 4 | +uid: 11zhRKeJCWWcD7IkTJBtuK3Mkvo1 |
| 5 | +datePublished: 2025-10-17 |
| 6 | +published: beta |
| 7 | +description: Learn how to set up LÖVE, a popular Lua framework used to make 2D games, and learn the basics to get started. |
| 8 | +header: https://github.com/codedex-io/projects/blob/main/projects/get-started-with-love2d-and-lua/images/banner.gif?raw=true |
| 9 | +bannerImage: https://github.com/codedex-io/projects/blob/main/projects/get-started-with-love2d-and-lua/images/banner.gif?raw=true |
| 10 | +readTime: 30 |
| 11 | +prerequisites: Lua |
| 12 | +versions: None |
| 13 | +tags: |
| 14 | + - game-dev |
| 15 | + - lua |
| 16 | +Courses: |
| 17 | + - lua |
| 18 | +--- |
| 19 | + |
| 20 | +## LÖVE2D |
| 21 | + |
| 22 | +<ImageZoom src="https://github.com/codedex-io/projects/blob/main/projects/get-started-with-love2d-and-lua/images/ERBG554.png?raw=true" /> |
| 23 | + |
| 24 | +[LÖVE2D](https://love2d.org/) is a popular [Lua](https://www.lua.org/about.html) framework that's widely used to make 2D games. It provides tools and functions to draw graphics, play sound, handle input, and manage physics, but it doesn't come with its own viewport or scene viewer like Unity or Roblox Studio. You'll use code to write game logic! |
| 25 | + |
| 26 | +This framework will be a great option if you want to: |
| 27 | + |
| 28 | +- 🧩 Build 2D games |
| 29 | +- 🎱 Prototype game ideas quickly |
| 30 | +- 🌙 Use Lua to explore object-oriented and modular programming concepts |
| 31 | +- ⌨️ Make games that run on Windows, macOS, and Linux |
| 32 | + |
| 33 | +LÖVE2D is minimal and lightweight. You write code, press play, and you're done. Roblox Studio and other game engines have a steeper learning curve due to their ecosystem, physics engine, and networking systems. |
| 34 | + |
| 35 | +Let's get started with downloading LÖVE2D. |
| 36 | + |
| 37 | +## Download LÖVE2D |
| 38 | + |
| 39 | +Lua comes included in LÖVE, and we can begin using it in our editor of choice once downloaded on our computer! |
| 40 | + |
| 41 | +Head to the [LÖVE](https://love2d.org/) website, select your device, and download. |
| 42 | + |
| 43 | +<ImageZoom src="https://github.com/codedex-io/projects/blob/main/projects/get-started-with-love2d-and-lua/images/VXC5433.png?raw=true" /> |
| 44 | + |
| 45 | +### Windows/Linux |
| 46 | + |
| 47 | +You'll want to add LÖVE to your **system environment** variables this time. Open your Control Panel, and search for **environment variables**. You'll see an option that says "Edit the system environment variables" |
| 48 | + |
| 49 | +- Click on this panel, and click **Environment Variables** and select `Path` in **System Variables for YOUR_COMPUTER**, and find `Path` and click **Edit**. |
| 50 | +- Click **New** and hit **Browse** |
| 51 | +- Browse to your program files and find where the LÖVE folder was saved when you downloaded the folder. |
| 52 | +- Select the folder, and press OK to exit. |
| 53 | + |
| 54 | +Add the LÖVE application to your Desktop for easy access. |
| 55 | + |
| 56 | +### MacOS |
| 57 | + |
| 58 | +Once downloaded, unzip the folder and click on the application. If you're unable to open it, head to System Settings > Privacy & Security > and allow LÖVE to be used on your computer. LÖVE will be blocked, and you'll have to accept an internet download. |
| 59 | + |
| 60 | +You'll need to add the LÖVE application to your Desktop or the Dock for easy access to run your game or application. |
| 61 | + |
| 62 | +## Running your first program |
| 63 | + |
| 64 | +To test that everything is functioning correctly, you'll run your first program |
| 65 | + |
| 66 | +- Create a folder and name it **Lua_example**, or whatever you would like! |
| 67 | +- Inside it, create a file called **main.lua**. LÖVE will look for this file to run your game; Otherwise, it won't work. |
| 68 | +- Open this file in your IDE of choice and type in the following lines of code: |
| 69 | + |
| 70 | + |
| 71 | +```lua |
| 72 | +function love.draw() |
| 73 | + love.graphics.print(“Hello World!!”, 300, 400) |
| 74 | +end |
| 75 | +``` |
| 76 | + |
| 77 | +Save your file. |
| 78 | + |
| 79 | +Then, return to the location where you saved your folder, and drag and drop the **Lua_example** folder into the shortcut icon on your desktop or the dock. |
| 80 | + |
| 81 | +In the command line, you can also run your code: |
| 82 | + |
| 83 | +On Mac, you can run: |
| 84 | + |
| 85 | +```terminal |
| 86 | +love /path/to/your/game/folder |
| 87 | +``` |
| 88 | + |
| 89 | +If `love` is not recognized, run the following command in your **Terminal**: |
| 90 | + |
| 91 | +```terminal |
| 92 | +sudo ln -s /Applications/love.app/Contents/MacOS/love /usr/local/bin/love |
| 93 | +``` |
| 94 | + |
| 95 | +On Windows, you'll run: |
| 96 | + |
| 97 | +```terminal |
| 98 | +love C:\path\to\your\game\folder |
| 99 | +``` |
| 100 | + |
| 101 | +You'll see your game appear! |
| 102 | + |
| 103 | +<ImageZoom src="https://github.com/codedex-io/projects/blob/main/projects/get-started-with-love2d-and-lua/images/KAKG29R.png?raw=true" /> |
| 104 | + |
| 105 | + |
| 106 | +## Programming in LÖVE2D |
| 107 | + |
| 108 | + |
| 109 | +To start programming LÖVE2D, you'll need to know that in every LÖVE game, you need 3 main callback functions: |
| 110 | + |
| 111 | +```lua |
| 112 | +function love.load() |
| 113 | + -- Runs once at startup |
| 114 | + -- Initialize variables, load assets |
| 115 | +end |
| 116 | + |
| 117 | +function love.update(dt) |
| 118 | + -- Runs every frame |
| 119 | + -- dt = delta time (time since last frame) |
| 120 | + -- Update game logic here |
| 121 | +end |
| 122 | + |
| 123 | +function love.draw() |
| 124 | + -- Runs every frame after update |
| 125 | + -- Draw everything to the screen |
| 126 | +end |
| 127 | +``` |
| 128 | + |
| 129 | +The `love.load()`, `love.update()`, and `love.draw()` functions all work together to provide you with the organization you'll need to program your game. This will look similar to other built-in functions if you're familiar with Phaser or P5.js, since they both follow this structure as well. |
| 130 | + |
| 131 | +You'll notice in our sample program that we drew some text in the `love.draw()` function. |
| 132 | + |
| 133 | +Here's another example you can copy and paste and look at how this code will look in your output: |
| 134 | + |
| 135 | +```lua |
| 136 | +function love.load() |
| 137 | + x = 50 |
| 138 | + y = 50 |
| 139 | + speed = 200 |
| 140 | +end |
| 141 | + |
| 142 | +function love.update(dt) |
| 143 | + if love.keyboard.isDown("right") then |
| 144 | + x = x + speed * dt |
| 145 | + end |
| 146 | + if love.keyboard.isDown("left") then |
| 147 | + x = x - speed * dt |
| 148 | + end |
| 149 | + if love.keyboard.isDown("down") then |
| 150 | + y = y + speed * dt |
| 151 | + end |
| 152 | + if love.keyboard.isDown("up") then |
| 153 | + y = y - speed * dt |
| 154 | + end |
| 155 | +end |
| 156 | + |
| 157 | +function love.draw() |
| 158 | + love.graphics.circle("fill", x, y, 25) |
| 159 | +end |
| 160 | +``` |
| 161 | +<ImageZoom src="https://github.com/codedex-io/projects/blob/main/projects/get-started-with-love2d-and-lua/images/BWKGJ33.gif?raw=true" /> |
| 162 | + |
| 163 | +- `function love.load()`: Use it to set up initial values and load resources |
| 164 | +- `function love.update(dt)`: Use it to update game logic, positions, check collisions, etc. |
| 165 | +- `function love.draw()`: Use it ONLY for drawing things to the screen |
| 166 | + |
| 167 | + |
| 168 | +## Next Steps |
| 169 | +Now that you're all set up, you can get started with programming your game ideas that you want to develop. Anything 2D: The framework is flexible. If you can imagine a 2D game, you can build it in LÖVE! |
| 170 | + |
| 171 | +Here are some ideas to get you started: |
| 172 | +- **Breakout/Brick Breaker**: A paddle at the bottom bounces a ball to destroy bricks at the top. Learn about physics, bouncing, collision with multiple objects, and incrementally harder levels. |
| 173 | +- **Top-Down Maze Game**: A player navigates through a maze, avoiding enemies that chase them. Collect items to reach the goal. Great for learning movement, pathfinding, and enemy AI basics. |
| 174 | + |
| 175 | + |
| 176 | +Here are some resources for you to check out: |
| 177 | +- [LÖVE Wiki](https://love2d.org/wiki/Main_Page) |
| 178 | +- [LÖVE Tutorials](https://love2d.org/wiki/Category:Tutorials) |
| 179 | +- [Getting Started with LÖVE](https://www.youtube.com/watch?v=tdTitF6z-UA&list=PLS9MbmO_ssyBAc9wBC85_WG9aT88KGxH8&index=14) |
| 180 | +- [Loading & Drawing Images in LÖVE](https://www.youtube.com/watch?v=2U2UdD2jT5U) |
| 181 | + |
| 182 | +Happy coding! |
0 commit comments