Introduction to programming for kids

It’s the summer holidays, after all. So I wanted to spent some time with my 7 years old daughter to teach her programming. ⌨️

I spent some time before discussing with her some of the basics concept verbally: the different parts of a computer, and basic elements of procedural programming languages (variable, condition, loops).

And recently, I proposed to practice and code something on the computer together. She was reluctant at first I guess the lectures I gave her before lost her haha.

So I decided to make a small game first to pique her interest 😯

I gave a quick thought and came up with that direction:

  • A small text game we can play in console (something I could finish in an hour)

  • I would be using Ruby (the least boilerplate possible and elegant code for beginners)

  • The game code would be one file only and linear as much as possible to be easy to read almost like a story with exit points where the player loses (easy to read)

  • I would use some variables, conditions and one loop to show her actual usage of them (something not too complex to introduce complexity step by step)

  • And to show her a text game can still be cool I would add some awesome ASCII arts 😎

After about an hour I had a game ⌚

You are an adventurer who just arrived in a city which has been claimed by a terrible dragon who is now living in the castle of the local king.

It’s cliché but simple enough for her to understand and me to come up with a story quickly.

The dragon game 🐲

Avatar

It’s a text game, but I still wanted to have a bit of incarnation for the player so that when she tries my game it would be citing her name.

So the code starts with that:

puts "Welcome adventurer, start by entering your name:"

name = ask

clear_screen

puts "Very well, #{name} ! We need your help.. \n"

We have one variable: name and I hid some of the complexity thanks to Ruby, by defining a function ask and clear_screen in another file that I wouldn’t show to her.

def ask 
  print "> "
  return gets.chomp
end

def clear_screen
  system('clear') || system('cls')
end

A good example of simplification of the reality

Once you entered your name, a character describes the awful situation with the dragon, and ask you to confirm if you are capable of helping them.

The code looks like that:

puts "Are you capable of defeating the dragon ? (yes or no)"

choice = ask

if choice == "no" 
    print "Ah !? "
    sleep(3)
    print "What a shame..."
    sleep(3)
    puts " thanks anyway. Farewell #{name}."

    exit
end

print "Super !"
sleep(1)
print " Super ! "
sleep(1)
puts "We are saved !"

There are several things interesting here.

  • First we are using a condition the first one so she can a practical example of a condition

  • Second we use that condition to exit early the game: game over we lose, thanks to exit

  • Third we use sleep(n) to give a bit of tension in the text showing up piece by piece. Combined with print which allows us to keep writing on the same line unlike puts

  • Fourth, an important thing that I discussed with my daughter for a while, we are simplifying the choice! Something we often do in game development or software in general. We do choices on how much of abstraction to use to represent the reality. We have to manage our time, resources to allocate them to the main feature, the main element that will end with the best experience. Hence sometimes creating illusions. Illusion of choice. Thus here, the player answers no, we stop the game. The player says anything but no, the game continues. Obviously we could try to convert the input from the player to lowercase to catch even uppercase, use a regex, or even test for the player input against ”yes” with a loop. But nothing of that would serve or improve really the experience of the game!

The hard part: a loop

puts "Hint: 48 + 21 - 5"
sleep(1)
puts "You are trying to enter a code:"

code = -1

while code != 64
    code = ask_number

    if code != 64 
        puts "It does not work, try something else..."
    end
end

We needed a loop to put in practice what I discussed with her. Here I went with a mathematical operation that the player needs to solve and until the player didn’t give the right answer he can try. We could have may be write it a bit more nicer without the condition inside, but again limited time, so it’s good enough.

Some others conditions

The game continues after with several tough situations where the player has to pick the right decision and we test that using several conditions. To alternate and go a bit further into conditions, I also decided to create a situation with 3 choices, only one winning. By using a if/else if condition.

puts "The dragon starts breathing fire strongly towards you ! Quick, what do you do ?"

puts "  1. You jump on the left side to avoid the flames."
puts "  2. You use your shield made in dragon skin."
puts "  3. You hide yourself behind a box on your right side."

choice = ask_number

if choice == 1
    # Some specific bad end 
    puts "..." 
    puts "Game Over \n"

    exit
