Difference between revisions of "Tuto Gideros Game Template1 Part 7 Game"
(wip) |
(wip) |
||
Line 3: | Line 3: | ||
This is going to be the last scene for our Gideros Game Template1 tutorial. A quick and simple one because you may decide to organize your game differently. | This is going to be the last scene for our Gideros Game Template1 tutorial. A quick and simple one because you may decide to organize your game differently. | ||
− | + | Usually, when I make games and prototypes, I put all my levels in one scene. When I change levels I reload the '''Game''' scene with a variable indicating the current level, eg.: currlevel = 1, 2, ... and load the appropriate assets (graphics, sounds, ...). | |
− | + | I have been using this kind of architecture for quite some time, instead of calling my game scenes Level1, Level2, ..., I decided to call it: '''LevelX'''. | |
− | |||
− | I have been using this kind of architecture for quite some time | ||
The '''Game''' scene ('''LevelX''') will mainly consist of: | The '''Game''' scene ('''LevelX''') will mainly consist of: | ||
+ | * a pause variable | ||
* moving the mouse cursor out of the way :-) | * moving the mouse cursor out of the way :-) | ||
− | * arranging our graphics into | + | * arranging our graphics into layers: ''background'', ''actors'', ''foreground'', ... |
* a game loop | * a game loop | ||
Line 137: | Line 136: | ||
== Code comments == | == Code comments == | ||
− | + | We start by defining a ''ispaused'' to pause the game using the letter P on the keyboard. | |
− | + | ||
− | * | + | === init === |
− | * add the game loop Event.ENTER_FRAME | + | We move the mouse cursor out of the way. Done! |
− | + | ||
+ | For the graphics, we will arrange them in a Sprite tree hierarchy: the main layer and all the other layers (Sprite). This way you could use the '''mainlayer''' Sprite as a camera, a viewport, ... . The tree for the LevelX scene looks like this: | ||
+ | *** mainlayer '''Sprite''' ''Parent'' | ||
+ | ** bglayer '''Sprite''' ''Child'' | ||
+ | ** bgfxlayer '''Sprite''' ''Child'' | ||
+ | ** actorslayer '''Sprite''' ''Child'' | ||
+ | ** fgfxlayer '''Sprite''' ''Child'' | ||
+ | ** fglayer '''Sprite''' ''Child'' | ||
+ | |||
+ | This is a Parent/Child relationship. | ||
+ | |||
+ | The layers are arranged in a pseudo "z" axis, the top layer being drawn first and the others on top: | ||
+ | * bglayer (drawn first) | ||
+ | * bgfxlayer | ||
+ | * actorslayer | ||
+ | * fgfxlayer | ||
+ | * fglayer (drawn last, on top of all the others) | ||
+ | |||
+ | The above layers are added to the main layer which is added to the scene Sprite and the root (stage) Sprite: | ||
+ | * root (stage) Gideros default '''Sprite''' | ||
+ | ** self (LevelX) inherit the '''Sprite''' class | ||
+ | *** mainlayer '''Sprite''' | ||
+ | |||
+ | All our images are added to the appropriate layer as a child. | ||
+ | |||
+ | |||
+ | |||
+ | add the game loop Event.ENTER_FRAME | ||
+ | listen to some key to pause the game, go back to the Menu scene, ... | ||
=== Sprite layers === | === Sprite layers === |
Revision as of 19:07, 27 October 2024
The Game Scene
This is going to be the last scene for our Gideros Game Template1 tutorial. A quick and simple one because you may decide to organize your game differently.
Usually, when I make games and prototypes, I put all my levels in one scene. When I change levels I reload the Game scene with a variable indicating the current level, eg.: currlevel = 1, 2, ... and load the appropriate assets (graphics, sounds, ...).
I have been using this kind of architecture for quite some time, instead of calling my game scenes Level1, Level2, ..., I decided to call it: LevelX.
The Game scene (LevelX) will mainly consist of:
- a pause variable
- moving the mouse cursor out of the way :-)
- arranging our graphics into layers: background, actors, foreground, ...
- a game loop
It makes sense to create the file in the scenes folder. You can call the file levelX.lua, and the code:
LevelX = Core.class(Sprite)
local ispaused = false
function LevelX:init()
-- move the cursor out of the way
if not application:isPlayerMode() then
local sw, sh = application:get("screenSize") -- the user's screen size!
application:set("cursorPosition", sw, sh) -- 0, 0
end
-- layer sprites
local mainlayer = Sprite.new() -- one Sprite to hold them all
local bglayer = Sprite.new() -- background layer
local bgfxlayer = Sprite.new() -- background fx layer
local actorslayer = Sprite.new() -- actors layer
local fgfxlayer = Sprite.new() -- foreground fx layer
local fglayer = Sprite.new() -- foreground layer
-- current level assets
local bggfx, fggfx
if g_currlevel == 1 then
bggfx = Pixel.new(math.random(0xffffff), 1, myappwidth, myappheight)
fggfx = Pixel.new(math.random(0xffffff), 1, myappwidth, myappheight/8)
elseif g_currlevel == 2 then
bggfx = Pixel.new(math.random(0xffffff), 1, myappwidth, myappheight)
elseif g_currlevel == 3 then
bggfx = Pixel.new(math.random(0xffffff), 1, myappwidth, myappheight)
fggfx = Pixel.new(math.random(0xffffff), 1, myappwidth, myappheight/16)
end
-- the player1
self.player1 = Player1.new()
-- position
if bggfx then bggfx:setPosition(0+myappleft, 0+myapptop) end
if fggfx then fggfx:setPosition(0+myappleft, myappheight+myapptop-fggfx:getHeight()) end
self.player1.x = myappwidth/2+myappleft
self.player1.y = myappheight/2+myapptop
self.player1:setPosition(self.player1.x, self.player1.y)
-- add children to corresponding layer
if bggfx then bglayer:addChild(bggfx) end -- add the background graphics to the background layer if any
if fggfx then fglayer:addChild(fggfx) end -- add the foreground graphics to the foreground layer if any
actorslayer:addChild(self.player1)
-- order
mainlayer:addChild(bglayer) -- first the background layer
mainlayer:addChild(bgfxlayer) -- then the background fx layer
mainlayer:addChild(actorslayer) -- then the actors layer
mainlayer:addChild(fgfxlayer) -- then the foreground fx layer
mainlayer:addChild(fglayer) -- lastely the foreground layer
-- the main sprite
self:addChild(mainlayer) -- the only Sprite layer that needs to be added to to scene tree hierarchy!
-- the game loop
self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
-- let's go
self:myKeysPressed()
end
-- game loop
local dt
function LevelX:onEnterFrame(e)
if not ispaused then
dt = e.deltaTime
if self.player1.isleft and not self.player1.isright and self.player1.x > 0 then -- LEFT
self.player1.vx = -self.player1.speed*dt
self.player1.flip = -1
elseif self.player1.isright and not self.player1.isleft and self.player1.x < myappwidth then -- RIGHT
self.player1.vx = self.player1.speed*dt
self.player1.flip = 1
else -- IDLE
self.player1.vx = 0
end
if self.player1.isup and not self.player1.isdown and self.player1.y > 0 then -- UP
self.player1.vy = -self.player1.speed*dt
elseif self.player1.isdown and not self.player1.isup and self.player1.y < myappheight then -- DOWN
self.player1.vy = self.player1.speed*dt
else
self.player1.vy = 0
end
-- actions
if self.player1.isactionpunch1 then
self.player1.vx *= 0.5*dt -- you choose, magik XXX
self.player1.vy *= 0.5*dt -- you choose, magik XXX
elseif self.player1.isactionpunch2 then
self.player1.vx *= 0.25*dt -- you choose, magik XXX
self.player1.vy *= 0.25*dt -- you choose, magik XXX
elseif self.player1.isactionpunch3 then
self.player1.vx *= 0.1*dt -- you choose, magik XXX
self.player1.vy *= 0.1*dt -- you choose, magik XXX
end
-- move and flip
self.player1.x += self.player1.vx
self.player1.y += self.player1.vy
self.player1:setPosition(self.player1.x, self.player1.y)
self.player1.sprite:setScale(self.player1.sx * self.player1.flip, self.player1.sy)
end
end
-- keys handler
function LevelX:myKeysPressed()
self:addEventListener(Event.KEY_DOWN, function(e) -- KEY_UP
if e.keyCode == KeyCode.ESC or e.keyCode == KeyCode.BACK then self:gotoScene() end -- MENU
if e.keyCode == KeyCode.P then ispaused = not ispaused print("is paused: "..tostring(ispaused)) end -- PAUSE
-- modifier
local modifier = application:getKeyboardModifiers()
local alt = (modifier & KeyCode.MODIFIER_ALT) > 0
if (not alt and e.keyCode == KeyCode.ENTER) then -- nothing here!
elseif alt and e.keyCode == KeyCode.ENTER then -- SWITCH FULLSCREEN
ismyappfullscreen = not ismyappfullscreen
application:setFullScreen(ismyappfullscreen)
end
end)
end
-- scenes navigation
function LevelX:gotoScene()
self:removeAllListeners()
for i = stage:getNumChildren(), 1, -1 do
stage:removeChildAt(i)
end collectgarbage()
stage:addChild(Transitions.new(Menu.new())) -- next scene
end
Code comments
We start by defining a ispaused to pause the game using the letter P on the keyboard.
init
We move the mouse cursor out of the way. Done!
For the graphics, we will arrange them in a Sprite tree hierarchy: the main layer and all the other layers (Sprite). This way you could use the mainlayer Sprite as a camera, a viewport, ... . The tree for the LevelX scene looks like this:
- mainlayer Sprite Parent
- bglayer Sprite Child
- bgfxlayer Sprite Child
- actorslayer Sprite Child
- fgfxlayer Sprite Child
- fglayer Sprite Child
This is a Parent/Child relationship.
The layers are arranged in a pseudo "z" axis, the top layer being drawn first and the others on top:
- bglayer (drawn first)
- bgfxlayer
- actorslayer
- fgfxlayer
- fglayer (drawn last, on top of all the others)
The above layers are added to the main layer which is added to the scene Sprite and the root (stage) Sprite:
- root (stage) Gideros default Sprite
- self (LevelX) inherit the Sprite class
- mainlayer Sprite
- self (LevelX) inherit the Sprite class
All our images are added to the appropriate layer as a child.
add the game loop Event.ENTER_FRAME listen to some key to pause the game, go back to the Menu scene, ...
Sprite layers
I create a main Sprite layer levelgfxlayer which will hold all the other layers (Sprites and Bitmaps).
The background layer is a Bitmap but you can change it to be a Sprite and add bitmaps to it.
The splat layer is a Sprite to add all sort of fx (splats in this case). Please feel free to rename any of the layers.
The action layer is a Sprite which we add all graphics that can be considered an actor (the player, the enemies, collectibles, ...)
Finally the foreground layer is a Bitmap which you can change to be a Sprite.
the game loop
If the game is not in Pause mode it will execute what's inside it.
keys handler
We also listen to any key press to switch the state of the game or go back to the Menu scene, ...
Next?
Well that's it for this Gideros Game Template1 tutorial.
We should be good to go and dive straight into coding the core of the game, all the Template stuff being taken care of.
This is now yours to use/modify (add scenes, ...).
I hope this tutorial was useful and will help you on your journey of making great games using Gideros Studio ;-)
the Project file
Media:GIDEROS_GAME_TEMPLATE1.zip (tip: right click and save link as)
Prev.: Tuto Gideros Game Template1 Part 6 Options
END
Tutorial - Gideros Game Template1