+ },
+ },
+ {
+ name='circ',
+ lines={
+ '-- a snazzy circle',
+ 'cx, cy, N = 30, 100, 100',
+ 'function dist2(x1,y1, x2,y2)',
+ ' return (x2-x1)^2 + (y2-y1)^2',
+ 'end',
+ '',
+ 'function car.draw()',
+ ' for x=cx-N,cx+N do',
+ ' for y=cy-N,cy+N do',
+ ' if dist2(cx,cy, x,y) < N*N then',
+ ' color(abs(x-cx)/N, abs(y-cy)/N, 0.5)',
+ ' pt(x,y)',
+ 'end end end end',
+ '',
+ 'function car.mousepressed(x,y, b)',
+ ' cx,cy = x,y',
+ 'end',
+ },
+ },
+ {
+ name='pacman',
+ lines={
+ '-- pacman',
+ 'function init_balls()',
+ ' balls = {}',
+ ' for x=50,love.graphics.getWidth()-1,50 do',
+ ' for y=30,love.graphics.getHeight()-1,50 do',
+ ' table.insert(balls, {x=x, y=y, show = true})',
+ 'end end end',
+ 'init_balls()',
+ '',
+ 'function eat_ball()',
+ ' for _,b in ipairs(balls) do',
+ ' if b.x == x and b.y == y then',
+ ' b.show = false',
+ 'end end end',
+ '',
+ 'x, y = 30, 30',
+ 'dx = 5',
+ 'dang = 0 -- change in mouth angle',
+ '',
+ 'function car.draw()',
+ ' love.graphics.setColor(0.8, 0.8, 0)',
+ " love.graphics.arc('fill', x,y, 20, dang + 0.5, dang + 2*math.pi - 0.5)",
+ ' for _,b in ipairs(balls) do',
+ ' if b.show then',
+ " love.graphics.circle('fill', b.x, b.y, 10)",
+ 'end end end',
+ '',
+ 'function car.update(dt)',
+ ' x = x+dx',
+ ' eat_ball()',
+ ' if x > love.graphics.getWidth() then',
+ ' y, dx = y+50, -5',
+ ' dang = math.pi',
+ ' elseif x < 0 then',
+ ' y, dx = y+50, 5',
+ ' dang = 0',
+ ' end',
+ ' if y > love.graphics.getHeight() then',
+ ' y = 30',
+ ' init_balls()',
+ ' end',
+ 'end',
+ },
+ },
+ {
+ name='trails',
+ lines={
+ '-- sweep trails with your finger',
+ '-- (fingers on a phone or tablet)',
+ 'W,H = g.getDimensions()',
+ 'T = 0.1 -- seconds',
+ 'U = 0',
+ '',
+ 'trails = {}',
+ '',
+ 'function car.draw()',
+ ' for _,trail in pairs(trails) do',
+ ' color(trail.r, trail.g,trail.b)',
+ ' for _,c in ipairs(trail) do',
+ " circ('fill', c.x,c.y, c.r)",
+ 'end end end',
+ '',
+ 'function car.update(dt)',
+ ' U = U + dt',
+ ' if U < T then return end',
+ ' U = U - T',
+ ' local touches = love.touch.getTouches()',
+ ' for _,id in ipairs(touches) do',
+ ' local x,y = love.touch.getPosition(id)',
+ ' if trails[id] then',
+ ' table.insert(trails[id], 1, {x=x, y=y, r=100})',
+ 'end end',
+ ' for id,trail in pairs(trails) do',
+ ' for i=#trail,1,-1 do',
+ ' local c = trail[i]',
+ ' c.r = c.r - 10',
+ ' if c.r <= 0 then table.remove(trail, i) end',
+ ' end',
+ ' if #trail == 0 then trails[id] = nil end',
+ ' end',
+ 'end',
+ '',
+ 'function car.touchpressed(id, x,y, ...)',
+ ' trails[id] = {r=rand(), g=rand(), b=rand()}',
+ 'end',