Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision |
robots-4 [2023/10/06 02:22] – created appledog | robots-4 [2023/10/06 02:33] (current) – appledog |
---|
| |
The solution to this is to create a 'tile' class which holds everying in that tile. Thus, gameMap becomes a 2d array (list!!) of tiles, and there is no wasted space here. | The solution to this is to create a 'tile' class which holds everying in that tile. Thus, gameMap becomes a 2d array (list!!) of tiles, and there is no wasted space here. |
| |
| == Imperative versus OOP Programming |
| What we are really doing here, on another level, is to refactor the code in an 'object-oriented way'. It would certainly be possible to add a sprite sheet using imperative programming, but the complexity of the code will keep rising and rising. The real trick, thus, is to understand why we are using classes. |
| |
| It's to collect relevant information. Not to provide methods. |
| |
| So for example if we want to add robot hit points, in the old gameMap 'r'/'R' based model we need a separate 2d array to hold monster hit points, or we need a way to attach it to the gameMap -- such as 'r7' for a robot with 7 hit points -- but then only print the 'r'. This kind of programming is known as a 'kludge'. Eventually either codependent arrays will be used, or something akin to 'struct'. Therefore, the easiest way to start using classes is to treat them as structs and try to collect information only. This allows us to use one array and not codependent arrays and the code will feel very 'comfortable'. It is likely never necessary to move past this point in optimization unless there is a serious problem with the code or th logic of the code. |
| |
| == How to start |
| First write the SpriteSheet and Sprite classes and have them draw the primary image in drawGame(). |
| |
| Next add thing classes, which contain a character -- and flags for 'seen' etc. so we don't need to do r/R (or just use r/R). Then start by adding a class to gameMap and not a character. Don't worry about a 'tile' class for now, but you could do that now, if required. Just add tiles to the map in setup and then add things to the tile's inventory. This also eases collision detection because we can first check if 'anything' is there vs. checking many things individually, or check only places which contain more than one object (instead of every space). |
| |
| This, if done correctly, should only take one class (1.5h) with Roger. |