Code to May 1
main.lua — Lua script, 3 kB (3817 bytes)
File contents
function love.load(arg) -- body... sprite= {} sprite.ship=love.graphics.newImage('stds/ship.png') sprite.foe=love.graphics.newImage('stds/foe.png') sprite.shot=love.graphics.newImage('stds/shot.png') sprite.background=love.graphics.newImage('stds/starscape.png') ship={} ship.x=200 ship.y = 200 ship.speed = 180 foes ={} bullets= {} gameState = 2 maxTime = 2 timer = maxTime myFont = love.graphics.newFont(40) end --++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ function love.update(dt) if gameState==2 then if love.keyboard.isDown("s")and ship.y < love.graphics.getHeight() then ship.y=ship.y + ship.speed*dt end if love.keyboard.isDown("w")and ship.y > 0 then ship.y=ship.y - ship.speed*dt end if love.keyboard.isDown("d") and ship.x < love.graphics.getWidth() then ship.x=ship.x + ship.speed*dt end if love.keyboard.isDown("a")and ship.x >0 then ship.x=ship.x - ship.speed*dt end end for i,f in ipairs(foes) do f.x=f.x+math.cos(foe_ship_angle(f))*f.speed*dt f.y=f.y+math.sin(foe_ship_angle(f))*f.speed*dt if distanceBetween(f.x, f.y, ship.x, ship.y) <30 then for i,f in ipairs(foes) do foes[i]=nil end end end for i,s in ipairs (bullets) do s.x=s.x+ math.cos(s.direction)*shot.speed*dt s.y=s.y+ math.sin(s.direction)*s.speed*dt end for i=#bullets,1,-1 do local b = bullets[i] if b.x<0 or b.y<0 or b.x >love.graphics.getWidth() or b.y>love.graphics.getHeight() then table.remove(bullets,i) end end for i,f in ipairs(foes) do for j,s in ipairs(bullets,i) do if distanceBetween (f.x,f.y,s.x,s.y) <20 then f.dead=true s.dead=true end end end for i=#foes,1,-1 do local f=foes[i] if f.dead==true then table.remove(foes, i) end end for i=#bullets,1,-1 do local s=bullets[i] if s.dead==true then table.remove(bullets,i) end end if gameState==2 then timer = timer-dt if timer <=0 then spawnFoe() maxTime=maxTime*.95 timer=maxTime end end end --========================================== function love.draw() love.graphics.draw (sprite.background) love.graphics.draw (sprite.ship,ship.x,ship.y,ship_mouse_angle(),nil,nil,sprite.ship:getWidth()/2,sprite.ship:getHeight()/2) for i,f in ipairs(foes) do love.graphics.draw(sprite.foe, f.x,f.y, foe_ship_angle(f),nil,nil,sprite.foe:getWidth()/2,sprite.foe:getHeight()/2 ) end for i,s in ipairs(bullets) do love.graphics.draw(sprite.shot, s.x,s.y, nil,.5,.5, sprite.shot:getWidth()/2,sprite.shot:getHeight()/2 ) end end function ship_mouse_angle() return math.atan2(ship.y-love.mouse.getY(),ship.x-love.mouse.getX())+math.pi end function foe_ship_angle(enemy) return math.atan2(ship.y-enemy.y,ship.x-enemy.x) end function spawnFoe() foe={} foe.x=0 foe.y=0 foe.speed=100 foe.dead=false local side=math.random(1,4) if side == 1 then foe.x=-30 foe.y=math.random(0,love.graphics.getHeight()) elseif side ==2 then foe.x = math.random(0,love.graphics.getWidth()) foe.y=-30 elseif side ==3 then foe.x = love.graphics.getWidth() + 30 foe.y = math.random(0,love.graphics.getHeight()) else foe.x = math.random(0,love.graphics.getWidth()) foe.y = love.graphics.getHeight() + 30 end table.insert(foes,foe) end function spawnShot() shot={} shot.x=ship.x shot.y=ship.y shot.speed=500 shot.direction=ship_mouse_angle() shot.dead=false table.insert(bullets,shot) end --function love.keypressed(key, scancode, isrepeat) -- if key == "space" then -- spawnFoe() -- end --end function love.mousepressed(x,y,b,istouch) if b==1 then spawnShot() end end function distanceBetween (x1,y1,x2,y2) return math.sqrt((y2-y1)^2 + (x2-x1)^2) end