Code to Apr 30

main.lua — Lua script, 3 kB (3220 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


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
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
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.math.random(0,love.graphics.getWidth())


  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
Document Actions