The other answers seem to miss the fact that you are in canada. Section 29.21 of Canada's Copyright Modernization Act provides an exception in that may apply to your case:
(1) It is not an infringement of copyright for an individual to use an existing work or other subject-matter or copy of one, which has been published or otherwise made available to the public, in the creation of a new work or other subject-matter in which copyright subsists and for the individual — or, with the individual’s authorization, a member of their household — to use the new work or other subject-matter or to authorize an intermediary to disseminate it, if
(a) the use of, or the authorization to disseminate, the new work or other subject-matter is done solely for non-commercial purposes;
(b) the source — and, if given in the source, the name of the author, performer, maker or broadcaster — of the existing work or other subject-matter or copy of it are mentioned, if it is reasonable in the circumstances to do so;
(c) the individual had reasonable grounds to believe that the existing work or other subject-matter or copy of it, as the case may be, was not infringing copyright; and
(d) the use of, or the authorization to disseminate, the new work or other subject-matter does not have a substantial adverse effect, financial or otherwise, on the exploitation or potential exploitation of the existing work or other subject-matter — or copy of it — or on an existing or potential market for it, including that the new work or other subject-matter is not a substitute for the existing one.
Essentially, if your gams is used "solely for non-commercial purposes," you attribute the source, the original work does not infringe anyone else's copyright (or at least, you reasonably believe that it does not), your game "does not have a substantial adverse effect, financial or otherwise, on the exploitation or potential exploitation of the existing work... — or copy of it — or on an existing or potential market for it," and your game "is not a substitute for the existing one," then you are most likely not infringing copyright in Canada.
The bold part is important; this exception is unique to Canada and is much broader than "fair use" and "fair dealing" in other countries. Your game will be infringing outside Canada, so unless you follow what one of the other answers says, it will need to stay in Canada. You said you want to "build code for a fan game and play it [your]self or share it with friends"; if you and your friends are all in Canada, and none of you take or send copies outside the country (strictly speaking, this includes not travelling internationally with devices that have the game installed), this should not affect your use case.
If you have more specific questions, it would be best to consult an attorney. If you plan to share your game in a way that allows the general public to access it, then you should definitely ask a lawyer.