From 204e3dbf68225dd5e32fe58ae228eb4ac45d75de Mon Sep 17 00:00:00 2001 From: David Luevano Alvarado Date: Sat, 28 May 2022 19:15:14 -0600 Subject: almost done with the flappybird entry, did some rendering tests, added new images --- blog/dst/g/flappybird_godot_devlog_1.html | 459 ++++++++++++++++++++++++++++++ blog/dst/index.html | 3 +- blog/dst/tag/@english.html | 1 + blog/dst/tag/@gamedev.html | 1 + blog/dst/tag/@godot.html | 118 ++++++++ 5 files changed, 581 insertions(+), 1 deletion(-) create mode 100644 blog/dst/g/flappybird_godot_devlog_1.html create mode 100644 blog/dst/tag/@godot.html (limited to 'blog/dst') diff --git a/blog/dst/g/flappybird_godot_devlog_1.html b/blog/dst/g/flappybird_godot_devlog_1.html new file mode 100644 index 0000000..83e9ae2 --- /dev/null +++ b/blog/dst/g/flappybird_godot_devlog_1.html @@ -0,0 +1,459 @@ + + + + + + + Creating a FlappyBird clone in Godot 3.5 devlog 1 -- Luévano's Blog + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + +
+ +
+

Creating a FlappyBird clone in Godot 3.5 devlog 1

+ +

I just have a bit of experience with Godot and with gamedev in general, so I started with this game as it is pretty straight forward. On a high level the main characteristics of the game are:

+ +

The game was originally developed with Godot 4.0 alpha 8, but it didn’t support HTML5 (webassembly) export… so I backported to Godot 3.5 rc1. The source code can be found here and if any doubts you can check that, it also contains the exported versions for HTML5, Windows and Linux (be aware that the sound might be too high and I’m too lazy to make it configurable, it was the last thing I added).

+

Not going to specify all the details, only the needed parts and what could be confusing, as the source code is available and can be inspected; also this assumes minimal knowledge of Godot in general. Usually when I mention that a set/change of something it usually it’s a property and it can be found under the Inspector on the relevant node, unless stated otherwise; also, all scripts attached have the same name as the scenes, but in snake_case (scenes/nodes in PascalCase).

+

Initial project setup

+

Directory structure

+

I’m basically going with what I wrote on Godot project structure recently, and probably with minor changes depending on the situation.

+

Config

+

Default import settings

+

Since this is just pixel art, the importing settings for textures needs to be adjusted so the sprites don’t look blurry. Go to Project -> Project settings… -> Import defaults and on the drop down select Texture, untick everything and make sure Compress/Mode is set to Lossless.

+
+Project settings - Import defaults - Texture settings. +
+
+

General settings

+

It’s also a good idea to setup some config variables project-wide. To do so, go to Project -> Project settings… -> General, select Application/config and add a new property (there is a text box at the top of the project settings window) for game scale: application/config/game_scale for the type use float and then click on add; configure the new property to 3.0; On the same window, also add application/config/version as a string, and make it 1.0.0 (or whatever number you want).

+
+Project settings - General - Game scale and version properties. +
+
+

For my personal preferences, also disable some of the GDScript debug warnings that are annoying, this is done at Project -> Project settings -> General, select Debug/GDScript and toggle off “Unused arguments”, “Unused signal” and “Return value discarded”, and any other that might come up too often and don’t want to see.

+
+Project settings - General - GDScript debug warnings +
+
+

Finally, set the initial window size in Project -> Project settings… -> General, select Display/Window and set Size/Width and Size/Height to 600 and 800, respectively. As well as the Stretch/Mode to “viewport”, and Stretch/Aspect to “keep”:

+
+Project settings - General - Initial window size +
+
+

Keybindings

+

I only used 3 actions (keybindings): jump, restart and toggle_debug (optional). To add custom keybindings (so that the Input.something() API can be used), go to Project -> Project settings -> Input Map and on the text box write “jump” and click add, then it will be added to the list and it’s just a matter of clicking the + sign to add a “Physical key”, press any key you want to be used to jump and click ok. Do the same for the rest of the actions.

+
+Project settings - Input Map - Adding necessary keybindings +
+
+

Layers

+

Finally, rename the physics layers so we don’t lose track of which layer is which. Go to Project -> Layer Names -> 2d Physics and change the first 5 layer names to (in order): “player”, “ground”, “pipe”, “ceiling” and “score”.

+
+Project settings - Layer Names - 2D Physics +
+
+

Assets

+

