Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --# Gradient
- Gradient = class()
- function blendColor( c1, c2, a )
- return color( c1.r * a + c2.r * (1 - a),
- c1.g * a + c2.g * (1 - a),
- c1.b * a + c2.b * (1 - a),
- 255 )
- end
- function gradient(startCol, endCol)
- -- you can accept and set parameters here
- local img = image(64,64)
- local a = 1
- local c = nil
- for y = 1,img.rawHeight do
- a = y / img.rawHeight
- for x = 1,img.rawWidth do
- c = blendColor( endCol, startCol, a )
- img:rawSet( x, y, c )
- end
- end
- return img
- end
- --# Hill
- Hill = class()
- function Hill:init(rad, pos, col)
- -- you can accept and set parameters here
- self.houses = {}
- self.color = col or color(0,0,0,255)
- self.position = pos or vec2(0,0)
- self.radius = rad
- -- add houses from -30 to 30 degrees
- local angle = -45
- local h = nil
- local v = vec2( 0, rad*0.99 )
- while angle < 45 do
- h = House(self.color)
- h.rotation = angle
- h.position = v:rotate( math.rad(angle) )
- table.insert( self.houses, h )
- angle = angle + math.random(7, 15)
- end
- end
- function Hill:draw()
- pushMatrix()
- pushStyle()
- fill(self.color)
- translate( self.position.x, self.position.y )
- ellipseMode( RADIUS )
- ellipse( 0, 0, self.radius )
- for _,v in pairs(self.houses) do
- v:draw()
- end
- popStyle()
- popMatrix()
- end
- function Hill:touched(touch)
- -- Codea does not automatically call this method
- end
- --# House
- House = class()
- function House:init(col)
- -- you can accept and set parameters here
- self.color = col or color(0,0,0,255)
- self.position = vec2(0,0)
- self.rotation = 0
- self.base = mesh()
- self.windows = mesh()
- local houseWidth = math.random( 30, 60 )
- local roofWidth = houseWidth * 1.3
- local houseHeight = houseWidth * 0.67
- local roofHeight = houseHeight * (math.random() * 0.2 + 0.8)
- self:addBase( houseWidth, houseHeight )
- if math.random() < 0.5 then
- self:addChimney( houseWidth, houseHeight, roofHeight )
- end
- self:addRoof( roofWidth, roofHeight, houseHeight )
- -- Make the house black
- self.base:setColors( self.color )
- if math.random() < 0.7 then
- self:addWindow( houseWidth, houseHeight )
- end
- end
- function House:addBase(w, h)
- self.base:addRect( 0, h/2, w, h )
- end
- function House:addRoof(w, h, bh)
- local verts = self.base.vertices
- table.insert( verts, vec3( -w/2, bh ) )
- table.insert( verts, vec3( 0, bh + h ) )
- table.insert( verts, vec3( w/2, bh ) )
- self.base.vertices = verts
- end
- function House:addChimney(w, bh, rh)
- local chimWidth = w * (math.random() * 0.1 + 0.2)
- local pos = -w/2 + chimWidth
- if math.random() < 0.5 then pos = w/2 - chimWidth end
- self.base:addRect( pos, bh + rh/2, chimWidth, rh )
- end
- function House:addWindow(w, h)
- -- Add a random window
- local winSize = w * (math.random() * 0.2 + 0.2)
- local randomX = math.random( -w/2 + winSize, w/2 - winSize )
- self.windows:addRect( randomX, h - winSize, winSize, winSize )
- self.windows:setColors( 255, 200, 0, 255 )
- end
- function House:draw()
- pushMatrix()
- translate( self.position.x, self.position.y )
- rotate( self.rotation )
- self.base:draw()
- self.windows:draw()
- popMatrix()
- end
- --# Main
- -- Use this function to perform your initial setup
- function setup()
- displayMode(FULLSCREEN)
- -- Lock renderer to starting orientation
- supportedOrientations(CurrentOrientation)
- hills = {}
- backgroundColor = color(22, 20, 27, 255)
- foregroundColor = color(54, 49, 69, 255)
- -- Create some hills
- local h = Hill(400, vec2(WIDTH/2, -30), backgroundColor)
- table.insert(hills,h)
- h = Hill(400, vec2(WIDTH/2 - 300, -100), backgroundColor)
- table.insert(hills,h)
- h = Hill(400, vec2(WIDTH/2 + 300, -100), backgroundColor)
- table.insert(hills,h)
- h = Hill(300, vec2(WIDTH/2 - 250, -150), foregroundColor)
- table.insert(hills,h)
- h = Hill(300, vec2(WIDTH/2 + 250, -150), foregroundColor)
- table.insert(hills,h)
- -- Create the sky
- local skyCol1 = color(168, 159, 197, 255)
- local skyCol2 = color(64, 48, 107, 255)
- sky = gradient(skyCol1, skyCol2)
- end
- -- This function gets called once every frame
- function draw()
- -- This sets a dark background color
- background(140, 94, 48, 255)
- sprite( sky, WIDTH/2, HEIGHT/2, WIDTH + 10, HEIGHT + 10 )
- -- Do your drawing here
- for _,v in pairs(hills) do
- v:draw()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement