<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Francisco Areas Guimaraes on Medium]]></title>
        <description><![CDATA[Stories by Francisco Areas Guimaraes on Medium]]></description>
        <link>https://medium.com/@franksands?source=rss-e430705ab73f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*U9SGoSP1bXs5-Cbq.jpg</url>
            <title>Stories by Francisco Areas Guimaraes on Medium</title>
            <link>https://medium.com/@franksands?source=rss-e430705ab73f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 10:39:43 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@franksands/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Usually it's just bad writing]]></title>
            <link>https://medium.com/@franksands/usually-its-just-bad-writing-8e0360c81d33?source=rss-e430705ab73f------2</link>
            <guid isPermaLink="false">https://medium.com/p/8e0360c81d33</guid>
            <category><![CDATA[videogames]]></category>
            <category><![CDATA[ludonarrative-dissonance]]></category>
            <category><![CDATA[writing]]></category>
            <category><![CDATA[conflict]]></category>
            <dc:creator><![CDATA[Francisco Areas Guimaraes]]></dc:creator>
            <pubDate>Tue, 31 Mar 2026 16:07:26 GMT</pubDate>
            <atom:updated>2026-03-31T16:07:26.087Z</atom:updated>
            <content:encoded><![CDATA[<p>So this text was a comment from <a href="https://www.youtube.com/watch?v=fY1FsMK_cos">this</a> video o YT</p><p>I also didn&#39;t think it would be the following post to exactly what I said in more detail in the last post, but it just shows how frequest I post here 😆</p><p>It’s an interesting concept, though I don’t agree with it. First of all, I never felt guilt for wanting to complete a game, because I want to see the whole story. It’s the same thing that saying that you are compelled to finish a book if you want to know the end of the story. The conflict you display I usually call “bad writing”. For example, in Prince of Persia 2008, after all the combat, fighting all the bosses and defeating the lord of darkness, the game will only progress if you release the lord of darkness to revive the princess. So either:</p><ol><li>The creators contradicted everything that was estabilished during the game which would be ludonarrative dissonance</li><li>They decided that they wanted to tell the story of an idiot that dooms the world to save his girlfriend, which I call bad writing.</li></ol><p>The Last of Us part 1 has the same 2 different possibilities:</p><ol><li>The creators wanted to make a sequel so they needed Joel and Ellie alive and because of that they created the ludonarrative dissonance of going against the whole game that estabilished that Ellie was the cure for the whole world</li><li>They wanted to tell the story that Joel doomed the world to save his adopted daughter, which just proves that the character learned nothing. There is no arc for Joel: the game starts with him losing his daughter in a situation that was out of his control. Because of this, he becomes jaded, secluded and does not trust on anyone. Then, he meets Ellie, which through the whole game tears down Joel’s barriers and he can start to see caring again for someone else. Untill here, we have a good character development, nothing revolutionary, but good. Then, at the end, the conclusion of the arc should be that Joel realizes he should care for other people and this time, he is in control, and able to save the whole world, so he should have let Ellie die to save the whole world. So again, bad writing</li></ol><p>We also maybe have a mix of 1. and 2. where the creators wanted to create a story about grief and the cost of losing your daughter and how that’s more important than saving the world, and this could a compelling story, but it would definitely not go well with the tone of the game they made, full of action, explosions and zombies. If this is what they wanted to do it would a mix of ludonarrative dissonance and bad writing.</p><p>I also disagree that this conflict only happens in videogames. Music has this, for example, when the lyrics are depressing but you have an upbeat rhythm, like Enjoy the Silence or Blue Monday. Movies also do this using a happy song to contrast a tragic scene, like in the 2001 animation of Metropolis.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8e0360c81d33" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Definitely NOT the last of this]]></title>
            <link>https://medium.com/@franksands/definitely-not-the-last-of-this-ca6fd8885c1f?source=rss-e430705ab73f------2</link>
            <guid isPermaLink="false">https://medium.com/p/ca6fd8885c1f</guid>
            <category><![CDATA[storytelling]]></category>
            <category><![CDATA[the-last-of-us]]></category>
            <category><![CDATA[essay]]></category>
            <category><![CDATA[videogames]]></category>
            <dc:creator><![CDATA[Francisco Areas Guimaraes]]></dc:creator>
            <pubDate>Sat, 19 Sep 2020 17:04:42 GMT</pubDate>
            <atom:updated>2020-09-19T17:04:42.156Z</atom:updated>
            <content:encoded><![CDATA[<p>This is only my second text on the subject of video games and I am already having a hard time coming up with puns for the title, so please bear with me (though there are no bears in the this game(there are some beautiful giraffes in the other hand(wow, I should stop this before we have an insurmountable amount of parenthesis)))</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/417/1*y5N2Ro6j_NVz5d01fgUkNA.png" /></figure><p>Anyway…The Last of Us from Naughty Dog is considered by many a &quot;masterpiece&quot; of storytelling and &quot;groundbreaking&quot; and a lot of other thing I would need to put in inverted commas, but let&#39;s just leave it at that. And I love that game: it&#39;s beautiful, the combat feels great, the stealth is even better, the performance of all actors is unbelievable, the world they created is rich and lived in and did I mention the scene with the giraffes? There is some brilliant work and effort put into this, but I never felt the story was one of the high points. I mean, it&#39;s wellmade, it does its job getting you from A to B, but it&#39;s one of the most beaten down, stereotypical &quot;jaded reluctant hero&quot; I&#39;ve ever seen. This might have been worsened by the fact that just as I was in the last portion of the game, I read <a href="https://twitter.com/FilmCritHULK">Film Critic Hulk</a>&#39;s <a href="https://web.archive.org/web/20200130182625/https://birthmoviesdeath.com/2013/07/03/film-crit-hulk-man-of-steel">essay</a> on the Man of Steel movie and I thought it was completely applicable to this game. And briefly his argument is that the movies fails to answer the BIG 7 questions of any story:</p><ul><li>What does this character want?</li><li>What does this character need?</li><li>How does these wants and needs conflict with each other within the character?</li><li>How do they conflict with the outside world?</li><li>How do they conflict with other characters?</li><li>How does the character change through these conflicts and how does the resolution affect them?</li><li>What impact does that change has on everyone else?</li></ul><p>So most good stories start with a conflict between what the main character wants (or thinks he wants) and what does he need. Man of Steel fails because although the character says he is conflicted and depressed and whatnot, we never see this enacted. He just punches harder until he gets what he wants. He starts the movie punching and he ends the movie punching and nothing gets resolved. There&#39;s no change, no character arc.</p><p>This is exactly what I felt with Joel from the The Last of Us: he starts as a grumpy jaded selfish person and he ends the game as a grumpy jaded selfish person. Not only that, but the whole world ends up far worse than it started, because his actions in the end stopped the chance of creating a cure for the infection in the game.</p><p>As an exercise let&#39;s answer those BIG 7 questions for The Last of Us:</p><ul><li>What does this character want?<br>Joel wants to be left alone and continue with his smuggles and petty crimes</li><li>What does this character need?<br>He needs to process the loss of his daughter and learn that the hardest part of this is learning to let go</li><li>How does these wants and needs conflict with each other within the character?<br>He is jaded, with good reason, and does not want to create attachments with anyone else exactly because he does not want to lose anyone else</li><li>How do they conflict with the outside world?<br>The world is in chaos and in desperate need of help, the opposite of Joel&#39;s behavior</li><li>How do they conflict with other characters?<br>He does not trust anyone and is belligerent to anyone that comes close</li></ul><p>So far so good, right? 5 out of 7 is not bad at all. I mean, this will not earn any Oscars or Nobels for being creative, but it looks like well structure drama: we have our characters defined, the conflict, what he needs to achieve. And then it just not goes anywhere. Sure, Joel becomes closer to Ellie along the story, and towards the end, they resemble the father/daughter relationship he had in the beginning of the game and that&#39;s the whole problem: he went just to where he was in the start. There is no change is his behavior, other than now he thinks Ellie is his daughter. So let&#39;s try to answer the last 2 questions:</p><ul><li>How does the character change through these conflicts and how does the resolution affect them?<br>He doesn&#39;t change. The resolution is that he lies to Ellie saying that there are a lot of other people with immunity, so that he can still pretend to have a daughter</li><li>What impact does that change has on everyone else?<br>The world is a worse place to live because the only chance for a cure was rejected</li></ul><p>Yeah…great way to end your epic story and showing that as a player your last 20ish hours of effort, work and killing all those clickers serves nothing, because in the end Joel just didn&#39;t felt like it.</p><p>Don&#39;t get me wrong, I am not saying all stories must have a happy ending and the hero gets the girl (or boy) and they ride towards the sunset. Some of the best scifi and horror stories have a dour end where the characters are powerless in face of certain doom. But this has to be built. You cannot create this epic story of one man against an army of zombies and only at the very end, you decide to flip a switch and turn it into a tragedy , or worse not even care about it. I mean, you can, but that&#39;s what you get as a result. This is worse in a video game, because you&#39;re not just watching somebody else making horrible decisions, you are controlling that character, so when he does something completely out of your reach, it is saying that all that time you invested in this character served for nothing.</p><p>At least, we will always have that scene with the giraffes</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/728/1*VEw-S-1LNqpCee-7PZhPVw.jpeg" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ca6fd8885c1f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Massive Effect]]></title>
            <link>https://medium.com/@franksands/massive-effect-1967bd6ef371?source=rss-e430705ab73f------2</link>
            <guid isPermaLink="false">https://medium.com/p/1967bd6ef371</guid>
            <category><![CDATA[essay]]></category>
            <category><![CDATA[games]]></category>
            <category><![CDATA[mass-effect]]></category>
            <category><![CDATA[bioware]]></category>
            <category><![CDATA[rpg]]></category>
            <dc:creator><![CDATA[Francisco Areas Guimaraes]]></dc:creator>
            <pubDate>Sun, 13 Sep 2020 15:51:48 GMT</pubDate>
            <atom:updated>2020-09-13T15:51:48.271Z</atom:updated>
            <content:encoded><![CDATA[<p>A lot of people have one game that really impacted them and they keep going back to it, be it for the story, the gameplay or because it has that character they always liked. For me, this game was the first Mass Effect and I will try to show why it’s brilliant and how I got to this conclusion.</p><p>I guess I always liked space games more than any other genre. In school, I would draw all sorts of controls on my desk and imagine I was controlling a spaceship. On my first console, I loved how you could move the ship in Asteroids and the precision required to touch the ground in Lunar Lander. I completely love the intro of Space Manbow in my MSX2, the way the pilot starts the ship with the press of a lever.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*75_nUlmigVn79X7WiAT0Ew.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6IlVMMekXe0rb2zjGlXGJg.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bzXQ0pysv8oPRk3Z1I6-GQ.png" /></figure><p>And so the games were progressing and now you could have longer cut scenes or more detailed art and space had frequently a special attention in my games.Around 1994 I started playing tabletop rpgs and it was a whole new world of creativity and exploration, you could do basically what you wanted, but my group didn’t have any sci fi settings, it was only elves and dwarfs, warriors and wizards. Computer rpgs were trying to catch on with the Baldur’s Gate series, but it felt to me like they were focusing on the wrong parts of what made a good rpg: they had all the rules and character sheets down, but there was something missing and frankly I was not really enticed by the fantasy setting.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/637/1*0vpXA_pg4b3umGgmI0zuDA.jpeg" /><figcaption>all these stars at your fingertips</figcaption></figure><p>In 2007, a great year for sci-fi games, with titles like Bioshock and Portal, I came across what I didn’t know I was looking for: the first Mass Effect. It had everything I wanted since I was a kid, you had your own ship, the only one with incredible new technologies and you did not had to follow or obey anyone, you could go and explore the whole milky way on your own leisure.</p><p>Of course, you couldn’t actually explore the whole galaxy, games don’t have the resources to do this today, even more 13 years ago. So, there were dozens of systems and several planets in each system, and you could visit all of them, but any planet that was not a plot point, had only generic looking plains and mountains and you could fight monsters and gather minerals. And it doesn’t sound a bit exciting describing it like this, but just the ability that I could look at this huge galaxy, pick and choose any planet and just go there, was completely incredible for me. I loved the idea that I was really exploring the Milky Way.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/273/1*pR6BCsG898QC-FulZA1-jQ.jpeg" /><figcaption>image by <a href="https://www.omocat-blog.com/post/18777749016/crocodile-tears">omocat</a></figcaption></figure><p>One thing that was always a bit of a joke among computer rpgs is that there almost never had a reason for your character to be able to just enter anyone’s houses and just break all of their vases and steal everything that wasn’t nailed down and talk to people to your exhaustion. Mass Effect had two brilliant solutions to this problem.</p><p>The first is to create one of the best excuses to be able to journey to any place you want, ask anyone all the questions and they have to answer you: You work for their version of Interpol. In the game there are these space secret service agents that in theory serve to uphold the peace, but in practice, they can do anything they want, so, of course their best agent turns evil and your character is sent to go after him. This setup gives you authorization to go anywhere you want, the whole galaxy is your jurisdiction, and since you’re like a federal agent, people have a reason to answer your questions and let you in their places. And although the bad guy is out there, there is not a ticking clock to go after him, he’s smart and does not leave a trail, so you might as well search in any way you think is best.</p><p>The second is that since your character is one of these secret agents, no one can’t really give you orders, they just give the scant information they collected, which are a few different planets that may have information about the villain’s past whereabouts or allies. And with this, they leave you to explore the stars. Of course, one could argue that this is basically the same plot of any RPG: you are the chosen one to search the kingdom and kill the bad guy and maybe there is a princess involved and honor, glory, etc. And this person would be 100% correct, but also, missing the point entirely. The plot is simple so you don’t have to worry about it and you can focus on meeting all the different and interesting aliens, learning the whole story, your own ship is full of people that you can talk to and see various details of how it works. You can go to the Citadel, the center of the Systems Alliance, which is a city sized space station, where you can go visit embassies and everywhere you go, the place seems lived and with people coming and going and discussing their lives. I think you can compare this with most MCU movies, where we all know they maybe have 3rd act problems, and Deus Ex Machina, and forgettable villains, but that is not the point: the point is those little dialogs in the Avengers Tower where each one tries to lift Thor’s hammer and they look like real people, like people you know, that just so happens to turn green or have a very expensive suit of armor.</p><p>So, after all is said and done, much more than the main story, saving the world and getting the girl (or boy) in the end, there is this whole universe that you can visit and experience and explore, and everytime you visit, there’s a different detail there.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1967bd6ef371" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Searching an xml in Python]]></title>
            <link>https://medium.com/@franksands/searching-an-xml-in-python-77d8028dea42?source=rss-e430705ab73f------2</link>
            <guid isPermaLink="false">https://medium.com/p/77d8028dea42</guid>
            <category><![CDATA[xml]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Francisco Areas Guimaraes]]></dc:creator>
            <pubDate>Fri, 01 Sep 2017 19:23:57 GMT</pubDate>
            <atom:updated>2017-09-01T19:23:57.111Z</atom:updated>
            <content:encoded><![CDATA[<p>Starting with version 2.7 ElementTree has a better support for XPath queries. XPath is a syntax to enable you to navigate through an xml like SQL is used to search through a database. Both find and findallfunctions support XPath. The xml below will be used for this example</p><pre>&lt;Catalog&gt;<br>    &lt;Books&gt;<br>        &lt;Book id=&quot;1&quot; price=&quot;7.95&quot;&gt;<br>            &lt;Title&gt;Do Androids Dream of Electric Sheep?&lt;/Title&gt;<br>            &lt;Author&gt;Philip K. Dick&lt;/Author&gt;<br>        &lt;/Book&gt;<br>        &lt;Book id=&quot;5&quot; price=&quot;5.95&quot;&gt;<br>            &lt;Title&gt;The Colour of Magic&lt;/Title&gt;<br>            &lt;Author&gt;Terry Pratchett&lt;/Author&gt;<br>        &lt;/Book&gt;<br>        &lt;Book id=&quot;7&quot; price=&quot;6.95&quot;&gt;<br>            &lt;Title&gt;The Eye of The World&lt;/Title&gt;<br>            &lt;Author&gt;Robert Jordan&lt;/Author&gt;<br>        &lt;/Book&gt;<br>    &lt;/Books&gt;<br>&lt;/Catalog&gt;</pre><p>Searching for all books:</p><pre>import xml.etree.cElementTree as ET<br>tree = ET.parse(&#39;sample.xml&#39;)<br>tree.findall(&#39;Books/Book&#39;)</pre><p>Searching for the book with title = ‘The Colour of Magic’:</p><pre>tree.find(&quot;Books/Book[Title=&#39;The Colour of Magic&#39;]&quot;) <br># always use &#39;&#39; in the right side of the comparison</pre><p>Searching for the book with id = 5:</p><pre>tree.find(&quot;Books/Book[@id=&#39;5&#39;]&quot;)<br># searches with xml attributes must have &#39;@&#39; before the name</pre><p>Search for the second book:</p><pre>tree.find(&quot;Books/Book[2]&quot;)<br># indexes starts at 1, not 0</pre><p>Search for the last book:</p><pre>tree.find(&quot;Books/Book[last()]&quot;)<br># &#39;last&#39; is the only xpath function allowed in ElementTree</pre><p>Search for all authors:</p><pre>tree.findall(&quot;.//Author&quot;)<br>#searches with // must use a relative path</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=77d8028dea42" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Why is Object Oriented Programming Useful? (With an RPG example, Javascript and Java)]]></title>
            <link>https://medium.com/@franksands/why-is-object-oriented-programming-useful-with-an-rpg-example-javascript-and-java-670e2d1c5505?source=rss-e430705ab73f------2</link>
            <guid isPermaLink="false">https://medium.com/p/670e2d1c5505</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Francisco Areas Guimaraes]]></dc:creator>
            <pubDate>Thu, 03 Dec 2015 14:43:33 GMT</pubDate>
            <atom:updated>2015-12-11T01:35:14.494Z</atom:updated>
            <content:encoded><![CDATA[<p>This article is an adaptation of the article written by Al Sweigart and posted at the <a href="http://inventwithpython.com/blog/2014/12/02/why-is-object-oriented-programming-useful-with-an-role-playing-game-example/">Invent with Python blog</a>. I found it one of the best ways to display the advantages of OOP but thought Python was not the best option for examples that clearly displayed each concept. Because of this, I adapted his article using Javascript for the initial examples and Java for the Object Oriented topics. I also changed some of the examples for to suit how I wanted to explain a scenario. This article assumes you have fundamental programming knowledge, like what are functions and variables, but mostly only that. Good reading.</p><p>This blog post is for those people still new to programming and have probably heard about “object-oriented programming”, “OOP”, “classes”, “inheritance/encapsulation/polymorphism”, and other computer science terms but still don’t get what exactly OOP is used for. In this post I’ll explain <em>why</em> OOP is used and how it makes coding easier. This post uses Javascript and java code, but the concepts apply to any programming language.</p><p>There are two key non-OOP concepts to understand right off the bat:</p><ol><li><strong>Duplicate code is a Bad Thing.</strong></li><li><strong>Code will always be changed.</strong></li></ol><p>Aside from small “throw-away” programs that are written for some single task and only ran once, you’ll almost always need to update your code to either <strong>fix bugs</strong> or <strong>add new features</strong>. A large part of writing good software is writing software that is readable and easy to change.</p><p>If you have copy/pasted code in your program, changing it requires making the same change to multiple places in your program. This is problematic. If you miss applying the change to some places, you’ll fail to fix the bug everywhere or implement the new feature inconsistently. <em>Duplicate code is a Bad Thing.</em> Having duplicate code in your program is setting yourself up for bugs and headache.</p><p>Functions let you get rid of duplicate code. You write the code in a function once, and just call the function everywhere your program needs that code run. Updating the function’s code updates it everywhere the function is called automatically. Just as functions makes updating code easier, using object-oriented programming techniques also organizes your code to make changes easier. <em>And code will always be changed.</em></p><p><strong>The Role Playing Game Example</strong></p><p>Most OOP tutorials stink. They have “Car” classes with “Honk” methods and other examples that are unrelatable to the actual programs that new coders write or have used before. So this blog will use an RPG-style video game (think World of Warcraft, Pokemon, or Dungeons &amp; Dragons). We’re used to thinking of things in these games as a collection of integers and strings. Check out all the numbers on this Diablo status screen or D &amp; D character sheet:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/888/1*zigVhhvuKY4Bm553IrQ9fA.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sKD19IRu0bNpjsH7Tq0C_A.jpeg" /></figure><p>Taking out the graphics from these RPG video games, the characters, armor, and other objects are just a bunch of integer or string values in variables. Without using object-oriented concepts, you could implement these things in Javascript code like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/800d6969a171f6ae9fa7fed7c947ee1d/href">https://medium.com/media/800d6969a171f6ae9fa7fed7c947ee1d/href</a></iframe><p>The above variable names are pretty generic. To add monsters in this program, you’ll need to rename the player character’s variables and make new ones for the monsters:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a25ccadb0081b4a308c5a24f918a1407/href">https://medium.com/media/a25ccadb0081b4a308c5a24f918a1407/href</a></iframe><p>But of course you’ll want to have more than one monster, but then you’ll have variables like <em>monster1Name</em>, <em>monster2Name</em>, etc. That’s a poor way to code, so maybe you’ll make the monster variables into arrays:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d79117b6f7a0a9351befd62a9b178716/href">https://medium.com/media/d79117b6f7a0a9351befd62a9b178716/href</a></iframe><p>Then you could just have the goblin’s stats be the values at index <em>0</em> in the lists, the dragon’s stats are at index <em>1</em>, and the orc’s stats are at index <em>2</em>. This way you could have multiple monsters contained in these variables.</p><p>But this kind of code easily leads to bugs. If your lists get out of sync, the program will no longer work correctly. Say the player vanquishes the goblin at index <em>0</em> and the program calls a <em>vanquishMonster</em> function. We created a <em>remove</em> function for removing items from an array, but the <em>vanquishMonster</em><strong> </strong>function has a bug and deletes the values from all the lists except (accidentally) <em>monsterAttack</em>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fa2e5c927487e1bc0a740878f878a55e/href">https://medium.com/media/fa2e5c927487e1bc0a740878f878a55e/href</a></iframe><p>The monster lists end up looking like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c69baa0fbe01bff30dce2886fe53e6a1/href">https://medium.com/media/c69baa0fbe01bff30dce2886fe53e6a1/href</a></iframe><p>Now it looks like the dragon’s attack is the goblin’s attack. And the Orc now has the attack the dragon had, imagine a dragon fumbling with a dagger and an orc throwing fire balls. <strong>This is quickly getting out of hand</strong><em>.</em> The problem is that you have <strong>one</strong> monster’s data spread out over multiple variables. You could fix this by putting a single monster’s data into a dictionary, and then have a list of dictionaries:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/993431e327f37eed8b661b3d75e56da0/href">https://medium.com/media/993431e327f37eed8b661b3d75e56da0/href</a></iframe><p>Gah. This code is getting complex. For example, if we add an inventory for each monster, it would be a dictionary-in-a-dictionary-in-a-list. What if things like spells or inventory items also have their own attributes and need to be dictionaries? What if an inventory item could be a sack, which itself contains other inventory items? This monsters dictionary would get intense.</p><p>This is something that object-oriented programming can solve by creating a new data type.</p><p><strong>Creating Classes</strong></p><p><strong>Classes are the blueprints for a new data type in your program.</strong> Object-oriented programming provides a new approach for modeling the armor, monsters, etc. much better than a hodge-podge of lists and dictionaries, even though OOP concepts it take some getting used to. Here we can see how to do this moving from Javascript to Java.</p><p>In fact, since the hero will have all the same features of monsters (health, inventory, etc.) we can just have a generic <em>LivingThing</em> class that the hero and monsters share. Your code can be changed to look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/88f4ca532379e372595275e69c3722e4/href">https://medium.com/media/88f4ca532379e372595275e69c3722e4/href</a></iframe><p>Hey look, using classes has already allowed you to use the same code for both player characters and monsters.</p><p>In the above code you are defining a new data type/class (pedantics aside, but the two terms are basically the same. See also: <a href="https://stackoverflow.com/questions/468145/what-is-the-difference-between-type-and-class">Stack Overflow — What’s the difference between a type and a class?</a>) called <em>LivingThing</em>. You can have <em>LivingThing</em> values/objects (again, two terms for basically the same thing) just like you can have integer values, string values, or boolean values.</p><p>Some Java-specific details about the above code:</p><pre>public class LivingThing {</pre><p>The above is a class statement that defines a new class, just like function statements define a new function in Javascript. The class’s name is LivingThing.</p><pre>public String name;<br>public int health;<br>public int magicPoints;<br>public String attack;</pre><p>These are the properties, or member variables, of the LivingThing class. The idea is the same as local variables in Javascript, but they’re attached to the class, just like the members of an array, or the items in a dictionary.</p><pre>public LivingThing(String name, int health, int magicPoints, String attack) {</pre><p>This is a function, or method, definition. The method is a function that is attached to a class, in the same way the properties belong to the class.<br>When a method has the same name as the class, it is called a constructor, because it’s the method use to build new objects for this class.</p><pre>this.name = name;<br>this.health = health;<br>this.magicPoints = magicPoints;<br>this.attack = attack;</pre><p>This is the constructor’s body. There are two important things to notice here:</p><ol><li>the <em>.</em> operator, used to access an object’s properties or methods</li><li>the <em>this</em> keyword, used to access the current object, the one object that was created with the constructor. <em>this.name = name</em> means “fill the object property ‘name’ with the value of the argument ‘name’ ”. It’s also useful to avoid name collision and having to give different names to the constructor arguments</li></ol><p>Calling the constructor</p><pre>LivingThing hero = new LivingThing(&quot;Elsa&quot;, 50, 80, &quot;Axe&quot;);<br>LivingThing[] monsters = new LivingThing[3];<br>monsters[0] = new LivingThing(&quot;Goblin&quot;, 20, 0, &quot;Dagger&quot;);<br>monsters[1] = new LivingThing(&quot;Dragon&quot;, 300, 200, &quot;Fireball&quot;);<br>monsters[2] = new LivingThing(&quot;Orc&quot;, 20, 0, &quot;Sword&quot;);</pre><p>When creating a new object in Java, we always need to use the <em>new</em> keyword, and then use the constructor we defined. The above <em>new LivingThing(“Elsa”, 50, 80, “Axe”)</em> call creates a LivingThing object and it’s stored in the <em>hero</em> variable. The above code also creates <em>LivingThing</em> objects for the three monsters and stores them in the <em>monsters</em> array.</p><p>And here’s where we begin to see the benefits of object-oriented programming. If another programmer is reading your code, when they see the <em>LivingThing()</em> call they know they can just look for “<em>class LivingThing</em>” and find out the details of everything they need to know about what a <em>LivingThing</em> is.</p><p>But an even bigger benefit comes when you try to update your <em>LivingThing</em> class.</p><p><strong>Updating Classes</strong></p><p>Say you wanted to add “hunger” levels to your RPG. If a hero or monster has a hunger level of 0, they are not at all hungry. But if their hunger level is above 100, then they take damage and their health value decreases each day. You could change your class to this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d64b7043d7520b58225e8fc5f6dbacae/href">https://medium.com/media/d64b7043d7520b58225e8fc5f6dbacae/href</a></iframe><p>Without changing <em>any</em> other line of code, <em>every</em> <em>LivingThing</em> object in your game now has a <em>hunger</em> level! You don’t have to worry about some <em>LivingThing</em> objects having the <em>hunger</em> property and some not: the very definition of what a <em>LivingThing</em> is has been updated.</p><p>You also don’t have to change any of the constructor calls since you didn’t add a new hunger level parameter to the constructor method. That’s because 0 is a good common-sense default value for a new <em>LivingThing</em> object’s hunger level. If you do add a new parameter to constructor for the hunger levels, you’ll have to update all the code that calls the constructor. But this is true for any function anyway.</p><p>If your RPG has a lot of default values for things, you can avoid a lot of “boilerplate” code by using classes with a constructor that sets the default for you.</p><p><strong>Methods</strong></p><p>Methods are useful for running code that affect the object itself. For example, you could just have code that changes the health of a LivingThing object directly:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/314728f9dcb2657163b212b039619a2b/href">https://medium.com/media/314728f9dcb2657163b212b039619a2b/href</a></iframe><p>But this isn’t a very robust way to handle taking damage. Lots of other game logic needs to be checked whenever something takes damage. For example, say you want to check if a character dies each time they take damage. You would need code like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/658c4241c385342174bb74274939cba2/href">https://medium.com/media/658c4241c385342174bb74274939cba2/href</a></iframe><p>The problem with the above approach is that you would need that check everywhere your code decreases the health of a LivingThing object. But duplicating code is a Bad Thing. The non-OOP way to prevent duplicate code would be to put it in a function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/37f5c81e8fdf53e145d120e5b09d5097/href">https://medium.com/media/37f5c81e8fdf53e145d120e5b09d5097/href</a></iframe><p>This is a better solution because any updates to <em>takeDamage()</em> (such as factoring in armor, protective spells, bonuses, etc.) just have to be added to the <em>takeDamage</em> function.</p><p>However, the downside is that when your program grows in size, it’s easy for <em>takeDamage()</em> to get lost in among them. It isn’t so clear that <em>takeDamage() </em>is related to the <em>LivingThing</em> class. If you have hundreds of functions in your program, it will be hard to figure out which ones are related to the <em>LivingThing</em> class.</p><p>The solution is to turn this function into a <em>LivingThing</em> method:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0e8ed01a54033678cb42dd7c0307a418/href">https://medium.com/media/0e8ed01a54033678cb42dd7c0307a418/href</a></iframe><p>Once your program has many classes, each with many methods and member variables, you will begin to see how OOP can help organize your programs to be more manageable.</p><p><strong>Public and Private Methods</strong></p><p>Methods and member variables can be marked as public or private. Public methods can be called and public member variables can be set by any code, inside or outside of the class. Private methods can be called and private member variables can be set only by code inside the object’s own class.</p><p>In Java, this “can be called/set” is strictly enforced by the compiler. The compiler will tell you if you’re trying to acess a method or property that you don’t have permission to use.</p><p>The reason for having this public/private distinction is to set expectations for how the class interacts with outside code. (See also: <a href="https://stackoverflow.com/questions/2620699/why-private-methods-in-the-object-oriented">Stack Overflow — Why “private” methods in the object oriented?</a>) The programmer of the class expects that other people won’t write code that calls the private methods or sets the private member variables.</p><p>For example, the <em>takeDamage</em> method factors in checking for death if health falls below 0. You may want to add all sorts of other checks in the code. Things like armor, agility, and protective spells could factor in to reduce the damage taken. The <em>LivingThing</em> object might be wearing an enchanted cape that <strong>heals</strong> them by adding the damage amount to their health instead of subtracting. All of this game logic can go in the <em>takeDamage</em> method.</p><p>But all of OOP organizing is for nothing if you accidentally put this code in there:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/829170a48c2232fcfadb68b212af2e8f/href">https://medium.com/media/829170a48c2232fcfadb68b212af2e8f/href</a></iframe><p>That <em>hero.health -= 50</em> will subtract 50 points of health, without taking into any consideration what armor Elsa is wearing, if she has protective spells, or is wearing that enchanted healing cape. This code bluntly decrements <em>health</em> by 50.</p><p>It’s easy to forget about the <em>takeDamage</em> method and accidentally write code like this. This doesn’t check if the hero object’s health member variable has dropped below 0. The program continues as though Elsa is alive even if she has negative health! This is a bug we can avoid with public/private members and methods.</p><p>If you change the <em>health</em> member variable to <em>private</em>, then the compiler will not let you make that mistake:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5a5e43c7e9b221e603e751d796574890/href">https://medium.com/media/5a5e43c7e9b221e603e751d796574890/href</a></iframe><p>It is impossible to write a program that illegally accesses a private member or method. Object-oriented programming helps prevent these kinds of bugs. Even with techniques like Java’s Reflection (<a href="http://stackoverflow.com/questions/37628/what-is-reflection-and-why-is-it-useful">Stack Overflow: What is reflection</a>), you won’t be able to access a private property or method outside the class.</p><p><strong>Inheritance</strong></p><p>Using the <em>LivingThing</em> class for dragons is nice, but dragons have a lot of other qualities in addition to the ones provided by <em>LivingThing</em>. So you want to create a new Dragon class that will have member variables like <em>airSpeed</em> and <em>breathType</em> (which can be string such as ‘fire’, ‘blizzard’, ‘lightning’, ‘poison gas’, etc).</p><p>Since Dragon objects will also have <em>health</em>, <em>magicPoints</em>, <em>attack</em>, and all the other things that <em>LivingThing</em> objects have, you <strong>could</strong> just create a new Dragon class and copy/paste all the code from your <em>LivingThing</em> class. But this would result in duplicate code, which is a <strong>Bad Thing</strong>.</p><p>Instead, make a Dragon class that is a <strong>subclass</strong> of the <em>LivingThing</em> class:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b6f32e942c4d939b6eeb36f7a8214643/href">https://medium.com/media/b6f32e942c4d939b6eeb36f7a8214643/href</a></iframe><p>This is effectively saying, “A <em>Dragon</em> is the same as a <em>LivingThing</em>, with some additional methods and member variables”. When you create a Dragon object, it will automatically have all the same methods and member variables as <em>LivingThing</em> (saving us from duplicating the code). But it could also have dragon-specific methods and member variables to it. Further, any code that deals with a <em>LivingThing</em> object can automatically handle a Dragon object because Dragon objects <strong>are</strong> LivingThing objects. This principle is called <strong>subtype polymorphism</strong>.</p><p>In practice, inheritance is easy to abuse though. You must be certain that any conceivable change or update you make to the <em>LivingThing</em> class would also be something you would want the <em>Dragon</em> class and <strong>every other</strong> subclass of <em>LivingThing</em> to also have. This might not always be so straightforward.</p><p>For example, what if you created <em>Monster</em> and <em>Hero</em> subclasses of the <em>LivingThing</em> class, and then created <em>FlyingMonster</em> and <em>MagicalMonster</em> subclasses from <em>Monster</em>. Would the new Dragon class be a subclass of <em>FlyingMonster</em> or <em>MagicalMonster</em>? Or maybe just its own subclass of <em>Monster</em>? One solution for this problem is <strong>interfaces</strong>. An interface is like a class, but without any implementation, it’s like a contract: any class that uses this interface is bound to have this methods and properties. Another difference is that while a class can only extend <strong>one</strong> other class, it can extend several interfaces, because since interfaces don’t have implementation, the class is only saying it will have to implement the methods described in those interfaces. To further differentiate class and interfaces, we use the interface keyword when defining interfaces and we use the <em>implements </em>keyword with when a class uses that interface:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4297726ceb6914980423d095c93a9200/href">https://medium.com/media/4297726ceb6914980423d095c93a9200/href</a></iframe><p>Even so, this is where inheritance and OOP start to get tricky and religious arguments over the “correct” way to design classes come about. I want to keep this blog post short and simple, so I’ll leave these topics as exercises for the reader to look into. (See also <a href="https://stackoverflow.com/questions/49002/prefer-composition-over-inheritance">Stack Overflow — Prefer composition over inheritance?</a> and <a href="https://en.wikipedia.org/wiki/Composition_over_inheritance">Wikipedia — Composition over inheritance</a>)</p><p><strong>Summary</strong></p><p>I hate programming tutorials for beginners that start with object-oriented programming. OOP is a very abstract concept. Until have some experience and are writing large programs, you won’t understand why using classes and objects makes programming easier. Instead, the neophyte is left with a steep learning curve to climb and no idea why they’re climbing it. I hope the RPG example has at least given you a glimpse as to why OOP can be helpful. There is MUCH more to OOP. If you want to learn more, try googling for “object oriented best practices” and “object oriented design patterns”.</p><p>If you are still confused by OOP concepts, feel free to write programs without classes. You don’t <strong>need</strong> them and they can result in over-engineered code. But once you have some coding experience under your belt, the benefits of object-oriented programming will become apparent. Good luck!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=670e2d1c5505" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>