Question: How do you draw a rectangle in Love2D?
Answer
In Love2D, drawing a rectangle is accomplished through the love.graphics.rectangle
function. Here's a basic example of how to use it:
function love.draw() -- Set the color for the rectangle (R, G, B, A) love.graphics.setColor(1, 0, 0, 1) -- Red color -- Draw a filled rectangle -- Parameters: mode, x, y, width, height love.graphics.rectangle('fill', 100, 100, 200, 150) -- Set a different color (optional) love.graphics.setColor(0, 1, 0, 1) -- Green color -- Draw an outline of a rectangle -- Parameters: mode, x, y, width, height love.graphics.rectangle('line', 400, 100, 200, 150) end
Here’s what each parameter of love.graphics.rectangle
means:
- The first parameter is the mode. It can be 'fill' to draw a filled rectangle, or 'line' to draw just the outline.
- The second and third parameters are the
x
andy
coordinates for the top-left corner of the rectangle. - The fourth and fifth parameters are the width and height of the rectangle.
Before drawing shapes, it's common to set the current drawing color using love.graphics.setColor()
. This function takes four arguments defining the red, green, blue, and alpha values, with each value being between 0 and 1.
When you're done drawing shapes or at the start of a new frame, it might be necessary to reset the drawing color to white with full opacity:
love.graphics.setColor(1, 1, 1, 1)
This ensures that any textures or images drawn afterward will render with their original colors and full opacity.
Was this content helpful?
Other Common Game Engines Questions (and Answers)
- Can You Use C# in Unreal Engine?
- Is Unreal Engine Open Source?
- Can Unreal Engine make 2D games?
- Does Unreal Engine Use C++?
- Does Unreal Engine Use Python?
- What Language Does Unreal Engine Use?
- Does Unreal Engine Use JavaScript?
- Does Unreal Engine work on Linux?
- How Do I Uninstall Unreal Engine 5?
- Is Blender or Unreal Engine Better?
- Is Unreal Engine Good for Beginners?
- Does Unreal Engine Work on Mac?
Free System Design on AWS E-Book
Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.
Switch & save up to 80%
Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement. Instantly experience up to a 25X boost in performance and 80% reduction in cost