love的繪圖函數,共有以下7個,以"love.graphics."開頭
line 直線
quad 矩形
arc 弧線
point 點
rectangle 多邊形
circle 圓
triangle 三角形
drawmode有line和fill兩種,分別指輪廓和填充
blendmode有additive alpha subtractive multiplicative premultiplied
colormode有modulate,replace,combine
以上具體的說明請看love的wiki
下面說一下文字。
可以直接使用love.graphics.print()輸出文字,其實love2d自帶一個ttf字體“love._vera_ttf”(Bitstream Vera Sans)
love可以使用兩種字體,ttf和imagefont。下面是imagefont的介紹
The imagefont file is an image file in a format that Löve can load. It can contain transparent pixels, so a PNG file is preferable, and it also needs to contain spacer color that will separate the different font glyphs.
The upper left pixel of the image file is always taken to be the spacer color. All columns that have this color as their uppermost pixel are interpreted as separators of font glyphs. The areas between these separators are interpreted as the actual font glyphs.
The width of the separator areas affect the spacing of the font glyphs. It is possible to have more areas in the image than are required for the font in the love.graphics.newImageFont() call. The extra areas are ignored.
imagefont是一個圖像文件的格式,包含透明像素,所以png是可取的,它也需要包含間隔的顏色,將不同的字體字形分開。
左上角像素被作為間隔的顏色。所有列最高部分的像素的顏色被解釋為分隔符的字體字形。這些分隔符之間的區域被解釋為實際的字體。
分隔符寬度的影響的字體間距,在圖像中,間隔區比字體區多會更好,額外的區域將被忽略。僅支持 256 characters。
加載文字會減慢速度,所以建議只使用一種字體,之后對該字體進行操作,如設置大小,顏色等,不要反復加載字體。
注意我在使用中文字體時,發現不論是顯示中文還是因為字體大小要17才可,不知怎么回事。(2012.12.07)
這個和字體文件有關,用win7雅黑字體可以設置更小的字體。(2013.8.31)
注意下面這段代碼會每次加載字體,讓速度變慢。(2013.8.31)
function love.draw() drawGraphics() useDefaultFont("hello",210,100,20) useImgFont("world",210,140) useTTFFont("中文",210,160,18) end
可以簡單的加個判斷即:
local isInit=false
function love.draw()
if not isInit then drawGraphics() useDefaultFont("hello",210,100,20) useImgFont("world",210,140) useTTFFont("中文",210,160,18) end end
love2d繪圖后,圖像依舊是存在的,不需要每次繪制,所以需要時才繪制。
main.lua代碼如下
function drawGraphics() love.graphics.setBlendMode("alpha") --默認混合模式 love.graphics.setColor(230, 44, 123) love.graphics.rectangle("fill", 50, 50, 100, 100) love.graphics.setColor(12, 100, 230) love.graphics.setBlendMode("multiplicative") love.graphics.rectangle("fill", 75, 75, 125, 125) end --你可以自己動手設置各種屬性,看看效果 function useDefaultFont(text,x,y,size) love.graphics.setColor(255,0,0) love.graphics.setBlendMode("alpha") --love.graphics.setColorMode("combine") love.graphics.print("default font size is 12",x,y) local font = love.graphics.newFont( size ) love.graphics.setFont(font) love.graphics.print(text,x,y+size) end function useTTFFont(text,x,y,size) local font=love.graphics.newFont("assets/mona.ttf",size) love.graphics.setFont(font) love.graphics.print(text,x,y) end
--imagefont實際是一副有序的字符圖 function useImgFont(text,x,y) local font = love.graphics.newImageFont("assets/imagefont.png", " abcdefghijklmnopqrstuvwxyz" .. "ABCDEFGHIJKLMNOPQRSTUVWXYZ0" .. "123456789.,!?-+/():;%&`'*#=[]\"") love.graphics.setFont(font) love.graphics.print(text, x, y) end function love.load() love.graphics.setBackgroundColor(54, 172, 248) end function love.draw() drawGraphics() useDefaultFont("hello",210,100,20) useImgFont("world",210,140) useTTFFont("中文",210,160,18) end function love.update(dt) end function love.keypressed(key) end
工程文件下載地址http://pan.baidu.com/share/link?shareid=123276&uk=1913510140