121 lines
2.5 KiB
Text
121 lines
2.5 KiB
Text
-- Lesson 20: Menus And Screens. The finished script, exactly as the lesson ends.
|
|
dofile("Singe/Framework.singe")
|
|
|
|
overlaySetResolution(discGetWidth(), discGetHeight())
|
|
singeSetPauseKeyEnabled(false)
|
|
|
|
local SHIP_Y = 300
|
|
|
|
local ship = spriteLoad(DIR .. "art/spaceship.png")
|
|
local hudFont = fontLoad("Singe/FreeSansBold.ttf", 20)
|
|
local shipX = 0
|
|
local speed = 2
|
|
local score = 0
|
|
local paused = false
|
|
local gui = nil
|
|
local page = nil
|
|
|
|
|
|
local function say(text)
|
|
if gui ~= nil then
|
|
guiSetValue(gui, page, "status", text)
|
|
end
|
|
end
|
|
|
|
|
|
local function closeMenu()
|
|
paused = false
|
|
singeSetPauseFlag(false)
|
|
if gui ~= nil then
|
|
guiHide(gui, page)
|
|
guiSetInput(gui, false)
|
|
end
|
|
end
|
|
|
|
|
|
local function openMenu()
|
|
paused = true
|
|
singeSetPauseFlag(true)
|
|
if gui ~= nil then
|
|
guiSetValue(gui, page, "score", string.format("%06d", score))
|
|
guiShow(gui, page)
|
|
guiSetInput(gui, true)
|
|
end
|
|
end
|
|
|
|
|
|
if singeHasGpu() then
|
|
gui = guiNew(overlayGetWidth(), overlayGetHeight())
|
|
page = guiLoad(gui, DIR .. "pause.rml")
|
|
guiHide(gui, page)
|
|
guiSetInput(gui, false)
|
|
|
|
guiSetHandler(gui, page, "resume", "click", function()
|
|
closeMenu()
|
|
end)
|
|
|
|
guiSetHandler(gui, page, "restart", "click", function()
|
|
score = 0
|
|
shipX = 0
|
|
closeMenu()
|
|
end)
|
|
|
|
guiSetHandler(gui, page, "quit", "click", function()
|
|
singeQuit()
|
|
end)
|
|
|
|
guiSetHandler(gui, page, "speed", "change", function(g, d, id, event, value)
|
|
speed = math.floor(tonumber(value))
|
|
say("Speed " .. speed)
|
|
end)
|
|
end
|
|
|
|
|
|
function onInputPressed(what)
|
|
if what == SWITCH_PAUSE then
|
|
if paused then
|
|
closeMenu()
|
|
else
|
|
openMenu()
|
|
end
|
|
end
|
|
end
|
|
|
|
|
|
function onOverlayUpdate()
|
|
overlayClear()
|
|
|
|
if not paused then
|
|
shipX = shipX + speed
|
|
if shipX > overlayGetWidth() then
|
|
shipX = -spriteGetWidth(ship)
|
|
score = score + 10
|
|
end
|
|
end
|
|
spriteDraw(ship, shipX, SHIP_Y)
|
|
|
|
fontSelect(hudFont)
|
|
colorForeground(255, 255, 255)
|
|
fontPrint(16, 16, string.format("SCORE %06d", score))
|
|
fontPrint(16, 40, "P pauses")
|
|
|
|
if paused then
|
|
if gui ~= nil then
|
|
guiDraw(gui)
|
|
else
|
|
colorForeground(255, 211, 90)
|
|
fontPrint(16, 70, "PAUSED -- no GPU, so no menu")
|
|
end
|
|
end
|
|
|
|
return OVERLAY_UPDATED
|
|
end
|
|
|
|
|
|
function onShutdown()
|
|
if gui ~= nil then
|
|
guiDelete(gui)
|
|
end
|
|
spriteUnload(ship)
|
|
fontUnload(hudFont)
|
|
end
|