Code-Shooty, final

main.lua — Lua script, 4 kB (4751 bytes)

File contents

function love.load(arg)
  sprite = {}
  sprite.ship = love.graphics.newImage('stds/ship.png')
  sprite.shot = love.graphics.newImage('stds/shot.png')
  sprite.foe = love.graphics.newImage('stds/foe.png')
  sprite.background = love.graphics.newImage('stds/starscape.png')

  ship = {}
  ship.x = love.graphics.getWidth()/2
  ship.y = love.graphics.getHeight()/2
  ship.speed = 180

  foes = {}
bullets = {}

  gameState = 1
  maxTime = 2
  timer = maxTime
  score = 0

  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("a") and ship.x > 0 then
      ship.x = ship.x - ship.speed * dt
    end

    if love.keyboard.isDown("d") and ship.x < love.graphics.getWidth() 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
        gameState = 1
        ship.x = love.graphics.getWidth()/2
        ship.y = love.graphics.getHeight()/2
      end
    end
  end

  for i,s in ipairs(bullets) do
    s.x = s.x + math.cos(s.direction) * s.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) do
      if distanceBetween(f.x, f.y, s.x, s.y) < 20 then
                s.dead = true
                f.dead = true
        score = score + 1
      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 * 0.95
      timer = maxTime
    end
  end
end

function love.draw()
  love.graphics.draw(sprite.background, 0, 0)

  if gameState == 1 then
    love.graphics.setFont(myFont)
    love.graphics.printf("Click anywhere to begin!", 0, 50, love.graphics.getWidth(), "center")
  end

  love.graphics.printf("Score: " .. score, 0, love.graphics.getHeight() - 100, love.graphics.getWidth(), "center")

  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, 0.5, 0.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 = 140
  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 and gameState == 2 then
    spawnShot()
end


  if gameState == 1 then
    gameState = 2

    maxTime = 2
    timer = maxTime
    score = 0
end






function distanceBetween(x1, y1, x2, y2)
  return math.sqrt((y2 - y1)^2 + (x2 - x1)^2)
end
end
Document Actions