Add readme

This commit is contained in:
Andros Fenollosa 2018-05-26 13:51:14 +02:00
parent b1847c91de
commit 7c8a2c990b
28 changed files with 255 additions and 0 deletions

29
README.md Normal file
View File

@ -0,0 +1,29 @@
# Creando sencillos juegos con Löve2D
![Löve2D Logo](logo.png)
Cada uno de los pasos se encuentra en la carpeta de **STEPS**.
- **1_hello**: Hola mundo.
- **2_layout**: Ventana.
- **3_background**: Fondo
- **4_spaceship1**: Nave estática.
- **4_spaceship2**: Nave animada.
- **5_move_spaceship**: Moviendo la nave.
- **6_asteroids1**: Creando un asteoride.
- **6_asteroids2**: Moviendo el asteroide.
- **6_asteroids2**: Generando asteroides aleatorios.
- **7_collision_detection**: Colisiones.
- **8_sounds**: Sonidos.
## Ejecutar el juego terminado
Descarga todos los archivos en zip y descomprimelos.
Instala löve desde su [página oficial](https://love2d.org/).
Ejecuta *love* seguido de la carpeta descargada del proyecto.
``` bash
love love2d-tutorial-runner
```

BIN
logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

View File

@ -93,6 +93,7 @@ function love.update(dt)
-- Colision -- Colision
if checkCollision(spaceship.x, spaceship.y[spaceship.pos], spaceship.img:getWidth(), spaceship.img:getHeight() / spaceship.num_frames / 2, asteroid.x, asteroid.y[asteroid.pos], asteroid.img:getWidth(), asteroid.img:getHeight()) then if checkCollision(spaceship.x, spaceship.y[spaceship.pos], spaceship.img:getWidth(), spaceship.img:getHeight() / spaceship.num_frames / 2, asteroid.x, asteroid.y[asteroid.pos], asteroid.img:getWidth(), asteroid.img:getHeight()) then
game.play = false game.play = false
sounds.die:stop()
sounds.die:play() sounds.die:play()
explosion.animate = true explosion.animate = true
explosion.x = spaceship.x explosion.x = spaceship.x

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 62 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

View File

@ -0,0 +1,110 @@
function love.load()
math.randomseed(os.time())
game = {}
game.width = 800
game.height = 480
game.play = true
love.window.setMode(game.width, game.height)
score = { x = 500, y = 40 }
-- Background
background = {}
background.img = love.graphics.newImage('assets/sprites/background.jpg')
background.x = 0
background.y = 0
-- Spaceship
spaceship = {}
spaceship.img = love.graphics.newImage('assets/sprites/spaceship.png')
spaceship.x = game.height / 8
spaceship.num_frames = 4
spaceship.pos_frame = 1
spaceship.frame_height = spaceship.img:getHeight() / spaceship.num_frames
spaceship.y = {
(game.height / 4) - (spaceship.img:getHeight() / 8),
2 * (game.height / 4) - (spaceship.img:getHeight() / 8),
3 * (game.height / 4) - (spaceship.img:getHeight() / 8),
}
spaceship.pos = 1
spaceship.frames = {
love.graphics.newQuad(0, spaceship.frame_height * 0, spaceship.img:getWidth(), spaceship.frame_height, spaceship.img:getWidth(), spaceship.img:getHeight()),
love.graphics.newQuad(0, spaceship.frame_height * 1, spaceship.img:getWidth(), spaceship.frame_height, spaceship.img:getWidth(), spaceship.img:getHeight()),
love.graphics.newQuad(0, spaceship.frame_height * 2, spaceship.img:getWidth(), spaceship.frame_height, spaceship.img:getWidth(), spaceship.img:getHeight()),
love.graphics.newQuad(0, spaceship.frame_height * 3, spaceship.img:getWidth(), spaceship.frame_height, spaceship.img:getWidth(), spaceship.img:getHeight())
}
-- Asteroids
num_asteroids = 3
asteroids = {}
for i = 1, num_asteroids do
asteroid = {}
asteroid.img = love.graphics.newImage('assets/sprites/asteroid_' .. tostring(math.random(1, 9)) .. '.png')
asteroid.x = game.width
asteroid.y = {
(game.height / 4) - (asteroid.img:getHeight() / 2),
2 * (game.height / 4) - (asteroid.img:getHeight() / 2),
3 * (game.height / 4) - (asteroid.img:getHeight() / 2),
}
asteroid.pos = 2
asteroid.speed = 600
asteroids[i] = asteroid
if checkCollision(spaceship.x, spaceship.y[spaceship.pos], spaceship.img:getWidth(), spaceship.img:getHeight() / spaceship.num_frames / 2, asteroid.x, asteroid.y[asteroid.pos], asteroid.img:getWidth(), asteroid.img:getHeight()) then
game.play = false
explosion.animate = true
explosion.x = spaceship.x
explosion.y = spaceship.y[spaceship.pos]
end
end
end
function love.draw()
-- Background
love.graphics.draw(background.img, background.x, background.y)
-- Spaceship
if game.play then
love.graphics.draw(spaceship.img, spaceship.frames[spaceship.pos_frame], spaceship.x, spaceship.y[spaceship.pos])
end
-- Asteroids
for key, asteroid in pairs(asteroids) do
love.graphics.draw(asteroid.img, asteroid.x, asteroid.y[asteroid.pos])
end
end
function love.update(dt)
-- Spaceship
if spaceship.pos_frame ~= spaceship.num_frames then
spaceship.pos_frame = spaceship.pos_frame + 1
else
spaceship.pos_frame = 1
end
-- Asteroids
for key, asteroid in pairs(asteroids) do
asteroid.x = asteroid.x - (dt * asteroid.speed)
if asteroid.x < -asteroid.img:getWidth() then
asteroid.x = game.width + math.random(0, game.width)
asteroid.pos = math.random(1, 3)
end
if checkCollision(spaceship.x, spaceship.y[spaceship.pos], spaceship.img:getWidth(), spaceship.img:getHeight() / spaceship.num_frames / 2, asteroid.x, asteroid.y[asteroid.pos], asteroid.img:getWidth(), asteroid.img:getHeight()) then
game.play = false
end
end
end
-- Controls
function love.keypressed(key, scancode, isrepeat)
if key == 'escape' then
love.event.push('quit')
end
if key == 'up' and spaceship.pos > 1 then
spaceship.pos = spaceship.pos - 1
elseif key == 'down' and spaceship.pos < 3 then
spaceship.pos = spaceship.pos + 1
end
end
function checkCollision(x1,y1,w1,h1, x2,y2,w2,h2)
return x1 < x2 + w2 and
x2 < x1 + w1 and
y1 < y2 + h2 and
y2 < y1 + h1
end

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 62 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

115
steps/8_sounds/main.lua Normal file
View File

@ -0,0 +1,115 @@
function love.load()
math.randomseed(os.time())
game = {}
game.width = 800
game.height = 480
game.play = true
love.window.setMode(game.width, game.height)
score = { x = 500, y = 40 }
-- Background
background = {}
background.img = love.graphics.newImage('assets/sprites/background.jpg')
background.x = 0
background.y = 0
-- Spaceship
spaceship = {}
spaceship.img = love.graphics.newImage('assets/sprites/spaceship.png')
spaceship.x = game.height / 8
spaceship.num_frames = 4
spaceship.pos_frame = 1
spaceship.frame_height = spaceship.img:getHeight() / spaceship.num_frames
spaceship.y = {
(game.height / 4) - (spaceship.img:getHeight() / 8),
2 * (game.height / 4) - (spaceship.img:getHeight() / 8),
3 * (game.height / 4) - (spaceship.img:getHeight() / 8),
}
spaceship.pos = 1
spaceship.frames = {
love.graphics.newQuad(0, spaceship.frame_height * 0, spaceship.img:getWidth(), spaceship.frame_height, spaceship.img:getWidth(), spaceship.img:getHeight()),
love.graphics.newQuad(0, spaceship.frame_height * 1, spaceship.img:getWidth(), spaceship.frame_height, spaceship.img:getWidth(), spaceship.img:getHeight()),
love.graphics.newQuad(0, spaceship.frame_height * 2, spaceship.img:getWidth(), spaceship.frame_height, spaceship.img:getWidth(), spaceship.img:getHeight()),
love.graphics.newQuad(0, spaceship.frame_height * 3, spaceship.img:getWidth(), spaceship.frame_height, spaceship.img:getWidth(), spaceship.img:getHeight())
}
-- Asteroids
num_asteroids = 3
asteroids = {}
for i = 1, num_asteroids do
asteroid = {}
asteroid.img = love.graphics.newImage('assets/sprites/asteroid_' .. tostring(math.random(1, 9)) .. '.png')
asteroid.x = game.width
asteroid.y = {
(game.height / 4) - (asteroid.img:getHeight() / 2),
2 * (game.height / 4) - (asteroid.img:getHeight() / 2),
3 * (game.height / 4) - (asteroid.img:getHeight() / 2),
}
asteroid.pos = 2
asteroid.speed = 600
asteroids[i] = asteroid
if checkCollision(spaceship.x, spaceship.y[spaceship.pos], spaceship.img:getWidth(), spaceship.img:getHeight() / spaceship.num_frames / 2, asteroid.x, asteroid.y[asteroid.pos], asteroid.img:getWidth(), asteroid.img:getHeight()) then
game.play = false
explosion.animate = true
explosion.x = spaceship.x
explosion.y = spaceship.y[spaceship.pos]
end
end
-- Sound
ambient = love.audio.newSource('assets/sounds/ambient.wav', 'static')
ambient:play()
ambient:setLooping(true)
end
function love.draw()
-- Background
love.graphics.draw(background.img, background.x, background.y)
-- Spaceship
if game.play then
love.graphics.draw(spaceship.img, spaceship.frames[spaceship.pos_frame], spaceship.x, spaceship.y[spaceship.pos])
end
-- Asteroids
for key, asteroid in pairs(asteroids) do
love.graphics.draw(asteroid.img, asteroid.x, asteroid.y[asteroid.pos])
end
end
function love.update(dt)
-- Spaceship
if spaceship.pos_frame ~= spaceship.num_frames then
spaceship.pos_frame = spaceship.pos_frame + 1
else
spaceship.pos_frame = 1
end
-- Asteroids
for key, asteroid in pairs(asteroids) do
asteroid.x = asteroid.x - (dt * asteroid.speed)
if asteroid.x < -asteroid.img:getWidth() then
asteroid.x = game.width + math.random(0, game.width)
asteroid.pos = math.random(1, 3)
end
if checkCollision(spaceship.x, spaceship.y[spaceship.pos], spaceship.img:getWidth(), spaceship.img:getHeight() / spaceship.num_frames / 2, asteroid.x, asteroid.y[asteroid.pos], asteroid.img:getWidth(), asteroid.img:getHeight()) then
game.play = false
end
end
end
-- Controls
function love.keypressed(key, scancode, isrepeat)
if key == 'escape' then
love.event.push('quit')
end
if key == 'up' and spaceship.pos > 1 then
spaceship.pos = spaceship.pos - 1
elseif key == 'down' and spaceship.pos < 3 then
spaceship.pos = spaceship.pos + 1
end
end
function checkCollision(x1,y1,w1,h1, x2,y2,w2,h2)
return x1 < x2 + w2 and
x2 < x1 + w1 and
y1 < y2 + h2 and
y2 < y1 + h1
end