feat: structure code samples to be runnable DRGTK games

This ensures we can easily test the code for a given chapter and verify
it does what is expected.
This commit is contained in:
Brett Chalupa 2022-12-03 07:07:33 -05:00
parent d54c75b5e1
commit c30165fa0d
8 changed files with 69 additions and 4 deletions

View file

@ -12,6 +12,18 @@ Run `mdbook serve` to boot up the web server that handles compiling everything.
When the code is pushed to the `main` branch on GitHub, an action runs that deploys it to the `gh-pages` branch.
## Running Samples
The repository contains complete games for each separate code sample to ensure the code works and runs.
Code samples live in `src/code` and then are organized by chapter and then sections within a chapter.
Clone this book repository into a DragonRuby GTK engine directory and run the samples with:
``` console
./dragonruby book/src/code/chapter_03/01_input
```
## Publishing
The 3D cover file is generated with https://diybookcovers.com/

View file

@ -142,7 +142,7 @@ You can move your dragon completely off the screen, lost in the great unknown! T
[TODO: drawing about boundaries]
``` ruby
{{#include code/chapter_02_movement_and_boundaries.rb}}
{{#include code/chapter_02/app/main.rb}}
```
Our dragon won't leave the screen. Woot woot! We've got some serious code here! Look at that logic. Here's what changed:
@ -152,7 +152,7 @@ We moved the width and height of the player into variables so that they're easie
Here's the good stuff. We check the boundary for the x axis:
``` ruby
{{#include code/chapter_02_movement_and_boundaries.rb:20:26}}
{{#include code/chapter_02/app/main.rb:20:26}}
```
We check the right side of the screen: if the current player's x position plus their width is greater than `args.grid.w`, then we set the x position to the width of the screen (`args.grid.w`) minus the width of the sprite. For example, if we move the sprite so it has the x position of 1284, 4 pixels past the right edge of the screen, we override that change and set it to 1280 minus the player's width.

View file

@ -1,10 +1,22 @@
# Spit Fire
Our next mission is to make our dragon spit fire because that's just what they do. We'll piece this whole thing into a game with a narrative and intentional design soon enough. But let's keep focusing on simple mechanics and code for right now.
Our next mission is to make our dragon spit fire because that's just what they do. We'll piece this whole thing into a game with win and lose states, a narrative, and intentional design soon enough. But let's keep focusing on simple mechanics and the code for right now.
## Fire on Input
[ displaying a label ]
In the last chapter, we used `args.inputs` to check for player input from the four main directions. If those inputs were being pressed, then we moved the dragon. Now let's check to see if the <kbd>Z</kbd> key is pressed to know when to have our dragon spit a fire ball.
To check if a key was pressed, we can use `args.inputs.keyboard.key_down` and then whatever key we want to check. So in our case, we'll check `args.inputs.keyboard.key_down.z`. In our `#tick` method, right above where we render the dragon sprite, let's check for that input:
``` ruby
{{#include code/chapter_03/01_input/app/main.rb:36:40}}
```
Using `puts` is a really helpful way to check that our game works as we expect it to. In this case, every tick where the Z key is pressed down, it prints the string "Z key pressed" to the console (open it iwth <kbd>~</kbd>, remember?). Run your game and press the Z key a bunch and then open your console.
<img alt="screenshot of DragonRuby console showing Z key pressed 6 times" src="./img/c03-console-puts.png" style="max-width: 420px">
## Expanded Control Support
## Moving Fireballs

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

View file

@ -0,0 +1,41 @@
def tick args
args.state.player_x ||= 120
args.state.player_y ||= 280
speed = 12
player_w = 100
player_h = 80
if args.inputs.left
args.state.player_x -= speed
elsif args.inputs.right
args.state.player_x += speed
end
if args.inputs.up
args.state.player_y += speed
elsif args.inputs.down
args.state.player_y -= speed
end
if args.state.player_x + player_w > args.grid.w
args.state.player_x = args.grid.w - player_w
end
if args.state.player_x < 0
args.state.player_x = 0
end
if args.state.player_y + player_h > args.grid.h
args.state.player_y = args.grid.h - player_h
end
if args.state.player_y < 0
args.state.player_y = 0
end
if args.inputs.keyboard.key_down.z
puts "Z key pressed"
end
args.outputs.sprites << [args.state.player_x, args.state.player_y, player_w, player_h, 'sprites/misc/dragon-0.png']
end

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 58 KiB