Godot - Large Game Field
In this simple game we will explore moving around a larger area than is displayed at any one time. The movement will be the same as the previous game, but this time we will have a background texture for our game world, which is larger than the game window. Our character can move around the world but you only see a part of it at a time.
Requirements Game world larger than is displayed at any one time (the window)
- Game world has some interesting appearance
- Character can move in response to player input (arrow keys)
- Player can see character at all times
- Character cannot move outside the game world
Here is the use case (in Visual Paradigm) for our only functional requirement, moving the character. The description includes the constraint that movement should take place within the bounds of the game field.
We’ll use a single scene in Godot to achieve this. Character will be a Sprite with the default Godot icon as before, with movement via keyboard as before. This time however we will prevent the character moving outside the bounds of the game world. The game world will be represented by a large background texture, so we’ll need a TextureRect object to contain that. To create a window on this world we will attach a camera to the sprite, which will follow it around. The area shown by the camera will be smaller than the world (the background texture). To organize these extra objects we’ll use a Node2D as the top component of our scene tree, so that the background and the sprite are separate rather than one being a child of the other. There can be only one node at the top of the scene tree and everything else is a child of that (or grandchild…). The Input object is not part of the scene, but it is part of the Game.
This time the Node2D will check Input for key presses, and move the Sprite if it is not already at the edge of the game field (defined by the background texture size). The camera, being a child of the sprite, simply follows it around, so I haven’t shown this in the sequence diagram. The only thing the game developer has to do is make sure the camera is set to be the active camera.
The background is an image filled with a pebbly pattern, 3000px x 2000px, set as the texture to be displayed by the TextureRect object. The game window is set to 1000px x 800px. so this is what the camera will show. In moving the sprite we check so see where it is, and if is already at an edge we don't move it any further in that direction. We have to make allowance for the width of the sprite (128px) because it's position is calculated from it's centre. The game window (sitting over the editor window) and the code are shown in the screenshots below
Because the script is attached to the Node2D object and not the Sprite object as in the previous post, the code can't just use position, which would be the position of the Node2D. It must find the position of the Sprite (child of the Node2D) with the identifier $Sprite.position.
Next time we'll start looking at some interactions between our character and other game objects.