Membuat Game Pong dengan Framework Love2D (1)

Tutorial ini adalah lanjutan dari tutorial sebelumnya https://www.ardhi.web.id/2020/07/membuat-game-pong-dengan-framework.html.

Kita tambahkan code berikut ke file main.lua
-- push is a library that will allow us to draw our game at a virtual
-- resolution, instead of however large our window is; used to provide
-- a more retro aesthetic
--
-- https://github.com/Ulydev/push
push = require 'push'
Tambahkan code tersebut di atas baris yang memuat
WINDOW_WIDTH = 1280
Kemudian tambahkan dua konstanta yaitu VIRTUAL_WIDTH dan VIRTUAL_HEIGHT
VIRTUAL_WIDTH = 432
VIRTUAL_HEIGHT = 243
Tambahkan konstanta tersebut di bawah baris yang memuat
WINDOW_HEIGHT = 720
Ubah fungsi love.load() menjadi sebagai berikut
function love.load()
    -- use nearest-neighbor filtering on upscaling and downscaling to prevent blurring of text 
    -- and graphics; try removing this function to see the difference!
    love.graphics.setDefaultFilter('nearest', 'nearest')

    -- initialize our virtual resolution, which will be rendered within our
    -- actual window no matter its dimensions; replaces our love.window.setMode call
    -- from the last example
    push:setupScreen(VIRTUAL_WIDTH, VIRTUAL_HEIGHT, WINDOW_WIDTH, WINDOW_HEIGHT, {
        fullscreen = false,
        resizable = false,
        vsync = true
    })
end

Tambahkan fungsi love.keypressed(key)
--[[
    Keyboard handling, dipanggil di setiap frame
]]
function love.keypressed(key)
    -- keys diakses menggunakan string name
    if key == 'escape' then
        -- keluar dari aplikasi
        love.event.quit()
    end
end
Kemudian update fungsi love.draw() sehingga menjadi sebagai berikut
function love.draw()
    -- begin rendering at virtual resolution
    push:apply('start')

    -- condensed onto one line from last example
    -- note we are now using virtual width and height now for text placement
    love.graphics.printf('Hello Pong!', 0, VIRTUAL_HEIGHT / 2 - 6, VIRTUAL_WIDTH, 'center')

    -- end rendering at virtual resolution
    push:apply('end')
end
Sehingga code lengkapnya adalah sebagai berikut
Kemudian tambahkan code push.lua dan simpan ke dalam folder yang sama dengan file main.lua.

File push.lua ini adalah library yang dapat menangani resolution handling, sehingga game developer bisa fokus membuat game dengan resolusi yang fixed. Jalankan script, hasilnya sebagai berikut
Kode program lengkap dapat diunduh di https://github.com/ardhiesta/pong

Comments

Popular posts from this blog

Contoh Inheritance (Pewarisan) di Java

Contoh Penerapan Interface di Pemrograman Java

Review Singkat Pilihan Transportasi Umum Rute Solo - Wonosobo