{"id":43,"date":"2008-02-08T01:35:10","date_gmt":"2008-02-08T00:35:10","guid":{"rendered":"http:\/\/www.bluej.org\/mrt\/?p=43"},"modified":"2020-05-15T15:46:26","modified_gmt":"2020-05-15T15:46:26","slug":"teaching-my-daughter-to-code-part-iv","status":"publish","type":"post","link":"https:\/\/blogs.kcl.ac.uk\/proged\/2008\/02\/08\/teaching-my-daughter-to-code-part-iv\/","title":{"rendered":"Teaching My Daughter To Code, Part IV: Return of the Daleks"},"content":{"rendered":"<p><a href=\"http:\/\/blogs.kcl.ac.uk\/proged\/wp-content\/blogs.dir\/192\/files\/2008\/02\/thedoctor-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-765 alignright\" src=\"http:\/\/blogs.kcl.ac.uk\/proged\/wp-content\/blogs.dir\/192\/files\/2008\/02\/thedoctor-1.png\" alt=\"\" width=\"220\" height=\"181\" \/><\/a>Welcome back, dear readers, to the fourth part of Sophie&#8217;s journey of writing a DrWho computer game with <a href=\"http:\/\/www.greenfoot.org\/\">Greenfoot<\/a> and Java.<\/p>\n<p>If you have read the previous parts, then thank you for sticking with us for so long! (If not, you may like to start reading here: <a href=\"https:\/\/blogs.kcl.ac.uk\/proged\/2008\/01\/20\/teaching-my-daughter-to-code\/\">Part I<\/a>, <a href=\"https:\/\/blogs.kcl.ac.uk\/proged\/2008\/01\/23\/teaching-my-daughter-to-code-part-ii\/\">Part II<\/a>, <a href=\"https:\/\/blogs.kcl.ac.uk\/proged\/2008\/02\/01\/teaching-my-daughter-to-code-part-iii\/\">Part III<\/a>).<\/p>\n<p>I&#8217;ll try to make it short today \u2013 it&#8217;s been a long day, and it&#8217;s getting late. But this programming session I&#8217;d like to record took place five days ago, I have only sparse notes, and I&#8217;d like to get it down before I forget too much. I have been busy this week, so I haven&#8217;t had time to write this up earlier, but there was so much lovely and encouraging feedback on the previous posts that encouraged me to continue writing this up.<\/p>\n<p>Thus, without further delay, on to the next task: Reaching the TARDIS with the energy pellets!<br \/>\n<!--more--><br \/>\nTo show you upfront what we did: Here is the <a href=\"http:\/\/www.greenfoot.org\/live\/DrWho4\/DrWho4.html\">result<\/a> of our work today. Go and <a href=\"http:\/\/www.greenfoot.org\/live\/DrWho4\/DrWho4.html\">have a quick play<\/a>!<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/blogs.kcl.ac.uk\/proged\/wp-content\/blogs.dir\/192\/files\/2008\/02\/window8.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-766\" src=\"http:\/\/blogs.kcl.ac.uk\/proged\/wp-content\/blogs.dir\/192\/files\/2008\/02\/window8.png\" alt=\"\" width=\"762\" height=\"489\" srcset=\"https:\/\/blogs.kcl.ac.uk\/proged\/files\/2008\/02\/window8.png 762w, https:\/\/blogs.kcl.ac.uk\/proged\/files\/2008\/02\/window8-300x193.png 300w, https:\/\/blogs.kcl.ac.uk\/proged\/files\/2008\/02\/window8-467x300.png 467w\" sizes=\"auto, (max-width: 762px) 100vw, 762px\" \/><\/a><\/p>\n<p>If you just had a play, you might have noticed what we did: more sound, a TARDIS, and counting pellets. But one thing after the other&#8230;<\/p>\n<p>After the last programming session, Sophie was really keen to continue. The next day was a Saturday, we were sitting at home, and Sophie showed Feena what she had done. We all played the game for a bit (trying to collect the energy pellets), and Sophie was itching to continue.<\/p>\n<p>What she was most keen on was using one of the sounds we had found the previous day. (We had found several Doctor Who sounds by googling around a bit.) So far, we had used her self-made sounds, but Sophie really liked one &#8220;THE DOCTOR MUST DIE!&#8221; sound clip (spoken by the Daleks!) which she wanted to use at the start of the game.<\/p>\n<p>So, that&#8217;s the first thing we did. Moving the sound file into the scenario sounds folder is easy, playing it is easy as well, but getting it to happen when the game starts is actually a little harder then it might seem.<\/p>\n<p>The Greenfoot world has an &#8216;act&#8217; method that gets called on every step of the execution, but it does not have an initialisation method that gets executed only once. (Except for the constructor, but that gets executed when the world is created, which is well before the game starts.)<\/p>\n<p>The trick we used is to use the world&#8217;s act method, and to count the act cycles. Then we can check whether we&#8217;re in the first act cycle, and play the sound only then.<\/p>\n<p>This was actually a very nice bit of code (pedagogically speaking) since it reinforced quite nicely what we had done before. It shows a different context of a variable and an if statement. And it&#8217;s a nice little piece of logical thinking to get the idea. Here is the code we wrote in the &#8220;Mars&#8221; class:<\/p>\n<pre>    <span style=\"color: #82121c\">public<\/span> <span style=\"color: #c71c2b\">void<\/span> act()\r\n    {\r\n        counter++;\r\n        <span style=\"color: #82121c\">if<\/span> (counter == 1)\r\n        {\r\n            Greenfoot.playSound(<span style=\"color: #236523\">\"doctordie.wav\"<\/span>);\r\n        }\r\n    }<\/pre>\n<p>We also added, of course, the declaration for the counter field at the top of the class.<\/p>\n<p>Once we had that done, we added the Tardis. This is easy by now: The Tardis is an entirely passive object in our game at the moment, so it was just a question of finding an image (easy with Google), creating an Actor subclass named &#8220;Tardis&#8221; and using the image (also easy). No code required.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/blogs.kcl.ac.uk\/proged\/wp-content\/blogs.dir\/192\/files\/2008\/02\/Tardis1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-768\" src=\"http:\/\/blogs.kcl.ac.uk\/proged\/wp-content\/blogs.dir\/192\/files\/2008\/02\/Tardis1.jpg\" alt=\"\" width=\"624\" height=\"177\" srcset=\"https:\/\/blogs.kcl.ac.uk\/proged\/files\/2008\/02\/Tardis1.jpg 624w, https:\/\/blogs.kcl.ac.uk\/proged\/files\/2008\/02\/Tardis1-300x85.jpg 300w, https:\/\/blogs.kcl.ac.uk\/proged\/files\/2008\/02\/Tardis1-500x142.jpg 500w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/a><\/p>\n<p>We then wanted to get the first game task complete: the Doctor should collect all six energy pellets, and then get to the Tardis. If he gets there, a &#8220;Tardis sound&#8221; (which we also found on the web) should be played, and the Doctor gets to the next level.<\/p>\n<p>First, we needed code to check whether the Doctor had reached the Tardis. We already had code that checked whether the Doctor ran into an energy pellet. It looked like this:<\/p>\n<pre>        <span style=\"color: #82121c\">if<\/span> (touches (EnergyPellet.<span style=\"color: #c71c2b\">class<\/span>))\r\n        {\r\n            remove (EnergyPellet.<span style=\"color: #c71c2b\">class<\/span>);\r\n            Greenfoot.playSound(<span style=\"color: #236523\">\"gotit.wav\"<\/span>);\r\n        }<\/pre>\n<p>It was now quite easy to adapt that to check whether he ran into the Tardis. Once I pointed out that we can reuse the same code, Sophie had no problem copying and adapting it:<\/p>\n<pre>        <span style=\"color: #82121c\">if<\/span> (touches (Tardis.<span style=\"color: #c71c2b\">class<\/span>))\r\n        {\r\n            Greenfoot.playSound(<span style=\"color: #236523\">\"tardis.wav\"<\/span>);\r\n        }<\/pre>\n<p>(This was all done inline in the Doctor&#8217;s &#8216;act&#8217; method. I was thinking that it&#8217;s time to introduce private methods at some stage to get a better code structure with nice short, cohesive methods. But I decided to leave that until another day.)<\/p>\n<p>This already worked somewhat \u2013 the sound played when the Doctor got to the Tardis \u2013 but it wasn&#8217;t quite right. It also played the sound when we hadn&#8217;t collect all energy pellets first.<\/p>\n<p>So we decided that there should be six pellets, and introduced a counter. We also discussed how to stop the simulation. I showed Sophie the Javadoc API for the Greenfoot class (which you can just open from within Greenfoot), and she then found the &#8216;stopSimulation&#8217; method. We worked out the syntax together, and she is getting close to understanding dot notation for method calls. Here&#8217;s the code we ended up with:<\/p>\n<pre>        <span style=\"color: #82121c\">int<\/span> pelletCounter = 0;\r\n        ....\r\n\r\n        <span style=\"color: #82121c\">if <\/span>(touches (EnergyPellet.<span style=\"color: #c71c2b\">class<\/span>))\r\n        {\r\n            remove (EnergyPellet.class);\r\n            Greenfoot.playSound(<span style=\"color: #236523\">\"gotit.wav\"<\/span>);\r\n            pelletCounter++;\r\n        }\r\n\r\n        <span style=\"color: #82121c\">if<\/span> (touches (Tardis.<span style=\"color: #c71c2b\">class<\/span>))\r\n        {\r\n            <span style=\"color: #82121c\">if<\/span> (pelletCounter == 6)\r\n            {\r\n                Greenfoot.playSound(<span style=\"color: #236523\">\"tardis.wav\"<\/span>);\r\n                Greenfoot.stopSimulation();\r\n            }\r\n        }<\/pre>\n<p><strong><em>Whoohoo<\/em><\/strong> \u2013 that was great! Some time of intensive playing followed with both Sophie and Feena getting very much into it and having great fun.<\/p>\n<p>The last thing we did was initialisation: So far, we had always placed all objects into the world manually. Now we wanted to add some code so that the objects (Doctor, Daleks, Pellets, Tardis) get placed automatically. The game can then start up properly without any necessary further setup.<\/p>\n<p>We can easily do that by adding the code to construct and place the objects into the constructor of the world object. Since the world gets instantiated automatically, we then also get the initial actors automatically.<\/p>\n<p>Here is the new constructor of our &#8216;Mars&#8217; class:<\/p>\n<pre>    <span style=\"color: #82121c\">public<\/span> Mars()\r\n    {\r\n        <span style=\"color: #314977\">super<\/span>(750, 450, 1);\r\n\r\n        addObject( <span style=\"color: #82121c\">new<\/span> Tardis(), 700,100);\r\n        addObject( <span style=\"color: #82121c\">new<\/span> Doctor(), 200, 400);\r\n\r\n        addObject( <span style=\"color: #82121c\">new<\/span> Dalek(), 420, 320);\r\n        addObject( <span style=\"color: #82121c\">new<\/span> Dalek(), 100, 120);\r\n\r\n        addObject( <span style=\"color: #82121c\">new<\/span> EnergyPellet(), 150, 75);\r\n        addObject( <span style=\"color: #82121c\">new<\/span> EnergyPellet(), 610, 290);\r\n        addObject( <span style=\"color: #82121c\">new<\/span> EnergyPellet(), 25, 200);\r\n        addObject( <span style=\"color: #82121c\">new<\/span> EnergyPellet(), 310, 160);\r\n        addObject( <span style=\"color: #82121c\">new<\/span> EnergyPellet(), 520, 110);\r\n        addObject( <span style=\"color: #82121c\">new<\/span> EnergyPellet(), 670, 400);\r\n    }<\/pre>\n<p>That was a good chance to discuss coordinate systems again (something that Sophie had seen in school \u2013 it was quite nice to show her some interesting use of something she had encountered in her maths class). The fact that the origin of the coordinate system in maths is in the bottom left and on computer screens typically in the top left was an interesting little detail.<\/p>\n<p>With this in place, we were done. It&#8217;s not the most riveting game in the world \u2013 I doubt anyone will get into heart attack territory from excitement anytime soon \u2013 but it&#8217;s a nice enough start for our level 1.<\/p>\n<p>Let&#8217;s see what Sophie thinks up for level 2&#8230;<\/p>\n<p>Summary:<\/p>\n<p>Session #4<br \/>\nTime: approx 45 min<br \/>\nResult (live on the Greenfoot site): <a href=\"https:\/\/www.greenfoot.org\/scenarios\/25296\">DrWho4<\/a><br \/>\n(Source code can be downloaded from the link above.)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Welcome back, dear readers, to the fourth part of Sophie&#8217;s journey of writing a DrWho computer game with Greenfoot and Java. If you have read the previous parts, then thank you for sticking with us for so long! (If not, &hellip; <a href=\"https:\/\/blogs.kcl.ac.uk\/proged\/2008\/02\/08\/teaching-my-daughter-to-code-part-iv\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":179,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15,8,13],"tags":[34,44,53,107],"class_list":["post-43","post","type-post","status-publish","format-standard","hentry","category-greenfoot-software-tools","category-programming","category-teaching","tag-coding","tag-dr-who","tag-greenfoot-software-tools","tag-teaching"],"_links":{"self":[{"href":"https:\/\/blogs.kcl.ac.uk\/proged\/wp-json\/wp\/v2\/posts\/43","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.kcl.ac.uk\/proged\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.kcl.ac.uk\/proged\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.kcl.ac.uk\/proged\/wp-json\/wp\/v2\/users\/179"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.kcl.ac.uk\/proged\/wp-json\/wp\/v2\/comments?post=43"}],"version-history":[{"count":3,"href":"https:\/\/blogs.kcl.ac.uk\/proged\/wp-json\/wp\/v2\/posts\/43\/revisions"}],"predecessor-version":[{"id":980,"href":"https:\/\/blogs.kcl.ac.uk\/proged\/wp-json\/wp\/v2\/posts\/43\/revisions\/980"}],"wp:attachment":[{"href":"https:\/\/blogs.kcl.ac.uk\/proged\/wp-json\/wp\/v2\/media?parent=43"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.kcl.ac.uk\/proged\/wp-json\/wp\/v2\/categories?post=43"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.kcl.ac.uk\/proged\/wp-json\/wp\/v2\/tags?post=43"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}