Mac 下安裝使用 Love2D

Mac 下安裝使用 Love2D

概述

Love2D 是一款開源的 2D 開發引擎, 使用 Lua, 支持Windows,Linux,Mac,Android以及iOS多種平臺, 在國外的遊戲開發者中很受歡迎, 官網地址, 今天試用了一下, 感受很是簡單, 很好上手.html

須要注意的一點是, Love2D 在最近的版本里常常修改函數, 因此若是遇到之前的代碼沒法運行時能夠查查官網文檔, 看看是否是函數名作了修改.macos

若是你常常使用 Lua, 而且想用 Lua 作一些圖形圖像方面的編程試驗, 那就能夠試試它. 本文介紹如何在 Mac 下安裝使用 Love2D.編程

安裝

首先下載最新的版本Love2D-0.10.1-macosx-x64, 不幸的是這個地址的服務器是亞馬遜的S3, 屬於國內沒法訪問的地址, 因此提供一個網盤下載的地址Love2D-0.10.1-macosx-x64-網盤下載.bash

下載好以後, 雙擊解壓, 獲得一個名爲 love.app 的應用程序, 爲方便使用, 把它拖到應用程序中.服務器

咱們通常習慣在終端下經過命令來使用, 因此須要加一個簡短的別名 love, 須要編輯文件~/.bash_profile, 加入這兩行:app

# alias to love
alias love="/Applications/love.app/Contents/MacOS/love"

保存後執行 source ~/.bash_profile, 這樣, 你就能夠直接在終端執行 love 命令了, 若是不帶參數執行會啓動一個新 Love2D窗口, 也能夠查看版本號:框架

Air:love admin$ love --version
LOVE 0.10.1 (Super Toast)
Air:love admin$

第一個 Love2D 項目

新建項目

Love2D 寫遊戲很是方便, 首先新建一個目錄 love(目錄名能夠隨便起), 接着在該目錄下新建一個文件 main.lua(該文件必須使用這個名字), 而後在 main.lua 中編寫遊戲邏輯便可, 能夠試試這段代碼:less

function love.draw()
    love.graphics.print("Hello World", 400, 300)
end

執行命令是用 love 調用目錄, 它會自動加載目錄內的 main.lua 文件, 命令以下:ide

love ./love

它會新建一個窗口, 而後打印 Hello World.函數

項目配置文件

還能夠新建一個名爲 conf.lua 的文件, 用來進行各項設置, 能夠參考的模板以下:

function love.conf(t)
    t.identity = nil                    -- The name of the save directory (string)
    t.version = "0.10.1"                -- The LÖVE version this game was made for (string)
    t.console = false                   -- Attach a console (boolean, Windows only)
    t.accelerometerjoystick = true      -- Enable the accelerometer on iOS and Android by exposing it as a Joystick (boolean)
    t.externalstorage = false           -- True to save files (and read from the save directory) in external storage on Android (boolean) 
    t.gammacorrect = false              -- Enable gamma-correct rendering, when supported by the system (boolean)
 
    t.window.title = "Untitled"         -- The window title (string)
    t.window.icon = nil                 -- Filepath to an image to use as the window's icon (string)
    t.window.width = 800                -- The window width (number)
    t.window.height = 600               -- The window height (number)
    t.window.borderless = false         -- Remove all border visuals from the window (boolean)
    t.window.resizable = false          -- Let the window be user-resizable (boolean)
    t.window.minwidth = 1               -- Minimum window width if the window is resizable (number)
    t.window.minheight = 1              -- Minimum window height if the window is resizable (number)
    t.window.fullscreen = false         -- Enable fullscreen (boolean)
    t.window.fullscreentype = "desktop" -- Choose between "desktop" fullscreen or "exclusive" fullscreen mode (string)
    t.window.vsync = true               -- Enable vertical sync (boolean)
    t.window.msaa = 0                   -- The number of samples to use with multi-sampled antialiasing (number)
    t.window.display = 1                -- Index of the monitor to show the window in (number)
    t.window.highdpi = false            -- Enable high-dpi mode for the window on a Retina display (boolean)
    t.window.x = nil                    -- The x-coordinate of the window's position in the specified display (number)
    t.window.y = nil                    -- The y-coordinate of the window's position in the specified display (number)
 
    t.modules.audio = true              -- Enable the audio module (boolean)
    t.modules.event = true              -- Enable the event module (boolean)
    t.modules.graphics = true           -- Enable the graphics module (boolean)
    t.modules.image = true              -- Enable the image module (boolean)
    t.modules.joystick = true           -- Enable the joystick module (boolean)
    t.modules.keyboard = true           -- Enable the keyboard module (boolean)
    t.modules.math = true               -- Enable the math module (boolean)
    t.modules.mouse = true              -- Enable the mouse module (boolean)
    t.modules.physics = true            -- Enable the physics module (boolean)
    t.modules.sound = true              -- Enable the sound module (boolean)
    t.modules.system = true             -- Enable the system module (boolean)
    t.modules.timer = true              -- Enable the timer module (boolean), Disabling it will result 0 delta time in love.update
    t.modules.touch = true              -- Enable the touch module (boolean)
    t.modules.video = true              -- Enable the video module (boolean)
    t.modules.window = true             -- Enable the window module (boolean)
    t.modules.thread = true             -- Enable the thread module (boolean)
