I do alot more game programming, so I'll try to make my tips more universal (but I'll try to give examples regardless). I also realize this post is knee deep in the TL:DR territory, so I tried to break things up and most of the length of this post is from the examples, so the first paragraph with the bold sentence is in a nutshell what I'm trying to say. So if that makes sense, feel free to move on and not look at the example I gave.
------------------------------------------------------------------------
One thing I found is to
code incrementally-- let's say you want to make a menu with buttons and such, I'd first make sure the program is out putting my graphics, then I'd work on each button at a time and test them frequently to make sure they do what you want.
This is an alternative to just coding everything at once and hoping it will work perfectly. This will actually take more time, because if something doesn't work, you'll have to go through each line of code and isolate things anyways to diagnose your problem.
------------------------------------------------------------------------
Another tip, is say you've declared a variable or function, rather just assuming it will do what you want, use some kind of
text output to see what that variable or function is doing.
Take for example, I was working on a game that used an inventory and I made a function that allowed me to search for a particular item in the inventory. Rather just assume the function was going to do what I wanted, I tested a few objects first and had the game tell me if it could find that item in the inventory.
This is really helpful because say you want to make another function that depends on that search function (say a function that only allows you to progress in the game if you have a certain item), the new progress function also won't work if the search function won't either.
------------------------------------------------------------------------
Lastly, use
lots of functions, I'm not sure how much this applies in web coding, but the point when my code improved so much more was when I started using functions for practically everything. It will make your code easier to manage and edit-- because you can just edit one function as opposed to hunting downing maybe 50 lines of code in a spaghetti string of programming. Also, logically, you can make your code more fluid.
For instance, say you're working on a game does doing certain things if you're near an object in a game. Say, you can only pull a lever if you're next to it. Now on paper, that might sound easy to find, but there's actually alot of math going into it-- like using the distance formula between the player and lever.
Now if you don't use a function for this, and say you have multiple levers, you'll have to repeat alot of code! It might look something like this:
if square_root( (player_x# - lever1_x# )^2 + (player_y# - lever1_y# )^2 ) < 50 and that's just for one lever! Also, notice that I'm using a square root function to even do this. Then for lever2 you'll have to do this:
square_root( (player_x# - lever2_x# )^2 + (player_y# - lever2_y# )^2 ....
square_root( (player_x# - lever3_x# )^2 + (player_y# - lever3_y# )^2 ...etc
Then you still gotta use that for if-then conditions and loops! Or I just write a function, call it near: so it could look like this:
Code: Select all
function near( player, object, threshold)
dist# = square_root( (player_x# - object_x# )^2 + (player_y# - object_y# )^2 )
return dist# < threshold
end function
This is saying, given a general object, return whether the player is a certain distance away from it (aka the threshold). So making a more general formula in my function allows me to take those lines of code and just write something like:
near(player, lever1, 50), near(player, lever2, 50), etc. I can also use this function for grabbing items as well, because I made it general enough. So I could write:
IF near(Keen, lollipop, 10) THEN score = score + 100
This is much more fluid than writing out the distance formula, and if something goes wrong, I just have to pop open the lid of the function and see what went wrong or what I need to adjust. Also, even if you coded that function horribly wrong, to correct it, you're just changing say 10 lines of code as opposed to changing 10 lines of code times the number of objects you might use that with. Much more manageable with functions!
Hope this helps! Sorry it took so long for me to say it! haha