For the assets I found out about a pack that contains just what I need: flappy-bird-assets by MegaCrash; I just did some minor modifications on the naming of the files. For the font I used Silver, and for the sound the resources from FlappyBird-N64 (which seems to be taken from 101soundboards.com which the orignal copyright holder is .Gears anyways).

+

Importing

+

Create the necessary directories to hold the respective assets and it’s just a matter of dragging and dropping, I used directories: res://entities/actors/player/sprites/, res://fonts/, res://levels/world/background/sprites/, res://levels/world/ground/sprites/, res://levels/world/pipe/sprites/, res://sfx/. For the player sprites, the “FileSystem” window looks like this (entities/actor directories are really not necessary):

+
+Player sprite imports +
+
+

It should look similar for other directories, except maybe for the file extensions. For example, for the sfx:

+
+SFX imports +
+
+

Scenes

+

Now it’s time to actually create the game, by creating the basic scenes that will make up the game. The hardest part and the most confusing is going to be the TileMaps, so that goes first.

+

TileMaps

+

I’m using a scene called “WorldTiles” with a Node2D node as root called the same. With 2 different TileMap nodes as children named “GroundTileMap” and “PipeTileMap” (these are their own scene); yes 2 different TileMaps because we need 2 different physics colliders (In Godot 4.0 you can have a single TileMap with different physics colliders in it). Each node has its own script. It should look something like this:

+
+Scene - WorldTiles (TileMaps) +
+
+

I used the following directory structure:

+
+Scene - WorldTiles - Directory structure +
+
+

To configure the GroundTileMap, select the node and click on “(empty)” on the TileMap/Tile set property and then click on “New TileSet”, then click where the “(empty)” used to be, a new window should open on the bottom:

+
+TileSet - Configuration window +
+
+

Click on the plus on the bottom left and you can now select the specific tile set to use. Now click on the yellow “+ New Single Tile”, activate the grid and select any of the tiles. Should look like this:

+
+TileSet - New single tile +
+
+

We need to do this because for some reason we can’t change the snap options before selecting a tile. After selecting a random tile, set up the Snap Options/Step (in the Inspector) and set it to 16x16 (or if using a different tile set, to it’s tile size):

+
+TileSet - Tile - Step snap options +
+
+

Now you can select the actual single tile. Once selected click on “Collision”, use the rectangle tool and draw the rectangle corresponding to that tile’s collision:

+
+TileSet - Tile - Selection and collision +
+
+

Do the same for the other 3 tiles. If you select the TileMap itself again, it should look like this on the right (on default layout it’s on the left of the Inspector):

+
+TileSet - Available tiles +
+
+

The ordering is important only for the “underground tile”, which is the filler ground, it should be at the end (index 3); if this is not the case, repeat the process (it’s possible to rearrange them but it’s hard to explain as it’s pretty weird).

+

At this point the tilemap doesn’t have any physics and the cell size is wrong. Select the “GroundTileMap”, set the TileMap/Cell/Size to 16x16, the TileMap/Collision/Layer set to bit 2 only (ground layer) and disable any TileMap/Collision/Mask bits. Should look something like this:

+
+TileMap - Cell size and collision configuration +
+
+

Now it’s just a matter of repeating the same for the pipes (“PipeTileMap”), only difference is that when selecting the tiles you need to select 2 tiles, as the pipe is 2 tiles wide, or just set the Snap Options/Step to 32x16, for example, just keep the cell size to 16x16.

+

Default ground tiles

+

I added few default ground tiles to the scene, just for testing purposes but I left them there. These could be place programatically, but I was too lazy to change things. On the “WorldTiles” scene, while selecting the “GroundTileMap”, you can select the tiles you want to paint with, and left click in the grid to paint with the selected tile. Need to place tiles from (-8, 7) to (10, 7) as well as the tile below with the filler ground (the tile position/coordinates show at the bottom left, refer to the image below):

+
+Scene - WorldTiles - Default ground tiles +
+
+

Player

+

On a new scene called “Player” with a KinematicBody2D node named “Player” as the root of the scene, then for the children: AnimatedSprite as “Sprite”, CollisionShape2D as “Collision” (with a circle shape) and 3 AudioStreamPlayers for “JumpSound”, “DeadSound” and “HitSound”. Not sure if it’s a good practice to have the audio here, since I did that at the end, pretty lazy. Then, attach a script to the “Player” node and then it should look like this:

+
+Scene - Player - Node setup +
+
+

Select the “Player” node and set the CollisionShape2D/Collision/Layer to 1 and the CollisionObject2D/Collision/Mask to 2 and 3 (ground and pipe).

