Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function love.load()
- images = {
- ship1 = love.graphics.newImage("ship1.png"),
- ship2 = love.graphics.newImage("ship2.png"),
- laser = love.graphics.newImage("laser.png"),
- rock = love.graphics.newImage("rock.png")
- }
- love.graphics.setBackgroundColor(0,0,0)
- love.graphics.setPointStyle("smooth")
- myFont = love.graphics.newFont(20)
- love.graphics.setFont(myFont)
- SetupParticles()
- world = love.physics.newWorld(0, 0)
- world:setCallbacks(beginContact, endContact, preSolve, postSolve)
- shipBody = love.physics.newBody(world, 400,300, "dynamic")
- shipBody:setLinearDamping(0.1)
- shipShape = love.physics.newRectangleShape(31, 34)
- shipFix = love.physics.newFixture(shipBody, shipShape)
- shipFix:setCategory(3)
- shipFix:setMask(2)
- rockets = 0
- destroyRock = 0
- destroyLaser = 0
- countdown = -1
- level = 0
- dead = 0
- stars = {} -- table which will hold our stars
- max_stars = 100 -- how many stars we want
- for i=1, max_stars do -- generate the coords of our stars
- local x = math.random(5, love.graphics.getWidth()-5) + 0.5 -- generate a "random" number for the x coord of this star
- local y = math.random(5, love.graphics.getHeight()-5) + 0.5 -- both coords are limited to the screen size, minus 5 pixels of padding
- local c = math.random(100,255)
- stars[i] = {x, y,c} -- stick the values into the table
- end
- make_rocks(5)
- end
- function love.draw()
- for i=1, #stars do -- loop through all of our stars
- local c = stars[i][3]
- love.graphics.setColor(c,c,c)
- love.graphics.point(stars[i][1], stars[i][2]) -- draw each point
- end
- love.graphics.setColor(255,255,255)
- for i, v in ipairs(lasers) do
- love.graphics.draw(images.laser,v.b:getX(),v.b:getY(),v.b:getAngle(),1,1,15,1)
- --local v1,v2,v3,v4 = v.f:getBoundingBox()
- --love.graphics.rectangle("line",v1,v2,v3-v1,v4-v2)
- end
- for i, v in ipairs(rocks) do
- love.graphics.draw(images.rock,v.body:getX(),v.body:getY(),v.body:getAngle(),v.size,v.size,v.ox,v.oy)
- --local v1,v2,v3,v4 = v.fix:getBoundingBox()
- --love.graphics.rectangle("line",v1,v2,v3-v1,v4-v2)
- end
- if dead == 0 then
- local shipImage = images.ship1
- if rockets == 1 then
- shipImage = images.ship2
- end
- love.graphics.draw(shipImage, shipBody:getX(), shipBody:getY(), shipBody:getAngle(), 1, 1, 15, 17)
- else
- love.graphics.print("You are dead!",350,200)
- if dead > 4 then
- love.graphics.print("Press Space to restart.",310,230)
- end
- end
- love.graphics.draw(rockParticles,0,0)
- love.graphics.draw(bigRocks,0,0)
- love.graphics.draw(shipBoom,0,0)
- if ( countdown > 0 ) then
- love.graphics.print("Prepare for next level!",300,200)
- end
- -- if rockets == 1 then
- -- love.graphics.draw(images.ship2, shipBody:getX(), shipBody:getY(), shipBody:getAngle(), 1, 1, 17, 15)
- -- else
- -- love.graphics.draw(images.ship1, shipBody:getX(), shipBody:getY(), shipBody:getAngle(), 1, 1, 17, 15)
- -- end
- end
- laser_cooldown = 0
- engine_cooldown = 0
- lasers = {}
- rocks = {}
- function fire_laser()
- local laser = {}
- local x = shipBody:getX()
- local y = shipBody:getY()
- local a = shipBody:getAngle()
- laser.b = love.physics.newBody(world,x,y,"dynamic")
- laser.b:setBullet(true)
- laser.s = love.physics.newRectangleShape(25,3)
- laser.f = love.physics.newFixture(laser.b,laser.s)
- laser.f:setMask(3)
- laser.f:setCategory(2)
- laser.b:setAngle(a)
- local laserXSpeed = math.cos(a) * 100
- local laserYSpeed = math.sin(a) * 100
- laser.b:applyLinearImpulse(laserXSpeed,laserYSpeed)
- table.insert(lasers,laser)
- lasersound = love.audio.newSource("laser.wav","static")
- love.audio.play(lasersound)
- end
- function love.update(dt)
- if laser_cooldown > 0 then
- laser_cooldown = laser_cooldown - dt
- if ( laser_cooldown < 0 ) then
- laser_cooldown = 0
- end
- end
- if engine_cooldown > 0 then
- engine_cooldown = engine_cooldown - dt
- if ( engine_cooldown < 0 ) then
- engine_cooldown = 0
- end
- end
- checkRocks()
- shipBody:setAngularVelocity(0)
- world:update(dt)
- rockParticles:update(dt)
- bigRocks:update(dt)
- shipBoom:update(dt)
- if dead > 0 then
- dead = dead + dt
- end
- if #rocks <= 0 then
- if ( countdown == -1 ) then
- countdown = 6
- youwin = love.audio.newSource("level.wav","static")
- love.audio.play(youwin)
- end
- if ( countdown > 0 ) then
- countdown = countdown - dt
- end
- if ( countdown <= 0 ) then
- countdown = -1
- level = level + 2
- make_rocks(5+level)
- shipBody:setPosition(400,300)
- end
- end
- if love.keyboard.isDown(" ") and laser_cooldown <= 0 then
- if dead == 0 then
- laser_cooldown = 0.5
- fire_laser()
- elseif dead > 4 then
- love.filesystem.load("main.lua")()
- love.load()
- end
- end
- for i, v in ipairs(lasers) do
- if v.b:getX() < 0 or v.b:getX() > 800 or v.b:getY() < 0 or v.b:getY() > 600 then
- table.remove(lasers,i)
- end
- end
- if love.keyboard.isDown("right") or love.keyboard.isDown("left") then
- local delta = -1
- if love.keyboard.isDown("right") then
- delta = 1
- end
- local a = shipBody:getAngle()
- a = a + ( dt * 5 * delta )
- shipBody:setAngle(a)
- end
- if love.keyboard.isDown("up") then
- rockets = 1
- local xForce = math.cos(shipBody:getAngle()) * 200
- local yForce = math.sin(shipBody:getAngle()) * 200
- shipBody:applyForce(xForce,yForce)
- if engine_cooldown <= 0 then
- local enginehum = love.audio.newSource("engine.wav","static")
- enginehum:play()
- engine_cooldown = 0.25
- end
- --if enginehum:isStopped() then
- -- enginehum:play()
- --end
- else
- rockets = 0
- --enginehum:stop()
- end
- if shipBody:getX() < 0 then
- shipBody:setX(shipBody:getX() + 800)
- end
- if shipBody:getY() < 0 then
- shipBody:setY(shipBody:getY() + 600)
- end
- if shipBody:getX() > 800 then
- shipBody:setX(shipBody:getX() - 800)
- end
- if shipBody:getY() > 600 then
- shipBody:setY(shipBody:getY() - 600)
- end
- for i, v in ipairs(rocks) do
- if v.body:getX() < -20 then
- v.body:setX(v.body:getX() + 840)
- end
- if v.body:getY() < -20 then
- v.body:setY(v.body:getY() + 640)
- end
- if v.body:getX() > 820 then
- v.body:setX(v.body:getX() - 840)
- end
- if v.body:getY() > 620 then
- v.body:setY(v.body:getY() - 640)
- end
- end
- end
- function make_rocks(total_rocks)
- for i = 1, total_rocks do
- local x = 0
- local y = 0
- local s = math.random() + 0.5
- if ( math.random(0,1) == 1 ) then
- x = math.random(0,800)
- else
- y = math.random(0,600)
- end
- createNewRock(x,y,s)
- end
- end
- function createNewRock(x,y,s)
- rock = {}
- rock.a = math.random() * 2 * math.pi
- rock.size = s
- rock.body = love.physics.newBody(world, x, y, "dynamic")
- rock.shape = love.physics.newPolygonShape(14*rock.size ,0,
- 31*rock.size ,0,
- 45*rock.size ,5*rock.size,
- 58*rock.size ,31*rock.size,
- 46*rock.size ,42*rock.size,
- 11*rock.size ,41*rock.size,
- 0 ,22*rock.size)
- --rock.shape = love.physics.newRectangleShape(59*rock.size,43*rock.size)
- rock.fix = love.physics.newFixture(rock.body,rock.shape)
- rock.fix:setCategory(1)
- rock.body:setAngle(rock.a)
- rock.ox = 0
- rock.oy = 0
- rock.body:applyAngularImpulse(math.random() * 200 - 100)
- rock.body:applyLinearImpulse(math.random() * 200 - 200, math.random() * 200 - 100)
- rock.health = rock.size * rock.size * 100
- table.insert(rocks,rock)
- end
- function beginContact(a, b, coll)
- end
- function endContact(a, b, coll)
- end
- function preSolve(a, b, coll)
- end
- function postSolve(a, b, coll)
- if ( a:getCategory() == 1 and b:getCategory() == 2 ) or ( a:getCategory() == 2 and b:getCategory() == 1 ) then
- local laserFix = a
- local rockFix = b
- if b:getCategory() == 2 then
- laserFix = b
- rockFix = a
- end
- --deleteLaser(laserFix)
- destroyLaser = laserFix
- local x1, y1, x2, y2 = coll:getPositions()
- rockParticles:setPosition(x1,y1)
- rockParticles:start()
- local rock,i = findRock(rockFix)
- rock.health = rock.health - 25
- local hit = love.audio.newSource("hit.wav","static")
- hit:play()
- if rock.health <= 0 then
- local x,y = rock.body:getPosition()
- bigRocks:setPosition(x,y)
- bigRocks:start()
- if rock.size >= 1 then
- newRockSize = rock.size - 0.5
- makeNewRock = 1
- newRockX = x
- newRockY = y
- end
- destroyRock = i
- local boom = love.audio.newSource("boom.wav","static")
- boom:play()
- end
- end
- if ( a:getCategory() == 1 and b:getCategory() == 3 ) or ( a:getCategory() == 3 and b:getCategory() == 1 ) then
- if dead == 0 then
- dead = 1
- local boom = love.audio.newSource("boom.wav","static")
- boom:play()
- local x,y = shipBody:getPosition()
- shipBoom:setPosition(x,y)
- shipBoom:start()
- end
- end
- end
- function checkRocks()
- if makeNewRock == 1 then
- makeNewRock = 0
- createNewRock(newRockX,newRockY,newRockSize)
- createNewRock(newRockX,newRockY,newRockSize)
- end
- if destroyRock ~= 0 then
- destroyARock(destroyRock)
- destroyRock = 0
- end
- if destroyLaser ~= 0 then
- deleteLaser(destroyLaser)
- destroyLaser = 0
- end
- end
- function destroyARock(i)
- local rock = rocks[i]
- rock.body:destroy()
- table.remove(rocks,i)
- end
- function deleteLaser(laserFix)
- for i, v in ipairs(lasers) do
- if v.f == laserFix then
- v.b:destroy()
- table.remove(lasers,i)
- end
- end
- end
- function findRock(rockFix)
- for i, v in ipairs(rocks) do
- if v.fix == rockFix then
- return v,i
- end
- end
- end
- function SetupParticles()
- rockParticles = love.graphics.newParticleSystem(images.rock,100)
- rockParticles:setEmissionRate(20)
- rockParticles:setSpeed(50,80)
- rockParticles:setSizes(0.4)
- rockParticles:setSizeVariation(0.5)
- rockParticles:setColors(150,150,150,200,150,150,150,0)
- rockParticles:setLifetime(0.25)
- rockParticles:setParticleLife(1)
- rockParticles:setRotation(0,2*math.pi)
- rockParticles:setSpread(2*math.pi)
- rockParticles:setSpin(-1,1,1)
- bigRocks = love.graphics.newParticleSystem(images.rock,100)
- bigRocks:setEmissionRate(20)
- bigRocks:setSpeed(50,60)
- bigRocks:setSizes(0.6)
- bigRocks:setSizeVariation(0.5)
- bigRocks:setColors(200,200,200,200,200,200,150,0)
- bigRocks:setLifetime(0.5)
- bigRocks:setParticleLife(1)
- bigRocks:setRotation(0,2*math.pi)
- bigRocks:setSpread(2*math.pi)
- bigRocks:setSpin(-1,1,1)
- shipBoom = love.graphics.newParticleSystem(images.rock,100)
- shipBoom:setEmissionRate(30)
- shipBoom:setSpeed(50,60)
- shipBoom:setSizes(0.2,0.6)
- shipBoom:setSizeVariation(0.5)
- shipBoom:setColors(200,200,20,200, 200,20,20,150, 200,20,20,100, 150,20,20,50, 20,20,20,0)
- shipBoom:setLifetime(3)
- shipBoom:setParticleLife(1)
- shipBoom:setRotation(0,2*math.pi)
- shipBoom:setSpread(2*math.pi)
- shipBoom:setSpin(-1,1,1)
- end
Add Comment
Please, Sign In to add comment