end

這個文件其實是執行了 love.conf(t) 函數來設置 Love2D 的各項配置參數, 若是有這個文件, 它會在全部其餘文件以前被加載運行.

Love2D 的程序框架

若是你用過 Processing, 你會感受這種程序結構很是熟悉, love.load() 只執行一次, love.update() 循環執行, 用於更新數據, love.draw() 循環執行, 用於繪製屏幕, 以下:

-- main.lua

-- Load some default values for our rectangle.
function love.load()
    x, y, w, h = 20, 20, 60, 20
end
 
-- Increase the size of the rectangle every frame.
function love.update(dt)
    w = w + 1
    h = h + 1
end
 
-- Draw a coloured rectangle.
function love.draw()
    love.graphics.setColor(0, 100, 100)
    love.graphics.rectangle("fill", x, y, w, h)
end

試試上面的程序.

對 shader 的支持

Love2D 支持 OpenGL, 不過好像支持的版本不高, glsl 1.2, 下面是一段來自半山無極:love2d教程11--着色器的例程:

function love.load()
    --爲了方便書寫
    gr, li, lf    = love.graphics, love.image, love.filesystem

    image            = gr.newImage('Love.jpg')
    width, height    = gr.getWidth(), gr.getHeight()
    effect = gr.newPixelEffect [[
        extern vec4 Cmin;
        extern vec4 Cmax;
        vec4 effect(vec4 color,Image tex,vec2 tc,vec2 pc)
        {    vec4 pixel = Texel(tex,tc);
            //vec4的四個份量分別是r,g,b,a
            //下面把圖片在cmax和cmin之間的像素的alpha份量設爲0,即透明
            if ((pixel.r<=Cmax.r && pixel.r>=Cmin.r) &&
                (pixel.g<=Cmax.g && pixel.g>=Cmin.g) &&
                (pixel.b<=Cmax.b && pixel.b>=Cmin.b))
            {pixel.a        = 0;}
            return pixel;
        }
        ]]

    --須要移除的像素範圍,這與具體的圖片相關,如此圖,背景爲藍色,主體爲粉紅,
    --除了紅色份量處的alpha不變,其餘份量處的alpha都設爲0
    remove_range = {
        r = { 0, 125 },
        g = { 0, 255 },
        b = { 0, 255 }
        }
    --opengl的顏色範圍0.0--1.0,值越小代表此份量佔的比例越小
    remove_min = {remove_range.r[1]/255,remove_range.g[1]/255,remove_range.b[1]/255,1}
    remove_max = {remove_range.r[2]/255,remove_range.g[2]/255,remove_range.b[2]/255,1}
    effect:send('Cmin',remove_min) --向cmin傳值
    effect:send('Cmax',remove_max)
    remove = false --透明變換開關

    -- 顏色變換效果
    effect2= love.graphics.newPixelEffect [[
        extern number time;
        vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords)
        {
        //這些函數爲了保證值在0.0--1.0之間,三角函數取值爲-1.0--1.0
            return vec4((1.0+sin(time))/2.0, abs(cos(time)), abs(sin(time)), 1.0);
        }

    ]]
    change=false --顏色變換開關
end

function love.draw()
    --因爲love是按前後順序繪圖,若是圖片不透明,此據會被擋住
    gr.print('you can not see this ,before the img transparented',10,40)
    if remove then
        gr.setPixelEffect( effect )
        gr.draw( image )
        gr.setPixelEffect() --還原默認的效果
        else
        gr.draw( image )
    end
    if change then
        gr.setPixelEffect(effect2)
        gr.rectangle('fill', 10,305,790,285)
        gr.setPixelEffect()

    end

    gr.print( 'Press <r> to change background to transparent.', 10, 10)
    gr.print( 'Press <c> to see the beautiful color.', 10, 25)

end

local t=0
function love.update(dt)
     t = t + dt
    effect2:send("time", t)

end

function love.keypressed(key)
    if key == 'escape'    then love.event.push( 'quit' )    end
    if key=="c" then change = not change end
    if key == 'r'        then remove = not remove end
end

不過咱們須要稍做修改, 由於有兩個函數的名字變了, Love2D0.90 版本中把函數 love.graphics.newPixelEffect 改爲了 love.graphics.newShader, 把函數 love.graphics.setPixelEffect 改爲了 love.graphics.setShader.

修改了這兩個函數就能夠正常運行了.

參考

Love2D官網百科
半山無極:使用lua開發遊戲-love2d教程彙總

相關文章
相關標籤/搜索