carousel.love/0078-Example_panes

273 lines
6.8 KiB
Plaintext

-- some examples that each get loaded into a pane
-- each example has a name, so we can avoid bringing back up an example someone deleted.
-- Be careful not to rename example panes; that will cause them to pop up again and annoy people who've already deleted them.
-- If you delete a name, be careful not to reuse it in future.
Example_panes = {
{
name='expr',
lines={
'-- Some examples; feel free to delete.',
'-- First example: you can perform',
'-- simple calculations.',
'',
'2+3',
'',
"-- Try hitting the 'run' button above.",
"-- When you're done, tap on the",
'-- right margin to go to the next',
'-- example.',
}
},
{
name='print',
lines={
'-- print() calls show up in the area',
"-- below. Hit 'run' and then try scrolling",
'-- through the results.',
'for i=1,20 do',
' print(i)',
'end',
},
},
{
name='draw',
lines={
'-- You can draw on the screen.',
"-- After you hit 'run', try hitting 'hide'.",
'-- The editor will disappear. Hit show',
"-- to bring it back.",
'love.graphics.line(100,100, 200,300)',
},
},
{
name='pattern',
lines={
'-- A larger drawing: A checkerboard',
'-- Notice Safe_width and Safe_height',
'-- for the bounds of the screen.',
'n = 100',
'for x=0,Safe_width,n do',
' for y=0,Safe_height,n do',
' if (x+y)/n%2 == 0 then',
' love.graphics.setColor(0.9, 0.7, 0.7)',
' else',
' love.graphics.setColor(0.7, 0.7, 0.9)',
' end',
" love.graphics.rectangle('fill', x,y, n,n)",
' end',
'end',
},
},
{
name='animation',
lines={
'-- For animations, you can define various',
'-- functions that will get called for you.',
'-- LÖVE looks for functions called',
'-- love.draw, love.update, etc.',
"-- You can't define those directly (they",
'-- contain Carousel code), but you can',
'-- define car.draw, car.update,etc.',
'Radius, dRadius = 100, 10',
'function car.draw()',
" love.graphics.circle('fill', 100,100, Radius)",
'end',
'',
'function car.update(dt)',
' Radius = Radius+dRadius',
' if Radius > 500 then dRadius = -dRadius end',
' if Radius < 30 then dRadius = -dRadius end',
'end',
},
},
{
name='interactive',
lines={
'-- For interactivity, LÖVE provides',
'-- functions like love.keypressed,',
'-- love.mousepressed, etc.',
'-- As before, define car.keypressed,',
'-- car.mousepressed, etc.',
'-- These interactive events only activate',
'-- when the editor is hidden using the',
"-- 'hide' button above.",
'--',
'-- Try running this example and then',
'-- pressing and holding the mouse and',
'-- typing keys.',
'-- Then try again after hiding the editor.',
'X, Y, dX = 100, 0, 10',
"Log = ''",
'',
'function car.draw()',
' love.graphics.line(X,Y, X,Y+300)',
' love.graphics.print(Log, 50,50)',
'end',
'',
'function car.update(dt)',
' if X > 500 then dX = -dX end',
' if X < 100 then dX = -dX end',
' X = X+dX',
'end',
'',
'function car.mousepressed(x,y, button)',
' Y = y',
'end',
'',
'function car.mousereleased(x,y, button)',
' Y = 0',
'end',
'',
'function car.keypressed(key)',
' Log = Log..key',
'end',
},
},
{
name='abbreviations',
lines={
'-- Some abbreviations to reduce typing.',
'g = love.graphics',
'pt, line = g.points, g.line',
'rect, poly = g.rectangle, g.polygon',
'circle, arc, ellipse = g.circle, g.arc, g.ellipse',
'color = g.setColor',
'min, max = math.min, math.max',
'floor, ceil = math.floor, math.ceil',
'abs, rand = math.abs, math.random',
'pi, cos, sin = math.pi, math.cos, math.sin',
'touches = love.touch.getTouches',
'touch = love.touch.getPosition',
'audio = love.audio.newSource',
"-- Hit 'run', Now they're available to other",
'-- panes.',
},
},
{
name='circ',
lines={
'-- A snazzy circle',
'-- Try hiding the editor and tapping',
'-- around on screen.',
'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,Safe_width-1,50 do',
' for y=30,Safe_height-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()',
' color(0.8, 0.8, 0)',
" arc('fill', x,y, 20, dang + 0.5, dang + 2*math.pi - 0.5)",
' for _,b in ipairs(balls) do',
' if b.show then',
" circle('fill', b.x, b.y, 10)",
'end end end',
'',
'function car.update(dt)',
' x = x+dx',
' eat_ball()',
' if x > Safe_width then',
' y, dx = y+50, -5',
' dang = math.pi',
' elseif x < 0 then',
' y, dx = y+50, 5',
' dang = 0',
' end',
' if y > Safe_height then',
' y = 30',
' init_balls()',
' end',
'end',
},
},
{
name='trails',
lines={
'-- This one is for a multitouch screen.',
'-- Try hiding the editor and swiping',
'-- your finger around.',
'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',
" circle('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',
' for _,id in ipairs(touches()) do',
' local x,y = touch(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',
},
},
{
name='beginning',
lines={
'-- Over to you. We hope you enjoy Lua Carousel!',
'-- Looking for more inspiration?',
'-- Try https://akkartik.itch.io/carousel/devlog',
'',
'-- P.S. As you create your programs,',
"-- don't forget to hit the 'save' button",
'-- above to preserve them across restarts.',
},
},
}