elsif choice == 3
    # Another specific bad end 
    puts "..."
    puts "Game Over \n"

    exit
end

It is interesting I believe to show her that conditions can be more complex. In her game we also used if choice == 1 || choice == 3 showing again an example of a more complex condition and a simplification of the game to allocate more resources on the rest of the game too.

Adding juice

My first draft of my game did not have any ASCII art, but after I thought it would be nice to add a bit of juice, or show her something cool in the middle of the game to break the pattern (https://lawsofux.com/von-restorff-effect/).

So here is an example of a very cool ASCII dragon I found online (thanks to joan stark and IronWing for their art) that I included in my game.

def dragon_fire 
  puts "                                       \\/"
  puts "                                       ^`'."
  puts "                                       ^   `'."
  puts "             (                         ^      `'."
  puts "           )  )        \\/              ^         `'."
  puts "         (   ) @       /^              ^            `'."
  puts "       )  )) @@  )    /  ^             ^               `'."
  puts "      ( ( ) )@@      /    ^            ^                  `'."
  puts "    ))  ( @@ @ )    /      ^           ^                     `'."
  puts "   ( ( @@@@@(@     /       |\\_/|,      ^                        `'."
  puts "  )  )@@@(@@@     /      _/~/~/~|C     ^                           `'."
  puts "((@@@(@@@@@(     /     _(@)~(@)~/\\C    ^                              `'."
  puts "  ))@@@(@@)@@   /     /~/~/~/~/`\\~`C   ^             __.__               `'."
  puts "   )@@@@(@@)@@@(     (o~/~o)^,) \\~ \\C  ^          .' -_'-'\"...             `."
  puts "    ( (@@@)@@@(@@@@@@_~^~^~,-/\\~ \\~ \\C/^        /`-~^,-~-`_~-^`;_           `."
  puts "      @ )@@@(@@@@@@@   \\^^^/  (`^\\.~^ C^.,  /~^~^~^/_^-_`~-`~-~^\\- /`'-./`'-. ;"
  puts "       (@ (@@@@(@@      `''  (( ~  .` .,~^~^-`-^~`/'^`-~ _`~-`_^-~\\         ^^"
  puts "           @jgs@             (((` ~ .-~-\\ ~`-_~`-/_-`~ `- ~-_- `~`;"
  puts "          /                 /~((((` . ~-~\\` `  ~ |:`-_-~_~`  ~ _`-`;"
  puts "         /                 /~-((((((`.\\-~-\\ ~`-`~^\\- ^_-~ ~` -_~-_`~`;"
  puts "        /                 /-~-/(((((((`\\~-~\\~`^-`~`\\ -~`~\\-^ -_~-_`~-`;"
  puts "       /                 /~-~/  `((((((|-~-|((`.-~.`Y`_,~`\\ `,- ~-_`~-`;"
  puts "      /              ___/-~-/     `\"\"\"\"|~-~|\"''    /~-^ .'    `:~`-_`~-~`;"
  puts "     /         _____/  /~-~/           |-~-|      /-~-~.`      `:~^`-_`^-:"
  puts "    /    _____/        ((((            ((((      (((((`           `:~^-_~-`;"
  puts "    \\___/                                                          `:_^-~`;"
  puts "                                                                    `:~-^`:"
  puts "                                                                  ,`~-~`,`"
  puts "                                                                 ,\"`~.,'"
  puts "                                                               ,\"-`,\"`"
  puts "                                                             ,\"_`,\""
  puts "                                                            ,\",\"`"
  puts "                                                         ;~-~_~~;"
  puts "                                                          '. ~.'    "
end

If you want to check out the game of my daughter and the dragon one I made, 🔎

Their entire code is available here: https://github.com/SonnyAD/mini-games

Note that the games are in French though. So feel free to translate or simply reuse the structure to practice programming with your kid too.

To conclude

I think that mini text game has been a good introduction to programming and she liked it. So it’s a success I believe. Since that day, we did another session where we added a rock, paper, scissors mini-game inside the game of my daughter. And we will be adding more and more complexity step by step.

0
Subscribe to my newsletter

Read articles from Sonny Alves Dias directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sonny Alves Dias
Sonny Alves Dias

Curious software engineer