+

For the “Sprite” node, when selecting it click on the “(empty)” for the AnimatedSprite/Frames property and click “New SpriteFrames”, click again where the “(empty)” used to be and ane window should open on the bottom:

+
+Scene - Player - SpriteFrames window +
+
+

Right off the bat, set the “Speed” to 10 FPS (bottom left) and rename “default” to “bird_1”. With the “bird_1” selected, click on the “Add frames from a Sprite Sheet”, which is the second button under “Animation Frames:” which looks has an icon of a small grid (next to the folder icon), a new window will popup where you need to select the respective sprite sheet to use and configure it for importing. On the “Select Frames” window, change the “Vertical” to 1, and then select all 4 frames (Ctrl + Scroll wheel to zoom in):

+
+Scene - Player - Sprite sheet importer +
+
+

After that, the SpriteFrames window should look like this:

+
+Scene - Player - SpriteFrames window with sprite sheet configured +
+
+

Finally, make sure the “Sprite” node has the AnimatedSprite/Animation is set to “bird_1” and that the “Collision” node is configured correctly for its size and position (I just have it as a radius of 7). As well as dropping the SFX files into the corresponding AudioStreamPlayer (into the AudioStreamPlayer/Stream property).

+

Other

+

These are really simple scenes that don’t require much setup:

+ +

Game

+

This is the actual “Game” scene that holds all the playable stuff, here we will drop in all the previous scenes; the root node is a Node2D and also has an attached script. Also need to add 2 additional AudioStreamPlayers for the “start” and “score” sounds, as well as a Sprite for the background (Sprite/Offset/Offset set to (0, 10)) and a Camera2D (Camera2D/Current set to true (checked)). It should look something like this:

+
+Scene - Game - Node setup +
+
+

The scene viewport should look something like the following:

+
+Scene - Game - Viewport +
+
+

UI

+

Fonts

+

We need some font “Resources” to style the Label fonts. Under the FileSystem window, right click on the fonts directory (create one if needed) and click on “New Resource…” and select DynamicFontData, save it in the “fonts” directory as “SilverDynamicFontData.tres” (“Silver” as it is the font I’m using) then double click the just created resource and set the DynamicFontData/Font Path to the actual “Silver.ttf” font (or whatever you want).

+

Then create a new resource and this time select DynamicFont, name it “SilverDynamicFont.tres”, then double click to edit and add the “SilverDynamicFontData.tres” to the DynamicFont/Font/Font Data property (and I personally toggled off the DynamicFont/Font/Antialiased property), now just set the DynamicFont/Settings/(Size, Outline Size, Outline Color) to 32, 1 and black, respectively (or any other values you want). It should look something like this:

+
+Resource - DynamicFont - Default font +
+
+

Do the same for another DynamicFont which will be used for the score label, named “SilverScoreDynamicFont.tres”. Only changes are Dynamic/Settings/(Size, Outline Size) which are set to 128 and 2, respectively. The final files for the fonts should look something like this:

+
+Resource - Dynamicfont - Directory structure +
+
+

Scene setup

+

This has a bunch of nested nodes, so I’ll try to be concise here. The root node is a CanvasLayer named “UI” with its own script attached, and for the children:

+ +

The scene ends up looking like this:

+
+Scene - UI - Node setup +
+
+

Main

+

This is the final scene where we connect the Game and the UI. It’s made of a Node2D with it’s own script attached and an instance of “Game” and “UI” as it’s children.

+

This is a good time to set the default scene when we run the game by going to Project -> Project settings… -> General and in Application/Run set the Main Scene to the “Main.tscn” scene.

+

Scripting

+

I’m going to keep this scripting part to the most basic code blocks, as it’s too much code, for a complete view you can head to the source code.

+

As of now, the game itself doesn’t do anything if we hit play. The first thing to do so we have something going on is to do the minimal player scripting.

+

Player

+

The most basic code needed so the bird goes up and down is to just detect “jump” key presses and add a negative jump velocity so it goes up (y coordinate is reversed in godot…), we also check the velocity sign of the y coordinate to decide if the animation is playing or not.

+
class_name Player
+extends KinematicBody2D
+
+export(float, 1.0, 1000.0, 1.0) var JUMP_VELOCITY: float = 380.0
+
+onready var sprite: AnimatedSprite = $Sprite
+
+var gravity: float = 10 * ProjectSettings.get_setting("physics/2d/default_gravity")
+var velocity: Vector2 = Vector2.ZERO
+
+
+func _physics_process(delta: float) -> void:
+    velocity.y += gravity * delta
+
+    if Input.is_action_just_pressed("jump"):
+        velocity.y = -JUMP_VELOCITY
+
+    if velocity.y < 0.0:
+        sprite.play()
+    else:
+        sprite.stop()
+
+    velocity = move_and_slide(velocity)
+
+

