Part 3 - Game Events & Movement
Before we start adding movement to our game we need to first understand the basics of the game event model in GX. And to understand the game event model let's first look at the game loop.
An important aspect of any game is the game loop. The pseudo-code for a typical game loop looks something like this:
You'll notice in the examples shown so far there is no main loop in our actual game code. That is because the GX engine is taking care of a lot of the heavy lifting here for us. Instead, the engine sends events to our game at various points so we can implement the desired behavior that is specific to our game. It does this by calling the GXOnGameEvent method that we added at the beginning of Part 1.
Ok, getting back to our code, if we want to move our little player character when the user presses certain keys we will need to handle the Update event. Let's add a Select...Case statement to our GXOnGameEvent to set ourselves up for handling multiple events and add a Case statement for the GXEVENT_UPDATE event type. To keep things organized, let's call a new OnUpdate method when the event is fired.
Now, let's add the logic in our new OnUpdate method to make the player move left or right when the left or right arrow keys are pressed:
Ok, we're starting to get somewhere now, we can move our player. However, he just seems to be sliding around without moving his feet and he doesn't change direction. So, let's add some animation. First let's add some constants to define our left and right animation sequences that we looked at in Part 2:
Now if you run the program you will see that our player now appears to be walking left or right based on the keys pressed. When the keys are released he stops and the walk animation is stopped since we called the GXAnimationStop method.
Before we start adding movement to our game we need to first understand the basics of the game event model in GX. And to understand the game event model let's first look at the game loop.
An important aspect of any game is the game loop. The pseudo-code for a typical game loop looks something like this:
Code: (Select All)
Initialization
Do
Input Handling
Game Logic / Updates
Physics & Collision Detection
Rendering
Timing/Frame Regulation
Loop
- Initialization
This is where the game sets up everything it needs, like loading assets, initializing variables, and preparing game objects.
- Input Handling
The loop checks for user inputs (keyboard, mouse, controller) and processes them so the game can react accordingly.
- Game Logic/Updates
Here, the game updates the state of the world based on inputs, AI behavior, physics, and other rules of the game.
- Physics & Collision Detection
The loop calculates object movements and interactions, ensuring things like gravity, collisions, and object responses are handled correctly.
- Rendering
The game draws the updated world to the screen. This includes rendering characters, backgrounds, UI elements, and visual effects.
- Timing/Frame Regulation
To ensure the game runs at a consistent speed, the loop manages time, often using a fixed or variable timestep to control updates and rendering.
You'll notice in the examples shown so far there is no main loop in our actual game code. That is because the GX engine is taking care of a lot of the heavy lifting here for us. Instead, the engine sends events to our game at various points so we can implement the desired behavior that is specific to our game. It does this by calling the GXOnGameEvent method that we added at the beginning of Part 1.
Ok, getting back to our code, if we want to move our little player character when the user presses certain keys we will need to handle the Update event. Let's add a Select...Case statement to our GXOnGameEvent to set ourselves up for handling multiple events and add a Case statement for the GXEVENT_UPDATE event type. To keep things organized, let's call a new OnUpdate method when the event is fired.
Code: (Select All)
...
Sub GXOnGameEvent (e As GXEvent)
Select Case e.event
Case GXEVENT_UPDATE: OnUpdate e
End Select
End Sub
Sub OnUpdate (e As GXEvent)
End Sub
...
Now, let's add the logic in our new OnUpdate method to make the player move left or right when the left or right arrow keys are pressed:
Code: (Select All)
...
Sub OnUpdate (e As GXEvent)
If GXKeyDown(GXKEY_LEFT) Then
GXEntityVX player, -40
ElseIf GXKeyDown(GXKEY_RIGHT) Then
GXEntityVX player, 40
Else
GXEntityVX player, 0
End If
End Sub
...
The GXKeyDown function will return true (-1) if the specified key is currently being pressed and false (0) when it is not. The GXEntityVX sets the entity's X or horizontal velocity. A negative value will indicate movement to the left. A positive value will indicate movement to the right. The value passed for the velocity is in pixels per second.Ok, we're starting to get somewhere now, we can move our player. However, he just seems to be sliding around without moving his feet and he doesn't change direction. So, let's add some animation. First let's add some constants to define our left and right animation sequences that we looked at in Part 2:
Code: (Select All)
...
'$Include: 'lib/gx.bi'
Const MOVE_RIGHT = 1
Const MOVE_LEFT = 2
GXSceneCreate 256, 144
GXSceneScale 2
...
Then we'll add a call to GXEntityAnimate to show the appropriate animation when the user presses a direction key. The third parameter to this method indicates the speed of the animation in frames per second.Code: (Select All)
...
Sub OnUpdate (e As GXEvent)
If GXKeyDown(GXKEY_LEFT) Then
GXEntityVX player, -40
GXEntityAnimate player, MOVE_LEFT, 10
ElseIf GXKeyDown(GXKEY_RIGHT) Then
GXEntityVX player, 40
GXEntityAnimate player, MOVE_RIGHT, 10
Else
GXEntityVX player, 0
GXEntityAnimateStop player
End If
End Sub
...
Now if you run the program you will see that our player now appears to be walking left or right based on the keys pressed. When the keys are released he stops and the walk animation is stopped since we called the GXAnimationStop method.