Hola a todos, hoy os explicaré como podemos realizar una pausa en nuestro juego con LOVE 2D de forma simple.
Es algo normal que en nuestro juego necesitamos que tenga una pausa, en LOVE 2D es bastante sencillo de hacer.
Solo necesitamos una variable booleana donde iremos guardando si el juego esta pausado o no.
Vamos a usar de base el rebote de una pelota, para ver el efecto. Esto es la base:
function love.load() pelota = {} pelota.x = love.graphics.getWidth() / 2 pelota.y = love.graphics.getHeight() / 2 pelota.radio = 20 pelota.derecha = true pelota.arriba = false velocidadPelota = 5 end function love.update(dt) if not pausa then rebotarPelota(pelota, velocidadPelota) end end function love.draw() love.graphics.circle("fill", pelota.x, pelota.y, pelota.radio) end function rebotarPelota(pelota, velocidad) if pelota.derecha then pelota.x = pelota.x + velocidad else pelota.x = pelota.x - velocidad end if pelota.arriba then pelota.y = pelota.y - velocidad else pelota.y = pelota.y + velocidad end if (pelota.x + pelota.radio) >= love.graphics.getWidth() then pelota.derecha = false end if (pelota.x - pelota.radio) <= 0 then pelota.derecha = true end if (pelota.y + pelota.radio) >= love.graphics.getHeight() then pelota.arriba = true end if (pelota.y - pelota.radio) <= 0 then pelota.arriba = false end end
Añadimos la variable booleana pausa en load, poniéndole a false inicialmente.
También pondremos la funcion love.keypressed para que cuando pulsemos el enter pausemos el juego.
function love.load() pelota = {} pelota.x = love.graphics.getWidth() / 2 pelota.y = love.graphics.getHeight() / 2 pelota.radio = 20 pelota.derecha = true pelota.arriba = false velocidadPelota = 5 --Pausa pausa = false end function love.update(dt) rebotarPelota(pelota, velocidadPelota) end function love.draw() love.graphics.circle("fill", pelota.x, pelota.y, pelota.radio) end function rebotarPelota(pelota, velocidad) if pelota.derecha then pelota.x = pelota.x + velocidad else pelota.x = pelota.x - velocidad end if pelota.arriba then pelota.y = pelota.y - velocidad else pelota.y = pelota.y + velocidad end if (pelota.x + pelota.radio) >= love.graphics.getWidth() then pelota.derecha = false end if (pelota.x - pelota.radio) <= 0 then pelota.derecha = true end if (pelota.y + pelota.radio) >= love.graphics.getHeight() then pelota.arriba = true end if (pelota.y - pelota.radio) <= 0 then pelota.arriba = false end end --funcion para cambiar el estado de la pausa function love.keypressed(key, scancode, isrepeat) if key == "return" then pausa = not pausa end end
Si pulsamos enter, no hacemos nada aun.Nos falta modificar el draw y el update.
Haremos que cuando la pausa este activa muestre un mensaje en el draw y que cuando no este activo, que la pelota este quieta.
function love.load() pelota = {} pelota.x = love.graphics.getWidth() / 2 pelota.y = love.graphics.getHeight() / 2 pelota.radio = 20 pelota.derecha = true pelota.arriba = false velocidadPelota = 5 --Pausa pausa = false end function love.update(dt) -- Si esta pausado, la pelota no se movera if not pausa then rebotarPelota(pelota, velocidadPelota) end end function love.draw() --Si esta pausado, mostramos un mensaje if pausa then love.graphics.setColor(255, 255, 255) love.graphics.print("Juego pausado, pulsa enter para regresar", (love.graphics.getWidth()/2)-100, love.graphics.getHeight()/2) love.graphics.setColor(105, 105, 105) end if not pausa then love.graphics.setColor(255, 255, 255) end love.graphics.circle("fill", pelota.x, pelota.y, pelota.radio) end function rebotarPelota(pelota, velocidad) if pelota.derecha then pelota.x = pelota.x + velocidad else pelota.x = pelota.x - velocidad end if pelota.arriba then pelota.y = pelota.y - velocidad else pelota.y = pelota.y + velocidad end if (pelota.x + pelota.radio) >= love.graphics.getWidth() then pelota.derecha = false end if (pelota.x - pelota.radio) <= 0 then pelota.derecha = true end if (pelota.y + pelota.radio) >= love.graphics.getHeight() then pelota.arriba = true end if (pelota.y - pelota.radio) <= 0 then pelota.arriba = false end end function love.keypressed(key, scancode, isrepeat) if key == "return" then pausa = not pausa end end
Vemos como se vería:
Si le damos al enter, se queda parado.
Si volvemos a pulsar, sigue la pelota rebotando.
Espero que os sea de ayuda. Si tenéis dudas, preguntad. Estamos para ayudarte.
Deja una respuesta