Love - Drawing shapes

From TRCCompSci - AQA Computer Science
Revision as of 13:05, 4 June 2019 by Admin (talk | contribs) (Drawing shapes)
Jump to: navigation, search

Requirements

You need to have followed the installation process for the Love engine.

You also need to have created a minimal game (ie a new folder, with a 'main.lua' file)

You need to have added this code to 'main.lua':

function love.load()

end

function love.update(dt)

end
 
function love.draw()

end

Drawing shapes

Now in the draw method add the following to draw a rectangle (or square):

love.graphics.rectangle('fill',0,0,100,50) -- 0,0 is the X & Y 100,50 is the Width & Height
love.graphics.rectangle('line', 150,0,200,200) -- 'fill' will fill the shape 'line' just give the outline

Now add the following to draw an circle:

love.graphics.circle('fill',0,250,100,100) -- 0,250 is the X & Y of the center point of the circle 100,100 is the Width & Height
love.graphics.circle('line', 150,250,200,200) -- 'fill' will fill the shape 'line' just give the outline

Drawing Lines

Now in the draw method add the following to draw a line:

love.graphics.setLineWidth(3)
love.graphics.setLineStyle("smooth")
love.graphics.line(15, 25, 69, 89)
love.graphics.line(200,50, 400,50, 500,300, 100,300, 200,50)

The setLineWidth will set the thickness of the line, the setLineStyle can either be "smooth" or "rough". Each pair of values are the X & Y of each point, the last line above gives 5 point 4 for the points and one to return to the starting coordinate.

Drawing a Polygon

Now in the draw menu add the following to draw a polygon:

love.graphics.polygon('fill', 100, 100, 200, 100, 150, 200)

Notice that you don't need to add a point for the end, it will automatically draw the line to the start. You could provide a list of points to draw like this:

local vertices = {100, 100, 200, 100, 150, 200}
love.graphics.polygon('fill', vertices)