Also, did you know you can just
Code: Select all
color = {255, 255, 255} -- r, g, b
love.graphics.setColor(color)
Instead of trying to name your RGB values, just have numerically indexed RGB. :)
Code: Select all
color = {255, 255, 255} -- r, g, b
love.graphics.setColor(color)
Code: Select all
--Let's make a class library--
--We first make a table as our base class object..
local class = {}
--Then we give it an extension function, this is what you call to make your new class.
--The setmetatable function is what ties the new class to the class library and any sub classes you make
function class:extend(subClass)
return setmetatable(subClass or {}, {__index = self})
end
--Then add a constructor function, this function is what you call to make a new copy of your object think of it like a big rubber stamp that stamps out objects.
--Notice the (...) at the end this will take any arguments you pass the function and pass them straight to the objects init function...
function class:new(...)
local copy = setmetatable({}, {__index = self})
return copy, copy:init(...)
end
--The init function you would write for most objects themselves because it is where you set up your objects.
--I put one here so it won't crash if I forget to write one
function class:init(...) end
--Always remember to return your table!
return class
Code: Select all
--first off require the class library...
local class = require "class"
--Now let's make a shape...
local shape = class:extend() --This is a base class so we don't pass a parent class
--First we write the init function that will be called as the object gets created...
--Notice we don't have to write the function shape:new(...) as that is in the class library and any function calls to shape will
--look in the shape table first and if the function is not found it will look to the parent which in this case is class
function shape:init(x, y, w, h, col)
self:setPosition(x, y)
self:setSize(x, y)
self.col = col
end
--Let's write those functions we just called as well :P
function shape:setPosition(x, y)
self.x, self.y = x, y
end
function shape:setSize(w, h)
self.w, self.h = w, h
end
function shape:getColour()
return self.col[1], self.col[2], self.col[3], self.col[4]
end
--Also something to draw to the screen...
function shape:draw()
love.graphics.setColor(self:getColour())
love.graphics.rectangle("fill", self.x - self.w / 2, self.y - self.h / 2, self.w, self.h)
end
return shape
Code: Select all
--first off require the shape library
local shape = require "shape"
--Start making a circle...
local circle = shape:extend()
--Circles need fewer arguments than the basic shape to define them so let's write a new init...
function circle:init(x, y, rad, col)
self:setPosition(x, y) --We can still use any functions we wrote in shape so our circle will use setPosition() from shape
self.radius = rad
self.col = col
end
--We probably need a new Draw too as rectangles can be a bit too quadrilateral to truly capture the essence of a circle...
function circle:draw()
love.graphics.setColor(self:getColour())
love.graphics.circle("fill", self.x, self.y, self.rad)
end
return circle
Code: Select all
local circle = require "circle"
function love.load()
myCircle = circle:new(50, 50, 10, {255,255,255,255})
end
function love.draw()
myCircle:draw()
end
Code: Select all
if not wearTheseGlasses() then
chewing_on_trashcan = true
end
Users browsing this forum: Bing [Bot], Google [Bot], Semrush [Bot] and 7 guests