Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --alphas and sort comp
- --seeing though things and sorting a table of objects
- iparameter("leafThickness",0,255,128)
- function setup()
- forest = {} --create a table called forest
- for i = 1,10 do
- --create 10 plants in the forest
- --this calls the function Plant:init(x,y)
- --the x is adjusted to 80% of screen width with 10% added
- --the trees show up in the middle 20% to 80%
- --this prevents the green part of the plant from going off the screen
- --the y is adjusted so the the base of the tree will start in the lower 80%
- --this prevents a plant being drawn off the top of the screen
- forest[i] = Plant((math.random() * WIDTH * .8)+(WIDTH*.1),math.random() * HEIGHT * .8)
- end
- --print before sort
- for i,v in ipairs(forest) do
- print(forest[i].rooty,i,v)
- end
- --sort the trees in reverse y (height) order
- --this prevents the trunk of one plant being drawn over the green part of another
- --reverse the "a.rooty > b.rooty" and respawn to see overlapping trunks
- --rooty is the y value (height) of a plant
- table.sort(forest, function(a,b) return a.rooty > b.rooty end)
- --print after sort
- for i,v in ipairs(forest) do
- print(forest[i].rooty,i,v)
- end
- end
- function draw()
- background(0, 0, 0, 255)
- --simple loop to draw all plants
- --this calls plant:draw for every plant in the forest table
- for i,v in ipairs(forest) do
- forest[i]:draw()
- end
- end
- Plant = class()
- --usally a seperate tab called plant
- function Plant:init(x,y)
- --store the given x and y as rootx and rooty
- self.rootx = x
- self.rooty = y
- --create a table of berry within this plant object
- self.berry = {}
- for i = 1, 100 do
- --create 100 berries
- --the numbers are just to keep the berries within the green part of the plan
- --the numbers are swags, using real math would have given a non-rectanle placement
- x = (math.random() * 74) + self.rootx - 41
- y = (math.random() * 30) + self.rooty + 39
- --record the position of each berry x and y as x and y
- self.berry[i]={x=x,y=y}
- end
- end
- function Plant:draw()
- --start with the bottom of the plant
- x = self.rootx
- y = self.rooty
- --draw trunk -- basically a brown Y
- stroke(156, 109, 25, 255)
- lineCapMode(PROJECT)
- strokeWidth(20)
- line(x,y,x,y+32)
- strokeWidth(10)
- line(x,y+32,x+16,y+48)
- line(x,y+32,x-16,y+48)
- --draw leaves, a green oval
- strokeWidth(5)
- --the 128 is what makes the leaves semi-see though
- fill(57, 255, 0, leafThickness)
- noStroke()
- ellipseMode(CORNERS)
- ellipse(x-50,y+30,x+50,y+80)
- fill(255, 0, 221, 255)
- --draw all the berries
- for i,v in ipairs(self.berry) do
- ellipse(self.berry[i].x,self.berry[i].y,self.berry[i].x+5,self.berry[i].y+5)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement