diff --git a/assets/img/asteroide1.png b/assets/img/asteroid1.png similarity index 100% rename from assets/img/asteroide1.png rename to assets/img/asteroid1.png diff --git a/assets/img/asteroide2.png b/assets/img/asteroid2.png similarity index 100% rename from assets/img/asteroide2.png rename to assets/img/asteroid2.png diff --git a/assets/img/asteroide3.png b/assets/img/asteroid3.png similarity index 100% rename from assets/img/asteroide3.png rename to assets/img/asteroid3.png diff --git a/assets/img/fondo.jpg b/assets/img/background.jpg similarity index 100% rename from assets/img/fondo.jpg rename to assets/img/background.jpg diff --git a/assets/img/nave.png b/assets/img/ship.png similarity index 100% rename from assets/img/nave.png rename to assets/img/ship.png diff --git a/bin/OS X/Alunizaje.app/Contents/Resources/GameIcon.icns b/bin/OS X/Alunizaje.app/Contents/Resources/GameIcon.icns index 1351b48..50c80c1 100644 Binary files a/bin/OS X/Alunizaje.app/Contents/Resources/GameIcon.icns and b/bin/OS X/Alunizaje.app/Contents/Resources/GameIcon.icns differ diff --git a/bin/OS X/Alunizaje.app/Contents/Resources/OS X AppIcon.icns b/bin/OS X/Alunizaje.app/Contents/Resources/OS X AppIcon.icns index fa8883d..50c80c1 100644 Binary files a/bin/OS X/Alunizaje.app/Contents/Resources/OS X AppIcon.icns and b/bin/OS X/Alunizaje.app/Contents/Resources/OS X AppIcon.icns differ diff --git a/main.lua b/main.lua index 64a95fa..c70cd28 100644 --- a/main.lua +++ b/main.lua @@ -1,9 +1,173 @@ +-- LOAD +function love.load() + -- Configuration + math.randomseed(os.time()) + window = { width = 1280, height = 720 } + love.window.setMode(window.width, window.height, {resizable=false}) + font = love.graphics.newFont(40) -- Font + love.graphics.setFont(font) + camera = { x = 0, y = 0, width = window.width, height = window.height } + debug = true + play = true + win = false + fullscreen = false + love.window.setFullscreen(fullscreen) + love.window.setTitle('Alunizaje') + background = { x = 0, y = 0, img = love.graphics.newImage('assets/img/background.jpg') } + canvas = { width = 1280, height = 2880 } + gravity = 2 + moon_margin = 100 + -- Physics + love.physics.setMeter(64) -- Height earth in meters + world = love.physics.newWorld(0, gravity * 64, true) -- Make earth + -- Ship + ship = { x = canvas.width / 2, y = 0 , power = 100 } + ship.body = love.physics.newBody(world, ship.x, ship.y, "dynamic") + ship.shape = love.physics.newCircleShape(20) + ship.fixture = love.physics.newFixture(ship.body, ship.shape, 1) + ship.fixture:setRestitution(0.9) + ship.img = love.graphics.newImage('assets/img/ship.png') + -- Generates initial asteroids + num_asteroids = 50 + max_speed_asteroids = 5 + img_asteroide = { + love.graphics.newImage('assets/img/asteroid1.png'), + love.graphics.newImage('assets/img/asteroid2.png'), + love.graphics.newImage('assets/img/asteroid3.png') + } + asteroids = {} + for i=1, num_asteroids do + 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 + +-- UPDATE +function love.update(dt) + if play then + -- Phytics world + world:update(dt) + + -- Controls + if love.keyboard.isDown('escape') or love.keyboard.isDown('q') then + love.event.push('quit') + end + + if love.keyboard.isDown("right") then + ship.body:applyForce(ship.power, 0) + elseif love.keyboard.isDown("left") then + ship.body:applyForce(-ship.power, 0) + end + if love.keyboard.isDown("up") then + ship.body:applyForce(0, -ship.power) + elseif love.keyboard.isDown("down") then + ship.body:applyForce(0, ship.power) + end + + -- Rotate asteroids + for key, value in pairs(asteroids) do + -- Rota + value.angle = asteroids[key].angle + (dt * math.pi / 10) + value.x = asteroids[key].x - asteroids[key].speed + end + + -- Destroy asteroids + for key, value in pairs(asteroids) do + if value.x + asteroids[key].img:getWidth() < 0 then + table.remove(asteroids, key) + end + end + + -- Create asteroids + if table_length(asteroids) < num_asteroids then + temp_img = img_asteroide[math.random(1, table_length(img_asteroide))] + asteroids[table_length(asteroids) + 1] = { + x = canvas.width + temp_img:getWidth(), + y = math.random(0, canvas.height - temp_img:getHeight()), + speed = math.random(1, max_speed_asteroids), + img = temp_img, + angle = math.random(0, 90)} + end + + -- Camera + camera.y = 0 + if camera.height / 2 <= ship.body:getY() then -- Top + camera.y = -ship.body:getY() + (camera.height / 2) + end + if canvas.height - camera.height / 2 < ship.body:getY() then -- Down + camera.y = -canvas.height + camera.height + end + + -- Collision + if ship.body:getY() <= 0 then -- Top game + x, y = ship.body:getLinearVelocity() + ship.body:setLinearVelocity(x, -y) + end + if ship.body:getX() + ship.img:getWidth() > canvas.width then -- Right game + x, y = ship.body:getLinearVelocity() + ship.body:setLinearVelocity(-x, y) + end + if ship.body:getX() < 0 then -- Left game + x, y = ship.body:getLinearVelocity() + ship.body:setLinearVelocity(-x, y) + end + if ship.body:getY() + ship.img:getHeight() + moon_margin >= canvas.height then -- Down game + ship.body:setLinearVelocity(0, 0) + win = true + play = false + end + + for key, value in pairs(asteroids) do -- Asteroids + if CheckCollision(asteroids[key].x - (asteroids[key].img:getWidth() / 2), asteroids[key].y - (asteroids[key].img:getHeight() / 2), asteroids[key].img:getWidth(), asteroids[key].img:getHeight(), ship.body:getX(), ship.body:getY(), ship.img:getWidth(), ship.img:getHeight()) then + play = false + end + end + else + sleep(2) + love.load() + end +end + +-- DRAW +function love.draw() + -- Camera + love.graphics.translate(0, camera.y) + -- Background + love.graphics.draw(background.img, background.x, background.y) + -- Asteroids + 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) + if debug then + love.graphics.rectangle("line", value.x - (value.img:getWidth() / 2), value.y - (value.img:getHeight() / 2), value.img:getWidth(), value.img:getHeight()) + end + end + -- Ship + love.graphics.draw(ship.img, ship.body:getX(), ship.body:getY()) + if debug then + love.graphics.rectangle("line", ship.body:getX(), ship.body:getY(), ship.img:getWidth(), ship.img:getHeight()) + end + -- Texts + if not play and not win then -- Game over + love.graphics.print('Game over', (camera.width / 2), -camera.y + (camera.height / 2)) + end + if not play and win then -- Win + love.graphics.print('You win!', (camera.width / 2), -camera.y + (camera.height / 2)) + end +end + +--------------------------------- Functions +-- Get Table length function table_length(T) local count = 0 for _ in pairs(T) do count = count + 1 end return count end +-- Pause system n seconds local clock = os.clock function sleep(n) -- seconds local t0 = clock() @@ -19,173 +183,4 @@ function CheckCollision(x1,y1,w1,h1, x2,y2,w2,h2) x2 < x1+w1 and y1 < y2+h2 and y2 < y1+h1 -end - -function love.load() - -- Configuración - math.randomseed(os.time()) - camara_width = 1280 - camara_height = 720 - debug = false - play = true - win = false - font = love.graphics.newFont(40) - love.graphics.setFont(font) - love.window.setMode(camara_width, camara_height, {resizable=false}) - -- love.window.setFullscreen(true) - love.window.setTitle('Alunizaje') - fondo = { x = 0, y = 0, img = love.graphics.newImage('assets/img/fondo.jpg') } - game_height = 2880 - game_width = 1280 - gravedad = 2 - distancia_luna = 100 - -- Fisicas - love.physics.setMeter(64) -- Altura del mundo en metros - world = love.physics.newWorld(0, gravedad * 64, true) -- Crea el mundo físico - - -- Nave fisicas - nave = {} - nave_x = game_width / 2 - nave_y = 0 - nave.body = love.physics.newBody(world, nave_x, nave_y, "dynamic") --place the body in the center of the world and make it dynamic, so it can move around - nave.shape = love.physics.newCircleShape(20) --the ball's shape has a radius of 20 - nave.fixture = love.physics.newFixture(nave.body, nave.shape, 1) -- Attach fixture to body and give it a density of 1. - nave.fixture:setRestitution(0.9) --let the ball bounce - nave.power = 100 - nave.img = love.graphics.newImage('assets/img/nave.png') - - -- Genera los asteroides - num_asteroides = 50 - max_speed = 5 - img_asteroide = { - love.graphics.newImage('assets/img/asteroide1.png'), - love.graphics.newImage('assets/img/asteroide2.png'), - love.graphics.newImage('assets/img/asteroide3.png') - } - asteroides = {} - for i=1, num_asteroides do - temp_img = img_asteroide[math.random(1, table_length(img_asteroide))] - asteroides[i] = { x = math.random(0, game_width - temp_img:getWidth()), - y = math.random(200, game_height - temp_img:getHeight()), - speed = math.random(1, max_speed), - img = temp_img, - angle = math.random(0, 90)} - end -end - -function love.update(dt) - if play then - -- Fisicas del mundo - world:update(dt) - - -- Controles - if love.keyboard.isDown('escape') then - love.event.push('quit') - end - - if love.keyboard.isDown("right") then - nave.body:applyForce(nave.power, 0) - elseif love.keyboard.isDown("left") then - nave.body:applyForce(-nave.power, 0) - end - if love.keyboard.isDown("up") then - nave.body:applyForce(0, -nave.power) - elseif love.keyboard.isDown("down") then - nave.body:applyForce(0, nave.power) - end - - - -- Asteroides rotar - for key, value in pairs(asteroides) do - -- Rota - value.angle = asteroides[key].angle + (dt * math.pi / 10) - value.x = asteroides[key].x - asteroides[key].speed - end - - -- Asteroides destruir - for key, value in pairs(asteroides) do - -- Destruye los que salen de pantalla - if value.x + asteroides[key].img:getWidth() < 0 then - table.remove(asteroides, key) - end - end - - -- Asteroides crear - if table_length(asteroides) < num_asteroides then - temp_img = img_asteroide[math.random(1, table_length(img_asteroide))] - asteroides[table_length(asteroides) + 1] = { - x = game_width + temp_img:getWidth(), - y = math.random(0, game_height - temp_img:getHeight()), - speed = math.random(1, max_speed), - img = temp_img, - angle = math.random(0, 90)} - end - - -- Camara - camara_y = 0 - if camara_height / 2 <= nave.body:getY() then -- Superior - camara_y = -nave.body:getY() + (camara_height / 2) - end - if game_height - camara_height / 2 < nave.body:getY() then --Inferior - camara_y = -game_height + camara_height - end - - -- Colision - if nave.body:getY() <= 0 then -- Arriba del juego - x, y = nave.body:getLinearVelocity() - nave.body:setLinearVelocity(x, -y) - end - if nave.body:getX() + nave.img:getWidth() > game_width then -- Derecha del juego - x, y = nave.body:getLinearVelocity() - nave.body:setLinearVelocity(-x, y) - end - if nave.body:getX() < 0 then -- Izquierda del juego - x, y = nave.body:getLinearVelocity() - nave.body:setLinearVelocity(-x, y) - end - if nave.body:getY() + nave.img:getHeight() + distancia_luna >= game_height then -- Abajo del juego - nave.body:setLinearVelocity(0, 0) - win = true - play = false - end - - for key, value in pairs(asteroides) do -- Con asteroides - if CheckCollision(asteroides[key].x - (asteroides[key].img:getWidth() / 2), asteroides[key].y - (asteroides[key].img:getHeight() / 2), asteroides[key].img:getWidth(), asteroides[key].img:getHeight(), nave.body:getX(), nave.body:getY(), nave.img:getWidth(), nave.img:getHeight()) then - play = false - end - end - else - sleep(2) - love.load() - end -end - -function love.draw() - -- Camara - love.graphics.translate(0, camara_y) - - -- Fondo - love.graphics.draw(fondo.img, fondo.x, fondo.y) - - -- Asteroides - for key, value in pairs(asteroides) do - love.graphics.draw(value.img, value.x, value.y, value.angle, 1, 1, value.img:getWidth() / 2, value.img:getHeight() / 2) - if debug then - love.graphics.rectangle("line", value.x - (value.img:getWidth() / 2), value.y - (value.img:getHeight() / 2), value.img:getWidth(), value.img:getHeight()) - end - end - - -- Nave - love.graphics.draw(nave.img, nave.body:getX(), nave.body:getY()) - if debug then - love.graphics.rectangle("line", nave.body:getX(), nave.body:getY(), nave.img:getWidth(), nave.img:getHeight()) - end - - -- Textos - if not play and not win then - love.graphics.print('Game over', (camara_width / 2), -camara_y + (camara_height / 2)) - end - if not play and win then - love.graphics.print('You win!', (camara_width / 2), -camara_y + (camara_height / 2)) - end end \ No newline at end of file