Code April 26
main.lua — Lua script, 2 kB (2451 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={} end --++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ function love.update(dt) if love.keyboard.isDown("s")then ship.y=ship.y + ship.speed*dt end if love.keyboard.isDown("w")then ship.y=ship.y - ship.speed*dt end if love.keyboard.isDown("d")then ship.x=ship.x + ship.speed*dt end if love.keyboard.isDown("a")then ship.x=ship.x - ship.speed*dt 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 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=10 foe.y=10 foe.speed=100 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