Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --# Main
- -- Code 14c - Images
- -- Bruce Elliott
- -- May 2013
- -- Coroutine draw multiple images to screen
- supportedOrientations(LANDSCAPE_RIGHT)
- displayMode(FULLSCREEN)
- function setup()
- print("Coroutine Example")
- useTouch = false
- obj = coroutine.create(object)
- coroutine.resume(obj)
- -- Remove comment to draw an object
- -- each time screen is touched--
- -- useTouch = true
- end
- -- This function gets called once every frame
- function draw()
- -- This sets the line thickness
- strokeWidth(2)
- if useTouch == true then
- -- wait
- else
- drawObject()
- end
- -- Do your drawing here
- end
- function object()
- -- img contains the nodes of an object
- -- to be drawn on the screen
- img ={}
- -- scale is used to change the length of img
- -- lines
- scale = 0
- while scale < 50 do
- -- img can have 2 to 15 nodes
- -- two nodes are used to draw a line
- n = math.random(2, 15)
- for i = 1, n do
- img[i] = buildNode(scale)
- end
- -- Move nodes on screen attempt
- -- to get even distribution for objects
- deltaX = math.random(1, WIDTH)
- deltaY = math.random(1, HEIGHT)
- -- Adjust nodes so they are on screen
- -- using math.min and WIDTH or HEIGHT
- -- This method simply forces nodes onto
- -- the screen which changes original
- -- image shape. Better might be to move
- -- all nodes enough so they are all on the
- -- screen and the image shape is maintained.
- for i = 1, n do
- img[i].x = math.min(img[i].x + deltaX, WIDTH)
- img[i].y = math.min(img[i].y + deltaY, HEIGHT)
- end
- -- stop coroutine and wait for drawObject
- -- to resume it
- coroutine.yield(n, img)
- -- Increment scale (length of img lines)
- scale = scale + 1
- end
- end
- function drawObject()
- -- resume the coroutine return:
- -- status - coroutine active
- -- drawN - number of nodes in object
- -- thisObj - table of node values
- -- "Done" will display in upper left corner after all
- -- objects have been drawn
- status, drawN, thisObj = coroutine.resume(obj)
- -- Set line color for this image
- setStroke()
- -- Determine if coroutine is active
- if status ~= false and thisObj ~= nil then
- -- Draw all but the last line in an object
- for i = 1, drawN - 1 do
- line(thisObj[i].x, thisObj[i].y, thisObj[i+1].x, thisObj[i+1].y)
- end
- -- Draw last line fron last node to first node
- line(thisObj[drawN].x, thisObj[drawN].y, thisObj[1].x, thisObj[1].y)
- else
- text("Done", 70, HEIGHT -50)
- end
- end
- function setStroke()
- -- Determine a random color for an
- -- object lines
- strokeRed = math.random(0, 255)
- strokeGreen = math.random(0, 255)
- strokeBlue = math.random(0, 255)
- stroke(strokeRed, strokeGreen, strokeBlue, 255)
- end
- function buildNode(scale)
- -- Create a node as a vec2
- p = vec2()
- -- Random x, y values
- p.x, p.y = getRan()
- -- Add scale to x, y
- p.x = p.x + scale
- p.y = p.y + scale
- -- print("node", p.x, p.y)
- -- return the node
- return p
- end
- function getRan()
- -- This determines the base x and y position
- -- of the object used by buildNode
- x = math.random(10,150)
- y = math.random(10,150)
- -- Return x, y
- return x, y
- end
- function touched(touch)
- if touch.state == ENDED and useTouch == true then
- drawObject()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement