There is no code to copy in this exercise, instead you make a game like in the last exercise and the author asks you to make it as big and fancy as you can including things covered so far like arrays, modules and methods.
I decided to go for an old style dungeon crawler based on an idiotic click and point adventure game me and my mate Jono envisioned making a few years ago:
You play the above rat-man thing, armed with your trusty ‘rat slayer’ sword on a dangerous quest to reclaim your underground dungeon kingdom inhabited by intelligent bipedal rat people from the usurper who dethroned you. Yes it is that dumb.
The game working game and code can be found here. I’m not going to go through and write a comment about every part of the game or draw a map however to summarise:
- The game uses methods to call different rooms and navigate in the same way as in the last exercise, if-else statements and while loops with incrementors are used to give more options so you can navigate to a degree.
- To create a hit point system to simulate fighting, instance variables were made with integer values of 100 assigned to each. The part of the game where you enter combat starts a loop that calls the various combat methods stored in the combat.rb file using the module. These update the values of the instance variables assigned to ‘@player’ and ‘@enemy’, the ‘dead’ method is called if you fall below 0.
- In the part of the game where you choose a weapon I used a hash to store the weapon name and assign a value that the the combat ‘attack’ method takes as a parameter. I wanted to have the weapon you choose have a name, e.g. crossbow that I could reference when in combat but I couldn’t access the key from inside the attack method, Ruby stated and had to use a new method .keys which returns the keys from a hash as an element in an array.
This exercise was about practicing everything covered so far in the book and it was good to combine it all into one single project for the purpose of revision. Rather than making a longer game I wanted to make a short one with only a few rooms but featuring more things you can do, for instance there is a room you have to unlock, an inventory system to aquire a weapon (using hash key-value pairs) and a basic combat system.
I didn’t know how to do any of that at the start so instead of writing a map I fumbled on and experimented as I went. As such the code itself isn’t great; the ways of carrying out tasks varied a lot as I tried different ways to do things and this make’s it difficult to read and the importance of seperation of concerns became apparent the more code I wrote. If I were to write it agian from the ground up I’d look to write the code in a more succint and flexible way – possibly creating seperate character and enemy objects with in built methods to manage combat, inventories, health and so on. It was a fun project to work on but I’m eager to move on.