Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
GX Platformer Tutorial
#5
Part 2 - Let's Start Coding

Setting the Scene
For any GX project there are a couple of steps that we always need to do in order to use the library.
First, we need to include the GX engine code:
Code: (Select All)
'$Include: 'lib/gx.bi'

'$Include: 'lib/gx.bm'
This will include all of the GX game data structures and methods that we will need to build our game. All of our specific game code will be added between these two include statements.

Next, we need to define our game event method. The GX engine will call this method for all events that occur in the game... more on this later.
Code: (Select All)
'$Include: 'lib/gx.bi'

Sub GXOnGameEvent (e As GXEvent)
End Sub
'$Include: 'lib/gx.bm'

Now we need to create our scene with GXSceneCreate. This is usually the first step in the setup of our game. Let's create a new scene that is 256 pixels by 144 pixels.
Code: (Select All)
'$Include: 'lib/gx.bi'

GXSceneCreate 256, 144

Sub GXOnGameEvent (e As GXEvent)
End Sub
'$Include: 'lib/gx.bm'

If you run the program at this point you will just see a small black screen.  So, let's add something there to look at...

Adding the Player
Let's create a new entity which represents our player character with the GXEntityCreate method:
Code: (Select All)
...
GXSceneCreate 256, 144

' Create the player
Dim Shared player As Long
player = GXEntityCreate("img/character.png", 16, 20, 4)
...
The first argument references the spritesheet image which should be used for the entity display.  The next two arguments define the width and height of the entity, respectively, and the last argument indicates how many frames of animation to expect.  Let's look a bit more closely at the character.png to see how GX expects spritesheets to be constructed...
     
As you can see from the image above, this spritesheet contains four different animation sequences with each distinct sequence placed on a separate row in the spritesheet image.  Each animation sequence has four frames of animation.

In order to see what affect this has had we need to tell GX to start handing game events and display.  We do this by calling the GXSceneStart method:
Code: (Select All)
...
' Create the player
Dim Shared player As Long
player = GXEntityCreate("img/character.png", 16, 20, 4)

GXSceneStart
...

If you run the program now you will see our little guy in the upper left corner.  Since we haven't yet specified any position for our player entity it has defaulted to (0, 0).  We can also see that the sprite image has defaulted to the first frame of the first animation sequence.
   

This is way too small though, let's scale it up a bit.  GX has this scaling built in for us.  We can do this by adding a call to GXSceneScale right after we create the scene:
Code: (Select All)
'$Include: 'lib/gx.bi'

GXSceneCreate 256, 144
GXSceneScale 2

...
This will tell the engine to scale the entire scene up to two times its original size and start to give us that retro pixel art look.  While we're in there, let's also reposition the player character.  We can do this with a call to the GXEntityPos method:
Code: (Select All)
...

' Create the player
Dim Shared player As Long
player = GXEntityCreate("img/character.png", 16, 20, 4)
GXEntityPos player, 120, 75

GXSceneStart
...
The first parameter is the handle to the player object that was returned from the call to GXEntityCreate.  This will be the case or all of the other GXEntity* methods.  The next two parameters specify the x and y position of the entity.  This will cause the upper left corner of the entity to be positioned at the indicated coordinates.
   

Ok, this is great and all, but a game needs to be more than a static image, next time we'll start tackling some movement and talk about game events...
Reply


Messages In This Thread
GX Platformer Tutorial - by dbox - 03-05-2025, 02:01 AM
RE: GX Platformer Tutorial - by grymmjack - 03-05-2025, 01:28 PM
RE: GX Platformer Tutorial - by dbox - 03-05-2025, 07:19 PM
RE: GX Platformer Tutorial - by dbox - 03-06-2025, 01:45 AM
RE: GX Platformer Tutorial - by dbox - 03-07-2025, 08:58 PM
RE: GX Platformer Tutorial - by madscijr - 03-08-2025, 12:32 AM
RE: GX Platformer Tutorial - by dbox - 03-08-2025, 02:24 AM
RE: GX Platformer Tutorial - by dbox - 03-08-2025, 06:03 AM



Users browsing this thread: 6 Guest(s)