日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

love2d教程3--输入和音乐

發布時間:2025/4/5 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 love2d教程3--输入和音乐 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

love2d的輸入包括love.keyboard,love.mouse,love.joystick(手柄)
鍵盤:
按鍵檢測是否按下可以用love.keyboard.isDown("鍵值"),如love.keyboard.isDown("up"),
檢測向上鍵是否按下,按下返回真.
還可以用love.keyboard.setKeyRepeat(delay, interval)設置重復按鍵的延時和間隔,
當延時(delay)設為0時,不允許按住一個鍵不放,interval沒明白怎么回事.如果你知道,請告訴我.
也可以在love.keypressed(key)回調函數中檢測是哪個按鍵(key),我試了一下,不支持按住一個
鍵不放.
鼠標:
鼠標在love.mouse模塊中,函數很多,可以自己查看wiki
手柄:
手柄在love.joystick模塊中,由于沒手柄,沒有試驗,與鍵盤,鼠標應該類似.

love2d的音樂功能在love.audio模塊里,底層使用openal,支持20多種音樂格式.
音樂只有一種數據類型"source".使用love.audio.newSource可以創建一個source對象
sound = love.audio.newSource("pling.wav", "static") --有static和stream兩種模式,分別對應短和長的音樂
默認是stream模式.
wiki上兩種模式的對比如下,可見對長音樂應該使用strem模式
Keep in mind that, if you pass love.audio.newSource "static" as a second argument,
the sound file will be expanded into memory, so if you load a 5MB compressed .ogg file
that way, it would consume ~50MB RAM when fully decompressed. Consider not using "static"
in such cases.

接著可以使用source對象的方法控制音樂的一些效果如:
sound:setVolume(0.9) -- 音量為90%
sound:setPitch(0.5) -- 音調為50%,類似頻率的高低

下面是我的示例程序,演示了鼠標軌跡的捕捉和音樂的播放.

tutor3.lua

local mousePos={} qx,qy=100,100 playSound=false function drawQuad(x,y,width,height)love.graphics.setColor(0, 255, 0)love.graphics.quad("fill",x,y,x+width,y,x+width,y+height,x,y+height) endfunction drawMouseTrack()love.graphics.setPointSize( 3 )local x, y = love.mouse.getPosition( )table.insert(mousePos,x)table.insert(mousePos,y)love.graphics.setColor(255,0,0)--取出mousePos table里的元素,下面即c語言的for(int i=1,i<mousePos.legth;i+=2)for i=1,#mousePos,2 dolove.graphics.point(mousePos[i],mousePos[i+1])end end

main.lua

require("tutor03")function love.load() sound = love.audio.newSource("assets/sound.wav", "static") music = love.audio.newSource("assets/music.mp3") --默認是stream,動態加載,適合播放時間長的音樂 music:setVolume(0.5) love.audio.play(music) endfunction love.draw()drawMouseTrack()drawQuad(qx,qy,20,20)love.graphics.print("qx=" .. qx .. " qy=" .. qy,20,20)endfunction love.update(dt)--按鍵檢測if(love.keyboard.isDown("up")) thenqy=qy-5endif(love.keyboard.isDown("down")) thenqy=qy+5endif(love.keyboard.isDown("left")) thenqx=qx-5endif(love.keyboard.isDown("right")) thenqx=qx+5end--檢測小方塊是否運動到窗口邊界if(qx>=780)thenqx=780playSound=trueelseif(qx<0)thenqx=0playSound=trueendif(qy>=580)thenqy=580playSound=trueelseif(qy<=0)thenqy=0playSound=trueendif(playSound==true) thenlove.audio.play(sound)playSound=falseendendfunction love.keypressed(key) --~ if(key=="up") then --~ qy=qy-5 --~ elseif(key=="down") then --~ qy=qy+5 --~ elseif(key=="left") then --~ qx=qx-5 --~ elseif(key=="right") then --~ qx=qx+5 --~ end end

源碼下載http://pan.baidu.com/share/link?shareid=123821&uk=1913510140

總結

以上是生活随笔為你收集整理的love2d教程3--输入和音乐的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。