Optimize code
This commit is contained in:
parent
a397edf8f1
commit
7d84d727c4
40
main.lua
40
main.lua
@ -123,43 +123,51 @@ function love.update(dt)
|
|||||||
fire.pos_frame = 1
|
fire.pos_frame = 1
|
||||||
end
|
end
|
||||||
-- Controls
|
-- Controls
|
||||||
|
local control_up, control_right, control_left, control_quit = false, false, false, false
|
||||||
-- Keyboard
|
-- Keyboard
|
||||||
if love.keyboard.isDown('escape') or love.keyboard.isDown('q') then
|
if love.keyboard.isDown('escape') or love.keyboard.isDown('q') then
|
||||||
love.event.push('quit')
|
control_quit = true
|
||||||
end
|
end
|
||||||
if love.keyboard.isDown('right') then
|
if love.keyboard.isDown('right') then
|
||||||
ship.body:applyForce(ship.power, 0)
|
control_right = true
|
||||||
fire.visible = true
|
|
||||||
sounds.fire:play()
|
|
||||||
elseif love.keyboard.isDown('left') then
|
elseif love.keyboard.isDown('left') then
|
||||||
ship.body:applyForce(-ship.power, 0)
|
control_left = true
|
||||||
fire.visible = true
|
|
||||||
sounds.fire:play()
|
|
||||||
end
|
end
|
||||||
if love.keyboard.isDown('up') then
|
if love.keyboard.isDown('up') then
|
||||||
ship.body:applyForce(0, -ship.power)
|
control_up = true
|
||||||
fire.visible = true
|
|
||||||
sounds.fire:play()
|
|
||||||
end
|
end
|
||||||
-- Mouse
|
-- Mouse
|
||||||
if love.mouse.isDown(1) then
|
if love.mouse.isDown(1) then
|
||||||
local x, y = love.mouse.getPosition()
|
local x, y = love.mouse.getPosition()
|
||||||
-- Up
|
-- Up
|
||||||
if x > width / 3 and x < width - (width / 3) and y < height / 3 then
|
if x > width / 3 and x < width - (width / 3) and y < height / 3 then
|
||||||
|
control_up = true
|
||||||
|
end
|
||||||
|
-- Left
|
||||||
|
if x < width / 3 then
|
||||||
|
control_left = true
|
||||||
|
elseif x > width - (width / 3) then
|
||||||
|
-- Right
|
||||||
|
control_right = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
-- Ship move
|
||||||
|
if control_up then
|
||||||
ship.body:applyForce(0, -ship.power)
|
ship.body:applyForce(0, -ship.power)
|
||||||
fire.visible = true
|
fire.visible = true
|
||||||
sounds.fire:play()
|
sounds.fire:play()
|
||||||
end
|
end
|
||||||
-- Left
|
if control_right then
|
||||||
if x < width / 3 then
|
|
||||||
ship.body:applyForce(-ship.power, 0)
|
|
||||||
fire.visible = true
|
|
||||||
sounds.fire:play()
|
|
||||||
elseif x > width - (width / 3) then
|
|
||||||
ship.body:applyForce(ship.power, 0)
|
ship.body:applyForce(ship.power, 0)
|
||||||
fire.visible = true
|
fire.visible = true
|
||||||
sounds.fire:play()
|
sounds.fire:play()
|
||||||
|
elseif control_left then
|
||||||
|
ship.body:applyForce(-ship.power, 0)
|
||||||
|
fire.visible = true
|
||||||
|
sounds.fire:play()
|
||||||
end
|
end
|
||||||
|
if control_quit then
|
||||||
|
love.event.push('quit')
|
||||||
end
|
end
|
||||||
-- Rotate asteroids
|
-- Rotate asteroids
|
||||||
for key, value in pairs(asteroids) do
|
for key, value in pairs(asteroids) do
|
||||||
|
Loading…
Reference in New Issue
Block a user