編輯環境 vsCode + love2D support(vsCode的插件) + love2D(c++寫的運作lua遊戲的一個引擎)udemy一個課介紹的
function love.load() ---love 的載入
button = {}
button.x = 200
button.y = 200
button.size = 50
score = 0
timer = 10
gameState = 1 --2 為開始 1 為關閉狀态
myFont = love.graphics.newFont(40)--設定字型
end
function love.update(dt) --delt time every frame --每dt 的更新
if gameState == 2 then --2 為開始狀态
if timer > 0 then
timer = timer -dt
end
if timer < 0 then --時間為0後狀态調整為關閉
timer = 0
gameState = 1
end
end
end
function love.draw() --每幀的畫
-- love.graphics.setColor(0,0,1) --love 11.0
-- love.graphics.rectangle("fill",200,400,200,100) --draw order
if gameState == 2 then
love.graphics.setColor(1,0,0)
love.graphics.circle("fill",button.x,button.y,button.size)
end
love.graphics.setFont(myFont)
love.graphics.setColor(1,1,1)
love.graphics.print("Score: " .. score) --列印分數 ..connect the string with the value (combine)
love.graphics.print("Timer: " .. math.ceil(timer),300,0) --列印時間
if gameState == 1 then
love.graphics.printf("RightClick anywhere to begin",--列印開始畫面
0,love.graphics.getHeight()/2,love.graphics.getWidth(),"center") --與print 的差別。
--在螢幕的中央列印一句話。
end
end
function love.mousepressed(x,y,b,istouch) --滑鼠點選的更新程式
if b == 1 and gameState == 2 then
if distanceBetween(love.mouse.getX(),love.mouse.getY(),button.x,button.y) <= button.size then
score = score + 1
button.x = math.random(button.size,love.graphics.getWidth() - button.size)
button.y = math.random(button.size,love.graphics.getHeight() - button.size)
end
end
if b == 2 and gameState == 1 then
gameState =2
timer = 10
end
end
function distanceBetween(x1,y1,x2,y2) --計算滑鼠點距離圓中心的距離
return math.sqrt((y2 - y1)^2 + (x2 - x1)^2)
end
右鍵點選任意位置開始,左鍵點選一下加一分,10秒後結束,計算分數,等待開始。