Character Select
can anyone explain to me how to make a character select screen? (to select, use left and right controls)
can anyone explain to me how to make a character select screen? (to select, use left and right controls)
You need to control the draw, update loop. Let only one object have access to the screen keyboard and update .
ControlLoop = class
constructor = function( call )
setCall( call )
end
draw = function()
screen.clear("purple")
end
update = function()
if keyboard.press.SPACE then
return this.call
else
return this
end
end
setCall = function( call )
this.call = call
end
end
Select = class extends ControlLoop
constructor = function( call )
super( call )
this.select = 0
end
draw = function()
screen.clear("orange")
screen.drawText( "LEFT RIGHT to select", 0 , 70, 30, "black")
screen.fillRect( -50, 0, 50, 100, "red")
screen.fillRect( 50, 0, 50, 100, "green")
screen.drawRect( 100 * this.select -50, 0, 60, 110 )
screen.drawText( " SPACE to EXIT ", 0, -70, 30, "black")
end
update = function()
if keyboard.press.LEFT then
this.select = 0
elsif keyboard.press.RIGHT then
this.select = 1
end
return super()
end
end
Game = class extends ControlLoop
constructor = function()
select = new Select( this )
end
draw = function()
screen.clear("blue")
screen.drawText(" You select " + select.select, 0, 0, 30, "white" )
screen.drawText(" Press SPACE to select", 0, -40, 40, "orange" )
end
update = function()
if keyboard.press.SPACE then
audio.beep("C")
return select
else
return this
end
end
end
init = function()
game = new Game()
current = game
end
update = function()
current = current.update()
end
draw = function()
current.draw()
end
I suggest using different scene classes with own update
and draw
functions. We will have scene
pointer to the current scene and you can change the current scene at any time assigning any scene to the pointer:
scene = selectionScene
scene = playingScene
scene = gameOverScene
The selection logic is contained in SelectionScene.update
function and corresponding rendering in SelectionScene.draw
. The other two scenes are provided as examples of data exchange between scenes and, again, scene switching
//list the hero selection sprites
local heroSprites = ["sprite_a", "sprite_b", "sprite_c"]
//define the hero count
local heroesCount = heroSprites.length
SelectionScene = class
selection = 0
update = function()
if keyboard.press.LEFT then
selection = selection - 1
end
if keyboard.press.RIGHT then
selection = selection + 1
end
if selection < 0 then
selection = heroesCount - 1
elsif selection >= heroesCount then
selection = 0
end
if keyboard.press.SPACE then
playingScene.restart(selection)
global.scene = playingScene
end
end
draw = function()
screen.clear()
local shift = -(heroesCount - 1) * 40 * 0.5
for i = 0 to heroesCount - 1
screen.drawSprite(heroSprites[i], i * (40) + shift, 0)
end
screen.drawRect(shift + selection * 40, 0, 40, 40)
end
end
PlayingScene = class
restart = function(i)
this.i = i
t = 0
end
update = function()
t = t + 1
if t >= 180 then
global.scene = gameOverScene
end
end
draw = function()
screen.clear()
screen.drawSprite(heroSprites[i], 0, 0)
screen.drawText(t + "/180", 0,-30, 16)
end
end
GameOverScene = class
update = function()
if keyboard.press.SPACE then
playingScene.restart(selection)
global.scene = selectionScene
end
end
draw = function()
screen.clear()
screen.drawText("Game Over", 0, 10, 16)
screen.drawText("Press SPACE to continue", 0, -10, 16)
end
end
//all the scenes
selectionScene = new SelectionScene()
playingScene = new PlayingScene()
gameOverScene = new GameOverScene()
//currentScene
scene = selectionScene
update = function()
//update current scene
scene.update()
end
draw = function()
//draw current scene
scene.draw()
end