Fix restart

This commit is contained in:
Andros Fenollosa 2016-10-18 23:46:06 +02:00
parent ff7683b726
commit 79e0a5d978
3 changed files with 43 additions and 29 deletions

BIN
assets/font/space_age.ttf Normal file

Binary file not shown.

View File

@ -3,15 +3,19 @@ function love.load()
-- Configuration -- Configuration
math.randomseed(os.time()) math.randomseed(os.time())
window = { width = 1280, height = 720 } window = { width = 1280, height = 720 }
fullscreen = false
love.window.setMode(window.width, window.height, {resizable=false}) love.window.setMode(window.width, window.height, {resizable=false})
font = love.graphics.newFont(40) -- Font font = love.graphics.newFont('assets/font/space_age.ttf', 40) -- Font
love.graphics.setFont(font) love.graphics.setFont(font)
text_restart = { text = 'You die!' }
text_restart.size = font:getWidth(text_restart.text)
text_good = { text = 'Good' }
text_good.size = font:getWidth(text_good.text)
camera = { x = 0, y = 0, width = window.width, height = window.height } camera = { x = 0, y = 0, width = window.width, height = window.height }
debug = true debug = false
play = true play = true
win = false win = false
level = { num = 1, x = 50, y = 50 } level = { num = 1, x = 50, y = 50 }
fullscreen = false
love.window.setFullscreen(fullscreen) love.window.setFullscreen(fullscreen)
love.window.setTitle('Alunizaje') love.window.setTitle('Alunizaje')
background = { x = 0, y = 0, img = love.graphics.newImage('assets/img/background.jpg') } background = { x = 0, y = 0, img = love.graphics.newImage('assets/img/background.jpg') }
@ -22,32 +26,21 @@ function love.load()
love.physics.setMeter(64) -- Height earth in meters love.physics.setMeter(64) -- Height earth in meters
world = love.physics.newWorld(0, gravity * 64, true) -- Make earth world = love.physics.newWorld(0, gravity * 64, true) -- Make earth
-- Ship -- Ship
ship = { x = canvas.width / 2, y = 0 , power = 100 , size_collition = 28, polygons_collition = 8 } ship = { x = canvas.width / 2, y = 0 , power = 1000 , size_collition = 28, polygons_collition = 8 }
ship.img = love.graphics.newImage('assets/img/ship.png') ship.img = love.graphics.newImage('assets/img/ship.png')
ship.body = love.physics.newBody(world, (canvas.width / 2) - (ship.img:getWidth() / 2) , ship.y, "dynamic") ship.body = love.physics.newBody(world, (canvas.width / 2) - (ship.img:getWidth() / 2) , ship.y, 'dynamic')
ship.shape = love.physics.newCircleShape(20) ship.shape = love.physics.newCircleShape(20)
ship.fixture = love.physics.newFixture(ship.body, ship.shape, 1) ship.fixture = love.physics.newFixture(ship.body, ship.shape, 1)
ship.fixture:setRestitution(0.9) ship.fixture:setRestitution(0.9)
-- Generates initial asteroids -- Generates initial asteroids
asteroids_collision = 40 asteroids_collision = 40
asteroids_collision_polygon = 8 asteroids_collision_polygon = 8
num_asteroids = 5
max_speed_asteroids = 5 max_speed_asteroids = 5
img_asteroide = { img_asteroide = {
love.graphics.newImage('assets/img/asteroid1.png'), love.graphics.newImage('assets/img/asteroid1.png'),
love.graphics.newImage('assets/img/asteroid2.png') love.graphics.newImage('assets/img/asteroid2.png')
} }
asteroids = {} restart(level.num)
for i=1, num_asteroids do
local temp_img = img_asteroide[math.random(1, table_length(img_asteroide))]
asteroids[i] = {
x = math.random(0, canvas.width - temp_img:getWidth()),
y = math.random(200, canvas.height - temp_img:getHeight()),
speed = math.random(1, max_speed_asteroids),
img = temp_img,
angle = math.random(0, 90)
}
end
end end
-- UPDATE -- UPDATE
@ -59,14 +52,14 @@ function love.update(dt)
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') love.event.push('quit')
end end
if love.keyboard.isDown("right") then if love.keyboard.isDown('right') then
ship.body:applyForce(ship.power, 0) ship.body:applyForce(ship.power, 0)
elseif love.keyboard.isDown("left") then elseif love.keyboard.isDown('left') then
ship.body:applyForce(-ship.power, 0) ship.body:applyForce(-ship.power, 0)
end end
if love.keyboard.isDown("up") then if love.keyboard.isDown('up') then
ship.body:applyForce(0, -ship.power) ship.body:applyForce(0, -ship.power)
elseif love.keyboard.isDown("down") then elseif love.keyboard.isDown('down') then
ship.body:applyForce(0, ship.power) ship.body:applyForce(0, ship.power)
end end
@ -95,10 +88,10 @@ function love.update(dt)
-- Camera -- Camera
camera.y = 0 camera.y = 0
if camera.height / 2 <= ship.body:getY() then -- Top if camera.height / 3 <= ship.body:getY() then -- Top
camera.y = -ship.body:getY() + (camera.height / 2) camera.y = -ship.body:getY() + (camera.height / 3)
end end
if canvas.height - camera.height / 2 < ship.body:getY() then -- Down if canvas.height - ((camera.height / 3) * 2) < ship.body:getY() then -- Down
camera.y = -canvas.height + camera.height camera.y = -canvas.height + camera.height
end end
@ -119,6 +112,7 @@ function love.update(dt)
ship.body:setLinearVelocity(0, 0) ship.body:setLinearVelocity(0, 0)
win = true win = true
play = false play = false
level.num = level.num + 1
end end
for key, value in pairs(asteroids) do -- Asteroids for key, value in pairs(asteroids) do -- Asteroids
@ -138,7 +132,7 @@ function love.update(dt)
end end
else else
sleep(2) sleep(2)
love.load() restart(level.num)
end end
end end
@ -152,20 +146,20 @@ function love.draw()
for key, value in pairs(asteroids) do for key, value in pairs(asteroids) do
love.graphics.draw(value.img, value.x, value.y, value.angle, 1, 1, value.img:getWidth() / 2, value.img:getHeight() / 2) love.graphics.draw(value.img, value.x, value.y, value.angle, 1, 1, value.img:getWidth() / 2, value.img:getHeight() / 2)
if debug then if debug then
love.graphics.circle("line", value.x, value.y, asteroids_collision, ship.polygons_collition) love.graphics.circle('line', value.x, value.y, asteroids_collision, ship.polygons_collition)
end end
end end
-- Ship -- Ship
love.graphics.draw(ship.img, ship.body:getX(), ship.body:getY()) love.graphics.draw(ship.img, ship.body:getX(), ship.body:getY())
if debug then if debug then
love.graphics.circle("line", ship.body:getX() + (ship.img:getWidth() / 2), ship.body:getY() + (ship.img:getHeight() / 2), ship.size_collition, ship.polygons_collition) love.graphics.circle('line', ship.body:getX() + (ship.img:getWidth() / 2), ship.body:getY() + (ship.img:getHeight() / 2), ship.size_collition, ship.polygons_collition)
end end
-- Texts -- Texts
if not play and not win then -- Game over if not play and not win then -- Game over
love.graphics.print('Game over', (camera.width / 2), -camera.y + (camera.height / 2)) love.graphics.print(text_restart.text, (camera.width / 2) - (text_restart.size / 2), -camera.y + (camera.height / 2))
end end
if not play and win then -- Win if not play and win then -- Win
love.graphics.print('You win!', (camera.width / 2), -camera.y + (camera.height / 2)) love.graphics.print(text_good, (camera.width / 2) - (text_good.size / 2), -camera.y + (camera.height / 2))
end end
love.graphics.print('Level ' .. level.num, level.x, level.y + -camera.y) love.graphics.print('Level ' .. level.num, level.x, level.y + -camera.y)
end end
@ -189,4 +183,24 @@ end
function checkCircleCollision(circle1, circle2) function checkCircleCollision(circle1, circle2)
local distance = math.sqrt((circle1.x - circle2.x) ^ 2 + (circle1.y - circle2.y) ^ 2) local distance = math.sqrt((circle1.x - circle2.x) ^ 2 + (circle1.y - circle2.y) ^ 2)
return distance <= circle1.radius + circle2.radius return distance <= circle1.radius + circle2.radius
end
function restart(level_arg)
num_asteroids = level_arg * 5
-- Generate asteroids
asteroids = {}
for i=1, num_asteroids do
local temp_img = img_asteroide[math.random(1, table_length(img_asteroide))]
asteroids[i] = {
x = math.random(0, canvas.width - temp_img:getWidth()),
y = math.random(200, canvas.height - temp_img:getHeight()),
speed = math.random(1, max_speed_asteroids),
img = temp_img,
angle = math.random(0, 90)
}
end
-- Set ship position
ship.body = love.physics.newBody(world, (canvas.width / 2) - (ship.img:getWidth() / 2) , ship.y, 'dynamic')
win = false
play = true
end end