Difference between revisions of "User:Darkfrei/example functions"
(Created page with " == Draw grid == <source lang="lua"> function draw_grid () local grid_size = 20 love.graphics.setLineWidth (1) love.graphics.setColor(0.25,0.25,0.25) local width, height...") |
(→Draw grid) |
||
Line 15: | Line 15: | ||
end | end | ||
end | end | ||
+ | </source> | ||
+ | |||
+ | |||
+ | == Beep == | ||
+ | |||
+ | <source lang="lua"> | ||
+ | |||
+ | local rate = 44100 | ||
+ | local length = 1/16 | ||
+ | local tone = 344 -- Hz | ||
+ | local p = math.floor(rate/tone) -- 128 | ||
+ | local soundData = love.sound.newSoundData(length*rate, rate, 16, 1) | ||
+ | for i=0, length*rate-1 do soundData:setSample(i, i%p>p/2 and 1 or -1) end | ||
+ | local source = love.audio.newSource(soundData) | ||
+ | source:play() | ||
</source> | </source> |
Revision as of 17:52, 23 August 2021
Draw grid
function draw_grid ()
local grid_size = 20
love.graphics.setLineWidth (1)
love.graphics.setColor(0.25,0.25,0.25)
local width, height = love.graphics.getDimensions( )
for x = grid_size, width-1, grid_size do
love.graphics.line(x, 0, x, height)
end
for y = grid_size, height-1, grid_size do
love.graphics.line(0, y, width, y)
end
end
Beep
local rate = 44100
local length = 1/16
local tone = 344 -- Hz
local p = math.floor(rate/tone) -- 128
local soundData = love.sound.newSoundData(length*rate, rate, 16, 1)
for i=0, length*rate-1 do soundData:setSample(i, i%p>p/2 and 1 or -1) end
local source = love.audio.newSource(soundData)
source:play()