You can play it now and you should be able to jump up and down, and the bird should stop on the ground (although you can keep jumping). One thing to notice is that when doing sprite.stop() it stays on the last frame, we can fix that using the code below (and then change sprite.stop() for _stop_sprite()):

+
func _stop_sprite() -> void:
+    if sprite.playing:
+        sprite.stop()
+    if sprite.frame != 0:
+        sprite.frame = 0
+
+

Where we just check that the last frame has to be the frame 0.

+

Now just a matter of adding other needed code for moving horizontally, add sound by getting a reference to the AudioStreamPlayers and doing sound.play() when needed, as well as handling death scenarios by adding a signal died at the beginning of the script and handle any type of death scenario using the below function:

+
func _emit_player_died() -> void:
+    # bit 2 corresponds to pipe (starts from 0)
+    set_collision_mask_bit(2, false)
+    dead = true
+    SPEED = 0.0
+    emit_signal("died")
+    # play the sounds after, because yield will take a bit of time,
+    # this way the camera stops when the player "dies"
+    velocity.y = -DEATH_JUMP_VELOCITY
+    velocity = move_and_slide(velocity)
+    hit_sound.play()
+    yield(hit_sound, "finished")
+    dead_sound.play()
+
+

Finally need to add the actual checks for when the player dies (like collision with ground or pipe) as well as a function that listens to a signal for when the player goes to the ceiling.

+

WorldDetector

+

The code is pretty simple, we just need a way of detecting if we ran out of ground and send a signal, as well as sending as signal when we start detecting ground/pipes behind us (to remove it) because the world is being generated as we move. The most basic functions needed are:

+
func _was_colliding(detector: RayCast2D, flag: bool, signal_name: String) -> bool:
+    if detector.is_colliding():
+        return true
+    if flag:
+        emit_signal(signal_name)
+        return false
+    return true
+
+
+func _now_colliding(detector: RayCast2D, flag: bool, signal_name: String) -> bool:
+    if detector.is_colliding():
+        if not flag:
+            emit_signal(signal_name)
+            return true
+    return false
+
+

We need to keep track of 3 “flags”: ground_was_colliding, ground_now_colliding and pipe_now_colliding (and their respective signals), which are going to be used to do the checks inside _physics_process. For example for checking for new ground: ground_now_colliding = _now_colliding(old_ground, ground_now_colliding, "ground_started_colliding").

+

WorldTiles

+

This script is what handles the “GroundTileMap” as well as the “PipeTileMap” and just basically functions as a “Signal bus” connecting a bunch of signals and just tracking how many pipes have been placed:

+
# omitting code for signal definitions and references to nodes
+export(int, 2, 20, 2) var PIPE_SEP: int = 6
+var tiles_since_last_pipe: int = PIPE_SEP - 1
+
+
+func _ready() -> void:
+    connect("place_ground", ground_tile_map, "_on_WorldTiles_place_ground")
+    connect("remove_ground", ground_tile_map, "_on_WorldTiles_remove_ground")
+    connect("place_pipe", pipe_tile_map, "_on_WorldTiles_place_pipe")
+    connect("remove_pipe", pipe_tile_map, "_on_WorldTiles_remove_pipe")
+
+
+func _on_WorldDetector_ground_stopped_colliding() -> void:
+    emit_signal("place_ground")
+
+    tiles_since_last_pipe += 1
+    if tiles_since_last_pipe == PIPE_SEP:
+        emit_signal("place_pipe")
+        tiles_since_last_pipe = 0
+
+
+func _on_WorldDetector_ground_started_colliding() -> void:
+    emit_signal("remove_ground")
+
+
+func _on_WorldDetector_pipe_started_colliding() -> void:
+    emit_signal("remove_pipe")
+
+

GroundTileMap

+

asdfg

+

Temp notes

+ + + + + +
+ +
+ + + + \ No newline at end of file diff --git a/blog/dst/index.html b/blog/dst/index.html index 320fe6b..a7f9a2a 100644 --- a/blog/dst/index.html +++ b/blog/dst/index.html @@ -88,13 +88,14 @@

Tags: -english, gamedev, rant, server, short, spanish, tools, tutorial, update

+english, gamedev, godot, rant, server, short, spanish, tools, tutorial, update

Articles